Module: SchemaAutoForeignKeys::AutoCreate
- Included in:
- Middleware::Migration::Column::MySQL, Middleware::Migration::Column::PostgreSQL, Middleware::Migration::Column::SQLite3
- Defined in:
- lib/schema_auto_foreign_keys/middleware/migration.rb,
lib/schema_auto_foreign_keys/middleware/migration.rb
Class Method Summary collapse
Instance Method Summary collapse
- #after(env) ⇒ Object
- #auto_fk?(env, config) ⇒ Boolean
- #auto_index?(env, config) ⇒ Boolean
- #auto_index_name(env) ⇒ Object
- #before(env) ⇒ Object
- #remove_auto_index(env) ⇒ Object
- #remove_auto_index?(env) ⇒ Boolean
- #set_auto_index(env) ⇒ Object
- #set_foreign_key(env) ⇒ Object
Class Method Details
.auto_index_name(from_table, column_name) ⇒ Object
79 80 81 82 83 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 79 def self.auto_index_name(from_table, column_name) name = "fk__#{fixup_schema_name(from_table)}_#{Array.wrap(column_name).join('_and_')}" name = name.slice(0, 27) + "_" + OpenSSL::Digest::MD5.new.hexdigest(name) if name.length > 60 name end |
.fixup_schema_name(table_name) ⇒ Object
85 86 87 88 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 85 def self.fixup_schema_name(table_name) # replace . with _ table_name.to_s.gsub(/[.]/, '_') end |
Instance Method Details
#after(env) ⇒ Object
41 42 43 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 41 def after(env) remove_auto_index(env) if env.operation == :change and remove_auto_index?(env) end |
#auto_fk?(env, config) ⇒ Boolean
45 46 47 48 49 50 51 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 45 def auto_fk?(env, config) return false if env..include? :foreign_key return false unless config.auto_create? return true if env.type == :reference return false if env.implements_reference return true if env.column_name.to_s =~ /_id$/ # later on add a config option for this end |
#auto_index?(env, config) ⇒ Boolean
53 54 55 56 57 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 53 def auto_index?(env, config) return false if env..include? :index return false unless env.[:foreign_key] return true if config.auto_index? end |
#auto_index_name(env) ⇒ Object
75 76 77 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 75 def auto_index_name(env) AutoCreate.auto_index_name(env.table_name, env.column_name) end |
#before(env) ⇒ Object
35 36 37 38 39 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 35 def before(env) config ||= env.caller.try(:schema_plus_foreign_keys_config) || SchemaPlus::ForeignKeys.config set_foreign_key(env) if auto_fk?(env, config) set_auto_index(env) if auto_index?(env, config) end |
#remove_auto_index(env) ⇒ Object
71 72 73 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 71 def remove_auto_index(env) env.caller.remove_index(env.table_name, :name => auto_index_name(env), :column => env.column_name, :if_exists => true) end |
#remove_auto_index?(env) ⇒ Boolean
59 60 61 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 59 def remove_auto_index?(env) env..include? :foreign_key and not env.[:foreign_key] end |
#set_auto_index(env) ⇒ Object
67 68 69 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 67 def set_auto_index(env) env.[:index] = { name: auto_index_name(env) } end |
#set_foreign_key(env) ⇒ Object
63 64 65 |
# File 'lib/schema_auto_foreign_keys/middleware/migration.rb', line 63 def set_foreign_key(env) env.[:foreign_key] = true end |