def test_get_schema_version(resources, version): base = resources / "db_version" / f"{version}.sqlite" s = Schema(file=base) s.file = base # on verifie par rapport au schema ancien stocké assert (base.parent / (version + ".sql")).read_text() == s.schema
def test_migration_check_success(self, tmpfile, caplogger): ddb = Database(provider="sqlite", filename=str(tmpfile)) init_onetable(ddb) s1 = Schema(ddb) s1.version = "0.5.0" v1 = s1.schema def check_cb(check_db): with db_session: assert ( check_db.execute( "select * from sqlite_master").fetchone()[4] == 'CREATE TABLE "bla" ("key" TEXT NOT NULL PRIMARY KEY, "texta" TEXT, "textb" TEXT, "textc" TEXT)' ) m = MakeMigrations(tmpfile, Version("1.3.2"), migrations) assert m(check_cb, lambda x: True) assert "Error" not in caplogger.read() ddb2 = Database(provider="sqlite", filename=str(tmpfile)) s2 = Schema(ddb2) assert ( s2.schema == """CREATE TABLE "bla" ("key" TEXT NOT NULL PRIMARY KEY, "texta" TEXT, "textb" TEXT, "textc" TEXT)\n""" ) assert s2.version == Version("1.3.2")
def test_version_set(): db = Database(provider="sqlite", filename=":memory:") s = Schema(file=db) assert s.version == Version("0") s.version = Version("12.34.56") with db_session: assert s.db.execute("PRAGMA user_version").fetchone()[0] == 123456 assert s.version == Version("12.34.56")
def test_migrations_same_version_is_cancelled(selfn, tmpfile, caplogger): ddb = Database(provider="sqlite", filename=str(tmpfile)) init_onetable(ddb) s1 = Schema(ddb) s1.version = "1.3.4" m = MakeMigrations(tmpfile, Version("1.3.4"), migrations) assert not hasattr(m, "migrator") m(lambda: True, lambda: True) assert "No migration needed" in caplogger.read()
def test_depuis_version(resources, tmpfilename, caplogger, version): base = resources / "db_version" / f"{version}.sqlite" shutil.copy(base, tmpfilename) assert make_migrations(tmpfilename), caplogger.read() assert (Schema(tmpfilename).framgments == Schema( resources / "db_version" / f"{schema_version}.sqlite").framgments) # test tdes données ok ddb = Database(provider="sqlite", filename=str(tmpfilename)) with db_session_disconnect_db(ddb): assert ddb.execute("select id from Page").fetchall()
def __init__( self, filename: Union[str, Path], # chemin vers la ddb actual_version: Union[ Version, str] = None, # version actuelle (dans les sources) migrations: dict = None, # pool de migrations ): # migrations = migrations self.actual_version = (actual_version if isinstance( actual_version, Version) else Version(actual_version)) self.old_file = Path(filename) # ddb à faire migrer # création d'une base temporaire pour effectuer les migrations tmp = tempfile.NamedTemporaryFile(suffix=".sqlite", delete=False) tmp.close() self.tmp_file = Path(tmp.name) shutil.copy(self.old_file, self.tmp_file) # duplication de la DDB # outils pour migrations self.tmp_db = Database(provider="sqlite", filename=tmp.name) self.schema = Schema(file=self.tmp_db) if self.schema.version == self.actual_version: logger.info(f"version {self.actual_version}: No migration needed") return self.migrator = Migrator(self.tmp_db, self.actual_version, migrations) logger.info( f"starting migrations from version {self.schema.version} to {self.actual_version}" )
def _backup_name(self): old_schema = Schema(file=self.old_file) backup_name = ( f"mycartable_backup-from_{old_schema.version}" f"-to_{self.actual_version}-{datetime.now().isoformat().replace(':','_')}" ) return backup_name
def test_generate_mapping_fail(self, tmpfile, caplogger): ddb = Database(provider="sqlite", filename=str(tmpfile)) Schema(ddb).version = "0.5.0" def check_cb(check_db): with db_session: check_db.execute("select * from NewTable").fetchall() return True def generate_cb(generate_db): raise IndexError() m = MakeMigrations(tmpfile, Version("1.3.2"), {}) assert not m(check_cb, generate_cb) assert "IndexError" in caplogger.read()
def test_generate_mapping_success(self, tmpfile): ddb = Database(provider="sqlite", filename=str(tmpfile)) Schema(ddb).version = "0.5.0" def check_cb(check_db): with db_session: check_db.execute("select * from NewTable").fetchall() return True def generate_cb(generate_db): class NewTable(generate_db.Entity): aaa = Required(int) m = MakeMigrations(tmpfile, Version("1.3.2"), {}) res = m(check_cb, generate_cb) assert res
def test_migration_check_fail(self, tmpfile, caplogger): ddb = Database(provider="sqlite", filename=str(tmpfile)) init_onetable(ddb) s1 = Schema(ddb) s1.version = "0.54" def check_cb(check_db): with db_session: assert ( check_db.execute( "select * from sqlite_master").fetchone()[4] == 'CREATE TABLE "bla" ("key" TEXT NOT NULL PRIMARY KEY, "textX" TEXT, "textb" TEXT, "textc" TEXT)' ) m = MakeMigrations(tmpfile, Version("1.3.2"), migrations) assert not m(check_cb, lambda x: True) assert "AssertionError" in caplogger.read() # check error then no change ddb2 = Database(provider="sqlite", filename=str(tmpfile)) assert Schema(ddb).schema == Schema(ddb2).schema assert Schema(ddb2).version == Version("0.54")
def test_version_get(resources, version): base = resources / "db_version" / f"{version}.sqlite" if version == "1.2.2": # exception pour la premiere version = "0" assert Schema(file=base).version == Version(version)
def store_db_and_schema(self): shutil.move(self.tmp_path, self.sqlite) schema = Schema(self.sqlite) schema.version = self.version schema.to_file(self.sql)
def compare_schema(self): actual_schema = self.sql.read_text() assert ( Schema(self.db).schema == actual_schema ), f"Les schémas actuel et stocké ne correspondent plus. version: {self.version}"
def __init__(self, db: Database, version: Version, migrations: dict): self.db = db self.version = version self.migrations = migrations self.schema = Schema(self.db)
def test_init_bind_create_file_add_schema_version(tmpfilename): ddb = Database() init_bind(ddb, filename=tmpfilename, create_db=True) s = Schema(ddb) assert s.version == Version(schema_version)