コード例 #1
0
ファイル: commands.py プロジェクト: MBuczek/interceptor
    def execute(self, command_args):
        if len(command_args) != 3:
            sys.stdout.write(
                'Error in executing command: "{}", Init command requires 2 arguments: "REPOSITORY_NAME", '
                '"PASSWORD_TO_DATABASE"'.format(command_to_plain_string(command_args).strip())
            )
        else:
            repo_name = command_args[1]
            db_password = command_args[2]
            repository = Repository.read_by_name(repo_name)
            host = repository.host
            port = repository.port
            database_driver = repository.database_driver
            database_name = repository.database_name
            username = repository.username

            driver_resolver = DBResolver()
            driver_resolver.resolve_db_driver(database_driver)

            if host != "localhost":
                host = host + ":" + port

            try:
                connection = driver_resolver.connect_to_db(
                    dbname=database_name, user=username, host=host, password=db_password
                )
            except OperationalError:
                sys.stdout.write("Error: Can't connect to database: {} on host: {}".format(database_name, host))
                return

            cursor = connection.cursor()

            try:
                cursor.execute(driver_resolver.get_init_query())
                connection.commit()
            except ProgrammingError:
                console_out("Info: Migration table already exists")

            connection.close()
コード例 #2
0
ファイル: commands.py プロジェクト: MBuczek/interceptor
    def execute(self, command_args):
        repo_name = command_args[1]
        db_password = command_args[2]

        repo = Repository.read_by_name(repo_name)

        scripts_path = repo.scripts_path
        host = repo.host
        port = repo.port
        database_driver = repo.database_driver
        database_name = repo.database_name
        username = repo.username

        driver_resolver = DBResolver()
        driver_resolver.resolve_db_driver(database_driver)

        if host != "localhost":
            host = host + ":" + port

        try:
            connection = driver_resolver.connect_to_db(
                dbname=database_name, user=username, host=host, password=db_password
            )
        except OperationalError:
            console_out("\nError: Can't connect to database: {} on host: {}".format(database_name, host))
            return
        except DatabaseDriverNotResolved:
            console_out(
                "\nError: Database driver not resolved. Can't connect to database: {} on host: {}".format(
                    database_name, host
                )
            )
            return

        cursor = connection.cursor()

        fm = FileManager(connection, scripts_path)
        fm.get_files_in_db()
        files_that_miss = fm.get_files_that_miss()

        try:
            fm.get_files_from_source_folder()
        except WrongFileNameException as ex:
            console_out(ex.msg)
            return

        files_to_execute = fm.get_files_to_execute()

        if files_that_miss:
            console_out("\nWarning: There are missing files: " + data_to_plain_string(files_that_miss))

        if files_to_execute is None:
            console_out("\nInfo: There are no files to execute.")
            return

        console_out("\nInfo: Executing files: " + data_to_plain_string(files_to_execute))
        gen = fm.read_files(files_to_execute)
        while True:
            try:
                cursor.execute(next(gen))
            except StopIteration:
                break

        for file in files_to_execute:
            cursor.execute(
                'INSERT INTO db_interceptor_migrations ("file_name","applied") VALUES (\''
                + str(file)
                + "','"
                + datetime.now().strftime("%Y/%m/%d %H:%M:%S")
                + "')"
            )

        connection.commit()
        connection.close()