summaryrefslogtreecommitdiff
path: root/migrate/tests/versioning/test_schemadiff.py
blob: f45a012e756f02518c678445dc583803e337c1ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# -*- coding: utf-8 -*-

import os

from sqlalchemy import *

from migrate.versioning import schemadiff

from migrate.tests import fixture

class SchemaDiffBase(fixture.DB):

    level = fixture.DB.CONNECT
    def _make_table(self,*cols,**kw):
        self.table = Table('xtable', self.meta,
            Column('id',Integer(), primary_key=True),
            *cols
        )
        if kw.get('create',True):
            self.table.create()

    def _assert_diff(self,col_A,col_B):
        self._make_table(col_A)
        self.meta.clear()
        self._make_table(col_B,create=False)
        diff = self._run_diff()
        # print diff
        self.assertTrue(diff)
        self.assertEqual(1,len(diff.tables_different))
        td = list(diff.tables_different.values())[0]
        self.assertEqual(1,len(td.columns_different))
        cd = list(td.columns_different.values())[0]
        label_width = max(len(self.name1), len(self.name2))
        self.assertEqual(('Schema diffs:\n'
             '  table with differences: xtable\n'
             '    column with differences: data\n'
             '      %*s: %r\n'
             '      %*s: %r')%(
                label_width,
                self.name1,
                cd.col_A,
                label_width,
                self.name2,
                cd.col_B
                ),str(diff))

class Test_getDiffOfModelAgainstDatabase(SchemaDiffBase):
    name1 = 'model'
    name2 = 'database'

    def _run_diff(self,**kw):
        return schemadiff.getDiffOfModelAgainstDatabase(
            self.meta, self.engine, **kw
            )

    @fixture.usedb()
    def test_table_missing_in_db(self):
        self._make_table(create=False)
        diff = self._run_diff()
        self.assertTrue(diff)
        self.assertEqual('Schema diffs:\n  tables missing from %s: xtable' % self.name2,
            str(diff))

    @fixture.usedb()
    def test_table_missing_in_model(self):
        self._make_table()
        self.meta.clear()
        diff = self._run_diff()
        self.assertTrue(diff)
        self.assertEqual('Schema diffs:\n  tables missing from %s: xtable' % self.name1,
            str(diff))

    @fixture.usedb()
    def test_column_missing_in_db(self):
        # db
        Table('xtable', self.meta,
              Column('id',Integer(), primary_key=True),
              ).create()
        self.meta.clear()
        # model
        self._make_table(
            Column('xcol',Integer()),
            create=False
            )
        # run diff
        diff = self._run_diff()
        self.assertTrue(diff)
        self.assertEqual('Schema diffs:\n'
            '  table with differences: xtable\n'
            '    %s missing these columns: xcol' % self.name2,
            str(diff))

    @fixture.usedb()
    def test_column_missing_in_model(self):
        # db
        self._make_table(
            Column('xcol',Integer()),
            )
        self.meta.clear()
        # model
        self._make_table(
            create=False
            )
        # run diff
        diff = self._run_diff()
        self.assertTrue(diff)
        self.assertEqual('Schema diffs:\n'
            '  table with differences: xtable\n'
            '    %s missing these columns: xcol' % self.name1,
            str(diff))

    @fixture.usedb()
    def test_exclude_tables(self):
        # db
        Table('ytable', self.meta,
              Column('id',Integer(), primary_key=True),
              ).create()
        Table('ztable', self.meta,
              Column('id',Integer(), primary_key=True),
              ).create()
        self.meta.clear()
        # model
        self._make_table(
            create=False
            )
        Table('ztable', self.meta,
              Column('id',Integer(), primary_key=True),
              )
        # run diff
        diff = self._run_diff(excludeTables=('xtable','ytable'))
        # ytable only in database
        # xtable only in model
        # ztable identical on both
        # ...so we expect no diff!
        self.assertFalse(diff)
        self.assertEqual('No schema diffs',str(diff))

    @fixture.usedb()
    def test_identical_just_pk(self):
        self._make_table()
        diff = self._run_diff()
        self.assertFalse(diff)
        self.assertEqual('No schema diffs',str(diff))


    @fixture.usedb()
    def test_different_type(self):
        self._assert_diff(
            Column('data', String(10)),
            Column('data', Integer()),
            )

    @fixture.usedb()
    def test_int_vs_float(self):
        self._assert_diff(
            Column('data', Integer()),
            Column('data', Float()),
            )

    # NOTE(mriedem): The ibm_db_sa driver handles the Float() as a DOUBLE()
    # which extends Numeric() but isn't defined in sqlalchemy.types, so we
    # can't check for it as a special case like is done in schemadiff.ColDiff.
    @fixture.usedb(not_supported='ibm_db_sa')
    def test_float_vs_numeric(self):
        self._assert_diff(
            Column('data', Float()),
            Column('data', Numeric()),
            )

    @fixture.usedb()
    def test_numeric_precision(self):
        self._assert_diff(
            Column('data', Numeric(precision=5)),
            Column('data', Numeric(precision=6)),
            )

    @fixture.usedb()
    def test_numeric_scale(self):
        self._assert_diff(
            Column('data', Numeric(precision=6,scale=0)),
            Column('data', Numeric(precision=6,scale=1)),
            )

    @fixture.usedb()
    def test_string_length(self):
        self._assert_diff(
            Column('data', String(10)),
            Column('data', String(20)),
            )

    @fixture.usedb()
    def test_integer_identical(self):
        self._make_table(
            Column('data', Integer()),
            )
        diff = self._run_diff()
        self.assertEqual('No schema diffs',str(diff))
        self.assertFalse(diff)

    @fixture.usedb()
    def test_string_identical(self):
        self._make_table(
            Column('data', String(10)),
            )
        diff = self._run_diff()
        self.assertEqual('No schema diffs',str(diff))
        self.assertFalse(diff)

    @fixture.usedb()
    def test_text_identical(self):
        self._make_table(
            Column('data', Text),
            )
        diff = self._run_diff()
        self.assertEqual('No schema diffs',str(diff))
        self.assertFalse(diff)

class Test_getDiffOfModelAgainstModel(Test_getDiffOfModelAgainstDatabase):
    name1 = 'metadataA'
    name2 = 'metadataB'

    def _run_diff(self,**kw):
        db_meta= MetaData()
        db_meta.reflect(self.engine)
        return schemadiff.getDiffOfModelAgainstModel(
            self.meta, db_meta, **kw
            )