File: db_synchronize_any.cpp

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 (451 lines) | stat: -rw-r--r-- 14,886 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
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
443
444
445
446
447
448
449
450
451
/* 
* Copyright (c) 2009, 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
*/

#include "db_alter_script_be.h"

#include "grtui/grt_wizard_plugin.h"
#include "grtui/wizard_view_text_page.h"

#include "../backend/db_plugin_be.h"
#include "base/string_utilities.h"
#include "base/log.h"

using namespace grtui;
using namespace mforms;
using namespace base;

#include "grtui/connection_page.h"
#include "schema_matching_page.h"
#include "fetch_schema_contents_page.h"
#include "synchronize_differences_page.h"

#include <boost/lambda/bind.hpp>


class DescriptionPage : public WizardPage
{
private:
  mforms::Label _description_text;
  mforms::CheckBox _show_description_check;
public:
  DescriptionPage(WizardForm *form)
    : WizardPage(form, "intro")
  {
    set_title(_("Introduction"));
    set_short_title(_("Introduction"));
    _description_text.set_wrap_text(true);
    _description_text.set_text("This wizard allows you to compare a target database or script "
                               "with the open model, external script or a second database and "
                               "apply these changes back to the target.\n"
                               "It's also possible to export the ALTER script generated to a "
                               "file for executing it afterwards.\n"
                               "The changes are applied one way only, to the target database "
                               "and the source is left untouched.");
    add(&_description_text,false,true);
    _show_description_check.set_text("Always show this page");
    _show_description_check.set_active(wizard()->grtm()->get_app_option_int("db.mysql.synchronizeAny:show_sync_help_page", 1) != 0);
    add_end(&_show_description_check,false, true);
  }

  virtual void leave(bool advancing)
  {
    if (advancing)
      wizard()->grtm()->set_app_option("db.mysql.synchronizeAny:show_sync_help_page", grt::IntegerRef(_show_description_check.get_active()));
  }
  
  virtual void enter(bool advancing)
  {
    if (advancing)
      if (!wizard()->grtm()->get_app_option_int("db.mysql.synchronizeAny:show_sync_help_page", 1))
        _form->go_to_next();
  }
};


#include "multi_source_selector_page.h"
#include "fetch_schema_names_multi_page.h"
#include "fetch_schema_contents_multi_page.h"

//---------------------------------------------------------------------------------------------------

class AlterViewResultPage : public ViewTextPage
{
public:
  AlterViewResultPage(WizardForm *form)
    : ViewTextPage(form, "viewdiff", (ViewTextPage::Buttons)(ViewTextPage::SaveButton|ViewTextPage::CopyButton), "SQL Files (*.sql)|*.sql")
  {
    set_short_title(_("Detected Changes"));
    set_title(_("Detected Changes to be Applied to Destination"));

  }

  void set_generate_text_slot(const boost::function<std::string()> &slot)
  {
    _generate= slot;
  }

  virtual void enter(bool advancing)
  {
    if (advancing)
    {
      std::string sql = _generate();
      _text.set_value(sql);
      values().gset("script",sql);
    }
  }

  virtual bool advance()
  {
    if (values().get_int("result") == DataSourceSelector::FileSource)
    {
      std::string path = values().get_string("result_path");
      if (!path.empty())
        save_text_to(path);
    }
    return true;  
  }

  virtual std::string next_button_caption()
  {
    return execute_caption();
  }

  virtual bool allow_cancel() const  { return false; }
  virtual bool next_closes_wizard() { return values().get_int("result") != DataSourceSelector::ServerSource; }

protected:  
  boost::function<std::string()> _generate;
};


//--------------------------------------------------------------------------------

class AlterApplyProgressPage : public WizardProgressPage
{
  bool _finished;
  Db_plugin *_dbplugin;

public:
  AlterApplyProgressPage(WizardForm *form) : WizardProgressPage(form, "apply_progress", false)
  {
    set_title(_("Applying Alter Progress"));
    set_short_title(_("Alter Progress"));

    add_async_task(_("Connect to DBMS"),
      boost::bind(&AlterApplyProgressPage::do_connect, this),
      _("Connecting to DBMS..."));

    add_async_task(_("Execute Alter Script"),
      boost::bind(&AlterApplyProgressPage::do_export, this),
      _("Applying Alter engineered SQL script in DBMS..."));

    TaskRow *task = add_async_task(_("Read Back Changes Made by Server"),
                                    boost::bind(&AlterApplyProgressPage::back_sync, this),
                                    _("Fetching back object definitions reformatted by server..."));

    task->process_finish= boost::bind(&AlterApplyProgressPage::export_finished, this, _1);

    end_adding_tasks(_("Applying Alter Finished Successfully"));

    set_status_text("");
  }


  virtual void enter(bool advancing)
  {
    _finished= false;

    if (advancing)
      reset_tasks();

    WizardProgressPage::enter(advancing);
  }


  virtual bool allow_back()
  {
    return WizardProgressPage::allow_back() && !_finished;
  }

  virtual bool allow_cancel()
  {
    return WizardProgressPage::allow_cancel() && !_finished;
  }


  bool do_connect()
  {
    execute_grt_task(boost::bind(boost::function<grt::ValueRef (bool)> (boost::lambda::constant(grt::ValueRef())),
        boost::bind(&DbConnection::test_connection,_dbplugin->db_conn())), false);
    return true;
  }  

  bool do_export()
  {
    _dbplugin->sql_script(values().get_string("script"));
    execute_grt_task(boost::bind(&Db_plugin::apply_script_to_db, _dbplugin, _1), false);

    return true;
  }

  bool back_sync()
  {
    execute_grt_task(boost::bind(&AlterApplyProgressPage::back_sync_, this), false);
    return true;
  }

  grt::IntegerRef back_sync_()
  {
    _dbplugin->read_back_view_ddl();
    return grt::IntegerRef(0);
  }

  bool perform_sync_model()
  {
    if (!_got_error_messages)
    {
//      _dbplugin->save_sync_profile(_dbplugin->model_catalog());
    }
    return true;
  }

  void export_finished(const grt::ValueRef &result)
  {
    _finished= true;
  }

  virtual bool next_closes_wizard()
  {
    return true;
  }

  void set_db_plugin(Db_plugin *pl)
  {
    _dbplugin= pl;
  }

};

//--------------------------------------------------------------------------------

class AlterScriptSynchronizeDifferencesPage : public SynchronizeDifferencesPage
{
public:
  AlterScriptSynchronizeDifferencesPage(WizardForm *form, DbMySQLDiffAlter *be):SynchronizeDifferencesPage(form,be)
  {
    _update_model.show(false);
    _update_source.set_text(_("Update Destination"));
    _update_source.set_tooltip(_("Update the database/script with changes detected in the source."));
    _heading.set_text("Double click arrows in the list to choose whether to ignore changes or update destination DB");
    _update_model.set_text(_("Source Database"));
    _update_model.set_tooltip(_("Source Database with detected changes."));
    _skip.set_text(_("Ignore"));
    _skip.set_tooltip(_("Ignore the change."));
    _update_source.set_text(_("Update Destination"));
    _update_source.set_tooltip(_("Update the database/script with changes."));
  };
};

class WbSynchronizeAnyWizard : public WizardPlugin
{
public:
  WbSynchronizeAnyWizard(grt::Module *module)
    : WizardPlugin(module), _be(grtm())
  {
    set_name("synchronize_any_wizard");
    // Start with intro page
    add_page(mforms::manage(_description_page= new DescriptionPage(this)));

    // Then pick source and target
    add_page(mforms::manage(_source_page= new MultiSourceSelectPage(this, true)));

    _left_db.grtm(grtm(), true);
    _right_db.grtm(grtm(), false);

    ConnectionPage *connect;
    // Pick source connection (optional)
    add_page(mforms::manage(connect= new ConnectionPage(this, "connect_source", "db.mysql.synchronizeAny:left_source_connection")));
    connect->set_db_connection(_left_db.db_conn());
    connect->set_title(std::string("Source Database: ").append(connect->get_title()));
    connect->set_short_title("Source Database");
    // Pick target connection (optional)
    add_page(mforms::manage(connect= new ConnectionPage(this, "connect_target", "db.mysql.synchronizeAny:right_source_connection")));
    connect->set_db_connection(_right_db.db_conn());
    connect->set_title(std::string("Target Database: ").append(connect->get_title()));
    connect->set_short_title("Target Database");

    // Fetch names from source and target if they're DBs, reveng script if they're files
    FetchSchemaNamesSourceTargetProgressPage *fetch_names_page;
    add_page(mforms::manage(fetch_names_page= new FetchSchemaNamesSourceTargetProgressPage(this, _source_page, "fetch_names")));
    fetch_names_page->set_load_schemata_slot(_left_db.db_conn(), boost::bind(&WbSynchronizeAnyWizard::load_schemata, this, &_left_db),
                                             _right_db.db_conn(), boost::bind(&WbSynchronizeAnyWizard::load_schemata, this, &_right_db));
    fetch_names_page->set_model_catalog(_be.get_model_catalog());

    // Pick what to synchronize
    _schema_match_page = new SchemaMatchingPage(this, "pick_schemata", "Source Schema", "Target Schema", true);
    add_page(mforms::manage(_schema_match_page));

    // Fetch contents from source and target, if they come from the database.. otherwise the schemas are already loaded (optional)
    FetchSchemaContentsSourceTargetProgressPage *fetch_schema_page;
    add_page(mforms::manage(fetch_schema_page= new FetchSchemaContentsSourceTargetProgressPage(this, _source_page, "fetch_schema")));
    fetch_schema_page->set_db_plugin(&_left_db, &_right_db);

    // Show differences
    _diffs_page= new AlterScriptSynchronizeDifferencesPage(this, &_be);
    _diffs_page->set_title(_("Differences Found"));
    add_page(mforms::manage(_diffs_page));

    AlterViewResultPage *page;
    add_page(mforms::manage(page= new AlterViewResultPage(this)));
    page->set_generate_text_slot(boost::bind(&WbSynchronizeAnyWizard::generate_alter, this));
    add_page(mforms::manage(_apply_page = new AlterApplyProgressPage(this)));
    _apply_page->set_db_plugin(&_right_db);

    set_title(_("Synchronize With Any Source"));
  }

  virtual ~WbSynchronizeAnyWizard()
  {
    _be.restore_overriden_names();
  }

  std::string generate_alter()
  {
    std::string report;
    try
    {
      report = _be.generate_alter();
    }
    catch (const std::exception &exc)
    {
      report = base::strfmt("Error generating alter script: %s", exc.what());
    }
    return report;
  }


  virtual WizardPage *get_next_page(WizardPage *current)
  {
    std::string curid= current ? current->get_id() : "";
    std::string nextid;

    if (curid == "source")
    {
      if (_source_page->get_left_source() == DataSourceSelector::ServerSource)
        nextid = "connect_source";
      else
      {
        if (_source_page->get_right_source() == DataSourceSelector::ServerSource)
          nextid = "connect_target";
        else
          nextid = "fetch_names";
      }
    }
    else if (curid == "connect_source")
    {
      if (_source_page->get_right_source() == DataSourceSelector::ServerSource)
        nextid = "connect_target";
      else
        nextid = "fetch_names";
    }
    else if (curid == "pick_schemata")
    {
      if (_source_page->get_left_source() == DataSourceSelector::ServerSource ||
          _source_page->get_right_source() == DataSourceSelector::ServerSource)
        nextid = "fetch_schema";
      else
        nextid = "diffs";
    }

    if (nextid.empty())
      nextid = WizardForm::get_next_page(current)->get_id();

    if (nextid == "diffs")
    {
      db_CatalogRef left_catalog, right_catalog;

      if (_source_page->get_left_source() == DataSourceSelector::ServerSource)
        left_catalog= _left_db.db_catalog();
      else if (_source_page->get_left_source() == DataSourceSelector::FileSource)
        left_catalog = db_CatalogRef::cast_from(values().get("left_file_catalog"));
      else if (_source_page->get_left_source() == DataSourceSelector::ModelSource)
        left_catalog = _be.get_model_catalog();

      if (_source_page->get_right_source() == DataSourceSelector::ServerSource)
        right_catalog = _right_db.db_catalog();
      else if (_source_page->get_right_source() == DataSourceSelector::FileSource)
        right_catalog = db_CatalogRef::cast_from(values().get("right_file_catalog"));
      else if (_source_page->get_right_source() == DataSourceSelector::ModelSource)
        right_catalog = _be.get_model_catalog();

      // Fix the names from original schemata so the comparison works correctly
      std::map<std::string, std::string> mapping(_schema_match_page->get_mapping());
      grt::ListRef<db_Schema> schemata = left_catalog->schemata();
      for (size_t i = 0; i < schemata.count(); i++)
      {
        db_SchemaRef schema(schemata[i]);
        if (mapping.find(schema->name()) != mapping.end())
        {
          // save the original name before proceeding so it can be retored before the wizard is closed
          schema->customData().set("db.mysql.synchronize:originalName", schema->name());
          schema->customData().set("db.mysql.synchronize:originalOldName", schema->oldName());

          std::string sname = mapping[schema->name()];
          schema->name(sname);
          schema->oldName(sname);
        }
      }
      _diffs_page->set_src(left_catalog);
      _diffs_page->set_dst(right_catalog);
    }
    return get_page_with_id(nextid);
  }


protected:
  DbMySQLDiffAlter _be;
  Db_plugin _left_db;
  Db_plugin _right_db;
  DescriptionPage* _description_page;
  FetchSchemaNamesSourceTargetProgressPage *_fetch_names_page;
  SchemaMatchingPage *_schema_match_page;
  MultiSourceSelectPage *_source_page;
  AlterScriptSynchronizeDifferencesPage *_diffs_page;
  AlterApplyProgressPage* _apply_page;
  bool connect1_apply;

  std::vector<std::string> load_schemata(Db_plugin *db)
  {
    std::vector<std::string> names;
    db->load_schemata(names);
    _be.set_db_options(db->load_db_options());
    return names;
  }
};


WizardPlugin *createWbSynchronizeAnyWizard(grt::Module *module, db_CatalogRef catalog)
{
  return new WbSynchronizeAnyWizard(module);
}

void deleteWbSynchronizeAnyWizard(WizardPlugin *plugin)
{
    delete plugin;
}