schema_plus_foreign_keys 0.1.6 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.github/workflows/prs.yml +134 -0
- data/.gitignore +1 -0
- data/.simplecov +20 -0
- data/Gemfile +4 -1
- data/README.md +49 -35
- data/Rakefile +3 -1
- data/gemfiles/Gemfile.base +1 -1
- data/gemfiles/activerecord-5.2/Gemfile.base +4 -0
- data/gemfiles/activerecord-5.2/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-5.2/Gemfile.postgresql +10 -0
- data/gemfiles/{activerecord-4.2.0 → activerecord-5.2}/Gemfile.sqlite3 +3 -3
- data/gemfiles/activerecord-6.0/Gemfile.base +4 -0
- data/gemfiles/activerecord-6.0/Gemfile.mysql2 +10 -0
- data/gemfiles/activerecord-6.0/Gemfile.postgresql +10 -0
- data/gemfiles/{activerecord-4.2.6 → activerecord-6.0}/Gemfile.sqlite3 +3 -3
- data/lib/schema_plus/foreign_keys/active_record/base.rb +2 -0
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract/schema_creation.rb +20 -0
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb +9 -47
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/foreign_key_definition.rb +4 -35
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb +15 -16
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/postgresql_adapter.rb +8 -6
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +14 -6
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb +10 -49
- data/lib/schema_plus/foreign_keys/active_record/migration/command_recorder.rb +4 -2
- data/lib/schema_plus/foreign_keys/middleware/dumper.rb +5 -3
- data/lib/schema_plus/foreign_keys/middleware/migration.rb +13 -5
- data/lib/schema_plus/foreign_keys/middleware/model.rb +2 -0
- data/lib/schema_plus/foreign_keys/middleware/mysql.rb +3 -1
- data/lib/schema_plus/foreign_keys/middleware/sql.rb +2 -15
- data/lib/schema_plus/foreign_keys/version.rb +3 -1
- data/lib/schema_plus/foreign_keys.rb +3 -1
- data/lib/schema_plus_foreign_keys.rb +2 -0
- data/schema_dev.yml +5 -4
- data/schema_plus_foreign_keys.gemspec +10 -8
- data/spec/deprecation_spec.rb +15 -112
- data/spec/foreign_key_definition_spec.rb +5 -3
- data/spec/foreign_key_spec.rb +26 -24
- data/spec/migration_spec.rb +73 -91
- data/spec/named_schemas_spec.rb +16 -14
- data/spec/schema_dumper_spec.rb +36 -35
- data/spec/spec_helper.rb +6 -4
- data/spec/support/reference.rb +3 -2
- metadata +48 -73
- data/.travis.yml +0 -24
- data/gemfiles/activerecord-4.2.0/Gemfile.base +0 -3
- data/gemfiles/activerecord-4.2.0/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-4.2.0/Gemfile.postgresql +0 -10
- data/gemfiles/activerecord-4.2.1/Gemfile.base +0 -3
- data/gemfiles/activerecord-4.2.1/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-4.2.1/Gemfile.postgresql +0 -10
- data/gemfiles/activerecord-4.2.1/Gemfile.sqlite3 +0 -10
- data/gemfiles/activerecord-4.2.6/Gemfile.base +0 -3
- data/gemfiles/activerecord-4.2.6/Gemfile.mysql2 +0 -10
- data/gemfiles/activerecord-4.2.6/Gemfile.postgresql +0 -10
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module ActiveRecord
|
3
5
|
module ConnectionAdapters
|
@@ -7,27 +9,24 @@ module SchemaPlus::ForeignKeys
|
|
7
9
|
|
8
10
|
#:enddoc:
|
9
11
|
|
10
|
-
def remove_column(table_name, column_name, type=nil, options
|
12
|
+
def remove_column(table_name, column_name, type=nil, **options)
|
11
13
|
foreign_keys(table_name).select { |foreign_key| Array.wrap(foreign_key.column).include?(column_name.to_s) }.each do |foreign_key|
|
12
14
|
remove_foreign_key(table_name, name: foreign_key.name)
|
13
15
|
end
|
14
|
-
super table_name, column_name, type, options
|
16
|
+
super table_name, column_name, type, **options
|
15
17
|
end
|
16
18
|
|
17
|
-
def remove_foreign_key(
|
18
|
-
from_table, to_table, options = normalize_remove_foreign_key_args(*args)
|
19
|
+
def remove_foreign_key(from_table, to_table = nil, **options)
|
19
20
|
if options[:if_exists]
|
20
21
|
foreign_key_name = get_foreign_key_name(from_table, to_table, options)
|
21
22
|
return if !foreign_key_name or not foreign_keys(from_table).detect{|fk| fk.name == foreign_key_name}
|
22
23
|
end
|
23
24
|
options.delete(:if_exists)
|
24
|
-
super from_table, to_table, options
|
25
|
+
super from_table, to_table, **options
|
25
26
|
end
|
26
27
|
|
27
28
|
def remove_foreign_key_sql(*args)
|
28
|
-
super
|
29
|
-
ret.sub!(/DROP CONSTRAINT/, 'DROP FOREIGN KEY') if ret
|
30
|
-
}
|
29
|
+
super&.sub(/DROP CONSTRAINT/, 'DROP FOREIGN KEY')
|
31
30
|
end
|
32
31
|
|
33
32
|
def foreign_keys(table_name, name = nil)
|
@@ -52,11 +51,11 @@ module SchemaPlus::ForeignKeys
|
|
52
51
|
on_update = ForeignKeyDefinition::ACTION_LOOKUP[on_update] || :restrict
|
53
52
|
on_delete = ForeignKeyDefinition::ACTION_LOOKUP[on_delete] || :restrict
|
54
53
|
|
55
|
-
options = { :
|
56
|
-
:
|
57
|
-
:
|
58
|
-
:
|
59
|
-
:
|
54
|
+
options = { name: name,
|
55
|
+
on_delete: on_delete,
|
56
|
+
on_update: on_update,
|
57
|
+
column: columns.gsub('`', '').split(', '),
|
58
|
+
primary_key: primary_keys.gsub('`', '').split(', ')
|
60
59
|
}
|
61
60
|
|
62
61
|
foreign_keys << ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
|
@@ -92,9 +91,9 @@ module SchemaPlus::ForeignKeys
|
|
92
91
|
to_table = table_namespace_prefix(to_table) + to_table
|
93
92
|
|
94
93
|
options = {
|
95
|
-
:
|
96
|
-
:
|
97
|
-
:
|
94
|
+
name: constraint_name,
|
95
|
+
column: columns.map { |row| row['column_name'] },
|
96
|
+
primary_key: columns.map { |row| row['referenced_column_name'] }
|
98
97
|
}
|
99
98
|
|
100
99
|
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, to_table, options)
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module ActiveRecord
|
3
5
|
module ConnectionAdapters
|
@@ -61,12 +63,12 @@ module SchemaPlus::ForeignKeys
|
|
61
63
|
on_update = ForeignKeyDefinition::ACTION_LOOKUP[on_update] || :no_action
|
62
64
|
on_delete = ForeignKeyDefinition::ACTION_LOOKUP[on_delete] || :no_action
|
63
65
|
|
64
|
-
options = { :
|
65
|
-
:
|
66
|
-
:
|
67
|
-
:
|
68
|
-
:
|
69
|
-
:
|
66
|
+
options = { name: name,
|
67
|
+
on_delete: on_delete,
|
68
|
+
on_update: on_update,
|
69
|
+
column: columns,
|
70
|
+
primary_key: primary_keys,
|
71
|
+
deferrable: deferrable }
|
70
72
|
|
71
73
|
foreign_keys << ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
|
72
74
|
from_table,
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module ActiveRecord
|
3
5
|
module ConnectionAdapters
|
@@ -8,6 +10,12 @@ module SchemaPlus::ForeignKeys
|
|
8
10
|
|
9
11
|
# :enddoc:
|
10
12
|
|
13
|
+
# Even though SQLite3 has limited support for foreign keys, ActiveRecord does not
|
14
|
+
# enable it by default.
|
15
|
+
def supports_foreign_keys?
|
16
|
+
true
|
17
|
+
end
|
18
|
+
|
11
19
|
def add_foreign_key(table_name, to_table, options = {})
|
12
20
|
raise NotImplementedError, "Sqlite3 does not support altering a table to add foreign key constraints (table #{table_name.inspect} to #{to_table.inspect})"
|
13
21
|
end
|
@@ -53,12 +61,12 @@ module SchemaPlus::ForeignKeys
|
|
53
61
|
on_delete = ForeignKeyDefinition::ACTION_LOOKUP[on_delete] || :no_action
|
54
62
|
deferrable = deferrable ? (initially_deferred ? :initially_deferred : true) : false
|
55
63
|
|
56
|
-
options = { :
|
57
|
-
:
|
58
|
-
:
|
59
|
-
:
|
60
|
-
:
|
61
|
-
:
|
64
|
+
options = { name: name,
|
65
|
+
on_update: on_update,
|
66
|
+
on_delete: on_delete,
|
67
|
+
column: columns,
|
68
|
+
primary_key: primary_keys,
|
69
|
+
deferrable: deferrable }
|
62
70
|
|
63
71
|
foreign_keys << ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(
|
64
72
|
from_table,
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
2
4
|
|
3
5
|
#
|
@@ -25,7 +27,7 @@ module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
|
25
27
|
# definition, via:
|
26
28
|
#
|
27
29
|
# create_table :widgets do |t|
|
28
|
-
# t.string :name, :
|
30
|
+
# t.string :name, index: true
|
29
31
|
# end
|
30
32
|
#
|
31
33
|
# For details about the :index option (including unique and multi-column indexes), see the
|
@@ -34,17 +36,17 @@ module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
|
34
36
|
# SchemaPlus::ForeignKeys 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
37
|
#
|
36
38
|
# create_table :posts do |t| # not DRY
|
37
|
-
# t.
|
39
|
+
# t.references :author
|
38
40
|
# end
|
39
|
-
# add_foreign_key :posts, :author_id, :
|
41
|
+
# add_foreign_key :posts, :author_id, references: :authors
|
40
42
|
#
|
41
43
|
# create_table :posts do |t| # DRYer
|
42
|
-
# t.
|
43
|
-
# t.foreign_key :author_id, :
|
44
|
+
# t.references :author
|
45
|
+
# t.foreign_key :author_id, references: :authors
|
44
46
|
# end
|
45
47
|
#
|
46
48
|
# create_table :posts do |t| # Dryest
|
47
|
-
# t.
|
49
|
+
# t.references :author, foreign_key: true
|
48
50
|
# end
|
49
51
|
#
|
50
52
|
# <b>NOTE:</b> In the standard configuration, SchemaPlus::ForeignKeys automatically
|
@@ -59,54 +61,13 @@ module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
|
59
61
|
# Finally, the configuration for foreign keys can be overriden on a per-table
|
60
62
|
# basis by passing Config options to Migration::ClassMethods#create_table, such as
|
61
63
|
#
|
62
|
-
# create_table :students, :
|
63
|
-
# t.
|
64
|
+
# create_table :students, foreign_keys: {auto_create: false} do
|
65
|
+
# t.references :student
|
64
66
|
# end
|
65
67
|
#
|
66
68
|
module TableDefinition
|
67
69
|
|
68
70
|
attr_accessor :schema_plus_foreign_keys_config #:nodoc:
|
69
71
|
|
70
|
-
if Gem::Requirement.new('= 4.2.0').satisfied_by?(::ActiveRecord.version)
|
71
|
-
def foreign_keys
|
72
|
-
@foreign_keys ||= []
|
73
|
-
end
|
74
|
-
|
75
|
-
def foreign_keys_for_table(*)
|
76
|
-
foreign_keys
|
77
|
-
end
|
78
|
-
elsif Gem::Requirement.new('< 4.2.6').satisfied_by?(::ActiveRecord.version)
|
79
|
-
def foreign_keys_for_table(table)
|
80
|
-
foreign_keys[table] ||= []
|
81
|
-
end
|
82
|
-
else
|
83
|
-
def foreign_keys_for_table(*)
|
84
|
-
foreign_keys
|
85
|
-
end
|
86
|
-
end
|
87
|
-
|
88
|
-
def foreign_key(*args) # (column_names, to_table, primary_key=nil, options=nil)
|
89
|
-
options = args.extract_options!
|
90
|
-
case args.length
|
91
|
-
when 1
|
92
|
-
to_table = args[0]
|
93
|
-
column_names = "#{to_table.to_s.singularize}_id"
|
94
|
-
when 2
|
95
|
-
column_names, to_table = args
|
96
|
-
when 3
|
97
|
-
ActiveSupport::Deprecation.warn "positional arg for foreign primary key is deprecated, use :primary_key option instead"
|
98
|
-
column_names, to_table, primary_key = args
|
99
|
-
options.merge!(:primary_key => primary_key)
|
100
|
-
else
|
101
|
-
raise ArgumentError, "wrong number of arguments (#{args.length}) for foreign_key(column_names, table_name, options)"
|
102
|
-
end
|
103
|
-
|
104
|
-
options.merge!(:column => column_names)
|
105
|
-
options.reverse_merge!(:name => ForeignKeyDefinition.default_name(self.name, column_names))
|
106
|
-
fk = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(self.name, AbstractAdapter.proper_table_name(to_table), options)
|
107
|
-
foreign_keys_for_table(fk.to_table) << fk
|
108
|
-
self
|
109
|
-
end
|
110
|
-
|
111
72
|
end
|
112
73
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module ActiveRecord
|
3
5
|
module Migration
|
@@ -6,7 +8,7 @@ module SchemaPlus::ForeignKeys
|
|
6
8
|
attr_accessor :schema_plus_foreign_keys_config #:nodoc:
|
7
9
|
|
8
10
|
# seems like this is fixing a rails bug:
|
9
|
-
# change_table foo, :
|
11
|
+
# change_table foo, bulk: true { |t| t.references :bar }
|
10
12
|
# results in an 'unknown method :add_reference_sql' (with mysql2)
|
11
13
|
#
|
12
14
|
# should track it down separately and submit a patch/fix to rails
|
@@ -16,7 +18,7 @@ module SchemaPlus::ForeignKeys
|
|
16
18
|
options[:references] = nil if polymorphic
|
17
19
|
# ugh. copying and pasting code from ::ActiveRecord::ConnectionAdapters::SchemaStatements#add_reference
|
18
20
|
index_options = options.delete(:index)
|
19
|
-
add_column(table_name, "#{ref_name}_id", :integer, options)
|
21
|
+
add_column(table_name, "#{ref_name}_id", :integer, **options)
|
20
22
|
add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
|
21
23
|
add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
|
22
24
|
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module Middleware
|
3
5
|
module Dumper
|
@@ -14,7 +16,7 @@ module SchemaPlus::ForeignKeys
|
|
14
16
|
@inline_fks = Hash.new{ |h, k| h[k] = [] }
|
15
17
|
@backref_fks = Hash.new{ |h, k| h[k] = [] }
|
16
18
|
|
17
|
-
env.connection.
|
19
|
+
env.connection.tables_only.each do |table|
|
18
20
|
if (fks = env.connection.foreign_keys(table)).any?
|
19
21
|
env.dump.data.has_fks = true
|
20
22
|
@inline_fks[table] = fks
|
@@ -37,7 +39,7 @@ module SchemaPlus::ForeignKeys
|
|
37
39
|
|
38
40
|
# Ignore the foreign key dumps at the end of the schema; we'll put them in/near their tables
|
39
41
|
def after(env)
|
40
|
-
env.dump.final.reject!
|
42
|
+
env.dump.final.reject!{ |it| it =~/foreign_key/ }
|
41
43
|
end
|
42
44
|
|
43
45
|
private
|
@@ -66,7 +68,7 @@ module SchemaPlus::ForeignKeys
|
|
66
68
|
def after(env)
|
67
69
|
dumped = {}
|
68
70
|
env.table.columns.each do |column|
|
69
|
-
if (foreign_key = env.dump.data.inline_fks[env.table.name].find
|
71
|
+
if (foreign_key = env.dump.data.inline_fks[env.table.name].find { |it| it.column.to_s == column.name })
|
70
72
|
column.options[:foreign_key] = {references: foreign_key.to_table}.merge foreign_key.options_for_dump
|
71
73
|
dumped[foreign_key] = true
|
72
74
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module Middleware
|
3
5
|
module Migration
|
@@ -22,7 +24,13 @@ module SchemaPlus::ForeignKeys
|
|
22
24
|
env.connection.foreign_keys(newname).each do |fk|
|
23
25
|
begin
|
24
26
|
env.connection.remove_foreign_key(newname, name: fk.name)
|
25
|
-
env.connection.add_foreign_key(newname, fk.to_table,
|
27
|
+
env.connection.add_foreign_key(newname, fk.to_table,
|
28
|
+
column: fk.column,
|
29
|
+
primary_key: fk.primary_key,
|
30
|
+
name: fk.name.sub(/#{oldname}/, newname),
|
31
|
+
on_update: fk.on_update,
|
32
|
+
on_delete: fk.on_delete,
|
33
|
+
deferrable: fk.deferrable)
|
26
34
|
rescue NotImplementedError
|
27
35
|
# sqlite3 can't remote or add foreign keys, so just skip it
|
28
36
|
end
|
@@ -123,9 +131,9 @@ module SchemaPlus::ForeignKeys
|
|
123
131
|
references = fk_opts.delete(:references)
|
124
132
|
case env.caller
|
125
133
|
when ::ActiveRecord::ConnectionAdapters::TableDefinition
|
126
|
-
env.caller.foreign_key(
|
134
|
+
env.caller.foreign_key(references, **fk_opts)
|
127
135
|
else
|
128
|
-
env.caller.add_foreign_key(env.table_name, references, fk_opts
|
136
|
+
env.caller.add_foreign_key(env.table_name, references, **fk_opts)
|
129
137
|
end
|
130
138
|
end
|
131
139
|
|
@@ -134,6 +142,7 @@ module SchemaPlus::ForeignKeys
|
|
134
142
|
return nil if opts.nil?
|
135
143
|
return :none if opts == false
|
136
144
|
opts = {} if opts == true
|
145
|
+
opts[:column] ||= env.column_name
|
137
146
|
opts[:references] ||= default_table_name(env)
|
138
147
|
opts[:on_update] ||= config.on_update
|
139
148
|
opts[:on_delete] ||= config.on_delete
|
@@ -141,7 +150,7 @@ module SchemaPlus::ForeignKeys
|
|
141
150
|
end
|
142
151
|
|
143
152
|
def remove_foreign_key_if_exists(env)
|
144
|
-
env.caller.remove_foreign_key(env.table_name.to_s, column: env.column_name.to_s, :
|
153
|
+
env.caller.remove_foreign_key(env.table_name.to_s, column: env.column_name.to_s, if_exists: true)
|
145
154
|
end
|
146
155
|
|
147
156
|
def default_table_name(env)
|
@@ -159,4 +168,3 @@ module SchemaPlus::ForeignKeys
|
|
159
168
|
end
|
160
169
|
end
|
161
170
|
end
|
162
|
-
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module Middleware
|
3
5
|
|
@@ -6,7 +8,7 @@ module SchemaPlus::ForeignKeys
|
|
6
8
|
module DropTable
|
7
9
|
|
8
10
|
def around(env)
|
9
|
-
if
|
11
|
+
if env.options[:force] == :cascade
|
10
12
|
env.connection.reverse_foreign_keys(env.table_name).each do |foreign_key|
|
11
13
|
env.connection.remove_foreign_key(foreign_key.from_table, name: foreign_key.name)
|
12
14
|
end
|
@@ -1,23 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module SchemaPlus::ForeignKeys
|
2
4
|
module Middleware
|
3
5
|
module Sql
|
4
6
|
module Table
|
5
|
-
def after(env)
|
6
|
-
foreign_keys = if env.table_definition.foreign_keys.is_a? Array
|
7
|
-
env.table_definition.foreign_keys
|
8
|
-
else
|
9
|
-
env.table_definition.foreign_keys.values.tap { |v| v.flatten! }
|
10
|
-
end
|
11
|
-
|
12
|
-
# create foreign key constraints inline in table definition
|
13
|
-
env.sql.body = ([env.sql.body] + foreign_keys.map(&:to_sql)).join(', ')
|
14
|
-
|
15
|
-
# prevents AR >= 4.2.1 from emitting add_foreign_key after the table
|
16
|
-
env.table_definition.foreign_keys.clear
|
17
|
-
end
|
18
|
-
|
19
7
|
module SQLite3
|
20
|
-
|
21
8
|
def before(env)
|
22
9
|
env.connection.execute('PRAGMA FOREIGN_KEYS = ON') if env.table_definition.foreign_keys.any?
|
23
10
|
end
|
data/schema_dev.yml
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
# coding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
lib = File.expand_path('../lib', __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
6
|
require 'schema_plus/foreign_keys/version'
|
@@ -18,15 +20,15 @@ Gem::Specification.new do |gem|
|
|
18
20
|
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
19
21
|
gem.require_paths = ["lib"]
|
20
22
|
|
21
|
-
gem.
|
22
|
-
|
23
|
+
gem.required_ruby_version = ">= 2.5.0"
|
24
|
+
|
25
|
+
gem.add_dependency "activerecord", ">= 5.2", "< 6.1"
|
26
|
+
gem.add_dependency "schema_plus_core", "~> 3.0.0"
|
27
|
+
gem.add_dependency "schema_plus_compatibility", "~> 1.0.0"
|
23
28
|
gem.add_dependency "valuable"
|
24
|
-
gem.add_dependency "its-it", "~> 1.2"
|
25
29
|
|
26
|
-
gem.add_development_dependency "bundler"
|
27
|
-
gem.add_development_dependency "rake", "~>
|
30
|
+
gem.add_development_dependency "bundler"
|
31
|
+
gem.add_development_dependency "rake", "~> 13.0"
|
28
32
|
gem.add_development_dependency "rspec", "~> 3.0"
|
29
|
-
gem.add_development_dependency "schema_dev", "~>
|
30
|
-
gem.add_development_dependency "simplecov"
|
31
|
-
gem.add_development_dependency "simplecov-gem-profile"
|
33
|
+
gem.add_development_dependency "schema_dev", "~> 4.1"
|
32
34
|
end
|
data/spec/deprecation_spec.rb
CHANGED
@@ -1,95 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe 'Deprecations' do
|
4
6
|
|
5
7
|
let(:migration) { ActiveRecord::Migration }
|
6
8
|
|
7
|
-
describe "on add_foreign_key", sqlite3: :skip do
|
8
|
-
before(:each) do
|
9
|
-
define_schema do
|
10
|
-
create_table :posts
|
11
|
-
create_table :comments do |t|
|
12
|
-
t.integer :post_id
|
13
|
-
end
|
14
|
-
end
|
15
|
-
class Comment < ::ActiveRecord::Base ; end
|
16
|
-
end
|
17
|
-
|
18
|
-
it "deprecates 4-argument form" do
|
19
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/4-argument/)
|
20
|
-
migration.add_foreign_key "comments", "post_id", "posts", "id"
|
21
|
-
expect(Comment).to reference(:posts, :id).on(:post_id)
|
22
|
-
end
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
describe "on remove_foreign_key", sqlite3: :skip do
|
27
|
-
before(:each) do
|
28
|
-
define_schema do
|
29
|
-
create_table :posts
|
30
|
-
create_table :comments do |t|
|
31
|
-
t.integer :post_id, foreign_key: true
|
32
|
-
end
|
33
|
-
end
|
34
|
-
class Comment < ::ActiveRecord::Base ; end
|
35
|
-
end
|
36
|
-
|
37
|
-
it "deprecates :column_names option" do
|
38
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/column_names/)
|
39
|
-
migration.remove_foreign_key "comments", "posts", column_names: "post_id"
|
40
|
-
Comment.reset_column_information
|
41
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
42
|
-
end
|
43
|
-
|
44
|
-
it "deprecates :references_column_names option" do
|
45
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_column_names.*primary_key/)
|
46
|
-
migration.remove_foreign_key "comments", "posts", references_column_names: "id"
|
47
|
-
Comment.reset_column_information
|
48
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
49
|
-
end
|
50
|
-
|
51
|
-
it "deprecates :references_table_name option" do
|
52
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_table_name.*to_table/)
|
53
|
-
migration.remove_foreign_key "comments", references_table_name: "posts"
|
54
|
-
Comment.reset_column_information
|
55
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "deprecates table-and-name form" do
|
59
|
-
Comment.reset_column_information
|
60
|
-
name = Comment.foreign_keys.first.name
|
61
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/name.*name: name/)
|
62
|
-
migration.remove_foreign_key "comments", name
|
63
|
-
Comment.reset_column_information
|
64
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
65
|
-
end
|
66
|
-
|
67
|
-
it "deprecates 3-argument form" do
|
68
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/3.*-argument/)
|
69
|
-
migration.remove_foreign_key "comments", "post_id", "posts"
|
70
|
-
Comment.reset_column_information
|
71
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
72
|
-
end
|
73
|
-
|
74
|
-
it "deprecates 4-argument form" do
|
75
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/4.*-argument/)
|
76
|
-
migration.remove_foreign_key "comments", "post_id", "posts", "id"
|
77
|
-
Comment.reset_column_information
|
78
|
-
expect(Comment).to_not reference(:posts, :id).on(:post_id)
|
79
|
-
end
|
80
|
-
|
81
|
-
it "raises error for 5 arguments" do
|
82
|
-
expect { migration.remove_foreign_key "zip", "a", "dee", "do", "da" }.to raise_error /Wrong number of arguments.*5/
|
83
|
-
end
|
84
|
-
|
85
|
-
end
|
86
|
-
|
87
9
|
describe "on foreign key definition" do
|
88
10
|
before(:each) do
|
89
11
|
define_schema do
|
90
12
|
create_table :posts
|
91
13
|
create_table :comments do |t|
|
92
|
-
t.
|
14
|
+
t.references :post, foreign_key: true
|
93
15
|
end
|
94
16
|
end
|
95
17
|
class Comment < ::ActiveRecord::Base ; end
|
@@ -100,32 +22,13 @@ describe 'Deprecations' do
|
|
100
22
|
Comment.foreign_keys.first
|
101
23
|
}
|
102
24
|
|
103
|
-
it "deprecates column_names" do
|
104
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/column_names/)
|
105
|
-
expect(definition.column_names).to eq(["post_id"])
|
106
|
-
end
|
107
|
-
|
108
|
-
it "deprecates references_column_names" do
|
109
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_column_names.*primary_key/)
|
110
|
-
expect(definition.references_column_names).to eq(["id"])
|
111
|
-
end
|
112
|
-
|
113
|
-
it "deprecates references_table_name" do
|
114
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/references_table_name.*to_table/)
|
115
|
-
expect(definition.references_table_name).to eq("posts")
|
116
|
-
end
|
117
|
-
|
118
|
-
it "deprecates table_name" do
|
119
|
-
expect(ActiveSupport::Deprecation).to receive(:warn).with(/table_name.*from_table/)
|
120
|
-
expect(definition.table_name).to eq("comments")
|
121
|
-
end
|
122
|
-
|
123
25
|
it "deprecates :set_null" do
|
124
26
|
expect(ActiveSupport::Deprecation).to receive(:warn).with(/set_null.*nullify/)
|
27
|
+
allow(ActiveSupport::Deprecation).to receive(:warn).with(/table_exists\? currently checks/)
|
125
28
|
define_schema do
|
126
29
|
create_table :posts
|
127
30
|
create_table :comments do |t|
|
128
|
-
t.
|
31
|
+
t.references :post, references: :posts, on_delete: :set_null
|
129
32
|
end
|
130
33
|
end
|
131
34
|
expect(definition.on_delete).to eq(:nullify)
|
@@ -134,16 +37,16 @@ describe 'Deprecations' do
|
|
134
37
|
end
|
135
38
|
|
136
39
|
describe "in table definition" do
|
137
|
-
it "
|
138
|
-
expect
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
40
|
+
it "raises error for 3 arguments" do
|
41
|
+
expect {
|
42
|
+
define_schema do
|
43
|
+
create_table :posts, primary_key: :funky
|
44
|
+
create_table :comments do |t|
|
45
|
+
t.references :post
|
46
|
+
t.foreign_key :post_id, :posts, :funky
|
47
|
+
end
|
144
48
|
end
|
145
|
-
|
146
|
-
expect(migration.foreign_keys("comments").first.primary_key).to eq("funky")
|
49
|
+
}.to raise_error /wrong number of arguments/i
|
147
50
|
end
|
148
51
|
|
149
52
|
it "raises error for 4 arguments" do
|
@@ -151,7 +54,7 @@ describe 'Deprecations' do
|
|
151
54
|
define_schema do
|
152
55
|
create_table :posts, primary_key: :funky
|
153
56
|
create_table :comments do |t|
|
154
|
-
t.
|
57
|
+
t.references :post
|
155
58
|
t.foreign_key :post_id, :posts, :funky, :town
|
156
59
|
end
|
157
60
|
end
|
@@ -1,9 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
describe "Foreign Key definition" do
|
4
6
|
|
5
7
|
let(:definition) {
|
6
|
-
options = {:
|
8
|
+
options = {name: "posts_user_fkey", column: :user, primary_key: :id}
|
7
9
|
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
8
10
|
}
|
9
11
|
|
@@ -12,13 +14,13 @@ describe "Foreign Key definition" do
|
|
12
14
|
end
|
13
15
|
|
14
16
|
it "dumps to sql with deferrable values" do
|
15
|
-
options = {:
|
17
|
+
options = {name: "posts_user_fkey", column: :user, primary_key: :id, deferrable: true}
|
16
18
|
deferred_definition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
17
19
|
expect(deferred_definition.to_sql).to eq(%Q{CONSTRAINT posts_user_fkey FOREIGN KEY (#{quote_column_name('user')}) REFERENCES #{quote_table_name('users')} (#{quote_column_name('id')}) DEFERRABLE})
|
18
20
|
end
|
19
21
|
|
20
22
|
it "dumps to sql with initially deferrable values" do
|
21
|
-
options = {:
|
23
|
+
options = {name: "posts_user_fkey", column: :user, primary_key: :id, deferrable: :initially_deferred}
|
22
24
|
initially_deferred_definition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
23
25
|
expect(initially_deferred_definition.to_sql).to eq(%Q{CONSTRAINT posts_user_fkey FOREIGN KEY (#{quote_column_name('user')}) REFERENCES #{quote_table_name('users')} (#{quote_column_name('id')}) DEFERRABLE INITIALLY DEFERRED})
|
24
26
|
end
|