57 lines
1.4 KiB
Ruby
57 lines
1.4 KiB
Ruby
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
|