1
0
Fork 0

Improve migration

This commit is contained in:
Alex Kotov 2019-07-23 01:33:41 +05:00
parent d8bf201a0e
commit 7a48bf65ff
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08

View file

@ -2,26 +2,14 @@
class InitialMigration < ActiveRecord::Migration[6.0]
def change
reversible do |dir|
dir.down do
execute 'DROP FUNCTION has_no_leading_trailing_whitespace'
end
dir.up do
execute <<~SQL
CREATE FUNCTION has_no_leading_trailing_whitespace (str text)
RETURNS boolean
IMMUTABLE
LANGUAGE plpgsql
AS
$$
BEGIN
RETURN str ~ '^[^[:space:]]+(.*[^[:space:]])?$';
END;
$$;
SQL
end
end
func :has_no_leading_trailing_whitespace, <<~SQL
(str text) RETURNS boolean IMMUTABLE LANGUAGE plpgsql AS
$$
BEGIN
RETURN str ~ '^[^[:space:]]+(.*[^[:space:]])?$';
END;
$$;
SQL
enum :sex, %i[male female]
@ -406,6 +394,18 @@ class InitialMigration < ActiveRecord::Migration[6.0]
private
def func(name, sql)
reversible do |dir|
dir.up do
execute "CREATE FUNCTION #{name} #{sql}"
end
dir.down do
execute "DROP FUNCTION #{name}"
end
end
end
def enum(name, values)
reversible do |dir|
dir.up do