Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit 2881e25

Browse files
Implementação de migration/boot de banco de dados
1 parent e9d1eef commit 2881e25

File tree

4 files changed

+134
-4
lines changed

4 files changed

+134
-4
lines changed
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
echo 'Booting database...'
2-
echo 'Connection to database...'
3-
# ./scripts/venv.sh
2+
# TODO futuramente usar o Flask migrate ou outra alternativa
3+
4+
echo 'Creating tables...'
5+
python3 ./scripts/migrations/mysql/migrate.py ./tests/datasets/database/structure/mysql/create.table.store.ocorens.sql
6+
python3 ./scripts/migrations/mysql/migrate.py ./tests/datasets/database/structure/mysql/create.table.store.products.sql
47

5-
echo 'Creating database...'
6-
echo 'Creating table...'
78
echo 'Inserting data in the table...'
9+
python3 ./scripts/migrations/mysql/migrate.py ./tests/datasets/database/seeders/mysql/seeder.table.store.ocorens.sql
10+
python3 ./scripts/migrations/mysql/migrate.py ./tests/datasets/database/seeders/mysql/seeder.table.store.products.sql

examples/lambda_sqs/scripts/boot.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ echo 'Validating jd installation..'
1212
echo 'Validate connection'
1313
./scripts/boot-validate-connection.sh
1414

15+
echo 'Booting db...'
16+
./scripts/boot-db.sh
17+
1518
echo 'Create the queues...'
1619
./scripts/boot-queues.sh
1720

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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')

examples/lambda_sqs/tests/component/helpers/database/mysql_helper.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ def sow_table(connection, table_name, file_name):
7171
line = seeder_file.readline().strip().replace(';', '')
7272

7373
connection.commit()
74+
result = True
7475
except Exception as ex:
7576
result = False
7677
connection.rollback()

0 commit comments

Comments
 (0)