File: test_mssql_migration.py

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (244 lines) | stat: -rw-r--r-- 12,999 bytes parent folder | download | duplicates (2)
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2 of the
# License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
# 02110-1301  USA

import unittest

import db_mssql_test_main
import grt

from grt.modules import DbMssqlRE, DbMssqlMigration

class TestMSSQLMigration(db_mssql_test_main.MssqlTestCase):
    @classmethod
    def setUpClass(cls):
        DbMssqlRE.connect(cls.connection, db_mssql_test_main.test_params['password'])
        self.state = db_migration_Migration()
        #version = DbMssqlRE.getServerVersion(cls.connection)
        #cls.context = grt.Dict()
        #cls.context['serverVersion'] = version
    
    def _set_catalog_schema_table(self, catalog_name, schema_name=None, table_name=None):
        catalog = grt.classes.db_mssql_Catalog()
        catalog.name = catalog_name
        schema = table = None
        if schema_name:
            schema = grt.classes.db_mssql_Schema()
            schema.name = schema_name
            schema.owner = catalog
            if table_name:
                table = grt.classes.db_mssql_Table()
                table.name = table_name
                table.owner = schema
        return catalog, schema, table

    def test_migrate_catalog(self):
        source_catalog = grt.classes.db_mssql_Catalog()
        source_catalog.name = '[My Test Name]'
        target_catalog = DbMssqlMigration.migrateCatalog(self.state, source_catalog)
        self.assertTrue(target_catalog != None)
        version = (target_catalog.version.majorNumber, target_catalog.version.minorNumber,
                   target_catalog.version.releaseNumber, target_catalog.version.buildNumber)
        self.assertEqual(version, (5, 5, 0, 0))
        self.assertEqual(target_catalog.name, 'My Test Name')

    def test_migrate_identifier(self):
        self.assertEqual('simple_identifier_01', DbMssqlMigration.migrateIdentifier('"simple_identifier_01"', None))
        self.assertEqual('simple identifier 02', DbMssqlMigration.migrateIdentifier('[simple identifier 02]', None))
        #l = grt.modules.Migration.get_log_size()
        DbMssqlMigration.migrateIdentifier('[]', None)
        #self.assertEqual(l+1, grt.modules.Migration.get_log_size())

    def test_migrate_schema(self):
        source_schema = grt.classes.db_mssql_Schema()
        source_schema.name = '[My Test Name]'
        target_catalog = grt.classes.db_mysql_Catalog()
        target_schema = DbMssqlMigration.migrateSchema(self.state, source_schema, target_catalog)
        self.assertTrue(target_schema != None)
        self.assertEqual(target_schema.name, 'My Test Name')

    def test_migrate_table(self):
        source_table = grt.classes.db_mssql_Table()
        source_table.name = '[My Test Name]'
        target_schema = grt.classes.db_mysql_Schema()
        target_table = DbMssqlMigration.migrateTableToMySQL(self.state, source_table, target_schema)
        self.assertTrue(target_table != None)
        self.assertEqual(target_table.name, 'My Test Name')

    def test_migrate_table_columns(self):
        # Prepare source objects:
        source_catalog, source_schema, source_table = self._set_catalog_schema_table('AdventureWorks', 'Person', 'Contact')
        res = DbMssqlRE.reverseEngineerUserDatatypes(self.state, self.connection, source_catalog)
        self.assertEqual(res, 0)
        res = DbMssqlRE.reverseEngineerTableColumns(self.state, self.connection, source_table)
        self.assertEqual(res, 0)

        # Do migration:
        target_catalog = DbMssqlMigration.migrateCatalog(self.state, source_catalog)
        self.assertTrue(target_catalog != None)
        target_table = grt.classes.db_mysql_Schema()
        res = DbMssqlMigration.migrateTableToMySQL(self.state, source_table, target_schema)
        self.assertEqual(res, 0)
        self.assertEqual(target_table.name, 'Contact')
        res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table)
        self.assertEqual(res, 0)
        self.assertEqual(len(target_table.columns), len(source_table.columns))
        # TODO: Extend this test


    @unittest.skip('FIXME: grt table addPrimaryKeyColumn function is not adding the column into the columns list of the PK')
    def test_migrate_table_pks(self):
        # Prepare source objects:
        source_catalog, source_schema, source_table = self._set_catalog_schema_table('AdventureWorks', 'Purchasing', 'VendorContact')
        res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog)
        self.assertEqual(res, 0)
        res = DbMssqlRE.reverseEngineerTableColumns(self.connection, source_table, grt.Dict())
        self.assertEqual(res, 0)
        res = DbMssqlRE.reverseEngineerTablePK(self.connection, source_table)
        self.assertEqual(res, 0)

        # Do migration:
        target_catalog = grt.classes.db_mysql_Catalog()
        res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog)
        self.assertEqual(res, 0)
        target_table = grt.classes.db_mysql_Table()
        res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table)
        self.assertEqual(res, 0)
        res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table)
        self.assertEqual(res, 0)
        res = DbMssqlMigration.migrateTablePrimaryKeyToMySQL(source_table, target_table)
        self.assertEqual(res, 0)

        pk_cols = [ column.name for column in target_table.columns if target_table.isPrimaryKeyColumn(column) ]
        self.assertEqual(set(['VendorID', 'ContactID']), set(pk_cols))

    def test_migrate_indices(self):
        # Prepare source objects:
        source_catalog, source_schema, source_table = self._set_catalog_schema_table('AdventureWorks', 'HumanResources', 'EmployeeAddress')
        res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog)
        self.assertEqual(res, 0)
        res = DbMssqlRE.reverseEngineerTableColumns(self.connection, source_table)
        self.assertEqual(res, 0)
        res = DbMssqlRE.reverseEngineerTableIndices(self.connection, source_table)
        self.assertEqual(res, 0)

        # Do migration:
        target_catalog = grt.classes.db_mysql_Catalog()
        res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog)
        self.assertEqual(res, 0)
        target_table = grt.classes.db_mysql_Table()
        res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table)
        self.assertEqual(res, 0)
        res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table)
        self.assertEqual(res, 0)
        res = DbMssqlMigration.migrateTableIndicesToMySQL(source_table, target_table)
        self.assertEqual(res, 0)

        # Additional tests:
        detected_index = 0
        for index in target_table.indices:
            if index.name == 'PK_EmployeeAddress_EmployeeID_AddressID':
                detected_index += 1
            elif index.name == 'AK_EmployeeAddress_rowguid':
                detected_index += 1
                self.assertEqual(index.unique, 1)
                self.assertEqual(index.isPrimary, 0)
                self.assertEqual(index.indexType, 'UNIQUE')
                self.assertEqual(set(icol.referencedColumn.name for icol in index.columns), set(['rowguid']))
        self.assertEqual(detected_index, 1)

    def test_migrate_foreign_keys(self):
        # Prepare the scenario migrating needed objects along the way:
        source_catalog = grt.classes.db_mssql_Catalog()
        source_catalog.name = 'AdventureWorks'

        res = DbMssqlRE.reverseEngineerUserDatatypes(self.connection, source_catalog)
        self.assertEqual(res, 0)

        for schema_name in ['Person', 'Purchasing']:
            source_schema = grt.classes.db_mssql_Schema()
            source_schema.name = schema_name
            source_schema.owner = source_catalog
            res = DbMssqlRE.reverseEngineerTables(self.connection, source_schema)
            self.assertEqual(res, 0)
            for table in source_schema.tables:
                # Only rev eng the relevant tables
                if source_schema.name + '.' + table.name not in ['Person.Contact', 'Person.ContactType', 'Purchasing.Vendor', 'Purchasing.VendorContact']:
                    continue
                res = DbMssqlRE.reverseEngineerTableColumns(self.connection, table)
                self.assertEqual(res, 0)
            source_catalog.schemata.append(source_schema)

        target_catalog = grt.classes.db_mysql_Catalog()
        res = DbMssqlMigration.migrateCatalog(source_catalog, target_catalog)
        self.assertEqual(res, 0)

        # Second pass through the tables, to be sure that all of the columns are there and to RevEng those
        # objects that require rev eng of table columns:
        source_target_schemata = zip(source_catalog.schemata, target_catalog.schemata)
        self.assertEqual(len(source_target_schemata), 2)
        test_target_table = None
        for source_schema, target_schema in source_target_schemata:
            res = DbMssqlMigration.migrateSchema(source_schema, target_schema)
            self.assertEqual(res, 0)

            self.assertTrue(len(source_schema.tables) > 0)
            self.assertEqual(len(source_schema.tables), len(target_schema.tables))
            for source_table, target_table in zip(source_schema.tables, target_schema.tables):
                self.assertEqual(source_table.name, target_table.name)
                # Only migrate the relevant tables
                if source_schema.name + '.' + source_table.name not in ['Person.Contact', 'Person.ContactType', 'Purchasing.Vendor', 'Purchasing.VendorContact']:
                    continue
                res = DbMssqlMigration.migrateTableToMySQL(source_table, target_table)
                self.assertEqual(res, 0)
                res = DbMssqlMigration.migrateTableColumnsToMySQL(source_table, target_table)
                self.assertEqual(res, 0)

                if source_table.name == 'VendorContact':
                    res = DbMssqlRE.reverseEngineerTableFKs(self.connection, source_table)
                    self.assertEqual(res, 0)
                    self.assertEqual(len(source_table.foreignKeys), 3)
                    res = DbMssqlMigration.migrateTableForeignKeysToMySQL(source_table, target_table)
                    self.assertEqual(res, 0)
                    test_target_table = target_table

        # The actual tests for the foreign keys migration:
        self.assertIsNotNone(test_target_table)
        self.assertEqual(len(test_target_table.foreignKeys), 3)
        found_foreign_keys = 0
        for foreign_key in test_target_table.foreignKeys:
            if foreign_key.name == 'FK_VendorContact_Contact_ContactID':
                found_foreign_keys += 1
                self.assertEqual(set(['ContactID']), set([column.name for column in foreign_key.columns]))
                self.assertEqual(foreign_key.referencedTable.name, 'Contact')
                self.assertEqual(foreign_key.deleteRule, 'NO_ACTION')
                self.assertEqual(foreign_key.updateRule, 'NO_ACTION')
                self.assertEqual(set(['ContactID']), set([column.name for column in foreign_key.referencedColumns]))
            elif foreign_key.name == 'FK_VendorContact_ContactType_ContactTypeID':
                found_foreign_keys += 1
                self.assertEqual(set(['ContactTypeID']), set([column.name for column in foreign_key.columns]))
                self.assertEqual(foreign_key.referencedTable.name, 'ContactType')
                self.assertEqual(foreign_key.deleteRule, 'NO_ACTION')
                self.assertEqual(foreign_key.updateRule, 'NO_ACTION')
                self.assertEqual(set(['ContactTypeID']), set([column.name for column in foreign_key.referencedColumns]))
            elif foreign_key.name == 'FK_VendorContact_Vendor_VendorID':
                found_foreign_keys += 1
                self.assertEqual(set(['VendorID']), set([column.name for column in foreign_key.columns]))
                self.assertEqual(foreign_key.referencedTable.name, 'Vendor')
                self.assertEqual(foreign_key.deleteRule, 'NO_ACTION')
                self.assertEqual(foreign_key.updateRule, 'NO_ACTION')
                self.assertEqual(set(['VendorID']), set([column.name for column in foreign_key.referencedColumns]))
        self.assertEqual(found_foreign_keys, 3)