Closed paeduardo closed 4 months ago
Hi @paeduardo. I'm guessing this is because transaction 0035
tries to affect both the schema and data in a single transaction. You can probably work around this using something like:
# 1. Comment out `migrations.RunPython(...)` on line 62 of 0035_account_currencies_json.py
# 2. Run the migration
./manage.py migrate hordak 0035
# 3. Manually run the SQL:
UPDATE hordak_account SET currencies_json = array_to_json(currencies)::jsonb;
# 4. Finish the migrations
./manage.py migrate hordak
I'm not sure what the solution is here as these migrations are already out in the wild.
@PetrDlouhy @nitsujri – Let's just watch out for this in the future (I.e. best use separate migrations for schema and data changes)
I'm going to close this now in the hope that the above workaround is sufficient, but if anyone is suffering with this then please leave a comment.
@adamcharnock Sorry for merging that.
Maybe we could make a migration branch. I would have to look into it, but maybe this could be solved with alternate migration history and merged migration, so that users who didn't update yet would have not face this problem.
Do you think that this could be added to the testing? E.g. adding some initial data and then migrating?
No worries, these things happen!
And I was just thinking about this. Being able to test these migrations would be great, but I'm not aware of Django built-in way to control migrations during testing. We could probably do it with a custom script though (migrate to migration X, load some data, finish migrations, check it all looks ok). This would be especially useful for Hordak 2.0.
EDIT: django-test-migrations
Hi @adamcharnock ! Thanks for support! Based on your solution I have splitted 0035_account_currencies_json.py migration file into 2 parts and have run it sequentially, after that 0036.., 0037.... And it worked perfectly! Splitted 0035... migration file: part 1: a0035_account_currencies_json.py
from django.db import migrations, models
import hordak
class Migration(migrations.Migration):
dependencies = [
("hordak", "0034_alter_account_currencies_alter_leg_amount_currency"),
]
operations = [
migrations.AddField(
model_name="account",
name="currencies_json",
field=models.JSONField(
db_index=True,
default=hordak.defaults.project_currencies,
),
),
]
part 2: 0035_account_currencies_json.py
from django.db import migrations, models
import hordak
def copy_currencies_data(apps, schema_editor):
# MySQL won't have any old data, because support has only now been added
if schema_editor.connection.vendor == "postgresql":
MyModel = apps.get_model(
"hordak", "Account"
) # replace 'myapp' and 'MyModel' with your actual app and model names
table_name = MyModel._meta.db_table
with schema_editor.connection.cursor() as cursor:
# only run this if there is data in the table (in which case we're an ARRAY to migrate);
# disregard if migrations are being run on a fresh database
if MyModel.objects.count() > 0:
cursor.execute(
f"""
UPDATE {table_name}
SET currencies_json = array_to_json(currencies)::jsonb;
"""
)
else:
cursor.execute(
f"""
UPDATE {table_name}
SET currencies_json = currencies;
"""
)
class Migration(migrations.Migration):
dependencies = [
("hordak", "a0035_account_currencies_json"),
]
operations = [
migrations.RunPython(copy_currencies_data),
]
Hi, when trying to apply hordak.0035_account_currencies_json (from django-hordak==1.13.0 to django-hordak==1.14.0) it appears that it cannot properly apply migration of account currencies field to JSON Anyone else also gets this error?
Django 4.2.9 python 3.12.1 postgres:12-bookworm django-tenants==3.6.1
Below log:
The above exception was the direct cause of the following exception: