Skip to content

Commit

Permalink
Switch to using native rails support for managing Postgres enumerations
Browse files Browse the repository at this point in the history
  • Loading branch information
tomhughes committed Nov 15, 2023
1 parent cebda5f commit f2f0cf1
Show file tree
Hide file tree
Showing 11 changed files with 14 additions and 37 deletions.
19 changes: 0 additions & 19 deletions config/initializers/migrate.rb
Expand Up @@ -42,25 +42,6 @@ def set_primary_key(table_name, constraint_name)

execute "ALTER TABLE #{table_name} ADD PRIMARY KEY USING INDEX #{constraint_name}"
end

def create_enumeration(enumeration_name, values)
execute "CREATE TYPE #{enumeration_name} AS ENUM ('#{values.join '\',\''}')"
end

def drop_enumeration(enumeration_name)
execute "DROP TYPE #{enumeration_name}"
end

def add_enumeration_value(enumeration_name, value)
execute "ALTER TYPE #{enumeration_name} ADD VALUE '#{value}'"
end

def rename_enumeration(old_name, new_name)
old_name = quote_table_name(old_name)
new_name = quote_table_name(new_name)

execute "ALTER TYPE #{old_name} RENAME TO #{new_name}"
end
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/007_add_relations.rb
@@ -1,7 +1,7 @@
class AddRelations < ActiveRecord::Migration[4.2]
def self.up
# enums work like strings but are more efficient
create_enumeration :nwr_enum, %w[Node Way Relation]
create_enum :nwr_enum, %w[Node Way Relation]

# a relation can have members much like a way can have nodes.
# differences:
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/039_add_more_controls_to_gpx_files.rb
Expand Up @@ -4,7 +4,7 @@ class Trace < ApplicationRecord
end

def self.up
create_enumeration :gpx_visibility_enum, %w[private public trackable identifiable]
create_enum :gpx_visibility_enum, %w[private public trackable identifiable]
add_column :gpx_files, :visibility, :gpx_visibility_enum, :default => "public", :null => false
Trace.where(:public => false).update_all(:visibility => "private")
add_index :gpx_files, [:visible, :visibility], :name => "gpx_files_visible_visibility_idx"
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/044_create_user_roles.rb
Expand Up @@ -6,7 +6,7 @@ class UserRole < ApplicationRecord
end

def self.up
create_enumeration :user_role_enum, %w[administrator moderator]
create_enum :user_role_enum, %w[administrator moderator]

create_table :user_roles do |t|
t.column :user_id, :bigint, :null => false
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/051_add_status_to_user.rb
Expand Up @@ -3,7 +3,7 @@ class User < ApplicationRecord
end

def self.up
create_enumeration :user_status_enum, %w[pending active confirmed suspended deleted]
create_enum :user_status_enum, %w[pending active confirmed suspended deleted]

add_column :users, :status, :user_status_enum, :null => false, :default => "pending"

Expand Down
2 changes: 1 addition & 1 deletion db/migrate/053_add_map_bug_tables.rb
@@ -1,6 +1,6 @@
class AddMapBugTables < ActiveRecord::Migration[4.2]
def self.up
create_enumeration :map_bug_status_enum, %w[open closed hidden]
create_enum :map_bug_status_enum, %w[open closed hidden]

create_table :map_bugs do |t|
t.integer :latitude, :null => false
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/057_add_map_bug_comment_event.rb
@@ -1,6 +1,6 @@
class AddMapBugCommentEvent < ActiveRecord::Migration[4.2]
def self.up
create_enumeration :map_bug_event_enum, %w[opened closed reopened commented hidden]
create_enum :map_bug_event_enum, %w[opened closed reopened commented hidden]

add_column :map_bug_comment, :event, :map_bug_event_enum
end
Expand Down
8 changes: 4 additions & 4 deletions db/migrate/20110521142405_rename_bugs_to_notes.rb
@@ -1,7 +1,7 @@
class RenameBugsToNotes < ActiveRecord::Migration[4.2]
def self.up
rename_enumeration "map_bug_status_enum", "note_status_enum"
rename_enumeration "map_bug_event_enum", "note_event_enum"
rename_enum "map_bug_status_enum", :to => "note_status_enum"
rename_enum "map_bug_event_enum", :to => "note_event_enum"

rename_table :map_bugs, :notes
rename_index :notes, "map_bugs_changed_idx", "notes_updated_at_idx"
Expand All @@ -23,7 +23,7 @@ def self.down
rename_index :notes, "notes_updated_at_idx", "map_bugs_changed_idx"
rename_table :notes, :map_bugs

rename_enumeration "note_event_enum", "map_bug_event_enum"
rename_enumeration "note_status_enum", "map_bug_status_enum"
rename_enum "note_event_enum", :to => "map_bug_event_enum"
rename_enum "note_status_enum", :to => "map_bug_status_enum"
end
end
2 changes: 1 addition & 1 deletion db/migrate/20120214210114_add_text_format.rb
@@ -1,6 +1,6 @@
class AddTextFormat < ActiveRecord::Migration[4.2]
def up
create_enumeration :format_enum, %w[html markdown text]
create_enum :format_enum, %w[html markdown text]
add_column :users, :description_format, :format_enum, :null => false, :default => "html"
add_column :user_blocks, :reason_format, :format_enum, :null => false, :default => "html"
add_column :diary_entries, :body_format, :format_enum, :null => false, :default => "html"
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/20160822153055_create_issues_and_reports.rb
@@ -1,6 +1,6 @@
class CreateIssuesAndReports < ActiveRecord::Migration[5.0]
def up
create_enumeration :issue_status_enum, %w[open ignored resolved]
create_enum :issue_status_enum, %w[open ignored resolved]

create_table :issues do |t|
t.string :reportable_type, :null => false
Expand Down
8 changes: 2 additions & 6 deletions db/migrate/20231029151516_add_importer_role.rb
@@ -1,9 +1,5 @@
class AddImporterRole < ActiveRecord::Migration[7.1]
def up
add_enumeration_value :user_role_enum, "importer"
end

def down
raise ActiveRecord::IrreversibleMigration
def change
add_enum_value :user_role_enum, "importer"
end
end

0 comments on commit f2f0cf1

Please sign in to comment.