bonfire-app/flavours/classic/repo/migrations/20210412094946_fp.exs
Mayel de Borniol 0ef85e77f9 auto-format
2022-09-12 16:34:14 +12:00

42 lines
1.1 KiB
Elixir
Executable file

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$;")
execute(
"CREATE OR REPLACE FUNCTION rename_column_if_exists(ptable TEXT, pcolumn TEXT, new_name TEXT)
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$
LANGUAGE plpgsql VOLATILE;"
)
flush()
execute(
"SELECT rename_column_if_exists('bonfire_data_social_feed_publish', 'object_id', 'activity_id') "
)
end
def down, do: nil
end