def test_downgrade_not_applied(tmpdir, caplog): manager = DatabaseManager('sqlite:///:memory:', directory=tmpdir) manager.revision() migrations = manager.migration_files manager.downgrade(migrations[0]) assert 'not yet applied: {}'.format(migrations[0]) in caplog.text
def test_migration_0006_to_0005(db_0006_with_data): # Verify we have data with orm.db: metric_0006 = orm.db.execute_sql('SELECT * FROM "Metric"').fetchall() data_0006 = orm.db.execute_sql('SELECT * FROM "Datapoint"').fetchall() migrations = orm.db.execute_sql( 'SELECT * FROM "migration_history"').fetchall() assert len(metric_0006) != 0 assert len(data_0006) != 0 # Make sure we have migration 0006 applied. msg = "Migration 0006 is not applied, it should be." assert any("0006" in m[1] for m in migrations), msg # Then downgrade to 0005. `orm.create_db` doesn't have any downgrade # capability, so we need to use `manager.downgrade()` manager = DatabaseManager(SqliteDatabase(str(db_0006_with_data))) manager.downgrade("0005") with orm.db: metric_0005 = orm.db.execute_sql('SELECT * FROM "Metric"').fetchall() data_0005 = orm.db.execute_sql('SELECT * FROM "Datapoint"').fetchall() migrations = orm.db.execute_sql( 'SELECT * FROM "migration_history"').fetchall() # Ensure that migration 0006 *is not* applied. msg = "Migration 0006 applied when it shouldn't be." assert not any("0006" in m[1] for m in migrations), msg # And that data still matches. assert len(metric_0005) != 0 assert metric_0005 == metric_0006 assert len(data_0005) != 0 assert data_0005 == data_0006
def outdated_db(up_to_date_db): """ Return a database file that is missing some migrations. """ # Rename things to avoid confusion path = up_to_date_db # Start with an up-to-date database, then downgrade manager = DatabaseManager(SqliteDatabase(str(path))) manager.downgrade() yield path
def test_downgrade_single(tmpdir, caplog): manager = DatabaseManager('sqlite:///:memory:', directory=tmpdir) manager.revision() manager.revision() manager.upgrade() migrations = manager.migration_files assert manager.db_migrations == (migrations[0], migrations[1],) assert not manager.diff manager.downgrade() assert 'downgrade: {}'.format(migrations[1]) in caplog.text assert manager.db_migrations == (migrations[0],) assert manager.diff == (migrations[1],)
def test_downgrade_single(tmpdir, caplog): manager = DatabaseManager('sqlite:///:memory:', directory=tmpdir) manager.revision() manager.revision() manager.upgrade() migrations = manager.migration_files assert manager.db_migrations == ( migrations[0], migrations[1], ) assert not manager.diff manager.downgrade() assert 'downgrade: {}'.format(migrations[1]) in caplog.text assert manager.db_migrations == (migrations[0], ) assert manager.diff == (migrations[1], )
def test_downgrade_target_error(tmpdir, caplog): manager = DatabaseManager('sqlite:///:memory:', directory=tmpdir) manager.revision() manager.downgrade('does-not-exist') assert 'could not find migration: does-not-exist' in caplog.text
def test_downgrade_nodiff(tmpdir, caplog): manager = DatabaseManager('sqlite:///:memory:', directory=tmpdir) manager.downgrade() assert 'migrations not yet applied!' in caplog.text