File: db_mysql_migration_grt.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 (151 lines) | stat: -rw-r--r-- 6,976 bytes parent folder | download
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
# Copyright (c) 2012, 2015, 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


from wb import DefineModule
import grt

from db_generic_migration_grt import GenericMigration

ModuleInfo = DefineModule(name= "DbMySQLMigration", author= "Oracle Corp.", version="1.0")

class MySQLMigration(GenericMigration):
    def migrateIdentifier(self, name, log, dots_allowed=True):
        return name

    def migrateDatatypeForColumn(self, state, source_column, target_column):
        def find_object_with_id(list, oid):
            for o in list:
                if o.__id__ == oid:
                    return o
            return None

        if source_column.simpleType:
            target_column.simpleType = find_object_with_id(source_column.owner.owner.owner.simpleDatatypes, source_column.simpleType.__id__)
            return target_column.simpleType is not None
        else:
            # migration error
            state.addMigrationLogEntry(2, source_column, target_column, 
                    'migrateTableColumnToMySQL: Cannot migrate column %s.%s because migration of its datatype is unsupported' % (source_column.owner.name, source_column.name))
            return False

            
    def migrateColumnDefaultValue(self, state, default_value, source_column, target_column):
        return default_value
        

    def migrateTableToMySQL(self, state, sourceTable, target_schema):
        targetTable = GenericMigration.migrateTableToMySQL(self, state, sourceTable, target_schema)

        # MySQL attributes
        for attr in ["tableEngine", "nextAutoInc", "password", "delayKeyWrite", "defaultCharacterSetName", "defaultCollationName", "mergeUnion", "mergeInsert",
                      "tableDataDir", "tableIndexDir", "packKeys", "raidType", "raidChunks", "raidChunkSize", "checksum", "rowFormat", "keyBlockSize", "avgRowLength", "minRows", "maxRows",
                      "partitionType", "partitionExpression", "partitionCount", "subpartitionType", "subpartitionExpression", "subpartitionCount"]:
            setattr(targetTable, attr, getattr(sourceTable, attr))

        if True:
            def copy_partitions(owner, part_list):
                l = []
                for src in part_list:
                    dst = grt.classes.db_mysql_PartitionDefinition()
                    for attr in ["name", "value", "comment", "dataDirectory", "indexDirectory", "maxRows", "minRows"]:
                        setattr(dst, attr, getattr(src, attr))
                    dst.owner = owner
                    dst.subpartitionDefinitions.extend(copy_partitions(dst, src.subpartitionDefinitions))
                    l.append(dst)
                return l
            # partition defs
            targetTable.partitionDefinitions.extend(copy_partitions(targetTable, sourceTable.partitionDefinitions))

        return targetTable


    def migrateTableColumnToMySQL(self, state, source_column, targetTable):
        target_column = GenericMigration.migrateTableColumnToMySQL(self, state, source_column, targetTable)
        # MySQL specific
        for attr in ["autoIncrement", "expression", "generated", "generatedStorage"]:
            setattr(target_column, attr, getattr(source_column, attr))

        return target_column


    def migrateTriggerToMySQL(self, state, source_trigger, target_table):
        target_trigger = GenericMigration.migrateTriggerToMySQL(self, state, source_trigger, target_table)
        target_trigger.commentedOut = 0
        return target_trigger


    def migrateViewToMySQL(self, state, source_view, target_schema):
        target_view = GenericMigration.migrateViewToMySQL(self, state, source_view, target_schema)
        target_view.commentedOut = 0
        return target_view


    def migrateRoutineToMySQL(self, state, source_routine, target_schema):
        target_routine = GenericMigration.migrateRoutineToMySQL(self, state, source_routine, target_schema)
        target_routine.commentedOut = 0
        return target_routine


instance = MySQLMigration()

@ModuleInfo.export(grt.STRING)
def getTargetDBMSName():
    return 'Mysql'

@ModuleInfo.export(grt.STRING, grt.STRING, grt.classes.GrtLogObject)
def migrateIdentifier(name, log):
    return instance.migrateIdentifier(name, log)

@ModuleInfo.export(grt.classes.db_Catalog, grt.classes.db_migration_Migration, grt.classes.db_Catalog)
def migrateCatalog(state, sourceCatalog):
    return instance.migrateCatalog(state, sourceCatalog)


@ModuleInfo.export(grt.classes.db_Schema, grt.classes.db_migration_Migration, grt.classes.db_Schema, grt.classes.db_Catalog)
def migrateSchema(state, sourceSchema, targetCatalog):
    return instance.migrateSchema(state, sourceSchema, targetCatalog)


@ModuleInfo.export(grt.classes.db_Table, grt.classes.db_migration_Migration, grt.classes.db_Table, grt.classes.db_Schema)
def migrateTableToMySQL(state, sourceTable, target_schema):
    return instance.migrateTableToMySQL(state, sourceTable, target_schema)


@ModuleInfo.export(grt.INT, grt.classes.db_migration_Migration, grt.classes.db_Table, grt.classes.db_Table)
def migrateTableToMySQL2ndPass(state, sourceTable, targetTable):
    return instance.migrateTableToMySQL2ndPass(state, sourceTable, targetTable)


@ModuleInfo.export(grt.classes.db_mysql_ForeignKey, grt.classes.db_migration_Migration, grt.classes.db_ForeignKey, grt.classes.db_Table)
def migrateTableForeignKeyToMySQL(state, source_fk, targetTable):
    return instance.migrateTableForeignKeyToMySQL(state, source_fk, targetTable)


@ModuleInfo.export(grt.classes.db_mysql_Trigger, grt.classes.db_migration_Migration, grt.classes.db_Trigger, grt.classes.db_Table)
def migrateTriggerToMySQL(state, source_trigger, target_table):
    return instance.migrateTriggerToMySQL(state, source_trigger, target_table)
  

@ModuleInfo.export(grt.classes.db_mysql_View, grt.classes.db_migration_Migration, grt.classes.db_View, grt.classes.db_Schema)
def migrateViewToMySQL(state, source_view, target_schema):
    return instance.migrateViewToMySQL(state, source_view, target_schema)


@ModuleInfo.export(grt.classes.db_mysql_Routine, grt.classes.db_migration_Migration, grt.classes.db_Routine, grt.classes.db_Schema)
def migrateRoutineToMySQL(state, source_routine, target_schema):
    return instance.migrateRoutineToMySQL(state, source_routine, target_schema)