~ubuntu-branches/ubuntu/saucy/migrate/saucy-proposed

« back to all changes in this revision

Viewing changes to migrate/versioning/genmodel.py

  • Committer: Bazaar Package Importer
  • Author(s): Jan Dittberner
  • Date: 2010-07-12 00:24:57 UTC
  • mfrom: (1.1.5 upstream) (2.1.8 sid)
  • Revision ID: james.westby@ubuntu.com-20100712002457-4j2fdmco4u9kqzm5
Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
"""
8
8
 
9
9
import sys
 
10
import logging
 
11
 
 
12
import sqlalchemy
10
13
 
11
14
import migrate
12
 
import sqlalchemy
13
 
 
14
 
 
 
15
import migrate.changeset
 
16
 
 
17
 
 
18
log = logging.getLogger(__name__)
15
19
HEADER = """
16
20
## File autogenerated by genmodel.py
17
21
 
34
38
    def __init__(self, diff, declarative=False):
35
39
        self.diff = diff
36
40
        self.declarative = declarative
37
 
        # is there an easier way to get this?
38
 
        dialectModule = sys.modules[self.diff.conn.dialect.__module__]
39
 
        self.colTypeMappings = dict((v, k) for k, v in \
40
 
                                        dialectModule.colspecs.items())
41
41
 
42
42
    def column_repr(self, col):
43
43
        kwarg = []
63
63
        # crs: not sure if this is good idea, but it gets rid of extra
64
64
        # u''
65
65
        name = col.name.encode('utf8')
66
 
        type = self.colTypeMappings.get(col.type.__class__, None)
67
 
        if type:
68
 
            # Make the column type be an instance of this type.
69
 
            type = type()
70
 
        else:
71
 
            # We must already be a model type, no need to map from the
72
 
            # database-specific types.
73
 
            type = col.type
 
66
 
 
67
        type_ = col.type
 
68
        for cls in col.type.__class__.__mro__:
 
69
            if cls.__module__ == 'sqlalchemy.types' and \
 
70
                not cls.__name__.isupper():
 
71
                if cls is not type_.__class__:
 
72
                    type_ = cls()
 
73
                break
74
74
 
75
75
        data = {
76
76
            'name': name,
77
 
            'type': type,
 
77
            'type': type_,
78
78
            'constraints': ', '.join([repr(cn) for cn in col.constraints]),
79
79
            'args': ks and ks or ''}
80
80
 
126
126
 
127
127
    def toUpgradeDowngradePython(self, indent='    '):
128
128
        ''' Assume model is most current and database is out-of-date. '''
129
 
 
130
 
        decls = ['meta = MetaData(migrate_engine)']
 
129
        decls = ['from migrate.changeset import schema',
 
130
                 'meta = MetaData()']
131
131
        for table in self.diff.tablesMissingInModel + \
132
 
                self.diff.tablesMissingInDatabase:
 
132
                self.diff.tablesMissingInDatabase + \
 
133
                self.diff.tablesWithDiff:
133
134
            decls.extend(self.getTableDefn(table))
134
135
 
135
136
        upgradeCommands, downgradeCommands = [], []
143
144
            upgradeCommands.append("%(table)s.create()" % {'table': tableName})
144
145
            downgradeCommands.append("%(table)s.drop()" % {'table': tableName})
145
146
 
 
147
        for modelTable in self.diff.tablesWithDiff:
 
148
            dbTable = self.diff.reflected_model.tables[modelTable.name]
 
149
            tableName = modelTable.name
 
150
            missingInDatabase, missingInModel, diffDecl = \
 
151
                self.diff.colDiffs[tableName]
 
152
            for col in missingInDatabase:
 
153
                upgradeCommands.append('%s.columns[%r].create()' % (
 
154
                        modelTable, col.name))
 
155
                downgradeCommands.append('%s.columns[%r].drop()' % (
 
156
                        modelTable, col.name))
 
157
            for col in missingInModel:
 
158
                upgradeCommands.append('%s.columns[%r].drop()' % (
 
159
                        modelTable, col.name))
 
160
                downgradeCommands.append('%s.columns[%r].create()' % (
 
161
                        modelTable, col.name))
 
162
            for modelCol, databaseCol, modelDecl, databaseDecl in diffDecl:
 
163
                upgradeCommands.append(
 
164
                    'assert False, "Can\'t alter columns: %s:%s=>%s"',
 
165
                    modelTable, modelCol.name, databaseCol.name)
 
166
                downgradeCommands.append(
 
167
                    'assert False, "Can\'t alter columns: %s:%s=>%s"',
 
168
                    modelTable, modelCol.name, databaseCol.name)
 
169
        pre_command = '    meta.bind = migrate_engine'
 
170
 
146
171
        return (
147
172
            '\n'.join(decls),
148
 
            '\n'.join(['%s%s' % (indent, line) for line in upgradeCommands]),
149
 
            '\n'.join(['%s%s' % (indent, line) for line in downgradeCommands]))
 
173
            '\n'.join([pre_command] + ['%s%s' % (indent, line) for line in upgradeCommands]),
 
174
            '\n'.join([pre_command] + ['%s%s' % (indent, line) for line in downgradeCommands]))
150
175
 
151
176
    def applyModel(self):
152
177
        """Apply model to current database."""
153
 
        # Yuck! We have to import from changeset to apply the
154
 
        # monkey-patch to allow column adding/dropping.
155
 
        from migrate.changeset import schema
156
178
 
157
179
        def dbCanHandleThisChange(missingInDatabase, missingInModel, diffDecl):
158
180
            if missingInDatabase and not missingInModel and not diffDecl: