|
| 1 | +class UpdateSfsgTiles < ActiveRecord::Migration[6.1] |
| 2 | + def up |
| 3 | + ActiveRecord::Base.transaction do |
| 4 | + @assertions_enabled = true |
| 5 | + |
| 6 | + ## sfsg-health |
| 7 | + create_category_relationship 'sfsg-health', 'Medical Care' |
| 8 | + create_category_relationship 'sfsg-health', 'Mental Health Care' |
| 9 | + create_category_relationship 'sfsg-health', 'Dental Care' |
| 10 | + create_category_relationship 'sfsg-health', 'HIV Treatment' |
| 11 | + create_category_relationship 'sfsg-health', 'STD/STI Treatment & Prevention' |
| 12 | + create_category_relationship 'sfsg-health', 'Hospice' |
| 13 | + create_category_relationship 'sfsg-health', 'In-Home Support' |
| 14 | + create_category_relationship 'sfsg-health', 'Assisted Living' |
| 15 | + create_category_relationship 'sfsg-health', 'Disease Screening' |
| 16 | + |
| 17 | + delete_category_relationship 'sfsg-health', 'Coronavirus-Related Urgent Care' |
| 18 | + delete_category_relationship 'sfsg-health', 'Coronavirus (COVID-19) Testing' |
| 19 | + |
| 20 | + ## sfsg-shelter |
| 21 | + create_category 'Adult Shelter Reservation' |
| 22 | + |
| 23 | + create_category_service_relationship 'Adult Shelter Reservation', 'Adult Shelter Reservation System' |
| 24 | + |
| 25 | + create_category_relationship 'sfsg-shelter', 'Adult Shelter Reservation' |
| 26 | + create_category_relationship 'sfsg-shelter', 'Family Shelters' |
| 27 | + |
| 28 | + delete_category_relationship 'sfsg-shelter', 'We are a family with children under 18 years old.' |
| 29 | + |
| 30 | + ## sfsg-lgbtqa |
| 31 | + create_category_relationship 'sfsg-lgbtqa', 'Medical Care' |
| 32 | + create_category_relationship 'sfsg-lgbtqa', 'Dental Care' |
| 33 | + create_category_relationship 'sfsg-lgbtqa', 'HIV Treatment' |
| 34 | + create_category_relationship 'sfsg-lgbtqa', 'STD/STI Treatment & Prevention' |
| 35 | + create_category_relationship 'sfsg-lgbtqa', 'Hospice' |
| 36 | + create_category_relationship 'sfsg-lgbtqa', 'In-Home Support' |
| 37 | + create_category_relationship 'sfsg-lgbtqa', 'Assisted Living' |
| 38 | + create_category_relationship 'sfsg-lgbtqa', 'Disease Screening' |
| 39 | + end |
| 40 | + end |
| 41 | + |
| 42 | + def down |
| 43 | + raise ActiveRecord::IrreversibleMigration |
| 44 | + end |
| 45 | + |
| 46 | + private |
| 47 | + |
| 48 | + def create_category(name) |
| 49 | + assert_category_does_not_exist name |
| 50 | + |
| 51 | + exec_query <<-SQL, "create category #{name}", [name] |
| 52 | + INSERT INTO categories (name, created_at, updated_at) |
| 53 | + VALUES ($1, now(), now()) |
| 54 | + ON CONFLICT (name) DO NOTHING; |
| 55 | + SQL |
| 56 | + end |
| 57 | + |
| 58 | + def create_category_relationship(parent_name, child_name) |
| 59 | + parent_id = select_value( |
| 60 | + "SELECT id FROM categories WHERE name = $1", |
| 61 | + "find parent #{parent_name}", |
| 62 | + [parent_name] |
| 63 | + ) |
| 64 | + |
| 65 | + child_id = select_value( |
| 66 | + "SELECT id FROM categories WHERE name = $1", |
| 67 | + "find child #{child_name}", |
| 68 | + [child_name] |
| 69 | + ) |
| 70 | + |
| 71 | + assert_category_relationship_does_not_exist parent_name, child_name |
| 72 | + |
| 73 | + exec_query <<-SQL, "create category relationship #{parent_name} -> #{child_name}", [parent_id, child_id] |
| 74 | + INSERT INTO category_relationships (parent_id, child_id) |
| 75 | + VALUES ($1, $2) |
| 76 | + ON CONFLICT (parent_id, child_id) DO NOTHING; |
| 77 | + SQL |
| 78 | + end |
| 79 | + |
| 80 | + def delete_category_relationship(parent_name, child_name) |
| 81 | + assert_category_relationship_exists parent_name, child_name |
| 82 | + |
| 83 | + parent_id = select_value( |
| 84 | + "SELECT id FROM categories WHERE name = $1", |
| 85 | + "find parent #{parent_name}", |
| 86 | + [parent_name] |
| 87 | + ) |
| 88 | + |
| 89 | + child_id = select_value( |
| 90 | + "SELECT id FROM categories WHERE name = $1", |
| 91 | + "find child #{child_name}", |
| 92 | + [child_name] |
| 93 | + ) |
| 94 | + |
| 95 | + exec_query <<-SQL, "delete category relationship #{parent_name} -> #{child_name}", [parent_id, child_id] |
| 96 | + DELETE FROM category_relationships |
| 97 | + WHERE parent_id = $1 AND child_id = $2; |
| 98 | + SQL |
| 99 | + end |
| 100 | + |
| 101 | + def assert_category_relationship_exists(parent_name, child_name) |
| 102 | + return unless @assertions_enabled |
| 103 | + |
| 104 | + parent_id = select_value( |
| 105 | + "SELECT id FROM categories WHERE name = $1", |
| 106 | + "find parent #{parent_name}", |
| 107 | + [parent_name] |
| 108 | + ) |
| 109 | + |
| 110 | + child_id = select_value( |
| 111 | + "SELECT id FROM categories WHERE name = $1", |
| 112 | + "find child #{child_name}", |
| 113 | + [child_name] |
| 114 | + ) |
| 115 | + |
| 116 | + raise "Expected parent category #{parent_name} to exist" if parent_id.nil? |
| 117 | + raise "Expected child category #{child_name} to exist" if child_id.nil? |
| 118 | + |
| 119 | + count = select_value( |
| 120 | + "SELECT COUNT(*) FROM category_relationships WHERE parent_id = $1 AND child_id = $2", |
| 121 | + "count relationship #{parent_name}->#{child_name}", |
| 122 | + [parent_id, child_id] |
| 123 | + ) |
| 124 | + raise "Expected relationship #{parent_name}->#{child_name} to exist, got #{count} results" unless count.to_i > 0 |
| 125 | + end |
| 126 | + |
| 127 | + def assert_category_relationship_does_not_exist(parent_name, child_name) |
| 128 | + return unless @assertions_enabled |
| 129 | + |
| 130 | + parent_id = select_value( |
| 131 | + "SELECT id FROM categories WHERE name = $1", |
| 132 | + "find parent #{parent_name}", |
| 133 | + [parent_name] |
| 134 | + ) |
| 135 | + |
| 136 | + child_id = select_value( |
| 137 | + "SELECT id FROM categories WHERE name = $1", |
| 138 | + "find child #{child_name}", |
| 139 | + [child_name] |
| 140 | + ) |
| 141 | + |
| 142 | + raise "Expected parent category #{parent_name} to exist" if parent_id.nil? |
| 143 | + raise "Expected child category #{child_name} to exist" if child_id.nil? |
| 144 | + |
| 145 | + count = select_value( |
| 146 | + "SELECT COUNT(*) FROM category_relationships WHERE parent_id = $1 AND child_id = $2", |
| 147 | + "count relationship #{parent_name}->#{child_name}", |
| 148 | + [parent_id, child_id] |
| 149 | + ) |
| 150 | + raise "Expected relationship #{parent_name}->#{child_name} to not exist, got #{count} results" unless count == 0 |
| 151 | + end |
| 152 | + |
| 153 | + def assert_category_does_not_exist(name) |
| 154 | + return unless @assertions_enabled |
| 155 | + |
| 156 | + count = select_value("SELECT COUNT(*) FROM categories WHERE name = $1", "count category #{name}", [name]) |
| 157 | + raise "Expected category #{name} to not exist, got #{count} results" unless count == 0 |
| 158 | + end |
| 159 | + |
| 160 | + def create_category_service_relationship(category_name, service_name) |
| 161 | + category_id = select_value( |
| 162 | + "SELECT id FROM categories WHERE name = $1", |
| 163 | + "find category #{category_name}", |
| 164 | + [category_name] |
| 165 | + ) |
| 166 | + |
| 167 | + service_id = select_value( |
| 168 | + "SELECT id FROM services WHERE name = $1", |
| 169 | + "find service #{service_name}", |
| 170 | + [service_name] |
| 171 | + ) |
| 172 | + |
| 173 | + if @assertions_enabled |
| 174 | + raise "Expected category #{category_name} to exist" if category_id.nil? |
| 175 | + raise "Expected service #{service_name} to exist" if service_id.nil? |
| 176 | + |
| 177 | + count = select_value( |
| 178 | + "SELECT COUNT(*) FROM categories_services WHERE category_id = $1 AND service_id = $2", |
| 179 | + "count category service #{category_name}->#{service_name}", |
| 180 | + [category_id, service_id] |
| 181 | + ) |
| 182 | + raise "Expected category service #{category_name}->#{service_name} to not exist, got #{count} results" unless count.to_i == 0 |
| 183 | + end |
| 184 | + |
| 185 | + exec_query <<-SQL, "create category service #{category_name} -> #{service_name}", [category_id, service_id] |
| 186 | + INSERT INTO categories_services (category_id, service_id) |
| 187 | + SELECT $1, $2 |
| 188 | + WHERE NOT EXISTS ( |
| 189 | + SELECT 1 FROM categories_services WHERE category_id = $1 AND service_id = $2 |
| 190 | + ); |
| 191 | + SQL |
| 192 | + end |
| 193 | +end |
0 commit comments