License MIT license
Lines 82
Keywords
SQL (1) SQLAlchemy (3) YAML (1)
Permissions
Owner: midorikid
Viewable by Everyone
Editable by All Siafoo Users
Hide
Stay up to dateembedded code automagically updates, each snippet and article has a feed Join Siafoo Now or Learn More

YAML Database Schema Parser for SQLAlchemy Atom Feed 0

In Brief Generates SQLAlchemy metadata from schema written in YAML. The metadata can then be used to build or interface with any database supported by SQLAlchemy.... more

Generates SQLAlchemy metadata from schema written in YAML. The metadata can then be used to build or interface with any database supported by SQLAlchemy.

Supported Data Types

Native SQL types as well as generic SQLAlchemy types can be used. A complete list can be found here.

Examples

The following function will pretty-print the metadata that is generated by the examples.

# 's
1>>> def printMetadata(metadata):
2... for table in metadata.table_iterator():
3... print 'Table %s: %s' % (table.name, [c.name for c in table.c])

The schema can take the form of a list of tables. A foreign key column can be generated for the child (many) table in a one-to-many relationship by placing a YAML alias in the parent (one) table.

# 's
 1>>> metadata = generateMetadata("""
2... - !Table &Items
3... name: items
4... columns:
5... description: String
6... - !Table
7... name: people
8... columns:
9... firstName: String
10... lastName: String
11... inventory: *Items
12... """)
13>>> printMetadata(metadata)
14Table items: ['id', 'description', 'people_id']
15Table people: ['id', 'lastName', 'firstName']

Alternately, child tables can be nested inside of a parent table taking advantage of YAML's tree structure. This is the prefered form when possible.

# 's
 1>>> metadata = generateMetadata("""
2... - !Table
3... name: people
4... columns:
5... firstName: String
6... lastName: String
7... inventory: !Table
8... name: items
9... columns:
10... description: String
11... """)
12>>> printMetadata(metadata)
13Table items: ['id', 'description', 'people_id']
14Table people: ['id', 'lastName', 'firstName']

Integer primary keys are automatically generated unless specified.

# 's
 1>>> metadata = generateMetadata("""
2... - !Table
3... name: people
4... primary_key: ssn
5... columns:
6... ssn: Integer
7... firstName: String
8... lastName: String
9... """)
10>>> printMetadata(metadata)
11Table people: ['ssn', 'lastName', 'firstName']

Todo

Column datatype engines can't take arguments yet. This makes it impossible to specify how large strings can be, for example.

One-to-one relationships are not currently possible. I haven't decided on an elegant way to represent them yet. Something like one of the following...

- !Table &People
  name: people
  columns:
    firstName: String
    mother: [key, *People]
    father: [key, *People]
- !Table &People
  name: people
  one_to_one: [mother, father]
  columns:
    firstName: String
    mother: *People
    father: *People