1+ class UpdateSfsgFinanceAndJobsCategories < ActiveRecord ::Migration [ 6.1 ]
2+ def up
3+ ActiveRecord ::Base . transaction do
4+ @assertions_enabled = true
5+
6+ rename_category (
7+ from : 'Financial assistance for living expenses' ,
8+ to : 'Financial Assistance'
9+ )
10+
11+ create_category_relationship 'sfsg-finance' , 'Legal Services'
12+ create_category_relationship 'sfsg-finance' , 'Tax Preparation'
13+ create_category_relationship 'sfsg-jobs' , 'Alternative Education & GED'
14+ end
15+ end
16+
17+ def down
18+ raise ActiveRecord ::IrreversibleMigration
19+ end
20+
21+ private
22+
23+ def assert_category_exists ( name )
24+ return unless @assertions_enabled
25+
26+ count = select_value ( "SELECT COUNT(*) FROM categories WHERE name = $1" , "count category #{ name } " , [ name ] )
27+ raise "Expected category #{ name } to exist, got #{ count } results" unless count == 1
28+ end
29+
30+ def assert_category_does_not_exist ( 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 not exist, got #{ count } results" unless count == 0
35+ end
36+
37+ def rename_category ( from :, to :)
38+ assert_category_exists from
39+ assert_category_does_not_exist to
40+
41+ exec_query <<-SQL , "rename category from #{ from } to #{ to } " , [ to , from ]
42+ UPDATE categories
43+ SET name = $1
44+ WHERE name = $2;
45+ SQL
46+ end
47+
48+ def create_category_relationship ( parent_name , child_name )
49+ parent_id = select_value (
50+ "SELECT id FROM categories WHERE name = $1" ,
51+ "find parent #{ parent_name } " ,
52+ [ parent_name ]
53+ )
54+
55+ child_id = select_value (
56+ "SELECT id FROM categories WHERE name = $1" ,
57+ "find child #{ child_name } " ,
58+ [ child_name ]
59+ )
60+
61+ assert_category_relationship_does_not_exist parent_name , child_name
62+
63+ exec_query <<-SQL , "create category relationship #{ parent_name } -> #{ child_name } " , [ parent_id , child_id ]
64+ INSERT INTO category_relationships (parent_id, child_id)
65+ VALUES ($1, $2);
66+ SQL
67+ end
68+
69+ def assert_category_relationship_does_not_exist ( parent_name , child_name )
70+ return unless @assertions_enabled
71+
72+ parent_id = select_value (
73+ "SELECT id FROM categories WHERE name = $1" ,
74+ "find parent #{ parent_name } " ,
75+ [ parent_name ]
76+ )
77+
78+ child_id = select_value (
79+ "SELECT id FROM categories WHERE name = $1" ,
80+ "find child #{ child_name } " ,
81+ [ child_name ]
82+ )
83+
84+ raise "Expected parent category #{ parent_name } to exist" if parent_id . nil?
85+ raise "Expected child category #{ child_name } to exist" if child_id . nil?
86+
87+ count = select_value (
88+ "SELECT COUNT(*) FROM category_relationships WHERE parent_id = $1 AND child_id = $2" ,
89+ "count relationship #{ parent_name } ->#{ child_name } " ,
90+ [ parent_id , child_id ]
91+ )
92+ raise "Expected relationship #{ parent_name } ->#{ child_name } to not exist, got #{ count } results" unless count . to_i == 0
93+ end
94+ end
0 commit comments