def test_upgrade_several(self): from ptah.migrate import revision, Version ptah.register_migration('test1', 'test1:path', 'Test migration') ptah.register_migration('test2', 'test2:path', 'Test migration') self.init_ptah() versions = dict((v.package, v.version_num) for v in ptah.get_session().query(Version).all()) rev1 = revision('test1') rev2 = revision('test2') sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'upgrade', 'test1', 'test2'] migrate.main() self._reset_stdout() versions = dict((v.package, v.version_num) for v in ptah.get_session().query(Version).all()) self.assertIn('test1', versions) self.assertIn('test2', versions) self.assertEqual(versions['test1'], rev1) self.assertEqual(versions['test2'], rev2)
def test_history(self): from ptah.migrate import revision, upgrade, Version ptah.register_migration('test1', 'test1:path', 'Test migration') ptah.register_migration('test2', 'test2:path', 'Test migration') self.init_ptah() rev1 = revision('test1', message='test1 step') rev2 = revision('test2', message='test2 step') upgrade('test1') upgrade('test2') sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'history', 'test1'] migrate.main() self.assertIn('test1 step', self.out.getvalue()) sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'history'] migrate.main() self.assertIn('test1 step', self.out.getvalue()) self.assertIn('test2 step', self.out.getvalue())
def test_history(self): from ptah.migrate import revision, upgrade ptah.register_migration('test1', 'test1:path', 'Test migration') ptah.register_migration('test2', 'test2:path', 'Test migration') self.init_ptah() revision('test1', message='test1 step') revision('test2', message='test2 step') upgrade('test1') upgrade('test2') sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'history', 'test1'] migrate.main() self.assertIn('test1 step', self.out.getvalue()) sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'history'] migrate.main() self.assertIn('test1 step', self.out.getvalue()) self.assertIn('test2 step', self.out.getvalue())
def test_upgrade_to_head_by_default(self): from ptah.migrate import Version, revision, upgrade ptah.register_migration('test', 'ptah.tests:migrations', force=True) self.init_ptah() revision('test', message='Test message1') rev2 = revision('test', message='Test message2') upgrade('test') versions = dict((v.package, v.version_num) for v in ptah.get_session().query(Version).all()) self.assertEqual(versions['test'], rev2)
def test_update_force(self): from ptah.migrate import Version, revision, update_versions ptah.register_migration('test', 'ptah.tests:migrations', force=True) self.init_ptah() revision('test', message='Test message') update_versions(self.registry) session = ptah.get_session() versions = dict( (v.package, v.version_num) for v in session.query(Version).all()) self.assertNotIn('test', versions)
def test_upgrade_to_head_by_default(self): from ptah.migrate import Version, revision, upgrade ptah.register_migration( 'test', 'ptah.tests:migrations', force=True) self.init_ptah() rev1 = revision('test', message='Test message1') rev2 = revision('test', message='Test message2') upgrade('test') versions = dict((v.package, v.version_num) for v in ptah.get_session().query(Version).all()) self.assertEqual(versions['test'], rev2)
def test_update_force(self): from ptah.migrate import Version, revision, update_versions ptah.register_migration( 'test', 'ptah.tests:migrations', force=True) self.init_ptah() revision('test', message='Test message') update_versions(self.registry) session = ptah.get_session() versions = dict((v.package, v.version_num) for v in session.query(Version).all()) self.assertNotIn('test', versions)
def test_startup_check_version_exit(self): from ptah.migrate import revision, check_version ptah.register_migration('test1', 'ptah.tests1:migrations') ptah.register_migration('test2', 'ptah.tests2:migrations') self.init_ptah() revision('test1', message='Test message1') revision('test2', message='Test message2') exc = None try: check_version(None) except SystemExit as err: # pragma: no cover exc = err self.assertIsInstance(exc, SystemExit)
def test_revision_custom(self): from ptah.migrate import revision rev = revision('test', rev='001', message='Test message') self.assertEqual(rev, '001') self.assertIn('001.py', os.listdir(self.dirs['test'])) self.assertRaises(KeyError, revision, 'test', rev='001')
def test_current_one(self): from ptah.migrate import revision, upgrade ptah.register_migration('test1', 'test1:path', 'Test migration') ptah.register_migration('test2', 'test2:path', 'Test migration') self.init_ptah() rev1 = revision('test1', message='test1 step') revision('test2', message='test2 step') upgrade('test1') sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'current', 'test1'] migrate.main() self.assertIn("Package 'test1' rev: %s(head) test1 step" % rev1, self.out.getvalue())
def test_revision_custom(self): from ptah.migrate import revision rev = revision('test', rev='001', message='Test message') self.assertEqual(rev, '001') self.assertIn('001.py', os.listdir(self.dirs['test'])) self.assertRaises( KeyError, revision, 'test', rev='001')
def test_current_one(self): from ptah.migrate import revision, upgrade, Version ptah.register_migration('test1', 'test1:path', 'Test migration') ptah.register_migration('test2', 'test2:path', 'Test migration') self.init_ptah() rev1 = revision('test1', message='test1 step') rev2 = revision('test2', message='test2 step') upgrade('test1') sys.argv[:] = ['ptah-migrate', 'ptah.ini', 'current', 'test1'] migrate.main() self.assertIn("Package 'test1' rev: %s(head) test1 step"%rev1, self.out.getvalue())
def test_startup_check_version_exit(self): from ptah.migrate import Version, revision, check_version ptah.register_migration( 'test1', 'ptah.tests1:migrations') ptah.register_migration( 'test2', 'ptah.tests2:migrations') self.init_ptah() rev1 = revision('test1', message='Test message1') rev2 = revision('test2', message='Test message2') exc = None try: check_version(None) except SystemExit as err: # pragma: no cover exc = err self.assertIsInstance(exc, SystemExit)
def test_update_version_exists(self): from ptah.migrate import Version, revision, update_versions ptah.register_migration('test', 'ptah.tests:migrations') self.init_ptah() revision('test', message='Test message') session = ptah.get_session() session.add(Version(package='test', version_num='123')) session.flush() update_versions(self.registry) versions = dict( (v.package, v.version_num) for v in session.query(Version).all()) self.assertIn('test', versions) self.assertEqual(versions['test'], '123')
def test_update_version_exists(self): from ptah.migrate import Version, revision, update_versions ptah.register_migration('test', 'ptah.tests:migrations') self.init_ptah() revision('test', message='Test message') session = ptah.get_session() session.add(Version(package='test', version_num='123')) session.flush() update_versions(self.registry) versions = dict((v.package, v.version_num) for v in session.query(Version).all()) self.assertIn('test', versions) self.assertEqual(versions['test'], '123')
def test_startup_check_version(self): from ptah.migrate import Version, revision, check_version ptah.register_migration('test1', 'ptah.tests1:migrations') ptah.register_migration('test2', 'ptah.tests2:migrations') self.init_ptah() rev1 = revision('test1', message='Test message1') rev2 = revision('test2', message='Test message2') session = ptah.get_session() session.add(Version(package='test1', version_num=rev1)) session.add(Version(package='test2', version_num=rev2)) session.flush() exc = None try: check_version(None) except SystemExit as err: # pragma: no cover exc = err self.assertIsNone(exc)
def test_startup_check_version(self): from ptah.migrate import Version, revision, check_version ptah.register_migration( 'test1', 'ptah.tests1:migrations') ptah.register_migration( 'test2', 'ptah.tests2:migrations') self.init_ptah() rev1 = revision('test1', message='Test message1') rev2 = revision('test2', message='Test message2') session = ptah.get_session() session.add(Version(package='test1', version_num=rev1)) session.add(Version(package='test2', version_num=rev2)) session.flush() exc = None try: check_version(None) except SystemExit as err: # pragma: no cover exc = err self.assertIsNone(exc)
def test_revision_default(self): from ptah.migrate import revision rev = revision('test', message='Test message') self.assertIn('{0}.py'.format(rev), os.listdir(self.dirs['test']))
def main(): parser = argparse.ArgumentParser(description="ptah migrate") parser.add_argument('config', metavar='config', help='ini config file') subparsers = parser.add_subparsers() # revision subparser = subparsers.add_parser(revision.__name__, help=revision.__doc__) subparser.add_argument('package', metavar='package', help='package name') subparser.add_argument("-r", "--revision", type=str, dest='revid', help="Unique revision id") subparser.add_argument("-m", "--message", type=str, dest='message', help="Message string to use with 'revision'") subparser.set_defaults(cmd='revision') # current subparser = subparsers.add_parser(current.__name__, help=current.__doc__) subparser.add_argument('package', metavar='package', nargs='*', help='package name') subparser.set_defaults(cmd='current') # upgrade subparser = subparsers.add_parser(upgrade.__name__, help=upgrade.__doc__) subparser.add_argument('package', metavar='package', nargs='*', help='package name') subparser.set_defaults(cmd='upgrade') # history subparser = subparsers.add_parser(history.__name__, help=history.__doc__) subparser.add_argument('package', metavar='package', nargs='*', help='package name') subparser.set_defaults(cmd='history') # list subparser = subparsers.add_parser('list', help='List registered migrations.') subparser.set_defaults(cmd='list') # parse args = parser.parse_args() # bootstrap pyramid env = scripts.bootstrap(args.config) if args.cmd == 'current': print('') if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: current(pkg) if args.cmd == 'revision': if args.revid: for ch in ',.;-': if ch in args.revid: print('Revision id contains forbidden characters') ptah.shutdown() return revision(args.package, args.revid, args.message) if args.cmd == 'upgrade': # create db schemas create_db_schema(env['registry'], False) for pkg in args.package: upgrade(pkg) if args.cmd == 'history': if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: history(pkg) if args.cmd == 'list': list_migrations(env['registry']) ptah.shutdown()
def main(): parser = argparse.ArgumentParser(description="ptah migrate") parser.add_argument("config", metavar="config", help="ini config file") subparsers = parser.add_subparsers() # revision subparser = subparsers.add_parser(revision.__name__, help=revision.__doc__) subparser.add_argument("package", metavar="package", help="package name") subparser.add_argument("-r", "--revision", type=str, dest="revid", help="Unique revision id") subparser.add_argument("-m", "--message", type=str, dest="message", help="Message string to use with 'revision'") subparser.set_defaults(cmd="revision") # current subparser = subparsers.add_parser(current.__name__, help=current.__doc__) subparser.add_argument("package", metavar="package", nargs="*", help="package name") subparser.set_defaults(cmd="current") # upgrade subparser = subparsers.add_parser(upgrade.__name__, help=upgrade.__doc__) subparser.add_argument("package", metavar="package", nargs="*", help="package name") subparser.set_defaults(cmd="upgrade") # history subparser = subparsers.add_parser(history.__name__, help=history.__doc__) subparser.add_argument("package", metavar="package", nargs="*", help="package name") subparser.set_defaults(cmd="history") # list subparser = subparsers.add_parser("list", help="List registered migrations.") subparser.set_defaults(cmd="list") # parse args = parser.parse_args() # bootstrap pyramid env = scripts.bootstrap(args.config) if args.cmd == "current": print("") if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: current(pkg) if args.cmd == "revision": if args.revid: for ch in ",.;-": if ch in args.revid: print("Revision id contains forbidden characters") ptah.shutdown() return revision(args.package, args.revid, args.message) if args.cmd == "upgrade": # create db schemas create_db_schema(env["registry"], False) for pkg in args.package: upgrade(pkg) if args.cmd == "history": if not args.package: args.package = ptah.get_cfg_storage(MIGRATION_ID).keys() for pkg in args.package: history(pkg) if args.cmd == "list": list_migrations(env["registry"]) ptah.shutdown()