|
| 1 | +defmodule PhoenixKit.Migrations.Postgres.V78 do |
| 2 | + @moduledoc """ |
| 3 | + V78: Add missing columns to AI tables from V41. |
| 4 | +
|
| 5 | + V41 conditionally added columns to AI tables (reasoning columns on endpoints, |
| 6 | + prompt tracking on requests), but only if the tables existed at the time. |
| 7 | + If the AI module was enabled after V41 ran, these columns were never created. |
| 8 | +
|
| 9 | + This migration ensures they exist. All operations are idempotent. |
| 10 | + """ |
| 11 | + |
| 12 | + use Ecto.Migration |
| 13 | + |
| 14 | + def up(opts) do |
| 15 | + prefix = Map.get(opts, :prefix, "public") |
| 16 | + |
| 17 | + # Reasoning columns on endpoints (from V41) |
| 18 | + if table_exists?(:phoenix_kit_ai_endpoints, prefix) do |
| 19 | + alter table(:phoenix_kit_ai_endpoints, prefix: prefix) do |
| 20 | + add_if_not_exists :reasoning_enabled, :boolean |
| 21 | + add_if_not_exists :reasoning_effort, :string, size: 20 |
| 22 | + add_if_not_exists :reasoning_max_tokens, :integer |
| 23 | + add_if_not_exists :reasoning_exclude, :boolean |
| 24 | + end |
| 25 | + end |
| 26 | + |
| 27 | + # Prompt tracking columns on requests (from V41) |
| 28 | + if table_exists?(:phoenix_kit_ai_requests, prefix) do |
| 29 | + alter table(:phoenix_kit_ai_requests, prefix: prefix) do |
| 30 | + add_if_not_exists :prompt_uuid, :uuid |
| 31 | + add_if_not_exists :prompt_name, :string, size: 255 |
| 32 | + end |
| 33 | + |
| 34 | + create_if_not_exists index(:phoenix_kit_ai_requests, [:prompt_uuid], |
| 35 | + name: :phoenix_kit_ai_requests_prompt_uuid_idx, |
| 36 | + prefix: prefix |
| 37 | + ) |
| 38 | + |
| 39 | + if table_exists?(:phoenix_kit_ai_prompts, prefix) do |
| 40 | + execute """ |
| 41 | + DO $$ |
| 42 | + BEGIN |
| 43 | + IF NOT EXISTS ( |
| 44 | + SELECT 1 FROM pg_constraint |
| 45 | + WHERE conname = 'phoenix_kit_ai_requests_prompt_uuid_fkey' |
| 46 | + AND conrelid = '#{prefix_str(prefix)}phoenix_kit_ai_requests'::regclass |
| 47 | + ) THEN |
| 48 | + ALTER TABLE #{prefix_str(prefix)}phoenix_kit_ai_requests |
| 49 | + ADD CONSTRAINT phoenix_kit_ai_requests_prompt_uuid_fkey |
| 50 | + FOREIGN KEY (prompt_uuid) |
| 51 | + REFERENCES #{prefix_str(prefix)}phoenix_kit_ai_prompts(uuid) |
| 52 | + ON DELETE SET NULL; |
| 53 | + END IF; |
| 54 | + END $$; |
| 55 | + """ |
| 56 | + end |
| 57 | + end |
| 58 | + |
| 59 | + execute "COMMENT ON TABLE #{prefix_str(prefix)}phoenix_kit IS '78'" |
| 60 | + end |
| 61 | + |
| 62 | + def down(opts) do |
| 63 | + prefix = Map.get(opts, :prefix, "public") |
| 64 | + |
| 65 | + if table_exists?(:phoenix_kit_ai_endpoints, prefix) do |
| 66 | + alter table(:phoenix_kit_ai_endpoints, prefix: prefix) do |
| 67 | + remove_if_exists :reasoning_enabled, :boolean |
| 68 | + remove_if_exists :reasoning_effort, :string |
| 69 | + remove_if_exists :reasoning_max_tokens, :integer |
| 70 | + remove_if_exists :reasoning_exclude, :boolean |
| 71 | + end |
| 72 | + end |
| 73 | + |
| 74 | + if table_exists?(:phoenix_kit_ai_requests, prefix) do |
| 75 | + execute """ |
| 76 | + DO $$ |
| 77 | + BEGIN |
| 78 | + IF EXISTS ( |
| 79 | + SELECT 1 FROM pg_constraint |
| 80 | + WHERE conname = 'phoenix_kit_ai_requests_prompt_uuid_fkey' |
| 81 | + AND conrelid = '#{prefix_str(prefix)}phoenix_kit_ai_requests'::regclass |
| 82 | + ) THEN |
| 83 | + ALTER TABLE #{prefix_str(prefix)}phoenix_kit_ai_requests |
| 84 | + DROP CONSTRAINT phoenix_kit_ai_requests_prompt_uuid_fkey; |
| 85 | + END IF; |
| 86 | + END $$; |
| 87 | + """ |
| 88 | + |
| 89 | + drop_if_exists index(:phoenix_kit_ai_requests, [:prompt_uuid], |
| 90 | + name: :phoenix_kit_ai_requests_prompt_uuid_idx, |
| 91 | + prefix: prefix |
| 92 | + ) |
| 93 | + |
| 94 | + alter table(:phoenix_kit_ai_requests, prefix: prefix) do |
| 95 | + remove_if_exists :prompt_uuid, :uuid |
| 96 | + remove_if_exists :prompt_name, :string |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + execute "COMMENT ON TABLE #{prefix_str(prefix)}phoenix_kit IS '77'" |
| 101 | + end |
| 102 | + |
| 103 | + defp table_exists?(table_name, prefix) do |
| 104 | + query = """ |
| 105 | + SELECT EXISTS ( |
| 106 | + SELECT FROM information_schema.tables |
| 107 | + WHERE table_schema = '#{prefix}' |
| 108 | + AND table_name = '#{table_name}' |
| 109 | + ) |
| 110 | + """ |
| 111 | + |
| 112 | + %{rows: [[exists]]} = PhoenixKit.RepoHelper.repo().query!(query) |
| 113 | + exists |
| 114 | + end |
| 115 | + |
| 116 | + defp prefix_str("public"), do: "public." |
| 117 | + defp prefix_str(prefix), do: "#{prefix}." |
| 118 | +end |
0 commit comments