| 1 | """Enhanced versions of schema.Table and schema.Column which establish | 
|---|
| 2 | desired state for different backends. | 
|---|
| 3 | """ | 
|---|
| 4 |  | 
|---|
| 5 | from sqlalchemy.test import testing | 
|---|
| 6 | from sqlalchemy import schema | 
|---|
| 7 |  | 
|---|
| 8 | __all__ = 'Table', 'Column', | 
|---|
| 9 |  | 
|---|
| 10 | table_options = {} | 
|---|
| 11 |  | 
|---|
| 12 | def Table(*args, **kw): | 
|---|
| 13 | """A schema.Table wrapper/hook for dialect-specific tweaks.""" | 
|---|
| 14 |  | 
|---|
| 15 | test_opts = dict([(k,kw.pop(k)) for k in kw.keys() | 
|---|
| 16 | if k.startswith('test_')]) | 
|---|
| 17 |  | 
|---|
| 18 | kw.update(table_options) | 
|---|
| 19 |  | 
|---|
| 20 | if testing.against('mysql'): | 
|---|
| 21 | if 'mysql_engine' not in kw and 'mysql_type' not in kw: | 
|---|
| 22 | if 'test_needs_fk' in test_opts or 'test_needs_acid' in test_opts: | 
|---|
| 23 | kw['mysql_engine'] = 'InnoDB' | 
|---|
| 24 |  | 
|---|
| 25 | # Apply some default cascading rules for self-referential foreign keys. | 
|---|
| 26 | # MySQL InnoDB has some issues around seleting self-refs too. | 
|---|
| 27 | if testing.against('firebird'): | 
|---|
| 28 | table_name = args[0] | 
|---|
| 29 | unpack = (testing.config.db.dialect. | 
|---|
| 30 | identifier_preparer.unformat_identifiers) | 
|---|
| 31 |  | 
|---|
| 32 | # Only going after ForeignKeys in Columns.  May need to | 
|---|
| 33 | # expand to ForeignKeyConstraint too. | 
|---|
| 34 | fks = [fk | 
|---|
| 35 | for col in args if isinstance(col, schema.Column) | 
|---|
| 36 | for fk in col.args if isinstance(fk, schema.ForeignKey)] | 
|---|
| 37 |  | 
|---|
| 38 | for fk in fks: | 
|---|
| 39 | # root around in raw spec | 
|---|
| 40 | ref = fk._colspec | 
|---|
| 41 | if isinstance(ref, schema.Column): | 
|---|
| 42 | name = ref.table.name | 
|---|
| 43 | else: | 
|---|
| 44 | # take just the table name: on FB there cannot be | 
|---|
| 45 | # a schema, so the first element is always the | 
|---|
| 46 | # table name, possibly followed by the field name | 
|---|
| 47 | name = unpack(ref)[0] | 
|---|
| 48 | if name == table_name: | 
|---|
| 49 | if fk.ondelete is None: | 
|---|
| 50 | fk.ondelete = 'CASCADE' | 
|---|
| 51 | if fk.onupdate is None: | 
|---|
| 52 | fk.onupdate = 'CASCADE' | 
|---|
| 53 |  | 
|---|
| 54 | if testing.against('firebird', 'oracle'): | 
|---|
| 55 | pk_seqs = [col for col in args | 
|---|
| 56 | if (isinstance(col, schema.Column) | 
|---|
| 57 | and col.primary_key | 
|---|
| 58 | and getattr(col, '_needs_autoincrement', False))] | 
|---|
| 59 | for c in pk_seqs: | 
|---|
| 60 | c.args.append(schema.Sequence(args[0] + '_' + c.name + '_seq', optional=True)) | 
|---|
| 61 | return schema.Table(*args, **kw) | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | def Column(*args, **kw): | 
|---|
| 65 | """A schema.Column wrapper/hook for dialect-specific tweaks.""" | 
|---|
| 66 |  | 
|---|
| 67 | test_opts = dict([(k,kw.pop(k)) for k in kw.keys() | 
|---|
| 68 | if k.startswith('test_')]) | 
|---|
| 69 |  | 
|---|
| 70 | c = schema.Column(*args, **kw) | 
|---|
| 71 | if testing.against('firebird', 'oracle'): | 
|---|
| 72 | if 'test_needs_autoincrement' in test_opts: | 
|---|
| 73 | c._needs_autoincrement = True | 
|---|
| 74 | return c | 
|---|