Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Generated manually to fix Language foreign key column length in M2M junction table
# See https://github.com/learningequality/studio/issues/5618
#
# When Language.id was changed from max_length=7 to max_length=14 in migration
# 0081, Django 1.9 did not cascade the primary key column size change to the
# many-to-many junction table column. This migration fixes that column for
# databases that were created before the migration squash.
#
# This migration is idempotent - it only alters the column if it is still varchar(7).
from django.db import migrations


# SQL to fix the column, checking if it needs to be altered first
FORWARD_SQL = """
DO $$
BEGIN
-- Fix contentcuration_channel_included_languages.language_id (M2M junction table)
IF EXISTS (
SELECT 1 FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = 'contentcuration_channel_included_languages'
AND column_name = 'language_id'
AND character_maximum_length = 7
) THEN
ALTER TABLE contentcuration_channel_included_languages
ALTER COLUMN language_id TYPE varchar(14);
END IF;
END $$;
"""

# Reverse SQL is a no-op since we don't want to shrink the columns back
# (that could cause data loss if longer language codes have been inserted)
REVERSE_SQL = """
-- No-op: Cannot safely reverse this migration as it may cause data loss
SELECT 1;
"""


class Migration(migrations.Migration):

dependencies = [
("contentcuration", "0154_alter_assessmentitem_type"),
]

operations = [
migrations.RunSQL(FORWARD_SQL, REVERSE_SQL),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
"""
Test for migration 0155_fix_language_foreign_key_length.

This test verifies that the migration correctly fixes the Language foreign key
column in the included_languages M2M junction table from varchar(7) to varchar(14).
"""
from django.db import connection
from django.db.migrations.executor import MigrationExecutor
from django.test import TransactionTestCase


# The M2M junction table column that should be fixed by the migration
TABLE_NAME = "contentcuration_channel_included_languages"
COLUMN_NAME = "language_id"


def get_column_max_length(table_name, column_name):
"""Get the character_maximum_length for a varchar column."""
with connection.cursor() as cursor:
cursor.execute(
"""
SELECT character_maximum_length
FROM information_schema.columns
WHERE table_schema = 'public'
AND table_name = %s
AND column_name = %s
""",
[table_name, column_name],
)
row = cursor.fetchone()
return row[0] if row else None


def set_column_to_varchar7(table_name, column_name):
"""Shrink a varchar column to varchar(7) to simulate bad production state."""
with connection.cursor() as cursor:
cursor.execute(
f"ALTER TABLE {table_name} ALTER COLUMN {column_name} TYPE varchar(7)"
)


class TestLanguageForeignKeyLengthMigration(TransactionTestCase):
"""
Test that migration 0155 fixes varchar(7) Language FK column to varchar(14).

This simulates the production database state where Language.id was changed
from max_length=7 to max_length=14, but Django 1.9 didn't cascade the change
to the M2M junction table column.
"""

def test_migration_fixes_varchar7_column(self):
# First, shrink column back to varchar(7) to simulate bad state
set_column_to_varchar7(TABLE_NAME, COLUMN_NAME)
# Verify the column is now varchar(7)
self.assertEqual(
get_column_max_length(TABLE_NAME, COLUMN_NAME),
7,
f"{TABLE_NAME}.{COLUMN_NAME} should be varchar(7) before migration",
)

# Run migration 0155
executor = MigrationExecutor(connection)
executor.migrate([("contentcuration", "0154_alter_assessmentitem_type")])
executor = MigrationExecutor(connection)
executor.loader.build_graph()
executor.migrate([("contentcuration", "0155_fix_language_foreign_key_length")])

# Verify column is now varchar(14)
self.assertEqual(
get_column_max_length(TABLE_NAME, COLUMN_NAME),
14,
f"{TABLE_NAME}.{COLUMN_NAME} should be varchar(14) after migration",
)