File: db_copy_source_target.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 (225 lines) | stat: -rw-r--r-- 9,333 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
# Copyright (c) 2012, 2014, 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 functools

import grt
import mforms

from grt import DBLoginError
from workbench.ui import WizardPage

from migration_source_selection import request_password, test_connectivity


class SourceTargetMainView(WizardPage):
    def __init__(self, main):
        super(SourceTargetMainView, self).__init__(main, 'Connection Selection')
        self._connections = []
        
    def create_ui(self):
        self.content.set_padding(20)

        self.content.add(mforms.newLabel('''Select the connection for the source MySQL server where databases will be copied from
and the destination server where they should be copied to.'''), False, True)

        source_panel = mforms.newPanel(mforms.TitledBoxPanel)
        source_panel.set_title('Source MySQL Server')

        box = mforms.newBox(False)
        box.set_spacing(12)
        box.set_padding(8)
        box.add(mforms.newLabel('Select the connection for the source MySQL server instance'), False, True)
        
        source_hbox = mforms.newBox(True)
        source_hbox.set_spacing(8)
        source_hbox.add(mforms.newLabel('Source Connection:'), False, True)
        self.source_selector = mforms.newSelector()
        source_hbox.add(self.source_selector, True, True)
        box.add(source_hbox, False, True)

        self.source_connection_status = mforms.newLabel('')
        self.source_connection_status.set_color('#aa3333')
        box.add(self.source_connection_status, True, True)
        self.source_selector.add_changed_callback(functools.partial(self.selector_changed, label=self.source_connection_status, selector=self.source_selector))

        source_panel.add(box)
        self.content.add(source_panel, False, True)

        target_panel = mforms.newPanel(mforms.TitledBoxPanel)
        target_panel.set_title('Destination MySQL Server')

        tbox = mforms.newBox(False)
        tbox.set_spacing(12)
        tbox.set_padding(8)
        tbox.add(mforms.newLabel('Select the connection object for the destination MySQL server instance'), False, True)
        
        target_hbox = mforms.newBox(True)
        target_hbox.set_spacing(8)
        target_hbox.add(mforms.newLabel('Target Connection:'), False, True)
        self.target_selector = mforms.newSelector()
        target_hbox.add(self.target_selector, True, True)
        tbox.add(target_hbox, False, True)

        self.target_connection_status = mforms.newLabel('')
        self.target_connection_status.set_color('#aa3333')
        tbox.add(self.target_connection_status, True, True)
        self.target_selector.add_changed_callback(functools.partial(self.selector_changed, label=self.target_connection_status, selector=self.target_selector))
        
        target_panel.add(tbox)
        self.content.add(target_panel, False, True)

        self.advanced_button.set_text('Test Connections')

        self.back_button.set_enabled(False)

        self.load_connections()
    
    def selector_changed(self, label, selector):
        self.connections_ok = False
        label.set_text('')

        if selector.get_selected_index() == selector.get_item_count()-1:
            grt.modules.Workbench.showConnectionManager()
            self.load_connections()

    def load_connections(self):
        def formatConnection(conn):
            i = conn.hostIdentifier
            return i[i.find('@')+1:]

        # filter out ssh connections until its supported
        self._connections = [conn for conn in grt.root.wb.rdbmsMgmt.storedConns if conn.driver and not conn.driver.name.endswith("SSH")]
        selector_items = ( ['Pick a Connection'] +
            ['%s (%s)' % (conn.name, formatConnection(conn)) for conn in self._connections] +
            ['-', 'Edit Connections...'] )

        self.source_selector.clear()
        self.source_selector.add_items(selector_items)
        self.source_connection_status.set_text('')
        self.source_selector.set_selected(0)
        
        self.target_selector.clear()
        self.target_selector.add_items(selector_items)
        self.target_connection_status.set_text('')
        self.target_selector.set_selected(0)

        self.connections_ok = False


    def test_connection(self, source, caption):
        info_label = self.source_connection_status if caption=='Source' else self.target_connection_status
        info_label.set_text('Testing network connectivity...')
        info_label.set_color('#aa3333')
        if test_connectivity(source.connection, 'Test %s DBMS Connection' % caption) == False:
            info_label.set_text('Server could not be contacted')
            return
        info_label.set_text('Testing connection to DBMS...')
 
        force_password = False
        attempt = 0
        while True:
            try:
                if not source.connect():
                    info_label.set_text('Could not connect to DBMS')
                    self.connections_ok = False
                    return
                info_label.set_color('#33aa33')
                info_label.set_text('Connection succeeded.')
                self.connections_ok = True
                break
            except (DBLoginError, SystemError), e:
                if attempt > 0:
                    if isinstance(e, DBLoginError) and not force_password:
                        force_password = True
                    else:
                        etext = str(e)
                        if etext.startswith('Error(') and ': error calling ' in etext:
                            try:
                                etext = eval(etext[7:etext.rfind('):')-1], {}, {})[1]
                            except:
                                pass
                        info_label.set_text('Could not connect to DBMS: %s' % etext)
                        self.connections_ok = False
                        return
                attempt += 1
                source.password = request_password(source.connection, force_password)
            except Exception, e:
                etext = str(e)
                if etext.startswith('Error(') and etext.endswith(')'):
                    etext = eval(etext[6:-1], {}, {})[1]
                info_label.set_text('Could not connect to DBMS: %s' % etext)
                self.connections_ok = False


    @property
    def source_connection(self):
        i = self.source_selector.get_selected_index()
        return  self._connections[i-1] if 0 < i < len(self._connections)+1 else None


    @property
    def target_connection(self):
        i = self.target_selector.get_selected_index()
        return  self._connections[i-1] if 0 < i < len(self._connections)+1 else None


    def go_advanced(self):
        if self.source_connection:
            self.main.plan.setSourceConnection(self.source_connection)
            self.test_connection(self.main.plan.migrationSource, 'Source')
        else:
            self.source_connection_status.set_text('Please select a connection')
            self.source_connection_status.set_color('#aa3333')
            self.connections_ok = False

        if self.target_connection and self.source_connection == self.target_connection:
            self.target_connection_status.set_text('Select different connections for source and target')
            self.target_connection_status.set_color('#aa3333')
            self.connections_ok = False
        elif self.target_connection:
            self.main.plan.setTargetConnection(self.target_connection)
            self.test_connection(self.main.plan.migrationTarget, 'Target')
        else:
            self.target_connection_status.set_text('Please select a connection')
            self.target_connection_status.set_color('#aa3333')
            self.connections_ok = False


    def go_next(self):
        if not self.connections_ok:
            self.go_advanced()
            if not self.connections_ok:
                return

        set_status_text = mforms.App.get().set_status_text
        set_status_text('Connecting to source DBMS...')
        # Set source connection and connect:
        self.main.plan.setSourceConnection(self.source_connection)
        self.main.plan.migrationSource.connect()

        # Set target connection and connect:
        set_status_text('Connecting to target DBMS...')
        self.main.plan.setTargetConnection(self.target_connection)
        self.main.plan.migrationTarget.connect()

        # Fetch schema names:
        set_status_text('Fetching schema names...')
        self.main.plan.migrationSource.doFetchSchemaNames()
        
        set_status_text('Ready')
        super(SourceTargetMainView, self).go_next()