Skip to content

Commit

Permalink
Use unique index everywhere by default
Browse files Browse the repository at this point in the history
  • Loading branch information
gsmetal committed Sep 4, 2017
1 parent 8e1390f commit 23fe1f0
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
23 changes: 20 additions & 3 deletions lib/uuidable/migration.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
module Uuidable
COLUMN_NAME = :uuid
COLUMN_TYPE = :binary
COLUMN_OPTIONS = { limit: 36, null: false, index: true }.freeze
COLUMN_OPTIONS = { limit: 36, null: false }.freeze
INDEX_OPTIONS = { unique: true }.freeze

# Module adds method to table definition
module TableDefinition
def uuid(opts = {})
column COLUMN_NAME, COLUMN_TYPE, opts.merge(COLUMN_OPTIONS)
index_opts = opts.delete(:index)
column_name = opts.delete(:column_name) || COLUMN_NAME

column column_name, COLUMN_TYPE, COLUMN_OPTIONS.merge(opts)
index column_name, INDEX_OPTIONS.merge(index_opts) unless index_opts == false
end
end

# Module adds method to alter table migration
module Migration
def add_uuid_column(table_name, opts = {})
add_column table_name, COLUMN_NAME, COLUMN_TYPE, opts.merge(COLUMN_OPTIONS)
index_opts = opts.delete(:index)
column_name = opts.delete(:column_name) || COLUMN_NAME

add_column table_name, column_name, COLUMN_TYPE, COLUMN_OPTIONS.merge(opts)

add_uuid_index(index_opts.merge(column_name: column_name)) unless index_opts == false
end

def add_uuid_index(table_name, opts = {})
column_name = opts.delete(:column_name) || COLUMN_NAME

add_index table_name, column_name, INDEX_OPTIONS.merge(opts)
end
end
end

if defined? ActiveRecord::ConnectionAdapters::TableDefinition
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Uuidable::TableDefinition
end
Expand Down
2 changes: 1 addition & 1 deletion lib/uuidable/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Uuidable
VERSION = '0.1.3'.freeze
VERSION = '0.2.0'.freeze
end

0 comments on commit 23fe1f0

Please sign in to comment.