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
|
/*
* 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
*/
#include "testgrt.h"
#include "grt_test_utility.h"
#include "grt/grt_manager.h"
#include "grtpp.h"
#include "synthetic_mysql_model.h"
#include "grtdb/diff_dbobjectmatch.h"
#include "grtdb/sync_profile.h"
#include "wb_mysql_import.h"
#include "db_mysql_public_interface.h"
#include "db_mysql_diffsqlgen.h"
#include "module_db_mysql.h"
#include "diff/changeobjects.h"
#include "diff/changelistobjects.h"
#include <boost/lambda/bind.hpp>
BEGIN_TEST_DATA_CLASS(sync_profile_test)
protected:
WBTester tester;
SqlFacade::Ref sql_parser;
DbMySQLImpl* diffsql_module;
grt::DbObjectMatchAlterOmf omf;
sql::ConnectionWrapper connection;
TEST_DATA_CONSTRUCTOR(sync_profile_test)
{
omf.dontdiff_mask = 3;
diffsql_module= tester.grt->get_native_module<DbMySQLImpl>();
ensure("DiffSQLGen module initialization", NULL != diffsql_module);
// init datatypes
populate_grt(tester.grt, tester);
// init database connection
connection= tester.create_connection_for_import();
sql_parser= SqlFacade::instance_for_rdbms_name(tester.grt, "Mysql");
ensure("failed to get sqlparser module", (NULL != sql_parser));
}
END_TEST_DATA_CLASS
TEST_MODULE(sync_profile_test, "Syncronize profiles tests");
//Test if sql generated for syntetic model is valid
TEST_FUNCTION(1)
{
grt::ValueRef e;
NormalizedComparer cmp(tester.grt);
tester.wb->new_document();
SynteticMySQLModel model1(&tester);
grt::StringRef tablename = model1.table->name();
//unaltered model to test diffs
const SynteticMySQLModel model2(&tester);
//Save unaltered names
db_mgmt_SyncProfileRef initial_old_names = create_sync_profile(model1.model, "test_profile", "");
update_sync_profile_from_schema(initial_old_names, model1.catalog->schemata()[0], false);
ensure("Not Valid Initial Old Names", initial_old_names->lastKnownDBNames().count() > 0);
//Rename table
model1.table->name("new_name");
//save updated names
db_mgmt_SyncProfileRef updated_old_names = create_sync_profile(model1.model, "test_profile", "");
update_sync_profile_from_schema(updated_old_names, model1.catalog->schemata()[0], false);
ensure("Not Valid Updated Old Names", updated_old_names->lastKnownDBNames().count() > 0);
//Check that table rename is seen by diff module
boost::shared_ptr<DiffChange> diff = diff_make(model1.catalog, model2.catalog, &omf);
ensure("Diff module broken", diff.get() != NULL);
model1.table->name(tablename);
//now model1.table will have it's initiall name
diff = diff_make(model1.catalog, model2.catalog, &omf);
ensure("Rename Failure", diff.get() == NULL);
//the only difference is oldName which should lead to drop/create of table
update_schema_from_sync_profile(model1.catalog->schemata()[0], updated_old_names);
diff = diff_make(model1.catalog, model2.catalog, &omf);
ensure("OldName only Diff", diff.get() != NULL);
tester.wb->close_document();
tester.wb->close_document_finish();
}
TEST_FUNCTION(2)
{
grt::ValueRef e;
std::auto_ptr<sql::Statement> stmt(connection->createStatement());
NormalizedComparer cmp(tester.grt);
//Kind of hack, atm we doesn't propertly cut server representation of procedures and vews, so just skip it
cmp.add_comparison_rule("sqlDefinition",boost::bind(boost::function<bool ()> (boost::lambda::constant(true))));
tester.wb->new_document();
SynteticMySQLModel model(&tester);
model.trigger->modelOnly(1);
model.view->modelOnly(1);
db_mysql_CatalogRef catalog = model.catalog;
cmp.init_omf(&omf);
boost::shared_ptr<DiffChange> create_change= diff_make(e, catalog, &omf);
boost::shared_ptr<DiffChange> drop_change= diff_make(catalog, e, &omf);
DictRef create_map(tester.grt);
DictRef drop_map(tester.grt);
grt::DictRef options(tester.grt);
options.set("UseFilteredLists", grt::IntegerRef(0));
options.set("OutputContainer", create_map);
options.set("CaseSensitive", grt::IntegerRef(omf.case_sensitive));
options.set("GenerateSchemaDrops", grt::IntegerRef(1));
diffsql_module->generateSQL(catalog, options, create_change);
options.set("OutputContainer", drop_map);
diffsql_module->generateSQL(catalog, options, drop_change);
diffsql_module->makeSQLExportScript(catalog, options, create_map, drop_map);
std::string export_sql_script= options.get_string("OutputScript");
execute_script(stmt.get(), export_sql_script,tester.wb->get_grt_manager());
std::list<std::string> schemata;
schemata.push_back(model.schema->name());
tester.grt->get_undo_manager()->disable();
db_mysql_CatalogRef cat1 = tester.db_rev_eng_schema(schemata);
if((cat1->schemata().get(0).is_valid()) && (cat1->schemata().get(0)->name() == "mydb"))
cat1->schemata().remove(0);
db_mysql_CatalogRef cat2 = grt::copy_object(cat1);
//Diff identical catalogs, no chages expected
boost::shared_ptr<DiffChange> diff = diff_make(cat1, cat2, &omf);
ensure("Diffs in copyies of same catalog", diff == NULL);
// This doesn't make sense anymore, we don't support renaming schemas, schemas with different
// names will be compared as being the same
//Rename schema to new_name, without new_name existing on server
//old schema should be dropped, new one created instead
//cat1->schemata().get(0)->name("new_name");
//diff = diff_make(cat2, cat1, &omf);
/*
{
// 1. generate alter
grt::StringListRef alter_map(tester.grt);
grt::ListRef<GrtNamedObject> alter_object_list(tester.grt);
grt::DictRef options(tester.grt);
options.set("UseFilteredLists", grt::IntegerRef(0));
options.set("OutputContainer", alter_map);
options.set("OutputObjectContainer", alter_object_list);
options.set("CaseSensitive", grt::IntegerRef(omf.case_sensitive));
diffsql_module->generateSQL(cat2, options, diff);
diffsql_module->makeSQLSyncScript(options, alter_map, alter_object_list);
std::string export_sql_script= options.get_string("OutputScript");
std::string drop_old_schema("DROP SCHEMA IF EXISTS `");
drop_old_schema.append(cat1->schemata().get(0)->name()).append("` ;\n");
execute_script(stmt.get(), drop_old_schema, tester.wb->get_grt_manager());
execute_script(stmt.get(), export_sql_script,tester.wb->get_grt_manager());
}
schemata.clear();
schemata.push_back(cat1->schemata().get(0)->name());
schemata.push_back(cat2->schemata().get(0)->name());
db_mysql_CatalogRef testcat = tester.db_rev_eng_schema(schemata);
if((testcat->schemata().get(0).is_valid()) && (testcat->schemata().get(0)->name() == "mydb"))
testcat->schemata().remove(0);
ensure("Initial stchema wasn't dropped on rename", testcat->schemata().count() == 1);
ensure("Renamed schema not found", testcat->schemata().get(0)->name() == cat1->schemata().get(0)->name());
//Now sync renamed schema with 'new_name' schema already on server
//This should lead to drop of old schema and altering new one
//Recreate initial schema
execute_script(stmt.get(), export_sql_script,tester.wb->get_grt_manager());
std::cout<<export_sql_script<<"\n\n\n";
//rev eng both schemas
db_mysql_CatalogRef cat1_and_cat2 = tester.db_rev_eng_schema(schemata);
//rename a table to check that alter did worked
cat1->schemata().get(0)->signal_refreshDisplay()->disconnect_all_slots();
cat1->schemata().get(0)->tables().get(0)->name("new_table_name");
diff = diff_make(cat1_and_cat2, cat1, &omf);
{
// 1. generate alter
grt::StringListRef alter_map(tester.grt);
grt::ListRef<GrtNamedObject> alter_object_list(tester.grt);
grt::DictRef options(tester.grt);
options.set("UseFilteredLists", grt::IntegerRef(0));
options.set("OutputContainer", alter_map);
options.set("OutputObjectContainer", alter_object_list);
options.set("CaseSensitive", grt::IntegerRef(omf.case_sensitive));
diffsql_module->generateSQL(cat1_and_cat2, options, diff);
diffsql_module->makeSQLSyncScript(options, alter_map, alter_object_list);
std::string export_sql_script= options.get_string("OutputScript");
std::cout << export_sql_script.c_str() << "\r\n";
execute_script(stmt.get(), export_sql_script,tester.wb->get_grt_manager());
}
db_mysql_CatalogRef altered_cat2 = tester.db_rev_eng_schema(schemata);
if((altered_cat2->schemata().get(0).is_valid()) && (altered_cat2->schemata().get(0)->name() == "mydb"))
altered_cat2->schemata().remove(0);
ensure("Initial stchema wasn't dropped on rename", altered_cat2->schemata().count() == 1);
ensure("Renamed schema not found", altered_cat2->schemata().get(0)->name() == cat1->schemata().get(0)->name());
ensure("Alter schema failed", altered_cat2->schemata().get(0)->tables().count() == 1);
ensure("Alter schema failed", altered_cat2->schemata().get(0)->tables().get(0)->name() == cat1->schemata().get(0)->tables().get(0)->name());
tester.wb->close_document();
tester.wb->close_document_finish();
*/
}
END_TESTS
|