Skip to content

Commit

Permalink
Fix for k8s PVC eventual consistency of migrations folder (#69)
Browse files Browse the repository at this point in the history
  • Loading branch information
Amanda Crawford authored Mar 24, 2020
1 parent 70b53e3 commit d07cebc
Showing 1 changed file with 30 additions and 11 deletions.
41 changes: 30 additions & 11 deletions data_resource_api/backwards_compatibility/upgrade_104_to_110.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
import sys
import time
from data_resource_api.app.utils.descriptor import DescriptorsLoader
from data_resource_api.db import Session, Checksum
from data_resource_api.db import Session, Checksum, Migrations
from data_resource_api.app.utils.db_handler import DBHandler
from data_resource_api.logging import LogFactory

Expand Down Expand Up @@ -50,7 +52,7 @@ def check_for_checksum_column():
def check_for_migrations_table():
session = Session()
query = """
SELECT 1
SELECT *
FROM information_schema.tables
WHERE table_name='migrations';
"""
Expand All @@ -62,12 +64,11 @@ def check_for_migrations_table():
count += 1

if count == 1:
print("Found the migrations column -- skipping import")
print("Found the migrations table -- skipping import")
return True

return False


# Create the changes to DB
def upgrade_checksum():
session = Session()
Expand Down Expand Up @@ -120,18 +121,34 @@ def push_descriptors():
finally:
session.close()

def is_migrations_loaded():
session = Session()
result = session.query(Migrations).count()
logger.info(f'Found {result} rows in migrations table')
if result == 0:
session.close()
return False
session.close()
return True


def push_migrations():
migrations = [f for f in os.listdir(MIGRATION_DIR) if f.endswith('.py')]
logger.info('Waiting 60s for migrations directory to load.......')
time.sleep(60)
migrations = [f for f in os.listdir(MIGRATION_DIR) if f.endswith(".py")]
logger.info(f'Found {len(migrations)} in {MIGRATION_DIR} directory')
for file_name in migrations:
if 'create_table_checksum_and_logs' in file_name:
try:
if 'create_table_checksum_and_logs' in file_name:
continue

full_file_path = os.path.join(MIGRATION_DIR, file_name)
with open(full_file_path, 'rb') as file_:
DBHandler.save_migration(file_name, file_.read())
except Exception as e:
logger.exception(f'Error pushing migration files {e}')
continue

full_file_path = os.path.join(MIGRATION_DIR, file_name)
with open(full_file_path, 'rb') as file_:
DBHandler.save_migration(file_name, file_.read())


def main():
if not check_for_checksum_column():
# upgrade the checksum table
Expand All @@ -144,7 +161,9 @@ def main():
# create migration table
create_migrations()
# iter over dir --- push all migrations
if not is_migrations_loaded():
push_migrations()
logger.info("Done with migrations...")

logger.info("Done! You are ready to run the normal Data Model Manager :D")
sys.exit(0)

0 comments on commit d07cebc

Please sign in to comment.