forked from abdojulari/transcript-editor
-
Notifications
You must be signed in to change notification settings - Fork 4
Migrate from String Based -> Integer Based Enums #158
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
Open
cjbmartinez
wants to merge
1
commit into
develop
Choose a base branch
from
feature/migrate-to-integer-based-enums
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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 |
|---|---|---|
| @@ -1,6 +1,5 @@ | ||
| class SiteAlert < ApplicationRecord | ||
| has_paper_trail | ||
|
|
||
| # TODO: Convert to integer-based enum | ||
| enum :level, { status: 'status', warning: 'warning', error: 'error' } | ||
| enum :level, { status: 0, warning: 1, error: 2 } | ||
| 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
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
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
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
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
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,14 @@ | ||
| class AddIntegerEnumColumns < ActiveRecord::Migration[8.0] | ||
| def change | ||
| # Add new integer columns (temporarily suffixed with _int) | ||
| # Also replicated the default value and null constraints from the existing | ||
| # string columns | ||
| add_column :transcripts, :process_status_int, :integer | ||
| add_column :site_alerts, :level_int, :integer, default: 0, null: false | ||
| add_column :user_roles, :transcribing_role_int, :integer, default: 0 | ||
|
|
||
| add_index :transcripts, :process_status_int | ||
| add_index :site_alerts, :level_int | ||
| add_index :user_roles, :transcribing_role_int | ||
| end | ||
| end |
69 changes: 69 additions & 0 deletions
69
db/migrate/20250715020636_convert_string_enums_to_integers.rb
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,69 @@ | ||
| class ConvertStringEnumsToIntegers < ActiveRecord::Migration[8.0] | ||
| def up | ||
| execute <<-SQL | ||
| UPDATE transcripts | ||
| SET process_status_int = CASE | ||
| WHEN process_status = 'started' THEN 0 | ||
| WHEN process_status = 'completed' THEN 1 | ||
| WHEN process_status = 'failed' THEN 2 | ||
| ELSE NULL | ||
| END | ||
| WHERE process_status IS NOT NULL; | ||
| SQL | ||
|
|
||
| execute <<-SQL | ||
| UPDATE site_alerts | ||
| SET level_int = CASE | ||
| WHEN level = 'status' THEN 0 | ||
| WHEN level = 'warning' THEN 1 | ||
| WHEN level = 'error' THEN 2 | ||
| ELSE NULL | ||
| END | ||
| WHERE level IS NOT NULL; | ||
| SQL | ||
|
|
||
| execute <<-SQL | ||
| UPDATE user_roles | ||
| SET transcribing_role_int = CASE | ||
| WHEN transcribing_role = 'registered_user' THEN 0 | ||
| WHEN transcribing_role = 'admin' THEN 1 | ||
| ELSE NULL | ||
| END | ||
| WHERE transcribing_role IS NOT NULL; | ||
| SQL | ||
| end | ||
|
|
||
| def down | ||
| execute <<-SQL | ||
| UPDATE transcripts | ||
| SET process_status = CASE | ||
| WHEN process_status_int = 0 THEN 'started' | ||
| WHEN process_status_int = 1 THEN 'completed' | ||
| WHEN process_status_int = 2 THEN 'failed' | ||
| ELSE NULL | ||
| END | ||
| WHERE process_status_int IS NOT NULL; | ||
| SQL | ||
|
|
||
| execute <<-SQL | ||
| UPDATE site_alerts | ||
| SET level = CASE | ||
| WHEN level_int = 0 THEN 'status' | ||
| WHEN level_int = 1 THEN 'warning' | ||
| WHEN level_int = 2 THEN 'error' | ||
| ELSE NULL | ||
| END | ||
| WHERE level_int IS NOT NULL; | ||
| SQL | ||
|
|
||
| execute <<-SQL | ||
| UPDATE user_roles | ||
| SET transcribing_role = CASE | ||
| WHEN transcribing_role_int = 0 THEN 'registered_user' | ||
| WHEN transcribing_role_int = 1 THEN 'admin' | ||
| ELSE NULL | ||
| END | ||
| WHERE transcribing_role_int IS NOT NULL; | ||
| SQL | ||
| end | ||
| end |
54 changes: 54 additions & 0 deletions
54
db/migrate/20250715020731_finalize_integer_enum_migration.rb
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,54 @@ | ||
| class FinalizeIntegerEnumMigration < ActiveRecord::Migration[8.0] | ||
| def up | ||
| remove_column :transcripts, :process_status | ||
| remove_column :site_alerts, :level | ||
| remove_column :user_roles, :transcribing_role | ||
|
|
||
| # Rename new integer columns to original names | ||
| rename_column :transcripts, :process_status_int, :process_status | ||
| rename_column :site_alerts, :level_int, :level | ||
| rename_column :user_roles, :transcribing_role_int, :transcribing_role | ||
| end | ||
|
|
||
| def down | ||
| rename_column :transcripts, :process_status, :process_status_int | ||
| rename_column :site_alerts, :level, :level_int | ||
| rename_column :user_roles, :transcribing_role, :transcribing_role_int | ||
|
|
||
| add_column :transcripts, :process_status, :string | ||
| add_column :site_alerts, :level, :string | ||
| add_column :user_roles, :transcribing_role, :string | ||
|
|
||
| execute <<-SQL | ||
| UPDATE transcripts | ||
| SET process_status = CASE | ||
| WHEN process_status_int = 0 THEN 'started' | ||
| WHEN process_status_int = 1 THEN 'completed' | ||
| WHEN process_status_int = 2 THEN 'failed' | ||
| ELSE NULL | ||
| END | ||
| WHERE process_status_int IS NOT NULL; | ||
| SQL | ||
|
|
||
| execute <<-SQL | ||
| UPDATE site_alerts | ||
| SET level = CASE | ||
| WHEN level_int = 0 THEN 'status' | ||
| WHEN level_int = 1 THEN 'warning' | ||
| WHEN level_int = 2 THEN 'error' | ||
| ELSE NULL | ||
| END | ||
| WHERE level_int IS NOT NULL; | ||
| SQL | ||
|
|
||
| execute <<-SQL | ||
| UPDATE user_roles | ||
| SET transcribing_role = CASE | ||
| WHEN transcribing_role_int = 0 THEN 'registered_user' | ||
| WHEN transcribing_role_int = 1 THEN 'admin' | ||
| ELSE NULL | ||
| END | ||
| WHERE transcribing_role_int IS NOT NULL; | ||
| 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.
nice!