bonfire-app/flavours/cooperation/repo/migrations/20210412094946_fp.exs

42 lines
1.1 KiB
Elixir
Raw Normal View History

2021-11-18 22:21:08 +00:00
defmodule Bonfire.Repo.Migrations.FP do
use Ecto.Migration
def up do
execute("create or replace function
column_exists(ptable text, pcolumn text, pschema text default 'public')
returns boolean
language sql stable strict
as $body$
-- does the requested table.column exist in schema?
select exists
( select null
from information_schema.columns
where table_name=ptable
and column_name=pcolumn
and table_schema=pschema
);
$body$;")
2022-09-12 04:34:14 +00:00
execute(
"CREATE OR REPLACE FUNCTION rename_column_if_exists(ptable TEXT, pcolumn TEXT, new_name TEXT)
2021-11-18 22:21:08 +00:00
RETURNS VOID AS $BODY$
BEGIN
-- Rename the column if it exists.
IF column_exists(ptable, pcolumn) THEN
EXECUTE FORMAT('ALTER TABLE IF EXISTS %I RENAME COLUMN %I TO %I;',
ptable, pcolumn, new_name);
END IF;
END$BODY$
2022-09-12 04:34:14 +00:00
LANGUAGE plpgsql VOLATILE;"
)
2021-11-18 22:21:08 +00:00
flush()
2022-09-12 04:34:14 +00:00
execute(
"SELECT rename_column_if_exists('bonfire_data_social_feed_publish', 'object_id', 'activity_id') "
)
2021-11-18 22:21:08 +00:00
end
def down, do: nil
end