Skip to content

Commit

Permalink
fix: make migrations applicable on fresh and old env
Browse files Browse the repository at this point in the history
There is a problem when running migrations users.0022 migraition on
fresh env since there is a custom code which touches the models and
those fields haven't been yet fully updated until in the 0034 migration.
So this tweaks those migrations dependencies based on migration apply
info.

Refs HP-2441
  • Loading branch information
nicobav committed Jul 12, 2024
1 parent 1a1dfc9 commit 93989dc
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
22 changes: 18 additions & 4 deletions users/migrations/0020_populate_cors_allowed_origins.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Generated by Django 2.1.11 on 2019-09-02 13:05

from django.db import migrations
from django.db import migrations, ProgrammingError

from users.signals import generate_and_save_allowed_origins_from_client_configurations

Expand All @@ -12,12 +12,26 @@ def populate_allowed_origin(apps, schema_editor):
def _noop(apps, schema_editor):
return

from django.db import connection

def check_migration_applied(app_name, migration_name):
try:
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM django_migrations WHERE app=%s AND name=%s", [app_name, migration_name])
row = cursor.fetchone()
except ProgrammingError:
return False
return bool(row)

def get_dependencies():
if check_migration_applied("users", "0020_populate_cors_allowed_origins"):
return [("users", "0019_allowedorigin")]
return [("users", "0034_application_algorithm_and_more")]


class Migration(migrations.Migration):

dependencies = [
('users', '0019_allowedorigin'),
]
dependencies = get_dependencies()

operations = [
migrations.RunPython(populate_allowed_origin, _noop),
Expand Down
66 changes: 66 additions & 0 deletions users/migrations/0034_application_algorithm_and_more.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Generated by Django 4.2.13 on 2024-07-04 13:39

from django.conf import settings
from django.db import migrations, models, ProgrammingError
import django.db.models.deletion
from django.db import connection

def check_migration_applied(app_name, migration_name):
try:
with connection.cursor() as cursor:
cursor.execute("SELECT * FROM django_migrations WHERE app=%s AND name=%s", [app_name, migration_name])
row = cursor.fetchone()
except ProgrammingError:
return False
return bool(row)

def get_dependencies():
if check_migration_applied("users", "0020_populate_cors_allowed_origins"):
return [("users", "0033_alter_loginmethod_provider_id")]
return [("users", "0019_allowedorigin")]

class Migration(migrations.Migration):

dependencies = get_dependencies()

operations = [
migrations.AddField(
model_name="application",
name="algorithm",
field=models.CharField(
blank=True,
choices=[
("", "No OIDC support"),
("RS256", "RSA with SHA-2 256"),
("HS256", "HMAC with SHA-2 256"),
],
default="",
max_length=5,
),
),
migrations.AlterField(
model_name="application",
name="authorization_grant_type",
field=models.CharField(
choices=[
("authorization-code", "Authorization code"),
("implicit", "Implicit"),
("password", "Resource owner password-based"),
("client-credentials", "Client credentials"),
("openid-hybrid", "OpenID connect hybrid"),
],
max_length=32,
),
),
migrations.AlterField(
model_name="application",
name="user",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.CASCADE,
related_name="%(app_label)s_%(class)s",
to=settings.AUTH_USER_MODEL,
),
),
]

0 comments on commit 93989dc

Please sign in to comment.