|
| 1 | +class AddNewDcyfCategories < ActiveRecord::Migration[6.1] |
| 2 | + def up |
| 3 | + ActiveRecord::Base.transaction do |
| 4 | + # Set this to true to enable assertions |
| 5 | + @assertions_enabled = true |
| 6 | + |
| 7 | + MAPPING.each do |top_category, categories| |
| 8 | + assert_category_exists(top_category) |
| 9 | + |
| 10 | + categories.each do |category| |
| 11 | + create_category(category) |
| 12 | + create_subcategory_relationship(top_category, category) |
| 13 | + end |
| 14 | + end |
| 15 | + |
| 16 | + # Unlike all the other subcategories, this one already exists, but we just |
| 17 | + # want to link it to another top-level category. |
| 18 | + assert_category_exists("Playgroups") |
| 19 | + assert_category_exists("Sports & Recreation") |
| 20 | + create_subcategory_relationship("Sports & Recreation", "Playgroups") |
| 21 | + end |
| 22 | + end |
| 23 | + |
| 24 | + def down |
| 25 | + raise ActiveRecord::IrreversibleMigration |
| 26 | + end |
| 27 | + |
| 28 | + private |
| 29 | + |
| 30 | + def assert_category_exists(name) |
| 31 | + return unless @assertions_enabled |
| 32 | + |
| 33 | + count = select_value("SELECT COUNT(*) FROM categories WHERE name = $1", "count category #{name}", [name]) |
| 34 | + raise "Expected category #{name} to exist, got #{count} results" unless count == 1 |
| 35 | + end |
| 36 | + |
| 37 | + def assert_category_does_not_exist(name) |
| 38 | + return unless @assertions_enabled |
| 39 | + |
| 40 | + count = select_value("SELECT COUNT(*) FROM categories WHERE name = $1", "count category #{name}", [name]) |
| 41 | + raise "Expected category #{name} to not exist, got #{count} results" unless count == 0 |
| 42 | + end |
| 43 | + |
| 44 | + def create_category(name) |
| 45 | + assert_category_does_not_exist name |
| 46 | + |
| 47 | + exec_query <<-SQL, "create category #{name}", [name] |
| 48 | + INSERT INTO categories (name, created_at, updated_at) |
| 49 | + VALUES ($1, now(), now()) |
| 50 | + ON CONFLICT (name) DO NOTHING; |
| 51 | + SQL |
| 52 | + end |
| 53 | + |
| 54 | + def category_id(name) |
| 55 | + id = select_value("SELECT id FROM categories WHERE name = $1", "get category id #{name}", [name]) |
| 56 | + raise "Category #{name} does not exist" if id.nil? |
| 57 | + id |
| 58 | + end |
| 59 | + |
| 60 | + def create_subcategory_relationship(top_category, subcategory) |
| 61 | + top_category_id = category_id(top_category) |
| 62 | + subcategory_id = category_id(subcategory) |
| 63 | + exec_query <<-SQL, "create subcategory relationship #{top_category} -> #{subcategory}", [top_category_id, subcategory_id] |
| 64 | + INSERT INTO category_relationships (parent_id, child_id) |
| 65 | + VALUES ($1, $2); |
| 66 | + SQL |
| 67 | + end |
| 68 | + |
| 69 | + |
| 70 | + MAPPING = { |
| 71 | + "Education" => [ |
| 72 | + "Truancy Prevention", |
| 73 | + ], |
| 74 | + "Sports & Recreation" => [ |
| 75 | + "Board Games", |
| 76 | + "Circus Arts", |
| 77 | + "Cycling", |
| 78 | + "Meditation", |
| 79 | + "Pilates", |
| 80 | + "Rock Climbing", |
| 81 | + "Ropes Course", |
| 82 | + "Sports Programs", |
| 83 | + "Zumba", |
| 84 | + ], |
| 85 | + } |
| 86 | +end |
0 commit comments