Imrpove migrations
This commit is contained in:
1 parent
f386499d2f
commit
4f28a59c7e
7 files changed
+187
-88
No files matched your search
@@ -1,19 +1,68 @@
|
||||
Sequel.migration do
|
||||
up do
|
||||
create_table(:account_statuses) do
|
||||
Integer :id, primary_key: true
|
||||
String :name, null: false, unique: true
|
||||
end
|
||||
|
||||
from(:account_statuses).import(
|
||||
[:id, :name],
|
||||
[
|
||||
[1, "Unverified"],
|
||||
[2, "Verified"],
|
||||
[3, "Closed"]
|
||||
]
|
||||
)
|
||||
|
||||
db = self
|
||||
|
||||
create_table(:accounts) do
|
||||
primary_key :id, type: :Bignum
|
||||
|
||||
foreign_key :status_id, :account_statuses,
|
||||
null: false,
|
||||
default: 1
|
||||
|
||||
String :email, null: false
|
||||
|
||||
index :email, unique: true
|
||||
if db.supports_partial_indexes?
|
||||
index :email, unique: true, where: { status_id: [1, 2] }
|
||||
else
|
||||
index :email, unique: true
|
||||
end
|
||||
end
|
||||
|
||||
create_table(:account_password_hashes) do
|
||||
foreign_key :id, :accounts, primary_key: true, type: :Bignum
|
||||
foreign_key :id, :accounts,
|
||||
primary_key: true,
|
||||
type: :Bignum
|
||||
|
||||
String :password_hash, null: false
|
||||
end
|
||||
|
||||
create_table(:account_verification_keys) do
|
||||
foreign_key :id, :accounts,
|
||||
primary_key: true,
|
||||
type: :Bignum
|
||||
|
||||
String :key, null: false
|
||||
|
||||
DateTime :requested_at,
|
||||
null: false,
|
||||
default: Sequel::CURRENT_TIMESTAMP
|
||||
|
||||
DateTime :email_last_sent,
|
||||
null: false,
|
||||
default: Sequel::CURRENT_TIMESTAMP
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
drop_table(:account_password_hashes, :accounts)
|
||||
drop_table(
|
||||
:account_verification_keys,
|
||||
:account_password_hashes,
|
||||
:accounts,
|
||||
:account_statuses
|
||||
)
|
||||
end
|
||||
end
|
||||
@@ -12,6 +12,8 @@ Sequel.migration do
|
||||
String :last_name
|
||||
String :organization_name
|
||||
|
||||
TrueClass :admin, null: false, default: false
|
||||
|
||||
index :account_id, unique: true
|
||||
|
||||
constraint :members_kind_valid,
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
Sequel.migration do
|
||||
up do
|
||||
alter_table(:members) do
|
||||
add_column :admin, TrueClass, null: false, default: false
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
alter_table(:members) do
|
||||
drop_column :admin
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,56 @@
|
||||
Sequel.migration do
|
||||
up do
|
||||
create_table(:memberships) do
|
||||
primary_key :id, type: :Bignum
|
||||
|
||||
foreign_key :member_id, :members,
|
||||
type: :Bignum,
|
||||
null: false
|
||||
|
||||
String :status, null: false, default: "pending"
|
||||
|
||||
DateTime :requested_at,
|
||||
null: false,
|
||||
default: Sequel::CURRENT_TIMESTAMP
|
||||
|
||||
DateTime :decided_at
|
||||
|
||||
foreign_key :recorded_by, :members,
|
||||
type: :Bignum
|
||||
|
||||
DateTime :ended_at
|
||||
String :end_reason
|
||||
|
||||
index :status
|
||||
|
||||
# Un seul dossier en attente ou une seule adhésion
|
||||
# en cours par adhérent.
|
||||
index :member_id,
|
||||
unique: true,
|
||||
where: { status: ["pending", "accepted"], ended_at: nil }
|
||||
|
||||
constraint :memberships_status_valid,
|
||||
Sequel.lit("status IN ('pending', 'accepted', 'rejected')")
|
||||
|
||||
constraint :memberships_decision_consistent,
|
||||
Sequel.lit(
|
||||
"(status = 'pending' AND decided_at IS NULL " \
|
||||
"AND recorded_by IS NULL) " \
|
||||
"OR (status IN ('accepted', 'rejected') " \
|
||||
"AND decided_at IS NOT NULL AND recorded_by IS NOT NULL)"
|
||||
)
|
||||
|
||||
constraint :memberships_end_consistent,
|
||||
Sequel.lit(
|
||||
"(ended_at IS NULL AND end_reason IS NULL) " \
|
||||
"OR (status = 'accepted' AND ended_at IS NOT NULL " \
|
||||
"AND end_reason IN ('resignation', 'death', " \
|
||||
"'dissolution', 'exclusion'))"
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
down do
|
||||
drop_table(:memberships)
|
||||
end
|
||||
end
|
||||
Reference in new issue
Block a user