Skip to content

Commit e03a5ef

Browse files
committed
wip
1 parent 0ce5ac8 commit e03a5ef

7 files changed

Lines changed: 347 additions & 20 deletions

File tree

app/models/event.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
# organization :string
2020
# participant_communications :text
2121
# private_signup_list :boolean default(FALSE), not null
22-
# registration_policy :jsonb
2322
# short_blurb :text
2423
# status :string default("active"), not null
2524
# team_mailing_list_name :text
@@ -31,21 +30,24 @@
3130
# convention_id :bigint not null
3231
# event_category_id :bigint not null
3332
# owner_id :bigint
33+
# registration_policy_id :bigint not null
3434
# updated_by_id :bigint
3535
#
3636
# Indexes
3737
#
38-
# index_events_on_convention_id (convention_id)
39-
# index_events_on_event_category_id (event_category_id)
40-
# index_events_on_owner_id (owner_id)
41-
# index_events_on_title_vector (title_vector) USING gin
42-
# index_events_on_updated_by_id (updated_by_id)
38+
# index_events_on_convention_id (convention_id)
39+
# index_events_on_event_category_id (event_category_id)
40+
# index_events_on_owner_id (owner_id)
41+
# index_events_on_registration_policy_id (registration_policy_id)
42+
# index_events_on_title_vector (title_vector) USING gin
43+
# index_events_on_updated_by_id (updated_by_id)
4344
#
4445
# Foreign Keys
4546
#
4647
# fk_rails_... (convention_id => conventions.id)
4748
# fk_rails_... (event_category_id => event_categories.id)
4849
# fk_rails_... (owner_id => users.id)
50+
# fk_rails_... (registration_policy_id => registration_policies.id)
4951
# fk_rails_... (updated_by_id => users.id)
5052
#
5153
# rubocop:enable Layout/LineLength, Lint/RedundantCopDisableDirective
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# rubocop:disable Layout/LineLength, Lint/RedundantCopDisableDirective
2+
# == Schema Information
3+
#
4+
# Table name: registration_policy_buckets
5+
#
6+
# id :bigint not null, primary key
7+
# counted :boolean default(TRUE), not null
8+
# description :text
9+
# expose_attendees :boolean default(FALSE), not null
10+
# flex :boolean default(FALSE), not null
11+
# key :string not null
12+
# minimum_slots :integer
13+
# name :text not null
14+
# position :integer not null
15+
# preferred_slots :integer
16+
# slots_limited :boolean default(FALSE), not null
17+
# total_slots :integer
18+
# created_at :datetime not null
19+
# updated_at :datetime not null
20+
# registration_policy_id :bigint not null
21+
#
22+
# Indexes
23+
#
24+
# idx_on_registration_policy_id_key_b71cb40026 (registration_policy_id,key) UNIQUE
25+
# index_registration_policy_buckets_on_registration_policy_id (registration_policy_id)
26+
#
27+
# rubocop:enable Layout/LineLength, Lint/RedundantCopDisableDirective
28+
class RegistrationPolicyBucket < ApplicationRecord
29+
end
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
class ConvertRegistrationPoliciesToTables < ActiveRecord::Migration[8.1]
2+
def change
3+
create_table :registration_policies do |t|
4+
t.boolean :prevent_no_preference_signups, null: false, default: false
5+
t.boolean :freeze_no_preference_buckets, null: false, default: false
6+
7+
t.timestamps
8+
end
9+
10+
create_table :registration_policy_buckets do |t|
11+
t.references :registration_policy, null: false
12+
t.integer :position, null: false
13+
t.string :key, null: false
14+
t.text :name, null: false
15+
t.text :description
16+
t.integer :minimum_slots
17+
t.integer :preferred_slots
18+
t.integer :total_slots
19+
t.boolean :slots_limited, default: false, null: false
20+
t.boolean :flex, default: false, null: false
21+
t.boolean :counted, default: true, null: false
22+
t.boolean :expose_attendees, default: false, null: false
23+
24+
t.timestamps
25+
26+
t.index [:registration_policy_id, :key], unique: true
27+
end
28+
29+
add_reference :events, :registration_policy, foreign_key: true
30+
31+
reversible do |dir|
32+
dir.up do
33+
data = select_rows("SELECT id, registration_policy FROM events")
34+
data.each do |(event_id, policy_json)|
35+
policy = JSON.parse(policy_json || "{}")
36+
now = Time.zone.now
37+
38+
registration_policy_id = exec_insert(
39+
"INSERT INTO registration_policies (prevent_no_preference_signups, freeze_no_preference_buckets, created_at, updated_at) VALUES ($1, $2, $3, $4)",
40+
"RegistrationPolicy insert",
41+
[
42+
!!policy["prevent_no_preference_signups"],
43+
!!policy["freeze_no_preference_buckets"],
44+
now,
45+
now
46+
],
47+
returning: [:id]
48+
).rows.first.first
49+
50+
now = Time.zone.now
51+
bucket_values = (policy["buckets"] || []).each_with_index.map do |bucket, index|
52+
{
53+
registration_policy_id:,
54+
position: index + 1,
55+
key: bucket["key"],
56+
name: bucket["name"] || bucket["key"],
57+
description: bucket["description"],
58+
minimum_slots: bucket["minimum_slots"],
59+
preferred_slots: bucket["preferred_slots"],
60+
total_slots: bucket["total_slots"],
61+
slots_limited: !!bucket["slots_limited"],
62+
flex: !!bucket["anything"],
63+
counted: !bucket["not_counted"], # we're inverting the meaning on this one
64+
expose_attendees: !!bucket["expose_attendees"],
65+
created_at: now,
66+
updated_at: now
67+
}
68+
end
69+
70+
exec_insert(
71+
<<~SQL.squish,
72+
INSERT INTO registration_policy_buckets
73+
(
74+
registration_policy_id,
75+
position,
76+
key,
77+
name,
78+
description,
79+
minimum_slots,
80+
preferred_slots,
81+
total_slots,
82+
slots_limited,
83+
flex,
84+
counted,
85+
expose_attendees,
86+
created_at,
87+
updated_at
88+
)
89+
SELECT
90+
registration_policy_id,
91+
position,
92+
key,
93+
name,
94+
description,
95+
minimum_slots,
96+
preferred_slots,
97+
total_slots,
98+
slots_limited,
99+
flex,
100+
counted,
101+
expose_attendees,
102+
created_at,
103+
updated_at
104+
FROM jsonb_populate_recordset(NULL::registration_policy_buckets, $1::jsonb)
105+
SQL
106+
"RegistrationPolicyBucket bulk insert",
107+
[bucket_values.to_json]
108+
)
109+
110+
exec_update(
111+
"UPDATE events SET registration_policy_id = $1 WHERE id = $2",
112+
"Event update",
113+
[registration_policy_id, event_id]
114+
)
115+
end
116+
end
117+
end
118+
119+
change_table :events, bulk: true do |t|
120+
t.remove :registration_policy, type: :jsonb
121+
t.change_null :registration_policy_id, false
122+
end
123+
end
124+
end

0 commit comments

Comments
 (0)