def test_auto(): from peewee_migrate.auto import diff_one, diff_many, model_to_code from peewee_migrate.cli import get_router router = get_router(path.join(CURDIR, 'migrations'), 'sqlite:///:memory:') router.run() migrator = router.migrator models = migrator.orm.values() Person_ = migrator.orm['person'] Tag_ = migrator.orm['tag'] code = model_to_code(Person_) assert code assert "default=dt.datetime.now" in code assert 'db_table = "person"' in code assert "order_by = ('-dob',)" in code changes = diff_many(models, [], migrator=migrator) assert len(changes) == 2 class Person(pw.Model): first_name = pw.IntegerField() last_name = pw.CharField(max_length=1024, null=True, unique=True) tag = pw.ForeignKeyField(Tag_, on_delete='CASCADE', related_name='persons') changes = diff_one(Person, Person_, migrator=migrator) assert len(changes) == 6 assert "on_delete='CASCADE'" in changes[0] assert "related_name='persons'" in changes[0] assert changes[-3] == "migrator.drop_not_null('person', 'last_name')" assert changes[-2] == "migrator.drop_index('person', 'last_name')" assert changes[ -1] == "migrator.add_index('person', 'last_name', unique=True)"
def test_auto(): from peewee_migrate.auto import diff_one, diff_many, model_to_code from peewee_migrate.cli import get_router router = get_router('tests/migrations', 'sqlite:///:memory:') router.run() migrator = router.migrator models = migrator.orm.values() Person_ = migrator.orm['person'] Tag_ = migrator.orm['tag'] code = model_to_code(Person_) assert code import ipdb; ipdb.set_trace() # XXX BREAKPOINT assert 'db_table = "person"' in code changes = diff_many(models, [], migrator=migrator) assert len(changes) == 2 class Person(pw.Model): first_name = pw.IntegerField() last_name = pw.CharField(max_length=1024, null=True) tag = pw.ForeignKeyField(Tag_, on_delete='CASCADE', related_name='persons') changes = diff_one(Person, Person_, migrator=migrator) assert len(changes) == 4 assert "on_delete='CASCADE'" in changes[0] assert "related_name='persons'" in changes[0] assert changes[-1] == "migrator.drop_not_null('person', 'last_name')"
def test_auto(): from peewee_migrate.auto import diff_one, diff_many, model_to_code from peewee_migrate.cli import get_router router = get_router(path.join(CURDIR, 'migrations'), 'sqlite:///:memory:') router.run() migrator = router.migrator models = migrator.orm.values() Person_ = migrator.orm['person'] Tag_ = migrator.orm['tag'] code = model_to_code(Person_) assert code assert 'table_name = "person"' in code changes = diff_many(models, [], migrator=migrator) assert len(changes) == 2 class Person(pw.Model): first_name = pw.IntegerField() last_name = pw.CharField(max_length=1024, null=True, unique=True) tag = pw.ForeignKeyField(Tag_, on_delete='CASCADE', related_name='persons') email = pw.CharField(index=True, unique=True) changes = diff_one(Person, Person_, migrator=migrator) assert len(changes) == 6 assert "on_delete='CASCADE'" in changes[0] assert "backref='persons'" in changes[0] assert changes[-3] == "migrator.drop_not_null('person', 'last_name')" assert changes[-2] == "migrator.drop_index('person', 'last_name')" assert changes[ -1] == "migrator.add_index('person', 'last_name', unique=True)" migrator.drop_index('person', 'email') migrator.add_index('person', 'email', unique=True) class Person(pw.Model): first_name = pw.CharField(unique=True) last_name = pw.CharField(max_length=255, index=True) dob = pw.DateField(null=True) birthday = pw.DateField(default=dt.datetime.now) email = pw.CharField(index=True, unique=True) changes = diff_one(Person_, Person, migrator=migrator) assert not changes class Color(pw.Model): id = pw.AutoField() name = pw.CharField(default='red') code = model_to_code(Color) assert "DEFAULT 'red'" in code
def compile_migrations(migrator, models, reverse=False): """Compile migrations for given models.""" source = migrator.orm.values() if reverse: source, models = models, source migrations = diff_many(models, source, migrator, reverse=reverse) if not migrations: return False migrations = NEWLINE + NEWLINE.join('\n\n'.join(migrations).split('\n')) return CLEAN_RE.sub('\n', migrations)
def compile_migrations(migrator, models, reverse=False): """Compile migrations for given models.""" source = migrator.orm.values() if reverse: source, models = models, source migrations = diff_many(models, source, migrator, reverse=reverse) if not migrations: return False migrations = NEWLINE + NEWLINE.join('\n\n'.join(migrations).split('\n')) return CLEAN_RE.sub('\n', migrations)
def test_auto(): from peewee_migrate.auto import diff_one, diff_many, model_to_code from peewee_migrate.cli import get_router router = get_router(path.join(CURDIR, 'migrations'), 'sqlite:///:memory:') router.run() migrator = router.migrator models = migrator.orm.values() Person_ = migrator.orm['person'] Tag_ = migrator.orm['tag'] code = model_to_code(Person_) assert code assert 'table_name = "person"' in code changes = diff_many(models, [], migrator=migrator) assert len(changes) == 2 class Person(pw.Model): first_name = pw.IntegerField() last_name = pw.CharField(max_length=1024, null=True, unique=True) tag = pw.ForeignKeyField(Tag_, on_delete='CASCADE', backref='persons') email = pw.CharField(index=True, unique=True) changes = diff_one(Person, Person_, migrator=migrator) assert len(changes) == 6 assert "on_delete='CASCADE'" in changes[0] assert "backref='persons'" in changes[0] assert changes[-3] == "migrator.drop_not_null('person', 'last_name')" assert changes[-2] == "migrator.drop_index('person', 'last_name')" assert changes[-1] == "migrator.add_index('person', 'last_name', unique=True)" migrator.drop_index('person', 'email') migrator.add_index('person', 'email', unique=True) class Person(pw.Model): first_name = pw.CharField(unique=True) last_name = pw.CharField(max_length=255, index=True) dob = pw.DateField(null=True) birthday = pw.DateField(default=dt.datetime.now) email = pw.CharField(index=True, unique=True) changes = diff_one(Person_, Person, migrator=migrator) assert not changes class Color(pw.Model): id = pw.AutoField() name = pw.CharField(default='red') code = model_to_code(Color) assert "DEFAULT 'red'" in code
def create(self, name='auto', auto=False): """Create a migration.""" migrate = rollback = '' if auto: if isinstance(auto, str): try: auto = import_module(auto) except ImportError: return self.logger.error('Invalid models module: %s', auto) if isinstance(auto, ModuleType): auto = list( filter(lambda m: isinstance(m, type) and issubclass( m, pw.Model)), (getattr(auto, model) for model in dir(auto))) # noqa for migration in self.diff: self.run_one(migration, self.migrator) models1 = auto models2 = list(self.migrator.orm.values()) migrate = diff_many(models1, models2) if not migrate: return self.logger.warn('No changes has found.') migrate = NEWLINE + NEWLINE.join('\n\n'.join(migrate).split('\n')) migrate = CLEAN_RE.sub('\n', migrate) rollback = diff_many(models2, models1) rollback = NEWLINE + NEWLINE.join( '\n\n'.join(rollback).split('\n')) rollback = CLEAN_RE.sub('\n', rollback) self.logger.info('Create a migration "%s"', name) path = self._create(name, migrate, rollback) self.logger.info('Migration has created %s', path) return path
def create(self, name='auto', auto=False): """Create a migration.""" migrate = rollback = '' if auto: if isinstance(auto, str): try: auto = import_module(auto) except ImportError: return self.logger.error('Invalid models module: %s', auto) if isinstance(auto, ModuleType): auto = list(filter( lambda m: isinstance(m, type) and issubclass(m, pw.Model)), (getattr(auto, model) for model in dir(auto))) # noqa for migration in self.diff: self.run_one(migration, self.migrator) models1 = auto models2 = list(self.migrator.orm.values()) migrate = diff_many(models1, models2) if not migrate: return self.logger.warn('No changes has found.') migrate = NEWLINE + NEWLINE.join('\n\n'.join(migrate).split('\n')) migrate = CLEAN_RE.sub('\n', migrate) rollback = diff_many(models2, models1) rollback = NEWLINE + NEWLINE.join('\n\n'.join(rollback).split('\n')) rollback = CLEAN_RE.sub('\n', rollback) self.logger.info('Create a migration "%s"', name) path = self._create(name, migrate, rollback) self.logger.info('Migration has created %s', path) return path
def test_auto(): from peewee_migrate.auto import diff_one, diff_many, model_to_code from peewee_migrate.cli import get_router router = get_router('tests/migrations', 'sqlite:///:memory:') router.run() migrator = router.migrator models = migrator.orm.values() Person_, _ = models code = model_to_code(Person_) assert code changes = diff_many(models, []) assert len(changes) == 2 class Person(pw.Model): first_name = pw.IntegerField() last_name = pw.CharField(max_length=1024) changes = diff_one(Person_, Person) assert len(changes) == 2
def run_db_migration(database): db_storage_dir = os.path.dirname(pavlov_central.storage.__file__) print(f'db_storage_dir={db_storage_dir}') db_migrations_dir = os.path.join(db_storage_dir, 'migrations') print(f'db_migrations_dir={db_migrations_dir}') router = Router(database, migrate_dir=db_migrations_dir, migrate_table='migration', logger=logging.getLogger(), ignore=[ 'basemodel', 'basedatamodel', 'frs5model', 'virtualmodel', 'baseftsmodel', 'ftsmodel', 'lsmtable' ]) print('Run migrations..') router.run() # check migrations src_models = load_models('pavlov_central.storage.models') print(f'src_models={src_models}') if router.ignore: src_models = [ m for m in src_models if m.get_table_name() not in router.ignore ] db_models = router.migrator.orm.values() print(f'db_models={db_models}') diff_found = diff_many(src_models, db_models, router.migrator, reverse=False) if len(diff_found) > 0: logging.warning('migrations diff_found={}'.format(diff_found)) print('migrations diff_found={}'.format(diff_found)) raise RuntimeError('Check db migrations is failed')