Ejemplo n.º 1
0
 def test_copy_db_4(self, pc_mock):
     """Verify no data is copied if en error is encountered during copying."""
     # Raise an error instead of calling pipe_commands() so that
     # the exception block is entered.
     pc_mock.side_effect = ValueError("Error raised")
     result = mysqldb_basic.copy_db(self.engine, DB2)
     self.assertEqual(result, 1)
Ejemplo n.º 2
0
 def test_copy_db_2(self):
     """Verify no data is copied since databases are the same."""
     before_v1 = test_db_utils.get_data(test_db_utils.version_table_query)
     result = mysqldb_basic.copy_db(self.engine, DB)
     after_v1 = test_db_utils.get_data(test_db_utils.version_table_query)
     with self.subTest():
         self.assertEqual(before_v1[0]["Version"], after_v1[0]["Version"])
     with self.subTest():
         self.assertEqual(result, 0)
Ejemplo n.º 3
0
 def test_copy_db_1(self):
     """Verify data from database1 is copied to database2."""
     before_v1 = test_db_utils.get_data(test_db_utils.version_table_query)
     before_v2 = test_db_utils.get_data(test_db_utils.version_table_query, db=DB2)
     result = mysqldb_basic.copy_db(self.engine, DB2)
     after_v1 = test_db_utils.get_data(test_db_utils.version_table_query)
     after_v2 = test_db_utils.get_data(test_db_utils.version_table_query, db=DB2)
     with self.subTest():
         self.assertNotEqual(before_v1[0]["Version"], before_v2[0]["Version"])
     with self.subTest():
         self.assertEqual(result, 0)
     with self.subTest():
         self.assertEqual(after_v1[0]["Version"], after_v2[0]["Version"])
Ejemplo n.º 4
0
 def test_copy_db_3(self):
     """Verify no data is copied since new database does not exist."""
     result = mysqldb_basic.copy_db(self.engine, DB3)
     self.assertEqual(result, 1)
Ejemplo n.º 5
0
def main(unparsed_args_list):
    """Run main freeze database pipeline."""
    args = parse_args(unparsed_args_list)
    ref_database = args.database
    reset = args.reset
    new_database = args.new_database_name
    prefix = args.prefix

    # Filters input: phage.Status=draft AND phage.HostGenus=Mycobacterium
    # Args structure: [['phage.Status=draft'], ['phage.HostGenus=Mycobacterium']]
    filters = args.filters

    # Create config object with data obtained from file and/or defaults.
    config = configfile.build_complete_config(args.config_file)
    mysql_creds = config["mysql"]

    # Verify database connection and schema compatibility.
    print("Connecting to the MySQL database...")
    alchemist1 = AlchemyHandler(database=ref_database,
                                username=mysql_creds["user"],
                                password=mysql_creds["password"])
    alchemist1.connect(pipeline=True)
    engine1 = alchemist1.engine
    mysqldb.check_schema_compatibility(engine1, "the freeze pipeline")

    # Get SQLAlchemy metadata Table object
    # table_obj.primary_key.columns is a
    # SQLAlchemy ColumnCollection iterable object
    # Set primary key = 'phage.PhageID'
    alchemist1.build_metadata()
    table = querying.get_table(alchemist1.metadata, TARGET_TABLE)
    for column in table.primary_key.columns:
        primary_key = column

    # Create filter object and then add command line filter strings
    db_filter = Filter(alchemist=alchemist1, key=primary_key)
    db_filter.values = []

    # Attempt to add filters and exit if needed.
    add_filters(db_filter, filters)

    # Performs the query
    db_filter.update()

    # db_filter.values now contains list of PhageIDs that pass the filters.
    # Get the number of genomes that will be retained and build the
    # MYSQL DELETE statement.
    keep_set = set(db_filter.values)
    delete_stmt = construct_delete_stmt(TARGET_TABLE, primary_key, keep_set)
    count_query = construct_count_query(TARGET_TABLE, primary_key, keep_set)
    phage_count = mysqldb_basic.scalar(alchemist1.engine, count_query)

    # Determine the name of the new database.
    if new_database is None:
        if prefix is None:
            prefix = get_prefix()
        new_database = f"{prefix}_{phage_count}"

    # Create the new database, but prevent overwriting of current database.
    if engine1.url.database != new_database:
        result = mysqldb_basic.drop_create_db(engine1, new_database)
    else:
        print(
            "Error: names of the reference and frozen databases are the same.")
        print("No database will be created.")
        result = 1

    # Copy database.
    if result == 0:
        print(f"Reference database: {ref_database}")
        print(f"New database: {new_database}")
        result = mysqldb_basic.copy_db(engine1, new_database)
        if result == 0:
            print(f"Deleting genomes...")
            alchemist2 = AlchemyHandler(database=new_database,
                                        username=engine1.url.username,
                                        password=engine1.url.password)
            alchemist2.connect(pipeline=True)
            engine2 = alchemist2.engine
            engine2.execute(delete_stmt)
            if reset:
                engine2.execute(RESET_VERSION)

            # Close up all connections in the connection pool.
            engine2.dispose()
        else:
            print("Unable to copy the database.")
        # Close up all connections in the connection pool.
        engine1.dispose()
    else:
        print(f"Error creating new database: {new_database}.")
    print("Freeze database script completed.")
Ejemplo n.º 6
0
def main(unparsed_args_list):
    """Run main conversion pipeline."""
    # Parse command line arguments
    args = parse_args(unparsed_args_list)
    config = configfile.build_complete_config(args.config_file)
    mysql_creds = config["mysql"]
    alchemist1 = AlchemyHandler(database=args.database,
                                username=mysql_creds["user"],
                                password=mysql_creds["password"])
    alchemist1.connect(pipeline=True)
    engine1 = alchemist1.engine


    target = args.schema_version
    actual = mysqldb.get_schema_version(engine1)
    steps, dir = get_conversion_direction(actual, target)

    # Iterate through list of versions and implement SQL files.
    if dir == "none":
        if args.verbose == True:
            print("No schema conversion is needed.")
        convert = False
    else:
        convert = True

    if convert == True:
        if (args.new_database_name is not None and
                args.new_database_name != args.database):
            result = mysqldb_basic.drop_create_db(engine1, args.new_database_name)
            if result == 0:
                result = mysqldb_basic.copy_db(engine1, args.new_database_name)
                if result == 0:
                    # Create a new connection to the new database.
                    alchemist2 = AlchemyHandler(database=args.new_database_name,
                                                username=engine1.url.username,
                                                password=engine1.url.password)
                    alchemist2.connect(pipeline=True)
                    engine2 = alchemist2.engine

                else:
                    print("Error: Unable to copy the database for conversion.")
                    convert = False
            else:
                print("Error: Unable to create the new database for conversion.")
                convert = False
        else:
            engine2 = engine1

        if convert == True:
            stop_step, summary = convert_schema(engine2, actual, dir,
                                                steps, verbose=args.verbose)
            engine2.dispose()
            if stop_step == target:
                if args.verbose == True:
                    print("\n\nThe database schema conversion was successful.")
            else:
                print("\n\nError: "
                      "The database schema conversion was not successful. "
                      f"Unable to proceed past schema version {stop_step}.")
            if args.verbose == True:
                print_summary(summary)
    engine1.dispose()