schema_plus 2.0.0.pre15 → 2.0.0.pre16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +6 -78
- data/lib/schema_plus.rb +1 -0
- data/lib/schema_plus/auto_foreign_keys.rb +31 -0
- data/lib/schema_plus/auto_foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +22 -0
- data/lib/schema_plus/auto_foreign_keys/active_record/migration/command_recorder.rb +14 -0
- data/lib/schema_plus/auto_foreign_keys/middleware/migration.rb +66 -0
- data/lib/schema_plus/{foreign_keys → auto_foreign_keys}/middleware/schema.rb +1 -1
- data/lib/schema_plus/version.rb +1 -1
- data/schema_plus.gemspec +2 -1
- data/spec/{schema_plus_foreign_keys → schema_auto_foreign_keys}/foreign_key_spec.rb +0 -0
- data/spec/{schema_plus_foreign_keys → schema_auto_foreign_keys}/migration_spec.rb +3 -2
- data/spec/{schema_plus_foreign_keys → schema_auto_foreign_keys}/schema_dumper_spec.rb +0 -0
- data/spec/{schema_plus_foreign_keys → schema_auto_foreign_keys}/schema_spec.rb +0 -0
- metadata +31 -32
- data/lib/schema_plus/foreign_keys.rb +0 -93
- data/lib/schema_plus/foreign_keys/active_record/base.rb +0 -33
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb +0 -168
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/foreign_key_definition.rb +0 -138
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/mysql2_adapter.rb +0 -126
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/postgresql_adapter.rb +0 -89
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/sqlite3_adapter.rb +0 -98
- data/lib/schema_plus/foreign_keys/active_record/connection_adapters/table_definition.rb +0 -108
- data/lib/schema_plus/foreign_keys/active_record/migration/command_recorder.rb +0 -35
- data/lib/schema_plus/foreign_keys/middleware/dumper.rb +0 -79
- data/lib/schema_plus/foreign_keys/middleware/migration.rb +0 -184
- data/lib/schema_plus/foreign_keys/middleware/model.rb +0 -15
- data/lib/schema_plus/foreign_keys/middleware/mysql.rb +0 -20
- data/lib/schema_plus/foreign_keys/middleware/sql.rb +0 -20
- data/lib/schema_plus/foreign_keys/version.rb +0 -3
- data/spec/schema_plus_foreign_keys/foreign_key_definition_spec.rb +0 -34
- data/spec/schema_plus_foreign_keys/named_schemas_spec.rb +0 -136
@@ -1,20 +0,0 @@
|
|
1
|
-
module SchemaPlus::ForeignKeys
|
2
|
-
module Middleware
|
3
|
-
|
4
|
-
module Mysql
|
5
|
-
module Migration
|
6
|
-
module DropTable
|
7
|
-
|
8
|
-
def around(env)
|
9
|
-
if (env.options[:force] == :cascade)
|
10
|
-
env.connection.reverse_foreign_keys(env.table_name).each do |foreign_key|
|
11
|
-
env.connection.remove_foreign_key(foreign_key.from_table, name: foreign_key.name)
|
12
|
-
end
|
13
|
-
end
|
14
|
-
yield env
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,20 +0,0 @@
|
|
1
|
-
module SchemaPlus::ForeignKeys
|
2
|
-
module Middleware
|
3
|
-
module Sql
|
4
|
-
module Table
|
5
|
-
def after(env)
|
6
|
-
foreign_keys = case ::ActiveRecord.version
|
7
|
-
when Gem::Version.new("4.2.0") then env.table_definition.foreign_keys
|
8
|
-
else env.table_definition.foreign_keys.values.tap { |v| v.flatten! }
|
9
|
-
end
|
10
|
-
|
11
|
-
# create foreign key constraints inline in table definition
|
12
|
-
env.sql.body = ([env.sql.body] + foreign_keys.map(&:to_sql)).join(', ')
|
13
|
-
|
14
|
-
# prevents AR >= 4.2.1 from emitting add_foreign_key after the table
|
15
|
-
env.table_definition.foreign_keys.clear
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
20
|
-
end
|
@@ -1,34 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "Foreign Key definition" do
|
4
|
-
|
5
|
-
let(:definition) {
|
6
|
-
options = {:name => "posts_user_fkey", :column => :user, :primary_key => :id}
|
7
|
-
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
8
|
-
}
|
9
|
-
|
10
|
-
it "dumps to sql with quoted values" do
|
11
|
-
expect(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')})})
|
12
|
-
end
|
13
|
-
|
14
|
-
it "dumps to sql with deferrable values" do
|
15
|
-
options = {:name => "posts_user_fkey", :column => :user, :primary_key => :id, :deferrable => true}
|
16
|
-
deferred_definition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
17
|
-
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
|
-
end
|
19
|
-
|
20
|
-
it "dumps to sql with initially deferrable values" do
|
21
|
-
options = {:name => "posts_user_fkey", :column => :user, :primary_key => :id, :deferrable => :initially_deferred}
|
22
|
-
initially_deferred_definition = ::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(:posts, :users, options)
|
23
|
-
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
|
-
end
|
25
|
-
|
26
|
-
def quote_table_name(table)
|
27
|
-
ActiveRecord::Base.connection.quote_table_name(table)
|
28
|
-
end
|
29
|
-
|
30
|
-
def quote_column_name(column)
|
31
|
-
ActiveRecord::Base.connection.quote_column_name(column)
|
32
|
-
end
|
33
|
-
|
34
|
-
end
|
@@ -1,136 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "with multiple schemas" do
|
4
|
-
def connection
|
5
|
-
ActiveRecord::Base.connection
|
6
|
-
end
|
7
|
-
|
8
|
-
before(:all) do
|
9
|
-
newdb = case connection.adapter_name
|
10
|
-
when /^mysql/i then "CREATE SCHEMA IF NOT EXISTS schema_plus_test2"
|
11
|
-
when /^postgresql/i then "CREATE SCHEMA schema_plus_test2"
|
12
|
-
when /^sqlite/i then "ATTACH ':memory:' AS schema_plus_test2"
|
13
|
-
end
|
14
|
-
begin
|
15
|
-
ActiveRecord::Base.connection.execute newdb
|
16
|
-
rescue ActiveRecord::StatementInvalid => e
|
17
|
-
raise unless e.message =~ /already exists/
|
18
|
-
end
|
19
|
-
|
20
|
-
class User < ::ActiveRecord::Base ; end
|
21
|
-
end
|
22
|
-
|
23
|
-
before(:each) do
|
24
|
-
define_schema(:auto_create => false) do
|
25
|
-
create_table :users, :force => true do |t|
|
26
|
-
t.string :login
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.users'
|
31
|
-
connection.execute 'CREATE TABLE schema_plus_test2.users (id ' + case connection.adapter_name
|
32
|
-
when /^mysql/i then "integer primary key auto_increment"
|
33
|
-
when /^postgresql/i then "serial primary key"
|
34
|
-
when /^sqlite/i then "integer primary key autoincrement"
|
35
|
-
end + ", login varchar(255))"
|
36
|
-
end
|
37
|
-
|
38
|
-
context "with foreign key in each schema" do
|
39
|
-
before(:each) do
|
40
|
-
class Comment < ::ActiveRecord::Base ; end
|
41
|
-
connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.comments'
|
42
|
-
connection.execute 'CREATE TABLE schema_plus_test2.comments (user_id integer,' + case connection.adapter_name
|
43
|
-
when /^mysql/i then "foreign key (user_id) references schema_plus_test2.users (id))"
|
44
|
-
when /^postgresql/i then "foreign key (user_id) references schema_plus_test2.users (id))"
|
45
|
-
when /^sqlite/i then "foreign key (user_id) references users (id))"
|
46
|
-
end
|
47
|
-
end
|
48
|
-
|
49
|
-
around(:each) do |example|
|
50
|
-
begin
|
51
|
-
example.run
|
52
|
-
ensure
|
53
|
-
connection.execute 'DROP TABLE IF EXISTS comments'
|
54
|
-
connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.comments'
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should not find foreign keys in other schema" do
|
59
|
-
connection.create_table :comments, :force => true do |t|
|
60
|
-
t.integer :user_id, :foreign_key => false
|
61
|
-
end
|
62
|
-
Comment.reset_column_information
|
63
|
-
expect(Comment.foreign_keys.length).to eq(0)
|
64
|
-
User.reset_column_information
|
65
|
-
expect(User.reverse_foreign_keys.length).to eq(0)
|
66
|
-
end
|
67
|
-
|
68
|
-
it "should find foreign keys in this schema" do
|
69
|
-
connection.create_table :comments, :force => true do |t|
|
70
|
-
t.integer :user_id, :foreign_key => true
|
71
|
-
end
|
72
|
-
Comment.reset_column_information
|
73
|
-
expect(Comment.foreign_keys.map(&:column).flatten).to eq(["user_id"])
|
74
|
-
User.reset_column_information
|
75
|
-
expect(User.reverse_foreign_keys.map(&:column).flatten).to eq(["user_id"])
|
76
|
-
end
|
77
|
-
|
78
|
-
end
|
79
|
-
|
80
|
-
context "foreign key migrations" do
|
81
|
-
before(:each) do
|
82
|
-
define_schema do
|
83
|
-
create_table "items", :force => true do |t|
|
84
|
-
end
|
85
|
-
create_table "schema_plus_test2.groups", :force => true do |t|
|
86
|
-
end
|
87
|
-
create_table "schema_plus_test2.members", :force => true do |t|
|
88
|
-
t.integer :item_id, :foreign_key => true unless SchemaDev::Rspec::Helpers.mysql?
|
89
|
-
t.integer :group_id, :foreign_key => { references: "schema_plus_test2.groups" }
|
90
|
-
end
|
91
|
-
end
|
92
|
-
class Group < ::ActiveRecord::Base
|
93
|
-
self.table_name = "schema_plus_test2.groups"
|
94
|
-
end
|
95
|
-
class Item < ::ActiveRecord::Base
|
96
|
-
self.table_name = "items"
|
97
|
-
end
|
98
|
-
class Member < ::ActiveRecord::Base
|
99
|
-
self.table_name = "schema_plus_test2.members"
|
100
|
-
end
|
101
|
-
end
|
102
|
-
|
103
|
-
around(:each) do |example|
|
104
|
-
begin
|
105
|
-
example.run
|
106
|
-
ensure
|
107
|
-
connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.members'
|
108
|
-
connection.execute 'DROP TABLE IF EXISTS schema_plus_test2.groups'
|
109
|
-
connection.execute 'DROP TABLE IF EXISTS items'
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
it "should find foreign keys" do
|
114
|
-
expect(Member.foreign_keys).not_to be_empty
|
115
|
-
end
|
116
|
-
|
117
|
-
it "should find reverse foreign keys" do
|
118
|
-
expect(Group.reverse_foreign_keys).not_to be_empty
|
119
|
-
end
|
120
|
-
|
121
|
-
it "should reference table in same schema" do
|
122
|
-
expect(Member.foreign_keys.map(&:to_table)).to include "schema_plus_test2.groups"
|
123
|
-
end
|
124
|
-
|
125
|
-
it "should reference table in default schema", :mysql => :skip do
|
126
|
-
expect(Member.foreign_keys.map(&:to_table)).to include "items"
|
127
|
-
end
|
128
|
-
|
129
|
-
it "should include the schema in the constraint name" do
|
130
|
-
expected_names = ["fk_schema_plus_test2_members_group_id"]
|
131
|
-
expected_names << "fk_schema_plus_test2_members_item_id" unless SchemaDev::Rspec::Helpers.mysql?
|
132
|
-
expect(Member.foreign_keys.map(&:name).sort).to match_array(expected_names.sort)
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
end
|