-
Notifications
You must be signed in to change notification settings - Fork 21
SFSG Tile Updates #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
SFSG Tile Updates #765
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,193 @@ | ||
| class UpdateSfsgTiles < ActiveRecord::Migration[6.1] | ||
| def up | ||
| ActiveRecord::Base.transaction do | ||
| @assertions_enabled = true | ||
|
|
||
| ## sfsg-health | ||
| create_category_relationship 'sfsg-health', 'Medical Care' | ||
| create_category_relationship 'sfsg-health', 'Mental Health Care' | ||
| create_category_relationship 'sfsg-health', 'Dental Care' | ||
| create_category_relationship 'sfsg-health', 'HIV Treatment' | ||
| create_category_relationship 'sfsg-health', 'STD/STI Treatment & Prevention' | ||
| create_category_relationship 'sfsg-health', 'Hospice' | ||
| create_category_relationship 'sfsg-health', 'In-Home Support' | ||
| create_category_relationship 'sfsg-health', 'Assisted Living' | ||
| create_category_relationship 'sfsg-health', 'Disease Screening' | ||
|
|
||
| delete_category_relationship 'sfsg-health', 'Coronavirus-Related Urgent Care' | ||
| delete_category_relationship 'sfsg-health', 'Coronavirus (COVID-19) Testing' | ||
|
|
||
| ## sfsg-shelter | ||
| create_category 'Adult Shelter Reservation' | ||
|
|
||
| create_category_service_relationship 'Adult Shelter Reservation', 'Adult Shelter Reservation System' | ||
|
|
||
| create_category_relationship 'sfsg-shelter', 'Adult Shelter Reservation' | ||
| create_category_relationship 'sfsg-shelter', 'Family Shelters' | ||
|
|
||
| delete_category_relationship 'sfsg-shelter', 'We are a family with children under 18 years old.' | ||
|
|
||
| ## sfsg-lgbtqa | ||
| create_category_relationship 'sfsg-lgbtqa', 'Medical Care' | ||
| create_category_relationship 'sfsg-lgbtqa', 'Dental Care' | ||
| create_category_relationship 'sfsg-lgbtqa', 'HIV Treatment' | ||
| create_category_relationship 'sfsg-lgbtqa', 'STD/STI Treatment & Prevention' | ||
| create_category_relationship 'sfsg-lgbtqa', 'Hospice' | ||
| create_category_relationship 'sfsg-lgbtqa', 'In-Home Support' | ||
| create_category_relationship 'sfsg-lgbtqa', 'Assisted Living' | ||
| create_category_relationship 'sfsg-lgbtqa', 'Disease Screening' | ||
| end | ||
| end | ||
|
|
||
| def down | ||
| raise ActiveRecord::IrreversibleMigration | ||
| end | ||
|
|
||
| private | ||
|
|
||
| def create_category(name) | ||
| assert_category_does_not_exist name | ||
|
|
||
| exec_query <<-SQL, "create category #{name}", [name] | ||
| INSERT INTO categories (name, created_at, updated_at) | ||
| VALUES ($1, now(), now()) | ||
| ON CONFLICT (name) DO NOTHING; | ||
| SQL | ||
| end | ||
|
|
||
| def create_category_relationship(parent_name, child_name) | ||
| parent_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find parent #{parent_name}", | ||
| [parent_name] | ||
| ) | ||
|
|
||
| child_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find child #{child_name}", | ||
| [child_name] | ||
| ) | ||
|
|
||
| assert_category_relationship_does_not_exist parent_name, child_name | ||
|
|
||
| exec_query <<-SQL, "create category relationship #{parent_name} -> #{child_name}", [parent_id, child_id] | ||
| INSERT INTO category_relationships (parent_id, child_id) | ||
| VALUES ($1, $2) | ||
| ON CONFLICT (parent_id, child_id) DO NOTHING; | ||
| SQL | ||
| end | ||
|
|
||
| def delete_category_relationship(parent_name, child_name) | ||
| assert_category_relationship_exists parent_name, child_name | ||
|
|
||
| parent_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find parent #{parent_name}", | ||
| [parent_name] | ||
| ) | ||
|
|
||
| child_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find child #{child_name}", | ||
| [child_name] | ||
| ) | ||
|
|
||
| exec_query <<-SQL, "delete category relationship #{parent_name} -> #{child_name}", [parent_id, child_id] | ||
| DELETE FROM category_relationships | ||
| WHERE parent_id = $1 AND child_id = $2; | ||
| SQL | ||
| end | ||
|
|
||
| def assert_category_relationship_exists(parent_name, child_name) | ||
| return unless @assertions_enabled | ||
|
|
||
| parent_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find parent #{parent_name}", | ||
| [parent_name] | ||
| ) | ||
|
|
||
| child_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find child #{child_name}", | ||
| [child_name] | ||
| ) | ||
|
|
||
| raise "Expected parent category #{parent_name} to exist" if parent_id.nil? | ||
| raise "Expected child category #{child_name} to exist" if child_id.nil? | ||
|
|
||
| count = select_value( | ||
| "SELECT COUNT(*) FROM category_relationships WHERE parent_id = $1 AND child_id = $2", | ||
| "count relationship #{parent_name}->#{child_name}", | ||
| [parent_id, child_id] | ||
| ) | ||
| raise "Expected relationship #{parent_name}->#{child_name} to exist, got #{count} results" unless count.to_i > 0 | ||
| end | ||
|
|
||
| def assert_category_relationship_does_not_exist(parent_name, child_name) | ||
| return unless @assertions_enabled | ||
|
|
||
| parent_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find parent #{parent_name}", | ||
| [parent_name] | ||
| ) | ||
|
|
||
| child_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find child #{child_name}", | ||
| [child_name] | ||
| ) | ||
|
|
||
| raise "Expected parent category #{parent_name} to exist" if parent_id.nil? | ||
| raise "Expected child category #{child_name} to exist" if child_id.nil? | ||
|
|
||
| count = select_value( | ||
| "SELECT COUNT(*) FROM category_relationships WHERE parent_id = $1 AND child_id = $2", | ||
| "count relationship #{parent_name}->#{child_name}", | ||
| [parent_id, child_id] | ||
| ) | ||
| raise "Expected relationship #{parent_name}->#{child_name} to not exist, got #{count} results" unless count == 0 | ||
| end | ||
|
|
||
| def assert_category_does_not_exist(name) | ||
| return unless @assertions_enabled | ||
|
|
||
| count = select_value("SELECT COUNT(*) FROM categories WHERE name = $1", "count category #{name}", [name]) | ||
| raise "Expected category #{name} to not exist, got #{count} results" unless count == 0 | ||
| end | ||
|
|
||
| def create_category_service_relationship(category_name, service_name) | ||
| category_id = select_value( | ||
| "SELECT id FROM categories WHERE name = $1", | ||
| "find category #{category_name}", | ||
| [category_name] | ||
| ) | ||
|
|
||
| service_id = select_value( | ||
| "SELECT id FROM services WHERE name = $1", | ||
| "find service #{service_name}", | ||
| [service_name] | ||
| ) | ||
|
|
||
| if @assertions_enabled | ||
| raise "Expected category #{category_name} to exist" if category_id.nil? | ||
| raise "Expected service #{service_name} to exist" if service_id.nil? | ||
|
|
||
| count = select_value( | ||
| "SELECT COUNT(*) FROM categories_services WHERE category_id = $1 AND service_id = $2", | ||
| "count category service #{category_name}->#{service_name}", | ||
| [category_id, service_id] | ||
| ) | ||
| raise "Expected category service #{category_name}->#{service_name} to not exist, got #{count} results" unless count.to_i == 0 | ||
| end | ||
|
|
||
| exec_query <<-SQL, "create category service #{category_name} -> #{service_name}", [category_id, service_id] | ||
| INSERT INTO categories_services (category_id, service_id) | ||
| SELECT $1, $2 | ||
| WHERE NOT EXISTS ( | ||
| SELECT 1 FROM categories_services WHERE category_id = $1 AND service_id = $2 | ||
| ); | ||
| SQL | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to create this as a real category? If we could make this a frontend-only thing, then it wouldn't pollute the actual DB and edit interface with this category.