|
| 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