schema_plus 2.0.0.pre2 → 2.0.0.pre3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.yml +3 -3
- data/Gemfile +2 -0
- data/README.md +4 -4
- data/TODO.md +3 -10
- data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.base +1 -1
- data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.mysql2 +0 -0
- data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.postgresql +0 -0
- data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.sqlite3 +0 -0
- data/lib/schema_plus.rb +13 -121
- data/lib/schema_plus/version.rb +1 -1
- data/lib/schema_plus_columns.rb +7 -0
- data/lib/{schema_column_plus → schema_plus_columns}/active_record/connection_adapters/column.rb +1 -1
- data/lib/schema_plus_columns/middleware/model.rb +16 -0
- data/lib/schema_plus_db_default.rb +13 -0
- data/lib/{schema_db_default → schema_plus_db_default}/active_record/attribute.rb +4 -4
- data/lib/{schema_db_default → schema_plus_db_default}/db_default.rb +1 -1
- data/lib/schema_plus_db_default/middleware.rb +30 -0
- data/lib/{schema_default_expr.rb → schema_plus_default_expr.rb} +6 -6
- data/lib/{schema_default_expr → schema_plus_default_expr}/active_record/connection_adapters/mysql2_adapter.rb +1 -1
- data/lib/{schema_default_expr → schema_plus_default_expr}/active_record/connection_adapters/postgresql_adapter.rb +1 -1
- data/lib/{schema_default_expr → schema_plus_default_expr}/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
- data/lib/{schema_default_expr → schema_plus_default_expr}/middleware.rb +21 -19
- data/lib/schema_plus_enums.rb +6 -0
- data/lib/{schema_pg_enums → schema_plus_enums}/active_record.rb +1 -1
- data/lib/schema_plus_enums/middleware.rb +22 -0
- data/lib/schema_plus_foreign_keys.rb +96 -0
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/base.rb +2 -2
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/abstract_adapter.rb +7 -7
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/foreign_key_definition.rb +4 -4
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/mysql2_adapter.rb +9 -9
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/postgresql_adapter.rb +5 -5
- data/lib/schema_plus_foreign_keys/active_record/connection_adapters/schema_statements.rb +33 -0
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/sqlite3_adapter.rb +5 -5
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/table_definition.rb +8 -8
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/migration/command_recorder.rb +5 -5
- data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/schema.rb +7 -7
- data/lib/{schema_plus → schema_plus_foreign_keys}/middleware/dumper.rb +11 -26
- data/lib/{schema_plus → schema_plus_foreign_keys}/middleware/migration.rb +14 -15
- data/lib/schema_plus_foreign_keys/middleware/model.rb +15 -0
- data/lib/schema_plus_foreign_keys/version.rb +3 -0
- data/lib/schema_plus_views.rb +16 -0
- data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/abstract_adapter.rb +1 -1
- data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/mysql2_adapter.rb +1 -1
- data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/postgresql_adapter.rb +1 -1
- data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
- data/lib/schema_plus_views/middleware.rb +49 -0
- data/schema_dev.yml +1 -1
- data/schema_plus.gemspec +3 -2
- data/spec/{schema_column_plus → schema_plus_columns}/column_spec.rb +0 -0
- data/spec/{schema_db_default → schema_plus_db_default}/column_spec.rb +1 -1
- data/spec/{schema_default_expr → schema_plus_default_expr}/column_default_spec.rb +0 -0
- data/spec/schema_plus_default_expr/migration_spec.rb +44 -0
- data/spec/{schema_default_expr → schema_plus_default_expr}/schema_dumper_spec.rb +2 -2
- data/spec/{schema_pg_enums → schema_plus_enums}/enum_spec.rb +0 -0
- data/spec/{schema_pg_enums → schema_plus_enums}/schema_dumper_spec.rb +0 -0
- data/spec/{foreign_key_definition_spec.rb → schema_plus_foreign_keys/foreign_key_definition_spec.rb} +0 -0
- data/spec/{foreign_key_spec.rb → schema_plus_foreign_keys/foreign_key_spec.rb} +2 -2
- data/spec/{migration_spec.rb → schema_plus_foreign_keys/migration_spec.rb} +13 -29
- data/spec/{named_schemas_spec.rb → schema_plus_foreign_keys/named_schemas_spec.rb} +0 -0
- data/spec/{schema_dumper_spec.rb → schema_plus_foreign_keys/schema_dumper_spec.rb} +2 -2
- data/spec/{schema_spec.rb → schema_plus_foreign_keys/schema_spec.rb} +7 -7
- data/spec/{schema_views → schema_plus_views}/named_schemas_spec.rb +7 -7
- data/spec/{schema_views → schema_plus_views}/views_spec.rb +0 -0
- data/spec/spec_helper.rb +3 -3
- metadata +102 -81
- data/init.rb +0 -1
- data/lib/schema_column_plus.rb +0 -7
- data/lib/schema_column_plus/middleware/model.rb +0 -22
- data/lib/schema_db_default.rb +0 -13
- data/lib/schema_db_default/middleware.rb +0 -30
- data/lib/schema_pg_enums.rb +0 -6
- data/lib/schema_pg_enums/middleware.rb +0 -23
- data/lib/schema_plus/active_record/connection_adapters/schema_statements.rb +0 -34
- data/lib/schema_plus/middleware/model.rb +0 -17
- data/lib/schema_views.rb +0 -16
- data/lib/schema_views/middleware.rb +0 -47
- data/spec/connection_spec.rb +0 -10
@@ -0,0 +1,22 @@
|
|
1
|
+
module SchemaPlusEnums
|
2
|
+
module Middleware
|
3
|
+
|
4
|
+
module Dumper
|
5
|
+
module Extensions
|
6
|
+
|
7
|
+
module Postgresql
|
8
|
+
|
9
|
+
def after(env)
|
10
|
+
env.connection.enums.each do |schema, name, values|
|
11
|
+
params = [name.inspect]
|
12
|
+
params << values.map(&:inspect).join(', ')
|
13
|
+
params << ":schema => #{schema.inspect}" if schema != 'public'
|
14
|
+
|
15
|
+
env.extensions << "create_enum #{params.join(', ')}"
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'valuable'
|
3
|
+
|
4
|
+
require 'schema_plus_foreign_keys/version'
|
5
|
+
require 'schema_plus_foreign_keys/active_record/base'
|
6
|
+
require 'schema_plus_foreign_keys/active_record/connection_adapters/abstract_adapter'
|
7
|
+
require 'schema_plus_foreign_keys/active_record/connection_adapters/table_definition'
|
8
|
+
require 'schema_plus_foreign_keys/active_record/connection_adapters/schema_statements'
|
9
|
+
require 'schema_plus_foreign_keys/active_record/schema'
|
10
|
+
require 'schema_plus_foreign_keys/active_record/connection_adapters/foreign_key_definition'
|
11
|
+
require 'schema_plus_foreign_keys/active_record/migration/command_recorder'
|
12
|
+
require 'schema_plus_foreign_keys/middleware/dumper'
|
13
|
+
require 'schema_plus_foreign_keys/middleware/migration'
|
14
|
+
require 'schema_plus_foreign_keys/middleware/model'
|
15
|
+
|
16
|
+
module SchemaPlusForeignKeys
|
17
|
+
module ActiveRecord
|
18
|
+
module ConnectionAdapters
|
19
|
+
autoload :Mysql2Adapter, 'schema_plus_foreign_keys/active_record/connection_adapters/mysql2_adapter'
|
20
|
+
autoload :PostgresqlAdapter, 'schema_plus_foreign_keys/active_record/connection_adapters/postgresql_adapter'
|
21
|
+
autoload :Sqlite3Adapter, 'schema_plus_foreign_keys/active_record/connection_adapters/sqlite3_adapter'
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# This global configuation options for SchemaPlusForeignKeys.
|
26
|
+
# Set them in +config/initializers/schema_plus_foreign_keys.rb+ using:
|
27
|
+
#
|
28
|
+
# SchemaPlusForeignKeys.setup do |config|
|
29
|
+
# ...
|
30
|
+
# end
|
31
|
+
#
|
32
|
+
class Config < Valuable
|
33
|
+
|
34
|
+
##
|
35
|
+
# :attr_accessor: auto_create
|
36
|
+
#
|
37
|
+
# Whether to automatically create foreign key constraints for columns
|
38
|
+
# suffixed with +_id+. Boolean, default is +true+.
|
39
|
+
has_value :auto_create, :klass => :boolean, :default => true
|
40
|
+
|
41
|
+
##
|
42
|
+
# :attr_accessor: auto_index
|
43
|
+
#
|
44
|
+
# Whether to automatically create indexes when creating foreign key constraints for columns.
|
45
|
+
# Boolean, default is +true+.
|
46
|
+
has_value :auto_index, :klass => :boolean, :default => true
|
47
|
+
|
48
|
+
##
|
49
|
+
# :attr_accessor: on_update
|
50
|
+
#
|
51
|
+
# The default value for +:on_update+ when creating foreign key
|
52
|
+
# constraints for columns. Valid values are as described in
|
53
|
+
# ForeignKeyDefinition, or +nil+ to let the database connection use
|
54
|
+
# its own default. Default is +nil+.
|
55
|
+
has_value :on_update
|
56
|
+
|
57
|
+
##
|
58
|
+
# :attr_accessor: on_delete
|
59
|
+
#
|
60
|
+
# The default value for +:on_delete+ when creating foreign key
|
61
|
+
# constraints for columns. Valid values are as described in
|
62
|
+
# ForeignKeyDefinition, or +nil+ to let the database connection use
|
63
|
+
# its own default. Default is +nil+.
|
64
|
+
has_value :on_delete
|
65
|
+
|
66
|
+
def merge(opts)
|
67
|
+
dup.update_attributes(opts)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
|
72
|
+
# Returns the global configuration, i.e., the singleton instance of Config
|
73
|
+
def self.config
|
74
|
+
@config ||= Config.new
|
75
|
+
end
|
76
|
+
|
77
|
+
# Initialization block is passed a global Config instance that can be
|
78
|
+
# used to configure SchemaPlusForeignKeys behavior. E.g., if you want to disable
|
79
|
+
# automation creation of foreign key constraints for columns name *_id,
|
80
|
+
# put the following in config/initializers/schema_plus_foreign_keys.rb :
|
81
|
+
#
|
82
|
+
# SchemaPlusForeignKeys.setup do |config|
|
83
|
+
# config.auto_create = false
|
84
|
+
# end
|
85
|
+
#
|
86
|
+
def self.setup # :yields: config
|
87
|
+
yield config
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.insert #:nodoc:
|
91
|
+
SchemaMonkey.include_once ::ActiveRecord::ConnectionAdapters::AbstractAdapter::SchemaCreation, SchemaPlusForeignKeys::ActiveRecord::ConnectionAdapters::AbstractAdapter::VisitTableDefinition
|
92
|
+
end
|
93
|
+
|
94
|
+
end
|
95
|
+
|
96
|
+
SchemaMonkey.register(SchemaPlusForeignKeys)
|
@@ -1,10 +1,10 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys
|
2
2
|
module ActiveRecord
|
3
|
-
#
|
3
|
+
# SchemaPlusForeignKeys adds several methods to the connection adapter (as returned by ActiveRecordBase#connection). See AbstractAdapter for details.
|
4
4
|
module ConnectionAdapters
|
5
5
|
|
6
6
|
#
|
7
|
-
#
|
7
|
+
# SchemaPlusForeignKeys adds several methods to
|
8
8
|
# ActiveRecord::ConnectionAdapters::AbstractAdapter. In most cases
|
9
9
|
# you don't call these directly, but rather the methods that define
|
10
10
|
# things are called by schema statements, and methods that query
|
@@ -113,7 +113,7 @@ module SchemaPlus
|
|
113
113
|
if fk = fks.detect { |fk| fk.match(test_fk) }
|
114
114
|
fk.name
|
115
115
|
else
|
116
|
-
raise "
|
116
|
+
raise "SchemaPlusForeignKeys: no foreign key constraint found on #{from_table.inspect} matching #{[to_table, options].inspect}" unless options[:if_exists]
|
117
117
|
nil
|
118
118
|
end
|
119
119
|
end
|
@@ -149,11 +149,11 @@ module SchemaPlus
|
|
149
149
|
|
150
150
|
module VisitTableDefinition
|
151
151
|
def self.included(base) #:nodoc:
|
152
|
-
base.alias_method_chain :visit_TableDefinition, :
|
152
|
+
base.alias_method_chain :visit_TableDefinition, :schema_plus_foreign_keys
|
153
153
|
end
|
154
154
|
|
155
|
-
def
|
156
|
-
create_sql =
|
155
|
+
def visit_TableDefinition_with_schema_plus_foreign_keys(o) #:nodoc:
|
156
|
+
create_sql = visit_TableDefinition_without_schema_plus_foreign_keys(o)
|
157
157
|
last_chunk = ") #{o.options}"
|
158
158
|
|
159
159
|
unless create_sql.end_with?(last_chunk)
|
@@ -1,6 +1,6 @@
|
|
1
1
|
require 'active_record/connection_adapters/abstract/schema_definitions'
|
2
2
|
|
3
|
-
module
|
3
|
+
module SchemaPlusForeignKeys
|
4
4
|
module ActiveRecord
|
5
5
|
module ConnectionAdapters
|
6
6
|
# Instances of this class are returned by the queries ActiveRecord::Base#foreign_keys and ActiveRecord::Base#reverse_foreign_keys (via AbstractAdapter#foreign_keys and AbstractAdapter#reverse_foreign_keys)
|
@@ -19,7 +19,7 @@ module SchemaPlus
|
|
19
19
|
|
20
20
|
def self.included(base)
|
21
21
|
base.class_eval do
|
22
|
-
alias_method_chain :initialize, :
|
22
|
+
alias_method_chain :initialize, :schema_plus_foreign_keys
|
23
23
|
end
|
24
24
|
end
|
25
25
|
|
@@ -46,7 +46,7 @@ module SchemaPlus
|
|
46
46
|
ACTIONS = { :cascade => "CASCADE", :restrict => "RESTRICT", :nullify => "SET NULL", :set_default => "SET DEFAULT", :no_action => "NO ACTION" }.freeze
|
47
47
|
ACTION_LOOKUP = ACTIONS.invert.freeze
|
48
48
|
|
49
|
-
def
|
49
|
+
def initialize_with_schema_plus_foreign_keys(from_table, to_table, options={})
|
50
50
|
[:on_update, :on_delete].each do |key|
|
51
51
|
if options[key] == :set_null
|
52
52
|
require 'byebug' ; byebug
|
@@ -55,7 +55,7 @@ module SchemaPlus
|
|
55
55
|
end
|
56
56
|
end
|
57
57
|
|
58
|
-
|
58
|
+
initialize_without_schema_plus_foreign_keys(from_table, to_table, options)
|
59
59
|
if column.is_a?(Array) and column.length == 1
|
60
60
|
options[:column] = column[0]
|
61
61
|
end
|
@@ -1,7 +1,7 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
|
-
#
|
4
|
+
# SchemaPlusForeignKeys includes a MySQL implementation of the AbstractAdapter
|
5
5
|
# extensions.
|
6
6
|
module Mysql2Adapter
|
7
7
|
|
@@ -9,21 +9,21 @@ module SchemaPlus
|
|
9
9
|
|
10
10
|
def self.included(base)
|
11
11
|
base.class_eval do
|
12
|
-
alias_method_chain :remove_column, :
|
13
|
-
alias_method_chain :rename_table, :
|
12
|
+
alias_method_chain :remove_column, :schema_plus_foreign_keys
|
13
|
+
alias_method_chain :rename_table, :schema_plus_foreign_keys
|
14
14
|
end
|
15
|
-
::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::SchemaCreation.send(:include,
|
15
|
+
::ActiveRecord::ConnectionAdapters::AbstractMysqlAdapter::SchemaCreation.send(:include, SchemaPlusForeignKeys::ActiveRecord::ConnectionAdapters::AbstractAdapter::VisitTableDefinition)
|
16
16
|
end
|
17
17
|
|
18
|
-
def
|
18
|
+
def remove_column_with_schema_plus_foreign_keys(table_name, column_name, type=nil, options={})
|
19
19
|
foreign_keys(table_name).select { |foreign_key| Array.wrap(foreign_key.column).include?(column_name.to_s) }.each do |foreign_key|
|
20
20
|
remove_foreign_key(table_name, name: foreign_key.name)
|
21
21
|
end
|
22
|
-
|
22
|
+
remove_column_without_schema_plus_foreign_keys(table_name, column_name, type, options)
|
23
23
|
end
|
24
24
|
|
25
|
-
def
|
26
|
-
|
25
|
+
def rename_table_with_schema_plus_foreign_keys(oldname, newname)
|
26
|
+
rename_table_without_schema_plus_foreign_keys(oldname, newname)
|
27
27
|
rename_foreign_keys(oldname, newname)
|
28
28
|
end
|
29
29
|
|
@@ -1,19 +1,19 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
|
-
# The Postgresql adapter implements the
|
5
|
+
# The Postgresql adapter implements the SchemaPlusForeignKeys extensions and
|
6
6
|
# enhancements
|
7
7
|
module PostgresqlAdapter
|
8
8
|
|
9
9
|
def self.included(base) #:nodoc:
|
10
10
|
base.class_eval do
|
11
|
-
alias_method_chain :rename_table, :
|
11
|
+
alias_method_chain :rename_table, :schema_plus_foreign_keys
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
16
|
-
|
15
|
+
def rename_table_with_schema_plus_foreign_keys(oldname, newname) #:nodoc:
|
16
|
+
rename_table_without_schema_plus_foreign_keys(oldname, newname)
|
17
17
|
rename_foreign_keys(oldname, newname)
|
18
18
|
end
|
19
19
|
|
@@ -0,0 +1,33 @@
|
|
1
|
+
module SchemaPlusForeignKeys::ActiveRecord::ConnectionAdapters
|
2
|
+
module SchemaStatements
|
3
|
+
|
4
|
+
def self.included(base) #:nodoc:
|
5
|
+
base.class_eval do
|
6
|
+
alias_method_chain :create_table, :schema_plus_foreign_keys
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
##
|
11
|
+
# :method: create_table
|
12
|
+
#
|
13
|
+
# SchemaPlusForeignKeys extends SchemaStatements::create_table to allow you to specify configuration options per table. Pass them in as a hash keyed by configuration set (see SchemaPlusForeignKeys::Config),
|
14
|
+
# for example:
|
15
|
+
#
|
16
|
+
# create_table :widgets, :foreign_keys => {:auto_create => true, :on_delete => :cascade} do |t|
|
17
|
+
# ...
|
18
|
+
# end
|
19
|
+
def create_table_with_schema_plus_foreign_keys(table, options = {})
|
20
|
+
options = options.dup
|
21
|
+
config_options = options.delete(:foreign_keys) || {}
|
22
|
+
|
23
|
+
# override rails' :force to cascade
|
24
|
+
drop_table(table, if_exists: true, cascade: true) if options.delete(:force)
|
25
|
+
|
26
|
+
create_table_without_schema_plus_foreign_keys(table, options) do |table_definition|
|
27
|
+
table_definition.schema_plus_config = SchemaPlusForeignKeys.config.merge(config_options)
|
28
|
+
yield table_definition if block_given?
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -1,8 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
|
-
#
|
5
|
+
# SchemaPlusForeignKeys includes an Sqlite3 implementation of the AbstractAdapter
|
6
6
|
# extensions.
|
7
7
|
module Sqlite3Adapter
|
8
8
|
|
@@ -10,7 +10,7 @@ module SchemaPlus
|
|
10
10
|
|
11
11
|
def self.included(base)
|
12
12
|
base.class_eval do
|
13
|
-
alias_method_chain :rename_table, :
|
13
|
+
alias_method_chain :rename_table, :schema_plus_foreign_keys
|
14
14
|
end
|
15
15
|
end
|
16
16
|
|
@@ -19,8 +19,8 @@ module SchemaPlus
|
|
19
19
|
execute('PRAGMA FOREIGN_KEYS = ON')
|
20
20
|
end
|
21
21
|
|
22
|
-
def
|
23
|
-
|
22
|
+
def rename_table_with_schema_plus_foreign_keys(oldname, newname) #:nodoc:
|
23
|
+
rename_table_without_schema_plus_foreign_keys(oldname, newname)
|
24
24
|
rename_foreign_keys(oldname, newname)
|
25
25
|
end
|
26
26
|
|
@@ -1,19 +1,19 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys::ActiveRecord::ConnectionAdapters
|
2
2
|
|
3
3
|
#
|
4
|
-
#
|
4
|
+
# SchemaPlusForeignKeys adds several methods to TableDefinition, allowing indexes
|
5
5
|
# and foreign key constraints to be defined within a
|
6
6
|
# <tt>create_table</tt> block of a migration, allowing for better
|
7
7
|
# encapsulation and more DRY definitions.
|
8
8
|
#
|
9
|
-
# For example, without
|
9
|
+
# For example, without SchemaPlusForeignKeys you might define a table like this:
|
10
10
|
#
|
11
11
|
# create_table :widgets do |t|
|
12
12
|
# t.string :name
|
13
13
|
# end
|
14
14
|
# add_index :widgets, :name
|
15
15
|
#
|
16
|
-
# But with
|
16
|
+
# But with SchemaPlusForeignKeys, the index can be defined within the create_table
|
17
17
|
# block, so you don't need to repeat the table name:
|
18
18
|
#
|
19
19
|
# create_table :widgets do |t|
|
@@ -31,7 +31,7 @@ module SchemaPlus::ActiveRecord::ConnectionAdapters
|
|
31
31
|
# For details about the :index option (including unique and multi-column indexes), see the
|
32
32
|
# documentation for Migration::ClassMethods#add_column
|
33
33
|
#
|
34
|
-
#
|
34
|
+
# SchemaPlusForeignKeys also supports creation of foreign key constraints analogously, using Migration::ClassMethods#add_foreign_key or TableDefinition#foreign_key or as part of the column definition, for example:
|
35
35
|
#
|
36
36
|
# create_table :posts do |t| # not DRY
|
37
37
|
# t.integer :author_id
|
@@ -47,13 +47,13 @@ module SchemaPlus::ActiveRecord::ConnectionAdapters
|
|
47
47
|
# t.integer :author_id, :foreign_key => true
|
48
48
|
# end
|
49
49
|
#
|
50
|
-
# <b>NOTE:</b> In the standard configuration,
|
50
|
+
# <b>NOTE:</b> In the standard configuration, SchemaPlusForeignKeys automatically
|
51
51
|
# creates foreign key constraints for columns whose names end in
|
52
52
|
# <tt>_id</tt>. So the above examples are redundant, unless automatic
|
53
53
|
# creation was disabled at initialization in the global Config.
|
54
54
|
#
|
55
|
-
#
|
56
|
-
# columns defined via <tt>t.references</tt>. However,
|
55
|
+
# SchemaPlusForeignKeys likewise by default automatically creates foreign key constraints for
|
56
|
+
# columns defined via <tt>t.references</tt>. However, SchemaPlusForeignKeys does not create
|
57
57
|
# foreign key constraints if the <tt>:polymorphic</tt> option is true
|
58
58
|
#
|
59
59
|
# Finally, the configuration for foreign keys can be overriden on a per-table
|
data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/migration/command_recorder.rb
RENAMED
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module Migration
|
4
4
|
module CommandRecorder
|
@@ -7,8 +7,8 @@ module SchemaPlus
|
|
7
7
|
|
8
8
|
def self.included(base) #:nodoc:
|
9
9
|
base.class_eval do
|
10
|
-
alias_method_chain :add_reference, :
|
11
|
-
alias_method_chain :invert_add_index, :
|
10
|
+
alias_method_chain :add_reference, :schema_plus_foreign_keys
|
11
|
+
alias_method_chain :invert_add_index, :schema_plus_foreign_keys
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
@@ -18,7 +18,7 @@ module SchemaPlus
|
|
18
18
|
#
|
19
19
|
# should track it down separately and submit a patch/fix to rails
|
20
20
|
#
|
21
|
-
def
|
21
|
+
def add_reference_with_schema_plus_foreign_keys(table_name, ref_name, options = {}) #:nodoc:
|
22
22
|
polymorphic = options.delete(:polymorphic)
|
23
23
|
options[:references] = nil if polymorphic
|
24
24
|
# ugh. copying and pasting code from ::ActiveRecord::ConnectionAdapters::SchemaStatements#add_reference
|
@@ -30,7 +30,7 @@ module SchemaPlus
|
|
30
30
|
self
|
31
31
|
end
|
32
32
|
|
33
|
-
def
|
33
|
+
def invert_add_index_with_schema_plus_foreign_keys(args)
|
34
34
|
table, columns, options = *args
|
35
35
|
[:remove_index, [table, (options||{}).merge(column: columns, if_exists: true)]]
|
36
36
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlusForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module Schema #:nodoc: all
|
4
4
|
def self.included(base)
|
@@ -8,18 +8,18 @@ module SchemaPlus
|
|
8
8
|
module ClassMethods
|
9
9
|
def self.extended(base)
|
10
10
|
class << base
|
11
|
-
alias_method_chain :define, :
|
11
|
+
alias_method_chain :define, :schema_plus_foreign_keys
|
12
12
|
end
|
13
13
|
end
|
14
14
|
|
15
|
-
def
|
15
|
+
def define_with_schema_plus_foreign_keys(info={}, &block)
|
16
16
|
fk_override = { :auto_create => false, :auto_index => false }
|
17
|
-
save = Hash[fk_override.keys.collect{|key| [key,
|
17
|
+
save = Hash[fk_override.keys.collect{|key| [key, SchemaPlusForeignKeys.config.send(key)]}]
|
18
18
|
begin
|
19
|
-
|
20
|
-
|
19
|
+
SchemaPlusForeignKeys.config.update_attributes(fk_override)
|
20
|
+
define_without_schema_plus_foreign_keys(info, &block)
|
21
21
|
ensure
|
22
|
-
|
22
|
+
SchemaPlusForeignKeys.config.update_attributes(save)
|
23
23
|
end
|
24
24
|
end
|
25
25
|
end
|