Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: replace alter field with adding a new field #4817

Merged
merged 4 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions api/tests/unit/users/test_unit_users_migrations.py
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"
)
51 changes: 47 additions & 4 deletions api/users/migrations/0039_alter_ffadminuser_first_name.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# Generated by Django 4.2.16 on 2024-11-04 17:09

from django.apps.registry import Apps
from django.db import migrations, models
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.models import F


def populate_new_first_name_field(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
FFAdminUser = apps.get_model("users", "FFAdminUser")

FFAdminUser.objects.update(first_name_v2=F("first_name"))


def populate_old_first_name_field(apps: Apps, schema_editor: BaseDatabaseSchemaEditor) -> None:
FFAdminUser = apps.get_model("users", "FFAdminUser")

FFAdminUser.objects.update(first_name=F("first_name_v2"))
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved


class Migration(migrations.Migration):
Expand All @@ -10,9 +24,38 @@ class Migration(migrations.Migration):
]

operations = [
migrations.AlterField(
migrations.AddField(
model_name="ffadminuser",
name="first_name",
field=models.CharField(max_length=150, verbose_name="first name"),
name="first_name_v2",
field=models.CharField(max_length=150, default=""),
),
migrations.RunPython(populate_new_first_name_field, reverse_code=populate_old_first_name_field),
migrations.SeparateDatabaseAndState(
state_operations=[
migrations.RenameField(
model_name="ffadminuser",
old_name="first_name",
new_name="_first_name_old",
),
migrations.RenameField(
model_name="ffadminuser",
old_name="first_name_v2",
new_name="first_name",
),
migrations.AlterField(
model_name="ffadminuser",
name="_first_name_old",
field=models.CharField(
db_column="first_name", max_length=30, verbose_name="first name"
),
),
migrations.AlterField(
model_name="ffadminuser",
name="first_name",
field=models.CharField(
db_column="first_name_v2", max_length=150, verbose_name="first name"
),
),
]
)
]
8 changes: 7 additions & 1 deletion api/users/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,9 @@ class FFAdminUser(LifecycleModel, AbstractUser):
email = models.EmailField(unique=True, null=False)
objects = UserManager()
username = models.CharField(unique=True, max_length=150, null=True, blank=True)
first_name = models.CharField("first name", max_length=150)
first_name = models.CharField(
"first name", max_length=150, db_column="first_name_v2"
)
last_name = models.CharField("last name", max_length=150)
google_user_id = models.CharField(max_length=50, null=True, blank=True)
github_user_id = models.CharField(max_length=50, null=True, blank=True)
Expand All @@ -111,6 +113,10 @@ class FFAdminUser(LifecycleModel, AbstractUser):
choices=SignUpType.choices, max_length=100, blank=True, null=True
)

_first_name_old = models.CharField(
"first name", max_length=30, db_column="first_name"
)

uuid = models.UUIDField(default=uuid.uuid4, editable=False, unique=True)

USERNAME_FIELD = "email"
Expand Down
Loading