Skip to content

Commit

Permalink
migrations: move aerich check before initial
Browse files Browse the repository at this point in the history
--fake-initial would skip those checks
  • Loading branch information
laggron42 committed Feb 21, 2025
1 parent 38a6703 commit beaee0e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 52 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,58 @@
# Generated by Django 5.1.4 on 2025-01-28 14:44

from django.db import migrations
import sys
from typing import TYPE_CHECKING

from django.db import connection, migrations

if TYPE_CHECKING:
from django.apps.registry import Apps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def execute_aerich_migration_check(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
with connection.cursor() as cursor:
cursor.execute(
"SELECT EXISTS(SELECT * FROM information_schema.tables "
"WHERE table_schema = 'public' AND table_name = 'aerich')"
)
has_aerich = cursor.fetchone()
assert has_aerich
if not has_aerich[0]:
# aerich presence not detected, new instance, keep going
return

cursor.execute("SELECT version FROM aerich ORDER BY id DESC")
version = cursor.fetchone()
assert version
if version[0] != "38_20250120182312_update.sql":
raise RuntimeError(
"Aerich migrations exists and are not up to date, cannot safely proceed!!\n\n"
"The old migration engine is being removed in favor of Django's own, you must "
"either have an empty table or have a perfectly up-to-date database to migrate.\n"
"The resolution is to roll back to version 2.24.2, run the bot once to complete "
"all migrations, then upgrade to the latest version.\n\n"
"Commands to run:\n"
" git checkout release/2.24.2\n"
" docker compose up --build bot\n"
' [Press Ctrl+C after seeing the line "Ran X migrations: ...."]\n'
" git switch -\n"
" docker compose up --build admin-panel\n"
" [Ensure all migrations complete successfully]"
)
elif "--fake-initial" not in sys.argv:
raise RuntimeError(
"Detected up-to-date Aerich table but --fake-initial flag missing\n\n"
"You are about to migrate from the Aerich migration engine to Django's own. "
"Your database is up-to-date, but you must run this command with the "
"--fake-initial flag to instruct Django to not override existing tables.\n\n"
'Please run "python3 manage.py migrate --fake-initial" to pass this migration.'
)


def aerich_move_backwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
# allows reversing the migration
pass


class Migration(migrations.Migration):
Expand All @@ -10,6 +62,10 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(
code=execute_aerich_migration_check,
reverse_code=aerich_move_backwards,
),
migrations.DeleteModel(
name="Ball",
),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,62 +1,16 @@
# Generated by Django 5.1.4 on 2025-02-12 11:31

import sys
from typing import TYPE_CHECKING

import django.db.models.deletion
from bd_models.models import Economy, Regime, Special
from django.db import connection, migrations, models
from django.db import migrations, models

if TYPE_CHECKING:
from django.apps.registry import Apps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor


def execute_aerich_migration_check(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
with connection.cursor() as cursor:
cursor.execute(
"SELECT EXISTS(SELECT * FROM information_schema.tables "
"WHERE table_schema = 'public' AND table_name = 'aerich')"
)
has_aerich = cursor.fetchone()
assert has_aerich
if not has_aerich[0]:
# aerich presence not detected, new instance, keep going
return

cursor.execute("SELECT version FROM aerich ORDER BY id DESC")
version = cursor.fetchone()
assert version
if version[0] != "38_20250120182312_update.sql":
raise RuntimeError(
"Aerich migrations exists and are not up to date, cannot safely proceed!!\n\n"
"The old migration engine is being removed in favor of Django's own, you must "
"either have an empty table or have a perfectly up-to-date database to migrate.\n"
"The resolution is to roll back to version 2.24.2, run the bot once to complete "
"all migrations, then upgrade to the latest version.\n\n"
"Commands to run:\n"
" git checkout release/2.24.2\n"
" docker compose up --build bot\n"
' [Press Ctrl+C after seeing the line "Ran X migrations: ...."]\n'
" git switch -\n"
" docker compose up --build admin-panel\n"
" [Ensure all migrations complete successfully]"
)
elif "--fake-initial" not in sys.argv:
raise RuntimeError(
"Detected up-to-date Aerich table but --fake-initial flag missing\n\n"
"You are about to migrate from the Aerich migration engine to Django's own. "
"Your database is up-to-date, but you must run this command with the "
"--fake-initial flag to instruct Django to not override existing tables.\n\n"
'Please run "python3 manage.py migrate --fake-initial" to pass this migration.'
)


def aerich_move_backwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
# allows reversing the migration
pass


def default_models_forward(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
default_economies = {
"Capitalist": "capitalist.png",
Expand Down Expand Up @@ -103,10 +57,6 @@ class Migration(migrations.Migration):
]

operations = [
migrations.RunPython(
code=execute_aerich_migration_check,
reverse_code=aerich_move_backwards,
),
migrations.CreateModel(
name="BlacklistedGuild",
fields=[
Expand Down

0 comments on commit beaee0e

Please sign in to comment.