Imrpove migrations
This commit is contained in:
1 parent
f386499d2f
commit
4f28a59c7e
7 files changed
+187
-88
No files matched your search
@@ -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