Fix accounting migrations so fresh deploys create regarding and event_name columns.
Make 0009/0010 idempotent and aligned with current models so server migrate after a wipe does not miss schema fields. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -1,11 +1,44 @@
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
def _table_columns(schema_editor, table):
|
||||
connection = schema_editor.connection
|
||||
with connection.cursor() as cursor:
|
||||
return {
|
||||
col.name
|
||||
for col in connection.introspection.get_table_description(cursor, table)
|
||||
}
|
||||
|
||||
|
||||
def _ensure_varchar_column(schema_editor, table, column, max_length):
|
||||
"""Add a non-null varchar column with empty default when missing; else clear NULLs."""
|
||||
connection = schema_editor.connection
|
||||
q_table = connection.ops.quote_name(table)
|
||||
q_column = connection.ops.quote_name(column)
|
||||
|
||||
if column in _table_columns(schema_editor, table):
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
f"UPDATE {q_table} SET {q_column} = '' WHERE {q_column} IS NULL"
|
||||
)
|
||||
return
|
||||
|
||||
with connection.cursor() as cursor:
|
||||
cursor.execute(
|
||||
f"ALTER TABLE {q_table} "
|
||||
f"ADD COLUMN {q_column} varchar({max_length}) DEFAULT '' NOT NULL"
|
||||
)
|
||||
|
||||
|
||||
def add_treasury_event_name(apps, schema_editor):
|
||||
"""
|
||||
Add TreasuryTxn.event_name when missing.
|
||||
Safe for fresh DBs and for DBs that already have the column from older deploys.
|
||||
"""
|
||||
_ensure_varchar_column(schema_editor, 'accounting_treasury_txns', 'event_name', 200)
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
"""
|
||||
DB already has event_name (NOT NULL) from an earlier manual/out-of-band change.
|
||||
Sync Django state and ensure NULLs become empty string so inserts never omit the column.
|
||||
"""
|
||||
|
||||
dependencies = [
|
||||
('accounting', '0008_invoice_cancelled_status'),
|
||||
@@ -21,10 +54,7 @@ class Migration(migrations.Migration):
|
||||
),
|
||||
],
|
||||
database_operations=[
|
||||
migrations.RunSQL(
|
||||
sql="UPDATE accounting_treasury_txns SET event_name = '' WHERE event_name IS NULL;",
|
||||
reverse_sql=migrations.RunSQL.noop,
|
||||
),
|
||||
migrations.RunPython(add_treasury_event_name, migrations.RunPython.noop),
|
||||
],
|
||||
),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user