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_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_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)
    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 = list(
            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)