Example #1
0
 def tearDownClass(cls):
     db = get_db_instance(client=MongoClient(test_config["mongo_uri"]),
                          cfg=test_config)
     col = db[test_config["migrations_db_name"]][
         test_config["migrations_coll_name"]]
     col.delete_one({"_id": cls.id1})
     col.delete_one({"_id": cls.id2})
     col.delete_one({"_id": cls.id3})
Example #2
0
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...")
Example #3
0
def revert_peacefully(mig_repo, parsed_migration_items):
    last_mig = mig_repo.get_last_migration()
    if last_mig:
        items = last_mig.migration_items
        migs_to_be_applied = query(parsed_migration_items) \
            .where(lambda pmig: pmig.file_name in items) \
            .order_by_descending(lambda pmig: pmig.order).to_list()

        while len(migs_to_be_applied) > 0:
            pmig = migs_to_be_applied.pop(0)
            print_green(
                f"REVERT: Migration file {pmig.file_name} is going to be applied..."
            )
            pmig.migration.fail(get_db_instance())

        last_mig.make_reverted()
        mig_repo.update_migration(last_mig.to_document())

    else:
        print_red(
            "Revert failed: There is no migration that can be reverted in the mongo collection.."
        )
 def test_it_should_return_db_with_given_cfg_and_client(self):
     db = get_db_instance(client={"test_db": "expected"},
                          cfg={"db_name": "test_db"})
     self.assertEqual(db, "expected")
 def test_it_should_return_db_with_default_cfg(self, get_config_mock):
     get_config_mock.return_value = {"db_name": "test_db"}
     db = get_db_instance(client={"test_db": "expected"})
     self.assertEqual(db, "expected")