activerecord-creating_foreign_keys 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/activerecord/creating_foreign_keys.rb +9 -0
- data/lib/activerecord/creating_foreign_keys/connection_specification/resolver.rb +23 -0
- data/lib/activerecord/creating_foreign_keys/schema_creation.rb +40 -0
- data/lib/activerecord/creating_foreign_keys/schema_statements.rb +42 -0
- data/lib/{active_record → activerecord}/creating_foreign_keys/version.rb +1 -1
- data/test/dummy/config/application.rb +1 -1
- metadata +7 -7
- data/lib/active_record/connection_adapters/mysql2/creating_foreign_keys/connection_specification/resolver.rb +0 -27
- data/lib/active_record/connection_adapters/mysql2/creating_foreign_keys/schema_creation.rb +0 -44
- data/lib/active_record/connection_adapters/mysql2/creating_foreign_keys/schema_statements.rb +0 -46
- data/lib/active_record/creating_foreign_keys.rb +0 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad851761b4eb31ec42e08fd116be1dcebd19652c
|
4
|
+
data.tar.gz: 39ffb4137360cdb52edef99d77619d8c8811eb89
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1c4242b13aa86fc1fb84f7a65a3aabc09b55aa88374618934044f38398da6e2df6fdbedac18f419fa38a2f8ba0dc01bf4cf867f3860365ad15194ec85f08fa4
|
7
|
+
data.tar.gz: 01ce381513bfaffb3f8318a702e94cbcb9597be0a26f552c699b643c66a2cb5dd54d6b3e7eb88f7109d0631cb43019be7571b9185c6f123aa62b4a02cc94812a
|
@@ -0,0 +1,9 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "active_support/lazy_load_hooks"
|
4
|
+
|
5
|
+
ActiveSupport.on_load(:active_record) do
|
6
|
+
require "activerecord/creating_foreign_keys/connection_specification/resolver"
|
7
|
+
|
8
|
+
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.prepend(ActiveRecord::CreatingForeignKeys::ConnectionSpecification::Resolver)
|
9
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module CreatingForeignKeys
|
5
|
+
module ConnectionSpecification
|
6
|
+
module Resolver
|
7
|
+
def spec(config)
|
8
|
+
connection_specification = super(config)
|
9
|
+
|
10
|
+
if connection_specification.config[:adapter] == "mysql2"
|
11
|
+
require "activerecord/creating_foreign_keys/schema_creation"
|
12
|
+
require "activerecord/creating_foreign_keys/schema_statements"
|
13
|
+
|
14
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter::SchemaCreation.prepend(ActiveRecord::CreatingForeignKeys::SchemaCreation)
|
15
|
+
ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(ActiveRecord::CreatingForeignKeys::SchemaStatements)
|
16
|
+
end
|
17
|
+
|
18
|
+
connection_specification
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module CreatingForeignKeys
|
5
|
+
module SchemaCreation
|
6
|
+
private
|
7
|
+
def visit_TableDefinition(o)
|
8
|
+
return super(o) if o.foreign_keys.empty?
|
9
|
+
|
10
|
+
name = o.name
|
11
|
+
create_sql = +"CREATE#{' TEMPORARY' if o.temporary} TABLE #{quote_table_name(name)} "
|
12
|
+
|
13
|
+
statements = o.columns.map { |c| accept c }
|
14
|
+
statements.concat(o.indexes.map { |column_name, options| index_in_create(name, column_name, options) })
|
15
|
+
statements.concat(o.foreign_keys.map { |to_table, options| foreign_key_in_create(o.name, to_table, options) })
|
16
|
+
|
17
|
+
create_sql << "(#{statements.join(', ')}) " if statements.present?
|
18
|
+
create_sql << "#{o.options}"
|
19
|
+
create_sql << " AS #{@conn.to_sql(o.as)}" if o.as
|
20
|
+
create_sql
|
21
|
+
end
|
22
|
+
|
23
|
+
def visit_ForeignKeyDefinition(o)
|
24
|
+
sql = +<<-SQL.strip_heredoc
|
25
|
+
CONSTRAINT #{quote_column_name(o.name)}
|
26
|
+
FOREIGN KEY (#{quote_column_name(o.column)})
|
27
|
+
REFERENCES #{quote_table_name(o.to_table)} (#{quote_column_name(o.primary_key)})
|
28
|
+
SQL
|
29
|
+
sql << " #{action_sql('DELETE', o.on_delete)}" if o.on_delete
|
30
|
+
sql << " #{action_sql('UPDATE', o.on_update)}" if o.on_update
|
31
|
+
sql
|
32
|
+
end
|
33
|
+
|
34
|
+
def foreign_key_in_create(from_table, to_table, options)
|
35
|
+
options = @conn.foreign_key_options(from_table, to_table, options)
|
36
|
+
accept ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, to_table, options)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActiveRecord
|
4
|
+
module CreatingForeignKeys
|
5
|
+
module SchemaStatements
|
6
|
+
def create_table(table_name, options = {})
|
7
|
+
td = create_table_definition table_name, options[:temporary], options[:options], options[:as]
|
8
|
+
|
9
|
+
if options[:id] != false && !options[:as]
|
10
|
+
pk = options.fetch(:primary_key) do
|
11
|
+
Base.get_primary_key table_name.to_s.singularize
|
12
|
+
end
|
13
|
+
|
14
|
+
td.primary_key pk, options.fetch(:id, :primary_key), options
|
15
|
+
end
|
16
|
+
|
17
|
+
yield td if block_given?
|
18
|
+
|
19
|
+
if options[:force] && table_exists?(table_name)
|
20
|
+
drop_table(table_name, options)
|
21
|
+
end
|
22
|
+
|
23
|
+
result = execute schema_creation.accept td
|
24
|
+
|
25
|
+
unless supports_indexes_in_create?
|
26
|
+
td.indexes.each_pair do |column_name, index_options|
|
27
|
+
add_index(table_name, column_name, index_options)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
result
|
32
|
+
end
|
33
|
+
|
34
|
+
def foreign_key_options(from_table, to_table, options) # :nodoc:
|
35
|
+
options = options.dup
|
36
|
+
options[:column] ||= foreign_key_column_for(to_table)
|
37
|
+
options[:name] ||= foreign_key_name(from_table, options)
|
38
|
+
options
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-creating_foreign_keys
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- hamuyuuki
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-11-
|
11
|
+
date: 2020-11-13 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -161,11 +161,11 @@ files:
|
|
161
161
|
- MIT-LICENSE
|
162
162
|
- README.md
|
163
163
|
- Rakefile
|
164
|
-
- lib/
|
165
|
-
- lib/
|
166
|
-
- lib/
|
167
|
-
- lib/
|
168
|
-
- lib/
|
164
|
+
- lib/activerecord/creating_foreign_keys.rb
|
165
|
+
- lib/activerecord/creating_foreign_keys/connection_specification/resolver.rb
|
166
|
+
- lib/activerecord/creating_foreign_keys/schema_creation.rb
|
167
|
+
- lib/activerecord/creating_foreign_keys/schema_statements.rb
|
168
|
+
- lib/activerecord/creating_foreign_keys/version.rb
|
169
169
|
- test/creating_foreign_keys_test.rb
|
170
170
|
- test/dummy/README.rdoc
|
171
171
|
- test/dummy/Rakefile
|
@@ -1,27 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
module ConnectionAdapters
|
5
|
-
module Mysql2
|
6
|
-
module CreatingForeignKeys
|
7
|
-
module ConnectionSpecification
|
8
|
-
module Resolver
|
9
|
-
def spec(config)
|
10
|
-
connection_specification = super(config)
|
11
|
-
|
12
|
-
if connection_specification.config[:adapter] == "mysql2"
|
13
|
-
require "active_record/connection_adapters/mysql2/creating_foreign_keys/schema_creation"
|
14
|
-
require "active_record/connection_adapters/mysql2/creating_foreign_keys/schema_statements"
|
15
|
-
|
16
|
-
ActiveRecord::ConnectionAdapters::Mysql2Adapter::SchemaCreation.prepend(ActiveRecord::ConnectionAdapters::Mysql2::CreatingForeignKeys::SchemaCreation)
|
17
|
-
ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(ActiveRecord::ConnectionAdapters::Mysql2::CreatingForeignKeys::SchemaStatements)
|
18
|
-
end
|
19
|
-
|
20
|
-
connection_specification
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
@@ -1,44 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
module ConnectionAdapters
|
5
|
-
module Mysql2
|
6
|
-
module CreatingForeignKeys
|
7
|
-
module SchemaCreation
|
8
|
-
private
|
9
|
-
def visit_TableDefinition(o)
|
10
|
-
return super(o) if o.foreign_keys.empty?
|
11
|
-
|
12
|
-
name = o.name
|
13
|
-
create_sql = +"CREATE#{' TEMPORARY' if o.temporary} TABLE #{quote_table_name(name)} "
|
14
|
-
|
15
|
-
statements = o.columns.map { |c| accept c }
|
16
|
-
statements.concat(o.indexes.map { |column_name, options| index_in_create(name, column_name, options) })
|
17
|
-
statements.concat(o.foreign_keys.map { |to_table, options| foreign_key_in_create(o.name, to_table, options) })
|
18
|
-
|
19
|
-
create_sql << "(#{statements.join(', ')}) " if statements.present?
|
20
|
-
create_sql << "#{o.options}"
|
21
|
-
create_sql << " AS #{@conn.to_sql(o.as)}" if o.as
|
22
|
-
create_sql
|
23
|
-
end
|
24
|
-
|
25
|
-
def visit_ForeignKeyDefinition(o)
|
26
|
-
sql = +<<-SQL.strip_heredoc
|
27
|
-
CONSTRAINT #{quote_column_name(o.name)}
|
28
|
-
FOREIGN KEY (#{quote_column_name(o.column)})
|
29
|
-
REFERENCES #{quote_table_name(o.to_table)} (#{quote_column_name(o.primary_key)})
|
30
|
-
SQL
|
31
|
-
sql << " #{action_sql('DELETE', o.on_delete)}" if o.on_delete
|
32
|
-
sql << " #{action_sql('UPDATE', o.on_update)}" if o.on_update
|
33
|
-
sql
|
34
|
-
end
|
35
|
-
|
36
|
-
def foreign_key_in_create(from_table, to_table, options)
|
37
|
-
options = @conn.foreign_key_options(from_table, to_table, options)
|
38
|
-
accept ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(from_table, to_table, options)
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
data/lib/active_record/connection_adapters/mysql2/creating_foreign_keys/schema_statements.rb
DELETED
@@ -1,46 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module ActiveRecord
|
4
|
-
module ConnectionAdapters
|
5
|
-
module Mysql2
|
6
|
-
module CreatingForeignKeys
|
7
|
-
module SchemaStatements
|
8
|
-
def create_table(table_name, options = {})
|
9
|
-
td = create_table_definition table_name, options[:temporary], options[:options], options[:as]
|
10
|
-
|
11
|
-
if options[:id] != false && !options[:as]
|
12
|
-
pk = options.fetch(:primary_key) do
|
13
|
-
Base.get_primary_key table_name.to_s.singularize
|
14
|
-
end
|
15
|
-
|
16
|
-
td.primary_key pk, options.fetch(:id, :primary_key), options
|
17
|
-
end
|
18
|
-
|
19
|
-
yield td if block_given?
|
20
|
-
|
21
|
-
if options[:force] && table_exists?(table_name)
|
22
|
-
drop_table(table_name, options)
|
23
|
-
end
|
24
|
-
|
25
|
-
result = execute schema_creation.accept td
|
26
|
-
|
27
|
-
unless supports_indexes_in_create?
|
28
|
-
td.indexes.each_pair do |column_name, index_options|
|
29
|
-
add_index(table_name, column_name, index_options)
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
result
|
34
|
-
end
|
35
|
-
|
36
|
-
def foreign_key_options(from_table, to_table, options) # :nodoc:
|
37
|
-
options = options.dup
|
38
|
-
options[:column] ||= foreign_key_column_for(to_table)
|
39
|
-
options[:name] ||= foreign_key_name(from_table, options)
|
40
|
-
options
|
41
|
-
end
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
@@ -1,9 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require "active_support/lazy_load_hooks"
|
4
|
-
|
5
|
-
ActiveSupport.on_load(:active_record) do
|
6
|
-
require "active_record/connection_adapters/mysql2/creating_foreign_keys/connection_specification/resolver"
|
7
|
-
|
8
|
-
ActiveRecord::ConnectionAdapters::ConnectionSpecification::Resolver.prepend(ActiveRecord::ConnectionAdapters::Mysql2::CreatingForeignKeys::ConnectionSpecification::Resolver)
|
9
|
-
end
|