コード例 #1
0
ファイル: managers.py プロジェクト: MBuczek/interceptor
 def get_files_from_source_folder(self):
     self._sf_files = [file for file in listdir(self._source_folder_path)
                       if isfile(self._source_folder_path + "/" + file) and
                       os.path.splitext(file)[1] == ".sql" or
                       os.path.splitext(file)[1] == ".txt"]
     errors = self.validate_files_name(self._sf_files)
     if errors:
         raise WrongFileNameException("\nError: Wrong name for files: " + data_to_plain_string(errors) +
                                      "\nThe file must start with a number, "
                                      "and the number part must be separated from the rest by __ \n")
     return sorted(self._sf_files, key=lambda z: z.split("__")[0])
コード例 #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()