def test_it_should_use_label_version_when_updating_schema_version(self): mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change( "create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") expected_query_calls = [ call( "if not exists ( select 1 from sysdatabases where name = 'migration_test' ) create database migration_test;" ), call( "if not exists ( select 1 from sysobjects where name = '__db_version__' and type = 'u' ) create table __db_version__ ( id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, version varchar(20) NOT NULL default '0', label varchar(255), name varchar(255), sql_up NTEXT, sql_down NTEXT)" ), call("insert into __db_version__ (version) values ('0')"), call('create table spam()'), call( 'insert into __db_version__ (version, label, name, sql_up, sql_down) values (%s, %s, %s, %s, %s);', ('20090212112104', 'label', '20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', 'create table spam();', 'drop table spam;')) ] self.assertEqual(expected_query_calls, self.db_mock.execute_non_query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [call('select count(*) from __db_version__;')] self.assertEqual(expected_execute_calls, self.db_mock.execute_scalar.mock_calls)
def test_it_should_execute_migration_down_and_update_schema_version(self): mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change( "drop table spam;", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", False, ) expected_query_calls = [ call( "if not exists ( select 1 from sysdatabases where name = 'migration_test' ) create database migration_test;" ), call( "if not exists ( select 1 from sysobjects where name = '__db_version__' and type = 'u' ) create table __db_version__ ( id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, version varchar(20) NOT NULL default '0', label varchar(255), name varchar(255), sql_up NTEXT, sql_down NTEXT)" ), call("insert into __db_version__ (version) values ('0')"), call("drop table spam"), call("delete from __db_version__ where version = %s;", ("20090212112104",)), ] self.assertEqual(expected_query_calls, self.db_mock.execute_non_query.mock_calls) self.db_mock.select_db.assert_called_with("migration_test") self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [call("select count(*) from __db_version__;")] self.assertEqual(expected_execute_calls, self.db_mock.execute_scalar.mock_calls)
def test_it_should_stop_process_when_an_error_occur_during_log_schema_version(self): self.execute_returns['insert into __db_version__ (version, label, name, sql_up, sql_down) values (%s, %s, %s, %s, %s);'] = Exception("invalid sql") try: mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") except Exception as e: self.assertEqual('error logging migration: invalid sql\n\n[ERROR DETAILS] SQL command was:\n20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', str(e)) self.assertTrue(isinstance(e, simple_db_migrate.core.exceptions.MigrationException)) expected_query_calls = [ call("if not exists ( select 1 from sysdatabases where name = 'migration_test' ) create database migration_test;"), call("if not exists ( select 1 from sysobjects where name = '__db_version__' and type = 'u' ) create table __db_version__ ( id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, version varchar(20) NOT NULL default '0', label varchar(255), name varchar(255), sql_up NTEXT, sql_down NTEXT)"), call("insert into __db_version__ (version) values ('0')"), call('create table spam()'), call('insert into __db_version__ (version, label, name, sql_up, sql_down) values (%s, %s, %s, %s, %s);', ('20090212112104', 'label', '20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', 'create table spam();', 'drop table spam;')) ] self.assertEqual(expected_query_calls, self.db_mock.execute_non_query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(1, self.db_mock.cancel.call_count) self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [ call('select count(*) from __db_version__;'), ] self.assertEqual(expected_execute_calls, self.db_mock.execute_scalar.mock_calls)
def test_it_should_log_execution_when_a_function_is_given_when_updating_schema_version(self): execution_log_mock = Mock() mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", execution_log=execution_log_mock) expected_execution_log_calls = [ call('create table spam()\n-- 1 row(s) affected\n'), call('migration 20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration registered\n') ] self.assertEqual(expected_execution_log_calls, execution_log_mock.mock_calls)
def test_it_should_not_execute_migration_if_fake_execution_is_set(self): mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change("create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", fake_execution=True) expected_execute_calls = [ call("if not exists ( select 1 from sysdatabases where name = 'migration_test' ) create database migration_test;"), call("if not exists ( select 1 from sysobjects where name = '__db_version__' and type = 'u' ) create table __db_version__ ( id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, version varchar(20) NOT NULL default '0', label varchar(255), name varchar(255), sql_up NTEXT, sql_down NTEXT)"), call("insert into __db_version__ (version) values ('0')"), call('insert into __db_version__ (version, label, name, sql_up, sql_down) values (%s, %s, %s, %s, %s);', ('20090212112104', None, '20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', 'create table spam();', 'drop table spam;')) ] self.assertEqual(expected_execute_calls, self.db_mock.execute_non_query.mock_calls) self.assertEqual(5, self.db_mock.close.call_count)
def test_it_should_stop_process_when_an_error_occur_during_log_schema_version( self): self.execute_returns[ 'insert into __db_version__ (version, label, name, sql_up, sql_down) values (%s, %s, %s, %s, %s);'] = Exception( "invalid sql") try: mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change( "create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", label_version="label") except Exception as e: self.assertEqual( 'error logging migration: invalid sql\n\n[ERROR DETAILS] SQL command was:\n20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', str(e)) self.assertTrue( isinstance( e, simple_db_migrate.core.exceptions.MigrationException)) expected_query_calls = [ call( "if not exists ( select 1 from sysdatabases where name = 'migration_test' ) create database migration_test;" ), call( "if not exists ( select 1 from sysobjects where name = '__db_version__' and type = 'u' ) create table __db_version__ ( id INT IDENTITY(1,1) NOT NULL PRIMARY KEY, version varchar(20) NOT NULL default '0', label varchar(255), name varchar(255), sql_up NTEXT, sql_down NTEXT)" ), call("insert into __db_version__ (version) values ('0')"), call('create table spam()'), call( 'insert into __db_version__ (version, label, name, sql_up, sql_down) values (%s, %s, %s, %s, %s);', ('20090212112104', 'label', '20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration', 'create table spam();', 'drop table spam;')) ] self.assertEqual(expected_query_calls, self.db_mock.execute_non_query.mock_calls) self.db_mock.select_db.assert_called_with('migration_test') self.assertEqual(1, self.db_mock.cancel.call_count) self.assertEqual(6, self.db_mock.close.call_count) expected_execute_calls = [ call('select count(*) from __db_version__;'), ] self.assertEqual(expected_execute_calls, self.db_mock.execute_scalar.mock_calls)
def test_it_should_log_execution_when_a_function_is_given_when_updating_schema_version( self): execution_log_mock = Mock() mssql = MSSQL(self.config_mock, self.db_driver_mock) mssql.change( "create table spam();", "20090212112104", "20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration", "create table spam();", "drop table spam;", execution_log=execution_log_mock) expected_execution_log_calls = [ call('create table spam()\n-- 1 row(s) affected\n'), call( 'migration 20090212112104_test_it_should_execute_migration_down_and_update_schema_version.migration registered\n' ) ] self.assertEqual(expected_execution_log_calls, execution_log_mock.mock_calls)