Пример #1
0
 def testEmptyUpFile(self):
     creator = MigrationCreator("migration_test", locationInTestspace())
     target1 = creator.createMigration(version=17, body="""select * from __mig_version__""")
     target2 = creator.createMigration(version=23, body="""""")
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeDatabasePlugin(self)
     self.assertRaisesRegexp(
         RuntimeError, "Invalid migration: Up file is empty", migrator.applyMigrations, ([target1, target2])
     )
     migrator.plugin.assertCurrentVersion(17)
Пример #2
0
 def testAppliesOutOfOrderMigrations(self):
     creator = MigrationCreator("migration_test", locationInTestspace())
     target1 = creator.createMigration(version=23, body="select 1")
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeMultiPlugin(self)
     migrator.applySingleMigration(target1)
     target2 = creator.createMigration(version=17, body="select 2")
     migrator.applyMigrations([target1, target2])
     migrator.plugin.assertCurrentVersion(23)
     migrator.plugin.assertCommandWasExecuted("select 1")
     migrator.plugin.assertCommandWasExecuted("select 2")
Пример #3
0
 def testAppliesNoopMigrations(self):
     creator = MigrationCreator("migration_test", locationInTestspace())
     target1 = creator.createMigration(version=23, body="select 1")
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeMultiPlugin(self)
     migrator.applyMigrations([target1])
     migrator.plugin.assertCurrentVersion(23)
     migrator.plugin.assertCommandWasExecuted("select 1")
     migrator.dry_run = True
     migrator.applyMigrations([creator.createMigration(version=27, body="select 2")])
     migrator.plugin.assertCurrentVersion(23)
     migrator.plugin.assertCommandWasNotExecuted("select 2")
Пример #4
0
 def testInvalidDatabase(self):
     conf = Config()
     conf.fromMap(testConfig)
     creator = MigrationCreator('asdf', locationInTestspace())
     target = creator.createMigration()
     migrator = MigrationApplier(locationInTestspace(), conf)
     self.assertRaises(BaseException, migrator.applyMigration, target)
Пример #5
0
 def testTwoMigrationsSeparately(self):
     command0 = "create table xxx (yyy integer primary key);"
     command1 = "create table aaa (bbb integer primary key);"
     creator = MigrationCreator("migration_test", locationInTestspace())
     targets = []
     targets.append(creator.createMigration(version=1, body=command0))
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeDatabasePlugin(self)
     migrator.applyMigrations(targets)
     migrator.plugin.assertCurrentVersion(1)
     targets.append(creator.createMigration(version=2, body=command1))
     migrator.applyMigrations(targets)
     migrator.plugin.assertCommandWasExecuted(command0)
     migrator.plugin.assertCommandWasExecuted(command1)
     migrator.plugin.assertCurrentVersion(2)
Пример #6
0
 def testCreatesTable(self):
     migrator = MigrationCreator('zyxw', locationInTestspace())
     target = migrator.createMigration(args=['tableName','column:type','column2:type2'])
     self.assertFileExists('zyxw', target, 'up')
     wholeFile = ''
     with open(locationInTestspace('zyxw', target, 'up'), 'r') as f:
         wholeFile = f.read()
     fileContent = """create table tableName (\n    column type,\n    column2 type2\n    );\n"""
     self.assertEqual(fileContent, wholeFile)
Пример #7
0
 def testPipesAdvancedOutput(self):
     command = "create table xxx (yyy integer primary key);"
     creator = MigrationCreator("migration_test", locationInTestspace())
     target = creator.createMigration(version=42, advanced=True, body="#!/bin/bash\necho -n '" + command + "' ")
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeDatabasePlugin(self)
     migrator.applyMigrations([target])
     migrator.plugin.assertCurrentVersion(42)
     migrator.plugin.assertCommandWasExecuted(command)
Пример #8
0
 def testAdvancedMigration(self):
     creator = MigrationCreator("migration_test", locationInTestspace())
     target = creator.createMigration(
         version=42, advanced=True, body="""#!/bin/bash\necho Hello World > testspace/test_output\n"""
     )
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeDatabasePlugin(self)
     migrator.applyMigrations([target])
     migrator.plugin.assertCurrentVersion(42)
     self.assertFileExists("test_output")
Пример #9
0
 def testDirectAppy(self):
     command = "create table xxx (yyy integer primary key);"
     creator = MigrationCreator("migration_test", locationInTestspace())
     target = creator.createMigration()
     self.assertFolderExists("migration_test", target)
     writeToFile(locationInTestspace("migration_test", target, "up"), command)
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeDatabasePlugin(self)
     migrator.applySingleMigration(target)
     migrator.plugin.assertCommandWasExecuted(command)
Пример #10
0
 def testSecondMigrationFails(self):
     command0 = "create table xxx (yyy integer primary key);"
     command1 = "alter blah blah blah;"
     creator = MigrationCreator("migration_test", locationInTestspace())
     targets = []
     targets.append(creator.createMigration(version=1, body=command0))
     targets.append(creator.createMigration(version=2, body=command1))
     conf = Config()
     conf.fromMap(testConfig)
     migrator = MigrationApplier(locationInTestspace(), conf)
     migrator.plugin = FakeDatabasePlugin(self, failOn=command1)
     try:
         migrator.applyMigrations(targets)
     except:  # Error is expected
         pass
     migrator.plugin.assertCurrentVersion(1)
     migrator.plugin.assertCommandWasExecuted(command0)
     migrator.plugin.assertCommandWasNotExecuted(command1)
Пример #11
0
 def testCreateMigrationWithBody(self):
     migrator = MigrationCreator("zyxw", locationInTestspace())
     body = "This is the body of the file."
     target = migrator.createMigration(body=body)
     with open(locationInTestspace('zyxw', target, 'up'), 'r') as f:
         self.assertEquals(body + "\n", f.readline())
Пример #12
0
 def testCreateMigrationWithHeader(self):
     migrator = MigrationCreator("zyxw", locationInTestspace())
     header = "This is the header of the file."
     target = migrator.createMigration(header=header)
     with open(locationInTestspace('zyxw', target, 'up'), 'r') as f:
         self.assertEquals("-- " + header + "\n", f.readline())
Пример #13
0
 def testAdvancedMigration(self):
     migrator = MigrationCreator("zyxw", locationInTestspace())
     name = migrator.createMigration(advanced=True)
     target = locationInTestspace('zyxw', name, 'up')
     self.assertExecutable(target)
Пример #14
0
 def testCreateSpecificMigration(self):
     migrator = MigrationCreator("zyxw", locationInTestspace())
     name = migrator.createMigration(version='abcdef')
     self.assertEquals('abcdef', name)
     target = locationInTestspace('zyxw', 'abcdef', 'up')
     self.assertEquals('testspace/zyxw/abcdef/up', target)
Пример #15
0
 def testCreateMigration(self):
     migrator = MigrationCreator("zyxw", locationInTestspace())
     target = migrator.createMigration()
     self.assertTrue(os.path.exists(locationInTestspace('zyxw', target)))
     self.assertTrue(os.path.exists(locationInTestspace('zyxw', target, 'up')))