diff --git a/mandelstudio/migrations/0006_address_country_i18n_columns.py b/mandelstudio/migrations/0006_address_country_i18n_columns.py new file mode 100644 index 0000000..58b1c91 --- /dev/null +++ b/mandelstudio/migrations/0006_address_country_i18n_columns.py @@ -0,0 +1,43 @@ +from django.db import migrations + + +LANGUAGES = ("en", "de", "fr", "es", "it", "pt", "ru") + + +def _add_column_if_missing(schema_editor, table_name, column_name, column_sql): + connection = schema_editor.connection + with connection.cursor() as cursor: + existing = { + row.name + for row in connection.introspection.get_table_description(cursor, table_name) + } + if column_name in existing: + return + quoted_table = schema_editor.quote_name(table_name) + quoted_column = schema_editor.quote_name(column_name) + cursor.execute( + f"ALTER TABLE {quoted_table} ADD COLUMN {quoted_column} {column_sql}" + ) + + +def add_address_i18n_columns(apps, schema_editor): + for lang in LANGUAGES: + _add_column_if_missing( + schema_editor, + "address_country", + f"printable_name_{lang}", + "varchar(128) NULL", + ) + + +class Migration(migrations.Migration): + dependencies = [ + ("mandelstudio", "0005_checkout_i18n_columns"), + ] + + operations = [ + migrations.RunPython( + add_address_i18n_columns, + migrations.RunPython.noop, + ) + ]