Switch to using native rails support for managing Postgres enumerations
This commit is contained in:
parent
cebda5ffb9
commit
f2f0cf1ad9
11 changed files with 14 additions and 37 deletions
|
@ -42,25 +42,6 @@ if defined?(ActiveRecord::ConnectionAdapters::AbstractAdapter)
|
||||||
|
|
||||||
execute "ALTER TABLE #{table_name} ADD PRIMARY KEY USING INDEX #{constraint_name}"
|
execute "ALTER TABLE #{table_name} ADD PRIMARY KEY USING INDEX #{constraint_name}"
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class AddRelations < ActiveRecord::Migration[4.2]
|
class AddRelations < ActiveRecord::Migration[4.2]
|
||||||
def self.up
|
def self.up
|
||||||
# enums work like strings but are more efficient
|
# 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.
|
# a relation can have members much like a way can have nodes.
|
||||||
# differences:
|
# differences:
|
||||||
|
|
|
@ -4,7 +4,7 @@ class AddMoreControlsToGpxFiles < ActiveRecord::Migration[4.2]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.up
|
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
|
add_column :gpx_files, :visibility, :gpx_visibility_enum, :default => "public", :null => false
|
||||||
Trace.where(:public => false).update_all(:visibility => "private")
|
Trace.where(:public => false).update_all(:visibility => "private")
|
||||||
add_index :gpx_files, [:visible, :visibility], :name => "gpx_files_visible_visibility_idx"
|
add_index :gpx_files, [:visible, :visibility], :name => "gpx_files_visible_visibility_idx"
|
||||||
|
|
|
@ -6,7 +6,7 @@ class CreateUserRoles < ActiveRecord::Migration[4.2]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.up
|
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|
|
create_table :user_roles do |t|
|
||||||
t.column :user_id, :bigint, :null => false
|
t.column :user_id, :bigint, :null => false
|
||||||
|
|
|
@ -3,7 +3,7 @@ class AddStatusToUser < ActiveRecord::Migration[4.2]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.up
|
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"
|
add_column :users, :status, :user_status_enum, :null => false, :default => "pending"
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class AddMapBugTables < ActiveRecord::Migration[4.2]
|
class AddMapBugTables < ActiveRecord::Migration[4.2]
|
||||||
def self.up
|
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|
|
create_table :map_bugs do |t|
|
||||||
t.integer :latitude, :null => false
|
t.integer :latitude, :null => false
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class AddMapBugCommentEvent < ActiveRecord::Migration[4.2]
|
class AddMapBugCommentEvent < ActiveRecord::Migration[4.2]
|
||||||
def self.up
|
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
|
add_column :map_bug_comment, :event, :map_bug_event_enum
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
class RenameBugsToNotes < ActiveRecord::Migration[4.2]
|
class RenameBugsToNotes < ActiveRecord::Migration[4.2]
|
||||||
def self.up
|
def self.up
|
||||||
rename_enumeration "map_bug_status_enum", "note_status_enum"
|
rename_enum "map_bug_status_enum", :to => "note_status_enum"
|
||||||
rename_enumeration "map_bug_event_enum", "note_event_enum"
|
rename_enum "map_bug_event_enum", :to => "note_event_enum"
|
||||||
|
|
||||||
rename_table :map_bugs, :notes
|
rename_table :map_bugs, :notes
|
||||||
rename_index :notes, "map_bugs_changed_idx", "notes_updated_at_idx"
|
rename_index :notes, "map_bugs_changed_idx", "notes_updated_at_idx"
|
||||||
|
@ -23,7 +23,7 @@ class RenameBugsToNotes < ActiveRecord::Migration[4.2]
|
||||||
rename_index :notes, "notes_updated_at_idx", "map_bugs_changed_idx"
|
rename_index :notes, "notes_updated_at_idx", "map_bugs_changed_idx"
|
||||||
rename_table :notes, :map_bugs
|
rename_table :notes, :map_bugs
|
||||||
|
|
||||||
rename_enumeration "note_event_enum", "map_bug_event_enum"
|
rename_enum "note_event_enum", :to => "map_bug_event_enum"
|
||||||
rename_enumeration "note_status_enum", "map_bug_status_enum"
|
rename_enum "note_status_enum", :to => "map_bug_status_enum"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class AddTextFormat < ActiveRecord::Migration[4.2]
|
class AddTextFormat < ActiveRecord::Migration[4.2]
|
||||||
def up
|
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 :users, :description_format, :format_enum, :null => false, :default => "html"
|
||||||
add_column :user_blocks, :reason_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"
|
add_column :diary_entries, :body_format, :format_enum, :null => false, :default => "html"
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
class CreateIssuesAndReports < ActiveRecord::Migration[5.0]
|
class CreateIssuesAndReports < ActiveRecord::Migration[5.0]
|
||||||
def up
|
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|
|
create_table :issues do |t|
|
||||||
t.string :reportable_type, :null => false
|
t.string :reportable_type, :null => false
|
||||||
|
|
|
@ -1,9 +1,5 @@
|
||||||
class AddImporterRole < ActiveRecord::Migration[7.1]
|
class AddImporterRole < ActiveRecord::Migration[7.1]
|
||||||
def up
|
def change
|
||||||
add_enumeration_value :user_role_enum, "importer"
|
add_enum_value :user_role_enum, "importer"
|
||||||
end
|
|
||||||
|
|
||||||
def down
|
|
||||||
raise ActiveRecord::IrreversibleMigration
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Reference in a new issue