defmodule DaProductApp.Repo.Migrations.AddVersionToParameterTemplates do use Ecto.Migration # Using up/down instead of change because: # 1. MySQL 8.0 does NOT support "ADD COLUMN IF NOT EXISTS" (MariaDB only) # 2. A prior partial run already committed the column (MySQL DDL auto-commits) # but the migration was never recorded — so we guard with column_exists?/3 def up do unless column_exists?(:parameter_templates, :version) do alter table(:parameter_templates) do add :version, :string, size: 50, default: "1.0.0" end end # Simple lookup index only. # A 4-column unique index (vendor + model + name + version) exceeds # MySQL's 3072-byte key-length limit for utf8mb4 tables. # (255+255+255+50 chars × 4 bytes = ~3260 bytes > 3072 byte limit) # Uniqueness of template identity is already enforced by the existing # (vendor, model, name) constraint created with the table. create_if_not_exists index(:parameter_templates, [:version]) end def down do drop_if_exists index(:parameter_templates, [:version]) if column_exists?(:parameter_templates, :version) do alter table(:parameter_templates) do remove :version end end end end