def setUpClass(cls): cls.repo = MigrationRepository(client=MongoClient( test_config["mongo_uri"]), cfg=test_config) mig1 = Migration(["2-b.py"]) cls.id1 = cls.repo.create(mig1) mig1.make_reverted() cls.last_mig = cls.repo.update_migration(mig1.to_document())
def setUpClass(cls): cls.repo = MigrationRepository(client=MongoClient(test_config["mongo_uri"]), cfg=test_config) mig1 = Migration(["2-b.py"]) time.sleep(0.5) mig2 = Migration(["1-a.py"]) cls.id1 = cls.repo.create(mig1) cls.id2 = cls.repo.create(mig2) cls.last_mig = cls.repo.get_last_migration()
def update_migration(self, mig_doc): updated = self._collection.find_one_and_replace( {"_id": mig_doc["_id"]}, mig_doc, return_document=ReturnDocument.AFTER) if updated: return Migration.load(updated) else: return None
def get_last_migration(self) -> Optional[Migration]: raw_last_mig = [ m for m in self._collection.find({ "is_reverted": False }).sort([("created_at", -1)]).limit(1) ] if len(raw_last_mig) != 0: return Migration.load(raw_last_mig[0]) else: return None
def prepare_mocks(cls): cls.mock_repo = MigrationRepository(cfg=MagicMock(), client=MagicMock()) cls.mock_repo.update_migration = MagicMock() cls.last_mig = Migration(["02-b.py", "03-c.py"]) cls.last_mig.make_reverted = MagicMock() cls.mock_repo.get_last_migration = MagicMock(return_value=cls.last_mig) cls.parsed1 = ParsedMigrationItem(FakeUserMigrationFile(), "01-a.py", 1) cls.parsed1.migration.fail = MagicMock() cls.parsed2 = ParsedMigrationItem(FakeUserMigrationFile(), "02-b.py", 2) cls.parsed2.migration.fail = MagicMock() cls.parsed3 = ParsedMigrationItem(FakeUserMigrationFile(), "03-c.py", 3) cls.parsed3.migration.fail = MagicMock()
def setUpClass(cls): cls.repo = MigrationRepository(client=MongoClient(test_config["mongo_uri"]), cfg=test_config) mig1 = Migration(["1-a.py"]) mig2 = Migration(["2-b.py"]) mig3 = Migration(["3-c.py"]) mig3.make_reverted() cls.id1 = cls.repo.create(mig1) cls.id2 = cls.repo.create(mig2) cls.id3 = cls.repo.create(mig3) cls.already_done_files = cls.repo.get_already_done_items(["1-a.py", "3-c.py"])
def get_already_done_items(self, file_names): result = [] for raw_mig in self._collection.find({ "migration_items": { "$in": file_names }, "is_reverted": False }): migration = Migration.load(raw_mig) result.append(migration) already_done_mig_files = set() for mig in result: for item in mig.migration_items: already_done_mig_files.add(item) return already_done_mig_files
def migrate_them_all(mig_repo, parsed_migration_items): already_done_mig_files = mig_repo.get_already_done_items( [p.file_name for p in parsed_migration_items]) migs_to_be_applied = query(parsed_migration_items) \ .where(lambda pmig: pmig.file_name not in already_done_mig_files) \ .order_by(lambda pmig: pmig.order).to_list() files_to_be_migrated = query(migs_to_be_applied).select( lambda pm: pm.file_name).to_list() while len(migs_to_be_applied) > 0: parsed_migration = migs_to_be_applied.pop(0) print_green( f"Migration file {parsed_migration.file_name} is going to be applied..." ) parsed_migration.migration.success(get_db_instance()) if len(files_to_be_migrated) > 0: migration = Migration(files_to_be_migrated) mig_repo.create(migration) else: print_green("All the migration files already applied, skipping...")
def setUpClass(cls): cls.repo = MigrationRepository(client=MongoClient( test_config["mongo_uri"]), cfg=test_config) cls.migration_for_test = Migration(["01-a.py"]) cls.fetched_id = cls.repo.create(cls.migration_for_test)
def create(self, migration: Migration): return self._collection.insert_one(migration.to_document()).inserted_id