Skip to content

Commit

Permalink
Switch database migration engine (#546)
Browse files Browse the repository at this point in the history
* switch to django's migration engine

* remove aerich

* update documentation

* generate default models

* skip path migration when there is no table found

* run migrations before running the bot

* missing line break

* handle missing config file

* test making a new migration

* models: add credits column

* Revert "models: add credits column"

This reverts commit 3221a5f.

* Properly add credits column this time
  • Loading branch information
laggron42 authored Feb 19, 2025
1 parent 7806d48 commit ddf1407
Show file tree
Hide file tree
Showing 52 changed files with 816 additions and 487 deletions.
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ repos:
- Pillow
- prometheus_client
- tortoise-orm
- aerich==0.6.3
- redis
- django
- dj_database_url
Expand Down
14 changes: 10 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,16 @@ Their configurations are already written in `pyproject.toml`, so it should work

## Migrations

> [!CAUTION]
> The migration engine we've been using in the past is severely broken, and will be removed soon
> in favor of Django's migration engine. In the meantime, all PRs that require database migrations
> are put on hold.
If you are modifying models definition, you need migrations to update the database schema.

First, synchronize your changes between `ballsdex/core/models.py` and
`admin_panel/bd_models/models.py`, they must be identical!

Then you can run `python3 manage.py makemigrations` to generate a migration file. Re-read its
contents to ensure there is only what you modified, and commit it.

You can read more about migrations
[here](https://docs.djangoproject.com/en/5.1/topics/migrations/), the engine is very extensive!

## Coding style

Expand Down
10 changes: 9 additions & 1 deletion admin_panel/admin_panel/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@

from ballsdex.settings import read_settings, settings

read_settings(Path("../config.yml"))
try:
read_settings(Path("../config.yml"))
except FileNotFoundError:
from rich import print

print(
"[yellow][bold]Could not find ../config.yml file.[/bold] "
"Please run the bot once to generate this file.[/yellow]"
)

BASE_DIR = Path(__file__).resolve().parent.parent

Expand Down
10 changes: 9 additions & 1 deletion admin_panel/bd_models/migrations/0002_move_upload_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from typing import TYPE_CHECKING

from bd_models.models import Ball, Economy, Regime, Special
from django.db import migrations
from django.db import connection, migrations
from django.db.models import Case, ImageField, When
from django.db.models.expressions import F, Value
from django.db.models.functions import Concat, Replace
Expand Down Expand Up @@ -60,6 +60,10 @@ def _check_reserved_names():


def move_forwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
# only run this migration if there are existing tables (migrating from aerich)
if "ball" not in connection.introspection.table_names():
return

# Run this first, as outdated objects are created by aerich's initial migrations
Regime.objects.update(**_replace_text("background"))
Special.objects.update(**_replace_text("background"))
Expand All @@ -80,6 +84,10 @@ def move_forwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):


def move_backwards(apps: "Apps", schema_editor: "BaseDatabaseSchemaEditor"):
# only run this migration if the tables weren't deleted
if "ball" not in connection.introspection.table_names():
return

Ball.objects.update(
**_replace_text("wild_card", reverse=True),
**_replace_text("collection_card", reverse=True),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
# Generated by Django 5.1.4 on 2025-01-28 14:44

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
("bd_models", "0002_move_upload_files"),
]

operations = [
migrations.DeleteModel(
name="Ball",
),
migrations.DeleteModel(
name="BallInstance",
),
migrations.DeleteModel(
name="BlacklistedGuild",
),
migrations.DeleteModel(
name="BlacklistedID",
),
migrations.DeleteModel(
name="BlacklistHistory",
),
migrations.DeleteModel(
name="Block",
),
migrations.DeleteModel(
name="Economy",
),
migrations.DeleteModel(
name="Friendship",
),
migrations.DeleteModel(
name="Guildconfig",
),
migrations.DeleteModel(
name="Player",
),
migrations.DeleteModel(
name="Regime",
),
migrations.DeleteModel(
name="Special",
),
migrations.DeleteModel(
name="Trade",
),
migrations.DeleteModel(
name="Tradeobject",
),
]
Loading

0 comments on commit ddf1407

Please sign in to comment.