|
| 1 | +import sys |
| 2 | +import re |
| 3 | +import os |
| 4 | +import logging |
| 5 | + |
| 6 | +os.environ["LOG_LEVEL"] = logging.getLevelName(logging.INFO) |
| 7 | + |
| 8 | +if __package__: |
| 9 | + current_path = os.path.abspath(os.path.dirname(__file__)).replace('/' + str(__package__), '', 1) |
| 10 | +else: |
| 11 | + current_path = os.path.abspath(os.path.dirname(__file__)) |
| 12 | + |
| 13 | +if not current_path[-1] == '/': |
| 14 | + current_path += '/' |
| 15 | + |
| 16 | +ROOT_DIR = current_path.replace('scripts/migrations/mysql/', '') |
| 17 | + |
| 18 | +_REGISTERED_PATHS = False |
| 19 | + |
| 20 | + |
| 21 | +def register_paths(): |
| 22 | + global _REGISTERED_PATHS |
| 23 | + if not _REGISTERED_PATHS: |
| 24 | + # path fixes, define the priority of the modules search |
| 25 | + sys.path.insert(0, ROOT_DIR) |
| 26 | + sys.path.insert(0, ROOT_DIR + 'venv/') |
| 27 | + sys.path.insert(1, ROOT_DIR + 'chalicelib/') |
| 28 | + sys.path.insert(1, ROOT_DIR + 'flask_app/') |
| 29 | + sys.path.insert(1, ROOT_DIR + 'lambda_app/') |
| 30 | + sys.path.insert(2, ROOT_DIR + 'vendor/') |
| 31 | + _REGISTERED_PATHS = True |
| 32 | + pass |
| 33 | + |
| 34 | + |
| 35 | +# register the paths |
| 36 | +register_paths() |
| 37 | + |
| 38 | +from lambda_app.logging import get_logger |
| 39 | +from lambda_app.boot import reset, load_dot_env, load_env |
| 40 | +from lambda_app.config import reset as reset_config, get_config |
| 41 | +from tests.component.helpers.database.mysql_helper import MySQLHelper |
| 42 | + |
| 43 | +logger = get_logger() |
| 44 | +logger.info("ROOT_DIR " + ROOT_DIR) |
| 45 | + |
| 46 | +command = None |
| 47 | +sql_file = None |
| 48 | +try: |
| 49 | + sql_file = sys.argv[1] |
| 50 | +except IndexError as err: |
| 51 | + logger.error(err) |
| 52 | + exit('Filename required') |
| 53 | + |
| 54 | +try: |
| 55 | + logger.info("Load configuration") |
| 56 | + # reset config and env |
| 57 | + reset() |
| 58 | + reset_config() |
| 59 | + # load integration |
| 60 | + APP_TYPE = os.environ['APP_TYPE'] |
| 61 | + if APP_TYPE == 'Flask': |
| 62 | + load_dot_env() |
| 63 | + else: |
| 64 | + load_env() |
| 65 | + config = get_config() |
| 66 | +except IndexError as err: |
| 67 | + logger.error(err) |
| 68 | + exit('Filename required') |
| 69 | + |
| 70 | + |
| 71 | +class Command: |
| 72 | + CREATE_TABLE = 'CREATE_TABLE' |
| 73 | + INSERT_INTO = 'INSERT_INTO' |
| 74 | + |
| 75 | + |
| 76 | +def get_commnad(line): |
| 77 | + command = None |
| 78 | + if "CREATE TABLE" in line: |
| 79 | + command = Command.CREATE_TABLE |
| 80 | + if "INSERT INTO" in line: |
| 81 | + command = Command.INSERT_INTO |
| 82 | + return command |
| 83 | + |
| 84 | + |
| 85 | +def get_table_name(content, only_table=False): |
| 86 | + table_name = None |
| 87 | + rx = re.search("CREATE TABLE (IF NOT EXISTS )?([\w.]+)", content) |
| 88 | + if not rx: |
| 89 | + rx = re.search("INSERT INTO ([\w.]+)", content) |
| 90 | + if rx: |
| 91 | + groups = rx.groups() |
| 92 | + if len(groups) > 0: |
| 93 | + table_name = groups[len(groups)-1] |
| 94 | + |
| 95 | + if table_name and only_table: |
| 96 | + table_name_parts = table_name.split('.') |
| 97 | + table_name = table_name_parts[len(table_name_parts)-1] |
| 98 | + return table_name |
| 99 | + |
| 100 | + |
| 101 | +try: |
| 102 | + connection = MySQLHelper.get_connection() |
| 103 | + with open(ROOT_DIR + sql_file, 'r') as f: |
| 104 | + content = f.read() |
| 105 | + f.close() |
| 106 | + |
| 107 | + command = get_commnad(content) |
| 108 | + file_name = ROOT_DIR + sql_file |
| 109 | + if command == Command.CREATE_TABLE: |
| 110 | + table_name = get_table_name(content) |
| 111 | + created = MySQLHelper.create_table(connection, table_name, file_name) |
| 112 | + if created is not False: |
| 113 | + logger.info("Table created") |
| 114 | + if command == Command.INSERT_INTO: |
| 115 | + table_name = get_table_name(content) |
| 116 | + sow = MySQLHelper.sow_table(connection, table_name, file_name) |
| 117 | + if sow is not False: |
| 118 | + logger.info("Table sow") |
| 119 | + |
| 120 | + |
| 121 | +except Exception as err: |
| 122 | + logger.error(err) |
| 123 | + exit('File not found') |
0 commit comments