11import os
2+ import sys
3+ import time
24from data_resource_api .app .utils .descriptor import DescriptorsLoader
3- from data_resource_api .db import Session , Checksum
5+ from data_resource_api .db import Session , Checksum , Migrations
46from data_resource_api .app .utils .db_handler import DBHandler
57from data_resource_api .logging import LogFactory
68
@@ -50,7 +52,7 @@ def check_for_checksum_column():
5052def check_for_migrations_table ():
5153 session = Session ()
5254 query = """
53- SELECT 1
55+ SELECT *
5456 FROM information_schema.tables
5557 WHERE table_name='migrations';
5658 """
@@ -62,12 +64,11 @@ def check_for_migrations_table():
6264 count += 1
6365
6466 if count == 1 :
65- print ("Found the migrations column -- skipping import" )
67+ print ("Found the migrations table -- skipping import" )
6668 return True
6769
6870 return False
6971
70-
7172# Create the changes to DB
7273def upgrade_checksum ():
7374 session = Session ()
@@ -120,18 +121,34 @@ def push_descriptors():
120121 finally :
121122 session .close ()
122123
124+ def is_migrations_loaded ():
125+ session = Session ()
126+ result = session .query (Migrations ).count ()
127+ logger .info (f'Found { result } rows in migrations table' )
128+ if result == 0 :
129+ session .close ()
130+ return False
131+ session .close ()
132+ return True
133+
123134
124135def push_migrations ():
125- migrations = [f for f in os .listdir (MIGRATION_DIR ) if f .endswith ('.py' )]
136+ logger .info ('Waiting 60s for migrations directory to load.......' )
137+ time .sleep (60 )
138+ migrations = [f for f in os .listdir (MIGRATION_DIR ) if f .endswith (".py" )]
139+ logger .info (f'Found { len (migrations )} in { MIGRATION_DIR } directory' )
126140 for file_name in migrations :
127- if 'create_table_checksum_and_logs' in file_name :
141+ try :
142+ if 'create_table_checksum_and_logs' in file_name :
143+ continue
144+
145+ full_file_path = os .path .join (MIGRATION_DIR , file_name )
146+ with open (full_file_path , 'rb' ) as file_ :
147+ DBHandler .save_migration (file_name , file_ .read ())
148+ except Exception as e :
149+ logger .exception (f'Error pushing migration files { e } ' )
128150 continue
129151
130- full_file_path = os .path .join (MIGRATION_DIR , file_name )
131- with open (full_file_path , 'rb' ) as file_ :
132- DBHandler .save_migration (file_name , file_ .read ())
133-
134-
135152def main ():
136153 if not check_for_checksum_column ():
137154 # upgrade the checksum table
@@ -144,7 +161,9 @@ def main():
144161 # create migration table
145162 create_migrations ()
146163 # iter over dir --- push all migrations
164+ if not is_migrations_loaded ():
147165 push_migrations ()
148166 logger .info ("Done with migrations..." )
149167
150168 logger .info ("Done! You are ready to run the normal Data Model Manager :D" )
169+ sys .exit (0 )
0 commit comments