~ubuntu-branches/debian/jessie/sqlalchemy/jessie

« back to all changes in this revision

Viewing changes to lib/sqlalchemy/testing/schema.py

  • Committer: Package Import Robot
  • Author(s): Piotr Ożarowski, Jakub Wilk, Piotr Ożarowski
  • Date: 2013-07-06 20:53:52 UTC
  • mfrom: (1.4.23) (16.1.17 experimental)
  • Revision ID: package-import@ubuntu.com-20130706205352-ryppl1eto3illd79
Tags: 0.8.2-1
[ Jakub Wilk ]
* Use canonical URIs for Vcs-* fields.

[ Piotr Ożarowski ]
* New upstream release
* Upload to unstable
* Build depend on python3-all instead of -dev, extensions are not built for
  Python 3.X 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
from . import exclusions
 
3
from .. import schema, event
 
4
from . import config
 
5
 
 
6
__all__ = 'Table', 'Column',
 
7
 
 
8
table_options = {}
 
9
 
 
10
 
 
11
def Table(*args, **kw):
 
12
    """A schema.Table wrapper/hook for dialect-specific tweaks."""
 
13
 
 
14
    test_opts = dict([(k, kw.pop(k)) for k in kw.keys()
 
15
                      if k.startswith('test_')])
 
16
 
 
17
    kw.update(table_options)
 
18
 
 
19
    if exclusions.against('mysql'):
 
20
        if 'mysql_engine' not in kw and 'mysql_type' not in kw:
 
21
            if 'test_needs_fk' in test_opts or 'test_needs_acid' in test_opts:
 
22
                kw['mysql_engine'] = 'InnoDB'
 
23
            else:
 
24
                kw['mysql_engine'] = 'MyISAM'
 
25
 
 
26
    # Apply some default cascading rules for self-referential foreign keys.
 
27
    # MySQL InnoDB has some issues around seleting self-refs too.
 
28
    if exclusions.against('firebird'):
 
29
        table_name = args[0]
 
30
        unpack = (config.db.dialect.
 
31
                  identifier_preparer.unformat_identifiers)
 
32
 
 
33
        # Only going after ForeignKeys in Columns.  May need to
 
34
        # expand to ForeignKeyConstraint too.
 
35
        fks = [fk
 
36
               for col in args if isinstance(col, schema.Column)
 
37
               for fk in col.foreign_keys]
 
38
 
 
39
        for fk in fks:
 
40
            # root around in raw spec
 
41
            ref = fk._colspec
 
42
            if isinstance(ref, schema.Column):
 
43
                name = ref.table.name
 
44
            else:
 
45
                # take just the table name: on FB there cannot be
 
46
                # a schema, so the first element is always the
 
47
                # table name, possibly followed by the field name
 
48
                name = unpack(ref)[0]
 
49
            if name == table_name:
 
50
                if fk.ondelete is None:
 
51
                    fk.ondelete = 'CASCADE'
 
52
                if fk.onupdate is None:
 
53
                    fk.onupdate = 'CASCADE'
 
54
 
 
55
    return schema.Table(*args, **kw)
 
56
 
 
57
 
 
58
def Column(*args, **kw):
 
59
    """A schema.Column wrapper/hook for dialect-specific tweaks."""
 
60
 
 
61
    test_opts = dict([(k, kw.pop(k)) for k in kw.keys()
 
62
                      if k.startswith('test_')])
 
63
 
 
64
    if not config.requirements.foreign_key_ddl.enabled:
 
65
        args = [arg for arg in args if not isinstance(arg, schema.ForeignKey)]
 
66
 
 
67
    col = schema.Column(*args, **kw)
 
68
    if 'test_needs_autoincrement' in test_opts and \
 
69
        kw.get('primary_key', False):
 
70
 
 
71
        # allow any test suite to pick up on this
 
72
        col.info['test_needs_autoincrement'] = True
 
73
 
 
74
        # hardcoded rule for firebird, oracle; this should
 
75
        # be moved out
 
76
        if exclusions.against('firebird', 'oracle'):
 
77
            def add_seq(c, tbl):
 
78
                c._init_items(
 
79
                    schema.Sequence(_truncate_name(
 
80
                            config.db.dialect, tbl.name + '_' + c.name + '_seq'),
 
81
                        optional=True)
 
82
                )
 
83
            event.listen(col, 'after_parent_attach', add_seq, propagate=True)
 
84
    return col
 
85
 
 
86
 
 
87
 
 
88
 
 
89
 
 
90
def _truncate_name(dialect, name):
 
91
    if len(name) > dialect.max_identifier_length:
 
92
        return name[0:max(dialect.max_identifier_length - 6, 0)] + \
 
93
                "_" + hex(hash(name) % 64)[2:]
 
94
    else:
 
95
        return name