From 6bbbb8cf84d7759c15b003f5976547ddb8b0b67b Mon Sep 17 00:00:00 2001 From: Adam Rutkowski Date: Fri, 7 Apr 2023 07:18:02 +0200 Subject: [PATCH] Full migration (to be submitted separately) --- ...30406110926_associate-goals-with-sites.exs | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 priv/repo/migrations/20230406110926_associate-goals-with-sites.exs diff --git a/priv/repo/migrations/20230406110926_associate-goals-with-sites.exs b/priv/repo/migrations/20230406110926_associate-goals-with-sites.exs new file mode 100644 index 000000000..efc21bd5f --- /dev/null +++ b/priv/repo/migrations/20230406110926_associate-goals-with-sites.exs @@ -0,0 +1,27 @@ +defmodule :"Elixir.Plausible.Repo.Migrations.Associate-goals-with-sites" do + use Ecto.Migration + + def up do + alter table(:goals) do + add :site_id, :integer, null: true + end + + execute """ + DELETE FROM goals g WHERE NOT EXISTS ( + SELECT 1 FROM sites s + WHERE s.domain = g.domain + ) + """ + + execute """ + UPDATE goals g SET site_id = ( + SELECT s.id FROM sites s WHERE s.domain = g.domain + ) + """ + + alter table(:goals) do + modify :site_id, references(:sites, on_delete: :delete_all), null: false + remove :domain + end + end +end