예제 #1
0
def install_db(database, db_filepath=None, schema_version=None):
    """Install database. If database already exists, it is first removed."""
    engine1, msg = mysqldb.get_engine(database="", echo=False)
    if engine1 is None:
        print("Invalid MySQL credentials.")
    else:
        result2 = mysqldb.drop_create_db(engine1, database)
        if result2 != 0:
            print("Unable to create new, empty database.")
        else:
            engine2, msg = mysqldb.get_engine(database=database,
                                              username=engine1.url.username,
                                              password=engine1.url.password,
                                              echo=False)
            if engine2 is None:
                print(f"No connection to the {database} database due "
                      "to invalid credentials or database.")
            else:
                if db_filepath is not None:
                    mysqldb.install_db(engine2, db_filepath)
                else:
                    mysqldb.execute_transaction(engine2, db_schema_0.STATEMENTS)
                    convert_args = ["pdm_utils.run", "convert", database,
                                    "-s", str(schema_version)]
                    convert_db.main(convert_args, engine2)
                # Close up all connections in the connection pool.
                engine2.dispose()
        # Close up all connections in the connection pool.
        engine1.dispose()
예제 #2
0
def main(unparsed_args_list, engine1=None):
    """Run main conversion pipeline."""
    # Parse command line arguments
    args = parse_args(unparsed_args_list)
    if engine1 is None:
        engine1 = mysqldb.connect_to_db(args.database)
    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.drop_create_db(engine1, args.new_database_name)
            if result == 0:
                result = mysqldb.copy_db(engine1, args.new_database_name)
                if result == 0:
                    # Create a new connection to the new database.
                    engine2, msg = mysqldb.get_engine(
                        database=args.new_database_name,
                        username=engine1.url.username,
                        password=engine1.url.password,
                        echo=False)
                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()
예제 #3
0
 def test_get_engine_10(self, input_mock):
     """Verify that engine is returned when database is provided and
     more than one attempt is needed for database."""
     input_mock.side_effect = ["invalid", db2]
     engine, msg = mysqldb.get_engine(username=user,
                                      password=pwd,
                                      attempts=3)
     with self.subTest():
         self.assertIsNotNone(engine)
예제 #4
0
 def test_get_engine_8(self, getpass_mock):
     """Verify that engine is returned when database is provided and
     more than one attempt is needed for password."""
     getpass_mock.side_effect = [user, "invalid", user, pwd]
     engine, msg = mysqldb.get_engine(database=db2, attempts=3)
     with self.subTest():
         self.assertTrue(getpass_mock.called)
     with self.subTest():
         self.assertIsNotNone(engine)
예제 #5
0
 def test_get_engine_6(self, getpass_mock):
     """Verify that no engine is returned when database is not provided and
     invalid user."""
     getpass_mock.side_effect = ["invalid", pwd]
     engine, msg = mysqldb.get_engine(database="", attempts=1)
     with self.subTest():
         self.assertTrue(getpass_mock.called)
     with self.subTest():
         self.assertIsNone(engine)
예제 #6
0
 def test_get_engine_4(self, getpass_mock):
     """Verify that no engine is returned when database is provided and
     invalid password."""
     getpass_mock.side_effect = [user, "invalid"]
     engine, msg = mysqldb.get_engine(database=db2, attempts=1)
     with self.subTest():
         self.assertTrue(getpass_mock.called)
     with self.subTest():
         self.assertIsNone(engine)
예제 #7
0
 def test_get_engine_2(self, getpass_mock):
     """Verify that engine returned with valid info
     when database is not provided."""
     getpass_mock.side_effect = [user, pwd]
     engine, msg = mysqldb.get_engine(database="")
     with self.subTest():
         self.assertTrue(getpass_mock.called)
     with self.subTest():
         self.assertIsNotNone(engine)
예제 #8
0
 def test_get_engine_9(self, input_mock):
     """Verify that engine is returned when database is provided by input."""
     input_mock.side_effect = [db2]
     engine, msg = mysqldb.get_engine(username=user,
                                      password=pwd,
                                      attempts=1)
     with self.subTest():
         self.assertTrue(input_mock.called)
     with self.subTest():
         self.assertIsNotNone(engine)
예제 #9
0
def main(unparsed_args_list):
    """Run main freeze database pipeline."""
    args = parse_args(unparsed_args_list)
    # engine1, msg = mysqldb.get_engine(database=args.database, echo=False)

    # Verify database connection and schema compatibility.
    print("Connecting to the MySQL database...")
    engine1 = mysqldb.connect_to_db(args.database)
    mysqldb.check_schema_compatibility(engine1, "the freeze pipeline")

    # Get the number of draft genomes.
    query = "SELECT count(*) as count FROM phage WHERE Status != 'draft'"
    result = engine1.execute(query).fetchall()
    phage_count = result[0]["count"]

    # Create the new frozen database folder e.g. Actinobacteriophage_XYZ.
    prefix = get_prefix()
    new_database = f"{prefix}_{phage_count}"

    # Create the new database
    result = mysqldb.drop_create_db(engine1, new_database)

    # Copy database.
    if result == 0:
        result = mysqldb.copy_db(engine1, new_database)
        if result == 0:
            print(f"Deleting 'draft' genomes...")
            engine2, msg = mysqldb.get_engine(username=engine1.url.username,
                                              password=engine1.url.password,
                                              database=new_database)

            statement3 = "DELETE FROM phage WHERE Status = 'draft'"
            engine2.execute(statement3)
            statement4 = "UPDATE version SET Version = 0"
            engine2.execute(statement4)
            # 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.")