-
Notifications
You must be signed in to change notification settings - Fork 375
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: replace alter field with adding a new field (#4817)
- Loading branch information
1 parent
ea6a169
commit 0d1c64a
Showing
4 changed files
with
150 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import pytest | ||
from django.conf import settings | ||
from django_test_migrations.migrator import Migrator | ||
|
||
pytestmark = pytest.mark.skipif( | ||
settings.SKIP_MIGRATION_TESTS is True, | ||
reason="Skip migration tests to speed up tests where necessary", | ||
) | ||
|
||
|
||
def test_0039_ffadminuser_first_name_v2__values_expected(migrator: Migrator) -> None: | ||
# Given | ||
old_state = migrator.apply_initial_migration( | ||
("users", "0038_create_hubspot_tracker") | ||
) | ||
OldFFAdminUser = old_state.apps.get_model("users", "FFAdminUser") | ||
|
||
user = OldFFAdminUser.objects.create(first_name="Testfirstname") | ||
|
||
# When | ||
new_state = migrator.apply_tested_migration( | ||
("users", "0039_alter_ffadminuser_first_name") | ||
) | ||
NewFFAdminUser = new_state.apps.get_model("users", "FFAdminUser") | ||
|
||
# Then | ||
assert NewFFAdminUser.objects.get(id=user.id).first_name == user.first_name | ||
|
||
|
||
def test_0039_ffadminuser_first_name_v2__reverse__values_expected( | ||
migrator: Migrator, | ||
) -> None: | ||
# Given | ||
old_state = migrator.apply_initial_migration( | ||
("users", "0039_alter_ffadminuser_first_name") | ||
) | ||
NewFFAdminUser = old_state.apps.get_model("users", "FFAdminUser") | ||
|
||
user = NewFFAdminUser.objects.create( | ||
first_name="TestfirstnameTestfirstnameTestfirstnameTestfirstname" | ||
) | ||
|
||
# When | ||
new_state = migrator.apply_tested_migration( | ||
("users", "0038_create_hubspot_tracker") | ||
) | ||
OldFFAdminUser = new_state.apps.get_model("users", "FFAdminUser") | ||
|
||
# Then | ||
assert ( | ||
OldFFAdminUser.objects.get(id=user.id).first_name | ||
== "TestfirstnameTestfirstnameTest" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
api/users/migrations/0040_alter_ffadminuser_first_name_fix.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
""" | ||
This migration exists because 0039 was retrospectively fixed to prevent locking issues | ||
on the users table. This migration ensures that: | ||
1. in environments where 0039 was run before the fix was added, the new column is | ||
correctly added to the database to match what the ORM expects and the data | ||
is correctly migrted to the new column. | ||
2. in environments where 0039 was run after the fix was added, nothing happens. | ||
Note that we only need to care about Postgres databases here because we did not release | ||
the bad migration for 0039 in a format that can be consumed by oracle / mysql. | ||
""" | ||
|
||
# Generated by Django 4.2.16 on 2024-11-12 18:05 | ||
import logging | ||
|
||
from django.db import migrations | ||
|
||
from core.migration_helpers import PostgresOnlyRunSQL | ||
|
||
forwards_sql = """ | ||
DO | ||
$do$ | ||
BEGIN | ||
IF NOT EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name='users_ffadminuser' and column_name='first_name_v2') THEN | ||
ALTER TABLE "users_ffadminuser" ADD COLUMN "first_name_v2" VARCHAR(150) NOT NULL DEFAULT ''; | ||
UPDATE "users_ffadminuser" SET "first_name_v2" = "first_name"; | ||
END IF; | ||
END | ||
$do$ | ||
""" | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
("users", "0039_alter_ffadminuser_first_name"), | ||
] | ||
|
||
operations = [ | ||
PostgresOnlyRunSQL(forwards_sql, reverse_sql=""), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters