Skip to content

Commit

Permalink
add changes to account for timeseries migration errors (#220)
Browse files Browse the repository at this point in the history
* add changes to account for timeseries migration errors

* get rid of unnecessary line
  • Loading branch information
adrian-codecov committed May 15, 2024
1 parent f271d03 commit 5ada529
Showing 1 changed file with 58 additions and 24 deletions.
82 changes: 58 additions & 24 deletions shared/django_apps/legacy_migrations/management/commands/migrate.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,39 +29,70 @@ def release(self):


class Command(MigrateCommand):
def _run_initial_codecov_migrations(self, args, options):
codecov_auth_options = {**options}
codecov_auth_options["fake"] = True
codecov_auth_options["app_label"] = "codecov_auth"
codecov_auth_options["migration_name"] = "0001"

core_options = {**options}
core_options["fake"] = True
core_options["app_label"] = "core"
core_options["migration_name"] = "0001"

reports_options = {**options}
reports_options["fake"] = True
reports_options["app_label"] = "reports"
reports_options["migration_name"] = "0001"

legacy_options = {**options}
legacy_options["app_label"] = "legacy_migrations"
legacy_options["migration_name"] = None

super().handle(*args, **codecov_auth_options)
super().handle(*args, **core_options)
super().handle(*args, **reports_options)
super().handle(*args, **legacy_options)

def _run_initial_timeseries_migrations(self, args, options):
django_auth = {**options}
django_auth["fake"] = True
django_auth["app_label"] = "auth"
django_auth["migration_name"] = "0001"

content_types = {**options}
content_types["fake"] = True
content_types["app_label"] = "contenttypes"
content_types["migration_name"] = "0001"
super().handle(*args, **django_auth)
super().handle(*args, **content_types)

def _fake_initial_migrations(self, cursor, args, options):
try:
cursor.execute("SELECT * FROM django_migrations;")
except ProgrammingError:
codecov_auth_options = {**options}
codecov_auth_options["fake"] = True
codecov_auth_options["app_label"] = "codecov_auth"
codecov_auth_options["migration_name"] = "0001"

core_options = {**options}
core_options["fake"] = True
core_options["app_label"] = "core"
core_options["migration_name"] = "0001"

reports_options = {**options}
reports_options["fake"] = True
reports_options["app_label"] = "reports"
reports_options["migration_name"] = "0001"

legacy_options = {**options}
legacy_options["app_label"] = "legacy_migrations"
legacy_options["migration_name"] = None

super().handle(*args, **codecov_auth_options)
super().handle(*args, **core_options)
super().handle(*args, **reports_options)
super().handle(*args, **legacy_options)
self._run_initial_codecov_migrations(args=args, options=options)

def _fake_initial_timeseries_migrations(self, cursor, args, options):
try:
# If this query doesn't recognize django_migration, nor has less than 2 entries in auth/contenttypes,
# it definitely doesn't have their initial migrations so we would run the initial timeseries migration
cursor.execute(
"SELECT COUNT(*) FROM django_migrations WHERE app = 'auth' or app = 'contenttypes';"
)
result = cursor.fetchone()
if result[0] < 2:
self._run_initial_timeseries_migrations(args=args, options=options)
except:
self._run_initial_codecov_migrations(args=args, options=options)
self._run_initial_timeseries_migrations(args=args, options=options)

def _obtain_lock(self):
"""
In certain environments we might be running mutliple servers that will try and run the migrations at the same time. This is
not safe to do. So we have the command obtain a lock to try and run the migration. If it cannot get a lock, it will wait
until it is able to do so before continuing to run. We need to wait for the lock instead of hard exiting on seeing another
until it is able to do so before continuing to run. We need to
wait for the lock instead of hard exiting on seeing another
server running the migrations because we write code in such a way that the server expects for migrations to be applied before
new code is deployed (but the opposite of new db with old code is fine).
"""
Expand Down Expand Up @@ -98,6 +129,9 @@ def handle(self, *args, **options):
with db_connection.cursor() as cursor:
self._fake_initial_migrations(cursor, args, options)

if database == "timeseries":
self._fake_initial_timeseries_migrations(cursor, args, options)

super().handle(*args, **options)
django_transaction.commit(database)
except:
Expand Down

0 comments on commit 5ada529

Please sign in to comment.