Add constraints to accounts
.username
This commit is contained in:
parent
cf104cb1b4
commit
01effc198c
2 changed files with 33 additions and 1 deletions
|
@ -0,0 +1,32 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
class AddLowerCaseConstraintToUsernameOfAccounts < ActiveRecord::Migration[6.0]
|
||||||
|
def change
|
||||||
|
reversible do |dir|
|
||||||
|
dir.up do
|
||||||
|
add_constraint :lower_case, 'username = lower(username)'
|
||||||
|
add_constraint :min_length, 'length(username) >= 3'
|
||||||
|
add_constraint :max_length, 'length(username) <= 36'
|
||||||
|
end
|
||||||
|
|
||||||
|
dir.down do
|
||||||
|
drop_constraint :lower_case
|
||||||
|
drop_constraint :min_length
|
||||||
|
drop_constraint :max_length
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def add_constraint(name, constraint)
|
||||||
|
execute 'ALTER TABLE accounts ' \
|
||||||
|
"ADD CONSTRAINT accounts_username_#{name}_check " \
|
||||||
|
"CHECK (#{constraint})"
|
||||||
|
end
|
||||||
|
|
||||||
|
def drop_constraint(name)
|
||||||
|
execute 'ALTER TABLE accounts ' \
|
||||||
|
"DROP CONSTRAINT accounts_username_#{name}_check"
|
||||||
|
end
|
||||||
|
end
|
|
@ -10,7 +10,7 @@
|
||||||
#
|
#
|
||||||
# It's strongly recommended that you check this file into your version control system.
|
# It's strongly recommended that you check this file into your version control system.
|
||||||
|
|
||||||
ActiveRecord::Schema.define(version: 2019_02_01_012021) do
|
ActiveRecord::Schema.define(version: 2019_02_01_013649) do
|
||||||
|
|
||||||
# These are extensions that must be enabled in order to support this database
|
# These are extensions that must be enabled in order to support this database
|
||||||
enable_extension "plpgsql"
|
enable_extension "plpgsql"
|
||||||
|
|
Reference in a new issue