Skip to content

Commit

Permalink
Replace alter field with adding a new field
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewelwell committed Nov 6, 2024
1 parent 2a7e734 commit cc9a531
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 4 deletions.
42 changes: 39 additions & 3 deletions api/users/migrations/0039_alter_ffadminuser_first_name.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Generated by Django 4.2.16 on 2024-11-04 17:09

from django.db import migrations, models
from django.db.models import F


def copy_first_names(apps, schema_editor):
FFAdminUser = apps.get_model("users", "FFAdminUser")

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


class Migration(migrations.Migration):
Expand All @@ -10,9 +17,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(copy_first_names, reverse_code=migrations.RunPython.noop),
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

0 comments on commit cc9a531

Please sign in to comment.