File: DataMigrator.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 (442 lines) | stat: -rw-r--r-- 19,577 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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
# Copyright (c) 2012, 2016 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 __future__ import with_statement
import os
import sys
import subprocess
import Queue
import grt
import re
import tempfile
from threading import Thread
from workbench.db_driver import get_connection_parameters
from workbench.utils import replace_string_parameters
from workbench.exceptions import NotConnectedError
from workbench import db_utils
from migration_source_selection import request_password


class HelperExited(Exception):
    def __init__(self, what, returncode):
        Exception.__init__(self, what)
        self.returncode = returncode


def mysql_conn_string(conn):
    param = conn.parameterValues
    if conn.driver.name == "MysqlNative":
        return "%(userName)s@%(hostName)s:%(port)s" % param
    elif conn.driver.name == "MysqlNativeSocket":
        if not param.get('socket', False):
            try:
                connection = db_utils.MySQLConnection(conn, password=request_password(conn))
                connection.connect()
            except (NotConnectedError, db_utils.MySQLError):
                raise Exception('There is no connection to the target MySQL server and the socket parameter in your '
                                'target connection settings is blank. Please check that your target server is running '
                                'or go back to the Target Selection page and set the socket parameter there.')

            result = connection.executeQuery("SHOW VARIABLES LIKE 'socket';")
            if result and result.nextRow():
                socket = result.stringByName('Value')
                param = { 'userName':param['userName'], 'socket':socket }
                connection.disconnect()
            else:
                raise Exception('Failed while querying the socket server variable and the socket parameter in your '
                                'target connection settings is blank. Please go back to the Target Selection page and '
                                'make sure that you have the socket parameter set.')
            
        return "%(userName)s@::%(socket)s" % param
    else:
        raise Exception("Connection method type %s is not supported for migration" % conn.driver.name)

def odbc_conn_string(conn, strip_password = False):
    conn_params = dict(conn.parameterValues)
    conn_params.update(get_connection_parameters(conn))

    connection_string_template = conn.driver.connectionStringTemplate or 'DRIVER={%driver%};SERVER=%host%;PORT=%port%;DATABASE={%database%};UID=%username%;PWD={%password%}'
    connstring = replace_string_parameters(connection_string_template, conn_params)
    if strip_password:
        connstring = re.sub("(PWD={[^;]}*|PWD=[^;]*)", "", connstring).rstrip(";")
    return connstring

def python_conn_string(conn):
    return conn.driver.driverLibraryName + '://' + conn.parameterValues['wbcopytables_connection_string']


class TableCopyWorker(Thread):
    def __init__(self, owner, args, result_queue):
        Thread.__init__(self)
        self._owner = owner
        self.result_queue = result_queue
        self._process_args = args
        grt.log_debug3("Migration", "Spawning copy worker task: %s" % args)
        self._owner.send_info(" ".join(args))
        if sys.platform == "win32":
            # shell=True causes a created window to be hidden by default, this prevents a popup to be shown
            # on the migration wizard
            self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                            universal_newlines=True, shell=True)
        else:
            self.process = subprocess.Popen(args, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
                            universal_newlines=True, close_fds=True)

    def feed_input(self, text):
        if self.process.poll() is None:
            if type(text) is unicode:
                text = text.encode("utf-8")
            self.process.stdin.write(text)
            self.process.stdin.flush()
        else:
            raise HelperExited("Table copy task finished unexpectedly", self.process.returncode)


    def run(self):
        try:
            while self.process.poll() is None:
                if self._owner.query_cancel_status():
                    raise grt.UserInterrupt("Canceled by user")
                line = self.process.stdout.readline()
                if line is not None:
                    type, _, msg = line.strip().partition(":")
                    if type in ("PROGRESS", "ERROR", "BEGIN", "END"):
                        self.result_queue.put((type, msg))
                    else:
                        self.result_queue.put(("LOG", line))

            # Processes any remaining output
            output = self.process.stdout.read()
            lines = output.split("\n")
            for line in lines:
                if line is not None:
                    type, _, msg = line.strip().partition(":")
                    if type in ("PROGRESS", "ERROR", "BEGIN", "END"):
                        self.result_queue.put((type, msg))
                    else:
                        self.result_queue.put(("LOG", msg))

            if self.process.returncode != 0:
                self.result_queue.put(("DONE", "Worker exited with status %s" % self.process.returncode))
            else:
                self.result_queue.put(("DONE", None))
        except grt.UserInterrupt, e:
            self._owner.send_info("Copy task interrupted by user, terminating %s..." % self.name)
            self.terminate()
            self.result_queue.put(("INTERRUPTED", None))
        except Exception, e:
            import traceback
            traceback.print_exc()
            self.result_queue.put(("DONE", str(e)))

    def terminate(self):
        if self.process.poll() is None:
            o, e = self.process.communicate()
            if o:
                last_progress = None
                for line in o.split("\n"):
                    if line.startswith("PROGRESS:"):
                        type, _, last_progress = line.strip().partition(":")
                    else:
                        self._owner.send_info(line)
                if last_progress:
                    self.result_queue.put((type, last_progress))
            if e:
                self._owner.send_info(e)

            # The process should be killed ONLY if it has not yet finished
            try:
                if hasattr(self.process, "terminate"):
                    self.process.terminate()
                else:
                    import signal
                    os.kill(self.process.pid, signal.SIGTERM)
            except OSError, e:
                if e.errno == 3:
                    pass
                else:
                    # can't kill process
                    self._owner.send_error("Unable to kill worker task %s: %s" % (self.process.id, e))

        self.process.wait()


class DataMigrator(object):
    copytable_path = "wbcopytables-bin"

    def __init__(self, message_target, options, srcconnobj, srcpassword, tgtconnobj, tgtpassword):
        assert hasattr(message_target, "send_info") and hasattr(message_target, "send_error") and hasattr(message_target, "send_progress")
        self._owner = message_target
        self._options = options
        self._src_conn_object = srcconnobj
        self._src_password = srcpassword or ''
        self._tgt_conn_object = tgtconnobj
        self._tgt_password = tgtpassword or ''
        self._resume = False

        # Container for the tasks...
        self._tasks = []
        self._processes = []
        self._error = None

    def count_table_rows(self, working_set):
        table_param = []
        if sys.platform == "win32":
            try:
                with tempfile.NamedTemporaryFile("w", delete=False) as table_file:
                    for task in working_set.values():
                        fields = []
                        fields.append(task["source_schema"])
                        fields.append(task["source_table"])
                        if self._resume:
                            fields.append(task["target_schema"])
                            fields.append(task["target_table"])
                            fields.append(task["source_primary_key"])
                            fields.append(task["target_primary_key"])
                            if task.get("select_expression", None):
                                fields.append(task["select_expression"])
                            else:
                                fields.append("*")
                        line = "\t".join(fields)
                        table_file.write(line + "\n")

                    table_param.append("--table-file=%s" % table_file.name)

            except IOError, e:
                raise Exception ("Error creating table file: %s" % e.strerror)
        else:
            for task in working_set.values():
                if self._resume:
                    table_param += ["--table", task["source_schema"], task["source_table"], task["target_schema"], task["target_table"], task["source_primary_key"], task["target_primary_key"]]
                    if task.get("select_expression", None):
                        table_param.append(task["select_expression"])
                    else:
                        table_param.append("*")
                else:
                    table_param += ["--table", task["source_schema"], task["source_table"]]

        stdout = ""

        if not self.copytable_path:
            raise RuntimeError("Path to wbcopytables not found")

        args = self.helper_basic_arglist(self._resume)

        if self._resume:
            args.append("--resume")

        argv = [self.copytable_path, "--count-only", "--passwords-from-stdin"] + args + table_param
        self._owner.send_info(" ".join(argv))

        if sys.platform == "win32":
            # shell=True causes a created window to be hidden by default, this prevents a popup to be shown
            # on the migration wizard
            out = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
        else:
            out = subprocess.Popen(argv, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

        if self._resume:
            passwords= (self._src_password+"\t"+self._tgt_password+"\n").encode("utf8") 
        else:               
            passwords= (self._src_password+"\n").encode("utf8")
        while out.poll() is None:
            o, e = out.communicate(passwords)
            passwords = None
            if o:
                stdout += o
            if e:
                for l in e.split("\n"):
                    self._owner.send_info(l)
            total = 0
        if out.returncode == 0:
            for schema, table, count in [l.split(":")[1:] for l in stdout.split("\n") if l.startswith("ROW_COUNT")]:
                count = int(count.strip())
                total += count
                working_set[schema+"."+table]["row_count"] = count
            return total
        else:
            self._owner.send_info(stdout)
            raise Exception("Error getting row count from source tables, wbcopytables exited with code %s" % out.returncode)

    def migrate_data(self, num_processes, working_set):
        table_param = []
        if sys.platform == "win32":
            try:
                with tempfile.NamedTemporaryFile("w", delete=False) as table_file:
                    for task in working_set.values():
                        fields = []
                        fields.append(task["source_schema"])
                        fields.append(task["source_table"])
                        fields.append(task["target_schema"])
                        fields.append(task["target_table"])
                        fields.append(task["source_primary_key"])
                        fields.append(task["target_primary_key"])
                        if task.get("select_expression", None):
                            fields.append(task["select_expression"])
                        else:
                            fields.append("*")
                        line = "\t".join(fields)
                        table_file.write(line + "\n")

                    table_param.append("--table-file=%s" % table_file.name)

            except IOError, e:
                raise Exception ("Error creating table file: %s" % e.strerror)
        else:
            for task in working_set.values():
                table_param += ["--table", task["source_schema"], task["source_table"], task["target_schema"], task["target_table"], task["source_primary_key"], task["target_primary_key"]]
                if task.get("select_expression", None):
                    table_param.append(task["select_expression"])
                else:
                    table_param.append("*")

        if len(working_set) < num_processes:
            num_processes = len(working_set)

        args = self.helper_basic_arglist(True)
        args += ["--progress", "--passwords-from-stdin"]

        if self._options.get("TruncateTargetTables", False):
            args.append("--truncate-target")
        if self._options.get("DebugTableCopy", False):
            args.append("--log-level=debug3")
        if self._options.get("DriverSendsDataAsUTF8", False):
            args.append("--force-utf8-for-source")

        args.append("--thread-count=" + str(num_processes));
        args.append('--source-rdbms-type=%s' % self._src_conn_object.driver.owner.name)

        if 'defaultCharSet' in self._src_conn_object.parameterValues.keys():
            default_charset = self._src_conn_object.parameterValues.get("defaultCharSet")
            if default_charset:
                args.append('--source-charset=%s' % default_charset)

        if self._resume:
            args.append("--resume")

        argv = [self.copytable_path] + args + table_param

        self._working_set = working_set
        self._result_queue = Queue.Queue(len(working_set))

        worker = TableCopyWorker(self._owner, argv, self._result_queue)
        worker.feed_input(self._src_password+"\t"+self._tgt_password+"\n")
        worker.start()
        results = self.process_until_done()
        worker.terminate()
        return results


    def helper_basic_arglist(self, include_target_conn):
        args = []
        if self._src_conn_object.driver.owner.name == "Mysql":
            args.append('--mysql-source="%s"' % mysql_conn_string(self._src_conn_object))
            if self._src_conn_object.parameterValues.get("OPT_ENABLE_CLEARTEXT_PLUGIN", False):
                args.append("--source-use-cleartext")
        elif (isinstance(self._src_conn_object.driver, grt.classes.db_mgmt_PythonDBAPIDriver) and
              self._src_conn_object.driver.driverLibraryName != 'pyodbc'):
            args.append('--pythondbapi-source="%s"' % python_conn_string(self._src_conn_object))
        else:
            args.append('--odbc-source="%s"' % odbc_conn_string(self._src_conn_object, True))

        if include_target_conn:
            args.append('--target="%s"' % mysql_conn_string(self._tgt_conn_object))
            if self._tgt_conn_object.parameterValues.get("OPT_ENABLE_CLEARTEXT_PLUGIN", False):
                args.append("--target-use-cleartext")

        return args


    def helper_connections_arglist(self):
        conn_args = { 'source_user': self._src_conn_object.parameterValues.get("userName", 'root'),
                      'source_instance': '',
                      'source_port': self._src_conn_object.parameterValues.get("port", 3306),
                      'target_port': self._tgt_conn_object.parameterValues.get("port", 3306),
                      'target_user': self._tgt_conn_object.parameterValues.get("userName", 'root'),
                      'source_rdbms':self._src_conn_object.driver.owner.name.lower()}

        return conn_args


    def process_until_done(self):
        total_row_count = 0
        for table in self._working_set.values():
            total_row_count += table["row_count"]

        progress_row_count = {}

        self.interrupted = False

        active_job_names = set()
        self._resume = False
        done = False

        while True:
            if done:
                # flush pending messages
                try:
                    _update_resume_status = getattr(self._owner, "_update_resume_status", None)
                    if callable(_update_resume_status):
                        _update_resume_status(self._resume)
                    msgtype, message = self._result_queue.get_nowait()
                except Queue.Empty:
                    break
            else:
                msgtype, message = self._result_queue.get()

            if msgtype == "BEGIN":
                target_table = message.split(":")[0]
                active_job_names.add(target_table)
                self._owner.send_info(message)
            elif msgtype == "END":
                target_table = message.split(":")[0]
                if target_table in active_job_names:
                    active_job_names.remove(target_table)
                self._owner.send_info(message)
                progress_row_count[target_table] = (True, progress_row_count.get(target_table, (False, 0))[1])

            elif msgtype == "ERROR":
                target_table = message.split(":")[0]
                if target_table in active_job_names:
                    active_job_names.remove(target_table)
                self._owner.send_error(message)

                self._owner.add_log_entry(2, target_table, message)
                grt.log_error("Migration", "%s\n"%message)
                self._resume = True

            elif msgtype == "PROGRESS":
                target_table, current, total = message.split(":")
                progress_row_count[target_table] = (False, int(current))
                self._owner.send_progress(float(sum([x[1] for x in progress_row_count.values()])) / total_row_count, "Copying %s" % ", ".join(active_job_names))
            elif msgtype == "LOG":
                self._owner.send_info(message)
            elif msgtype == "DONE":
                done = True
                if message:
                    self._resume = True
                    self._owner.send_error("Copy helper exited with an error: %s" % message)
                else:
                    self._owner.send_info("Copy helper has finished")
            elif msgtype == "INTERRUPTED":
                done = True
                self.interrupted = True
                self._resume = True
                self._owner.send_info("Copy helper was aborted by user")
            else:
                self._owner.send_info(msgtype + ": " + message)

        return progress_row_count