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

« back to all changes in this revision

Viewing changes to migrate/versioning/schemadiff.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:
1
1
"""
2
2
   Schema differencing support.
3
3
"""
 
4
import logging
 
5
 
4
6
import sqlalchemy
5
 
 
 
7
from migrate.changeset import SQLA_06
 
8
 
 
9
 
 
10
log = logging.getLogger(__name__)
6
11
 
7
12
def getDiffOfModelAgainstDatabase(model, conn, excludeTables=None):
8
13
    """
55
60
        """
56
61
        # Setup common variables.
57
62
        cc = self.conn.contextual_connect()
58
 
        schemagenerator = self.conn.dialect.schemagenerator(
59
 
            self.conn.dialect, cc)
60
 
 
 
63
        if SQLA_06:
 
64
            from sqlalchemy.ext import compiler
 
65
            from sqlalchemy.schema import DDLElement
 
66
            class DefineColumn(DDLElement):
 
67
                def __init__(self, col):
 
68
                    self.col = col
 
69
            
 
70
            @compiler.compiles(DefineColumn)
 
71
            def compile(elem, compiler, **kw):
 
72
                return compiler.get_column_specification(elem.col)
 
73
            
 
74
            def get_column_specification(col):
 
75
                return str(DefineColumn(col).compile(dialect=self.conn.dialect))
 
76
        else:
 
77
            schemagenerator = self.conn.dialect.schemagenerator(
 
78
                self.conn.dialect, cc)
 
79
            def get_column_specification(col):
 
80
                return schemagenerator.get_column_specification(col)
 
81
                
61
82
        # For each in model, find missing in database.
62
83
        for modelName, modelTable in self.model.tables.items():
63
84
            if modelName in self.excludeTables:
64
85
                continue
65
86
            reflectedTable = self.reflected_model.tables.get(modelName, None)
66
 
            if reflectedTable:
 
87
            if reflectedTable is not None:
67
88
                # Table exists.
68
89
                pass
69
90
            else:
75
96
            if reflectedName in self.excludeTables:
76
97
                continue
77
98
            modelTable = self.model.tables.get(reflectedName, None)
78
 
            if modelTable:
 
99
            if modelTable is not None:
79
100
                # Table exists.
80
101
 
81
102
                # Find missing columns in database.
82
103
                for modelCol in modelTable.columns:
83
104
                    databaseCol = reflectedTable.columns.get(modelCol.name,
84
105
                                                             None)
85
 
                    if databaseCol:
 
106
                    if databaseCol is not None:
86
107
                        pass
87
108
                    else:
88
109
                        self.storeColumnMissingInDatabase(modelTable, modelCol)
89
110
 
90
111
                # Find missing columns in model.
91
112
                for databaseCol in reflectedTable.columns:
 
113
                    
 
114
                    # TODO: no test coverage here?   (mrb)
 
115
                    
92
116
                    modelCol = modelTable.columns.get(databaseCol.name, None)
93
 
                    if modelCol:
 
117
                    if modelCol is not None:
94
118
                        # Compare attributes of column.
95
119
                        modelDecl = \
96
 
                            schemagenerator.get_column_specification(
97
 
                            modelCol)
 
120
                            get_column_specification(modelCol)
98
121
                        databaseDecl = \
99
 
                            schemagenerator.get_column_specification(
100
 
                            databaseCol)
 
122
                            get_column_specification(databaseCol)
101
123
                        if modelDecl != databaseDecl:
102
124
                            # Unfortunately, sometimes the database
103
125
                            # decl won't quite match the model, even