schema_plus 2.0.0.pre5 → 2.0.0.pre6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +41 -51
- data/lib/schema_plus.rb +10 -10
- data/lib/schema_plus/db_default.rb +7 -0
- data/lib/{schema_plus_db_default → schema_plus/db_default}/active_record/attribute.rb +1 -1
- data/lib/{schema_plus_db_default → schema_plus/db_default}/db_default.rb +2 -2
- data/lib/{schema_plus_db_default → schema_plus/db_default}/middleware.rb +1 -1
- data/lib/{schema_plus_default_expr.rb → schema_plus/default_expr.rb} +8 -8
- data/lib/{schema_plus_default_expr → schema_plus/default_expr}/active_record/connection_adapters/column.rb +1 -1
- data/lib/{schema_plus_default_expr → schema_plus/default_expr}/active_record/connection_adapters/mysql2_adapter.rb +1 -1
- data/lib/{schema_plus_default_expr → schema_plus/default_expr}/active_record/connection_adapters/postgresql_adapter.rb +1 -1
- data/lib/{schema_plus_default_expr → schema_plus/default_expr}/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
- data/lib/{schema_plus_default_expr → schema_plus/default_expr}/middleware.rb +1 -1
- data/lib/schema_plus/enums.rb +6 -0
- data/lib/{schema_plus_enums → schema_plus/enums}/active_record.rb +1 -1
- data/lib/{schema_plus_enums → schema_plus/enums}/middleware.rb +1 -1
- data/lib/{schema_plus_foreign_keys.rb → schema_plus/foreign_keys.rb} +21 -21
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/base.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/connection_adapters/abstract_adapter.rb +5 -5
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/connection_adapters/foreign_key_definition.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/connection_adapters/mysql2_adapter.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/connection_adapters/postgresql_adapter.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/connection_adapters/sqlite3_adapter.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/connection_adapters/table_definition.rb +8 -8
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/migration/command_recorder.rb +1 -1
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/active_record/schema.rb +4 -4
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/middleware/dumper.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/middleware/migration.rb +2 -2
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/middleware/model.rb +1 -1
- data/lib/{schema_plus_foreign_keys → schema_plus/foreign_keys}/middleware/sql.rb +1 -1
- data/lib/schema_plus/foreign_keys/version.rb +3 -0
- data/lib/schema_plus/tables.rb +15 -0
- data/lib/{schema_plus_tables → schema_plus/tables}/active_record/connection_adapters/abstract_adapter.rb +2 -2
- data/lib/{schema_plus_tables → schema_plus/tables}/active_record/connection_adapters/mysql2_adapter.rb +2 -2
- data/lib/{schema_plus_tables → schema_plus/tables}/active_record/connection_adapters/postgresql_adapter.rb +2 -2
- data/lib/{schema_plus_tables → schema_plus/tables}/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
- data/lib/schema_plus/version.rb +1 -1
- data/schema_plus.gemspec +2 -1
- data/spec/schema_plus_db_default/column_spec.rb +1 -1
- data/spec/schema_plus_default_expr/schema_dumper_spec.rb +1 -1
- data/spec/schema_plus_foreign_keys/migration_spec.rb +13 -13
- data/spec/schema_plus_foreign_keys/schema_dumper_spec.rb +78 -11
- data/spec/schema_plus_foreign_keys/schema_spec.rb +6 -6
- data/spec/spec_helper.rb +5 -5
- data/spec/support/helpers.rb +1 -1
- data/spec/support/matchers/have_index.rb +1 -1
- data/spec/support/matchers/reference.rb +1 -1
- metadata +51 -42
- data/lib/schema_plus_columns.rb +0 -7
- data/lib/schema_plus_columns/active_record/connection_adapters/column.rb +0 -65
- data/lib/schema_plus_columns/middleware/model.rb +0 -16
- data/lib/schema_plus_db_default.rb +0 -7
- data/lib/schema_plus_enums.rb +0 -6
- data/lib/schema_plus_foreign_keys/version.rb +0 -3
- data/lib/schema_plus_tables.rb +0 -15
- data/spec/schema_plus_columns/column_spec.rb +0 -156
@@ -1,7 +1,7 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
|
-
#
|
4
|
+
# SchemaPlus::ForeignKeys includes a MySQL implementation of the AbstractAdapter
|
5
5
|
# extensions.
|
6
6
|
module Mysql2Adapter
|
7
7
|
|
@@ -1,8 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
|
-
# The Postgresql adapter implements the
|
5
|
+
# The Postgresql adapter implements the SchemaPlus::ForeignKeys extensions and
|
6
6
|
# enhancements
|
7
7
|
module PostgresqlAdapter
|
8
8
|
|
@@ -1,8 +1,8 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
|
-
#
|
5
|
+
# SchemaPlus::ForeignKeys includes an Sqlite3 implementation of the AbstractAdapter
|
6
6
|
# extensions.
|
7
7
|
module Sqlite3Adapter
|
8
8
|
|
@@ -1,19 +1,19 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys::ActiveRecord::ConnectionAdapters
|
2
2
|
|
3
3
|
#
|
4
|
-
#
|
4
|
+
# SchemaPlus::ForeignKeys 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 SchemaPlus::ForeignKeys 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 SchemaPlus::ForeignKeys, 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 SchemaPlusForeignKeys::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
|
+
# 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
35
|
#
|
36
36
|
# create_table :posts do |t| # not DRY
|
37
37
|
# t.integer :author_id
|
@@ -47,13 +47,13 @@ module SchemaPlusForeignKeys::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, SchemaPlus::ForeignKeys 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
|
+
# SchemaPlus::ForeignKeys likewise by default automatically creates foreign key constraints for
|
56
|
+
# columns defined via <tt>t.references</tt>. However, SchemaPlus::ForeignKeys 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
|
@@ -1,16 +1,16 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys
|
2
2
|
module ActiveRecord
|
3
3
|
module Schema
|
4
4
|
module ClassMethods
|
5
5
|
|
6
6
|
def define(*args)
|
7
7
|
fk_override = { :auto_create => false, :auto_index => false }
|
8
|
-
save = Hash[fk_override.keys.collect{|key| [key,
|
8
|
+
save = Hash[fk_override.keys.collect{|key| [key, SchemaPlus::ForeignKeys.config.send(key)]}]
|
9
9
|
begin
|
10
|
-
|
10
|
+
SchemaPlus::ForeignKeys.config.update_attributes(fk_override)
|
11
11
|
super
|
12
12
|
ensure
|
13
|
-
|
13
|
+
SchemaPlus::ForeignKeys.config.update_attributes(save)
|
14
14
|
end
|
15
15
|
end
|
16
16
|
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys
|
2
2
|
module Middleware
|
3
3
|
module Dumper
|
4
4
|
|
@@ -41,7 +41,7 @@ module SchemaPlusForeignKeys
|
|
41
41
|
|
42
42
|
def break_fk_cycles(env) #:nodoc:
|
43
43
|
env.dump.strongly_connected_components.select{|component| component.size > 1}.each do |tables|
|
44
|
-
table = tables.sort.
|
44
|
+
table = tables.sort.last
|
45
45
|
backref_fks = @inline_fks[table].select{|fk| tables.include?(fk.to_table)}
|
46
46
|
@inline_fks[table] -= backref_fks
|
47
47
|
env.dump.dependencies[table] -= backref_fks.collect(&:to_table)
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::ForeignKeys
|
2
2
|
module Middleware
|
3
3
|
module Migration
|
4
4
|
|
@@ -71,7 +71,7 @@ module SchemaPlusForeignKeys
|
|
71
71
|
commands_length = env.caller.commands.length
|
72
72
|
end
|
73
73
|
|
74
|
-
config = (env.caller.try(:schema_plus_config) ||
|
74
|
+
config = (env.caller.try(:schema_plus_config) || SchemaPlus::ForeignKeys.config)
|
75
75
|
fk_args = get_fk_args(env, config)
|
76
76
|
|
77
77
|
# remove existing fk and auto-generated index in case of change of fk on existing column
|
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'schema_plus/core'
|
2
|
+
|
3
|
+
require_relative 'tables/active_record/connection_adapters/abstract_adapter'
|
4
|
+
|
5
|
+
module SchemaPlus::Tables
|
6
|
+
module ActiveRecord
|
7
|
+
module ConnectionAdapters
|
8
|
+
autoload :Mysql2Adapter, 'schema_plus/tables/active_record/connection_adapters/mysql2_adapter'
|
9
|
+
autoload :PostgresqlAdapter, 'schema_plus/tables/active_record/connection_adapters/postgresql_adapter'
|
10
|
+
autoload :Sqlite3Adapter, 'schema_plus/tables/active_record/connection_adapters/sqlite3_adapter'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
SchemaMonkey.register SchemaPlus::Tables
|
@@ -1,11 +1,11 @@
|
|
1
|
-
module
|
1
|
+
module SchemaPlus::Tables
|
2
2
|
module ActiveRecord
|
3
3
|
module ConnectionAdapters
|
4
4
|
module PostgresqlAdapter
|
5
5
|
# pg gem defines a drop_table with fewer options than our Abstract
|
6
6
|
# one, so use the abstract one instead
|
7
7
|
def drop_table(name, options={})
|
8
|
-
|
8
|
+
SchemaPlus::Tables::ActiveRecord::ConnectionAdapters::AbstractAdapter.instance_method(:drop_table).bind(self).call(name, options)
|
9
9
|
end
|
10
10
|
end
|
11
11
|
end
|
data/lib/schema_plus/version.rb
CHANGED
data/schema_plus.gemspec
CHANGED
@@ -25,7 +25,8 @@ Gem::Specification.new do |gem|
|
|
25
25
|
gem.add_dependency "valuable"
|
26
26
|
gem.add_dependency "schema_plus_core", "~> 0.1"
|
27
27
|
gem.add_dependency "schema_monkey_rails", "~> 0.1", ">= 0.1.2"
|
28
|
-
gem.add_dependency "
|
28
|
+
gem.add_dependency "schema_plus_columns", "~> 0.1"
|
29
|
+
gem.add_dependency "schema_plus_indexes", "~> 0.1", ">= 0.1.3"
|
29
30
|
gem.add_dependency "schema_plus_pg_indexes", "~> 0.1", ">= 0.1.3"
|
30
31
|
gem.add_dependency "schema_plus_views", "~> 0.1"
|
31
32
|
|
@@ -504,53 +504,53 @@ describe ActiveRecord::Migration do
|
|
504
504
|
end
|
505
505
|
|
506
506
|
it "should auto-index if specified in global options" do
|
507
|
-
|
507
|
+
SchemaPlus::ForeignKeys.config.auto_index = true
|
508
508
|
add_column(:post_id, :integer) do
|
509
509
|
expect(@model).to have_index.on(:post_id)
|
510
510
|
end
|
511
|
-
|
511
|
+
SchemaPlus::ForeignKeys.config.auto_index = false
|
512
512
|
end
|
513
513
|
|
514
514
|
it "should auto-index foreign keys only" do
|
515
|
-
|
515
|
+
SchemaPlus::ForeignKeys.config.auto_index = true
|
516
516
|
add_column(:state, :integer) do
|
517
517
|
expect(@model).not_to have_index.on(:state)
|
518
518
|
end
|
519
|
-
|
519
|
+
SchemaPlus::ForeignKeys.config.auto_index = false
|
520
520
|
end
|
521
521
|
|
522
522
|
# MySQL creates an index on foreign key and we can't override that
|
523
523
|
it "should allow to overwrite auto_index options in column definition", :mysql => :skip do
|
524
|
-
|
524
|
+
SchemaPlus::ForeignKeys.config.auto_index = true
|
525
525
|
add_column(:post_id, :integer, :index => false) do
|
526
526
|
expect(@model).not_to have_index.on(:post_id)
|
527
527
|
end
|
528
|
-
|
528
|
+
SchemaPlus::ForeignKeys.config.auto_index = false
|
529
529
|
end
|
530
530
|
|
531
531
|
it "should use default on_update action" do
|
532
|
-
|
532
|
+
SchemaPlus::ForeignKeys.config.on_update = :cascade
|
533
533
|
add_column(:post_id, :integer) do
|
534
534
|
expect(@model).to reference.on(:post_id).on_update(:cascade)
|
535
535
|
end
|
536
|
-
|
536
|
+
SchemaPlus::ForeignKeys.config.on_update = nil
|
537
537
|
end
|
538
538
|
|
539
539
|
it "should use default on_delete action" do
|
540
|
-
|
540
|
+
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
541
541
|
add_column(:post_id, :integer) do
|
542
542
|
expect(@model).to reference.on(:post_id).on_delete(:cascade)
|
543
543
|
end
|
544
|
-
|
544
|
+
SchemaPlus::ForeignKeys.config.on_delete = nil
|
545
545
|
end
|
546
546
|
|
547
547
|
it "should allow to overwrite default actions" do
|
548
|
-
|
549
|
-
|
548
|
+
SchemaPlus::ForeignKeys.config.on_delete = :cascade
|
549
|
+
SchemaPlus::ForeignKeys.config.on_update = :restrict
|
550
550
|
add_column(:post_id, :integer, :foreign_key => { :on_update => :nullify, :on_delete => :nullify}) do
|
551
551
|
expect(@model).to reference.on(:post_id).on_delete(:nullify).on_update(:nullify)
|
552
552
|
end
|
553
|
-
|
553
|
+
SchemaPlus::ForeignKeys.config.on_delete = nil
|
554
554
|
end
|
555
555
|
|
556
556
|
protected
|
@@ -4,7 +4,7 @@ require 'stringio'
|
|
4
4
|
describe "Schema dump" do
|
5
5
|
|
6
6
|
before(:all) do
|
7
|
-
|
7
|
+
SchemaPlus::ForeignKeys.setup do |config|
|
8
8
|
config.auto_create = false
|
9
9
|
end
|
10
10
|
ActiveRecord::Migration.suppress_messages do
|
@@ -113,18 +113,85 @@ describe "Schema dump" do
|
|
113
113
|
expect { dump_schema }.to_not raise_error
|
114
114
|
end
|
115
115
|
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
116
|
+
["comments", "posts", "users"].each do |table|
|
117
|
+
it "should dump constraints for table #{table.inspect} after the table definition" do
|
118
|
+
dump = dump_schema.gsub(/#[^\n*]/m, '')
|
119
|
+
expect(dump =~ %r{create_table "#{table}"}).to be < (dump =~ %r{foreign_key.*"#{table}"})
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
["comments", "posts"].each do |table|
|
124
|
+
qtable = table.inspect
|
125
|
+
it "should dump comments for delayed constraint definition referencing table #{qtable}" do
|
126
|
+
expect(dump_schema).to match(%r{# foreign key references #{qtable}.*create_table #{qtable}.*add_foreign_key \S+, #{qtable}}m)
|
127
|
+
end
|
122
128
|
end
|
123
129
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
|
130
|
+
context 'with complicated schemas' do
|
131
|
+
before(:all) do
|
132
|
+
|
133
|
+
SchemaPlus::ForeignKeys.setup do |config|
|
134
|
+
config.auto_create = false
|
135
|
+
end
|
136
|
+
ActiveRecord::Migration.suppress_messages do
|
137
|
+
ActiveRecord::Schema.define do
|
138
|
+
connection.tables.each do |table| drop_table table, :cascade => true end
|
139
|
+
|
140
|
+
create_table :grade_systems, force: true do |t|
|
141
|
+
t.string :name
|
142
|
+
t.integer :school_id
|
143
|
+
t.integer :parent_id
|
144
|
+
t.integer :profile_id
|
145
|
+
end
|
146
|
+
|
147
|
+
create_table :schools, force: true do |t|
|
148
|
+
t.string :name
|
149
|
+
t.integer :default_grade_system_id
|
150
|
+
end
|
151
|
+
|
152
|
+
create_table :academic_years, force: true do |t|
|
153
|
+
t.string :name
|
154
|
+
t.integer :school_id
|
155
|
+
end
|
156
|
+
|
157
|
+
create_table :buildings, force: true do |t|
|
158
|
+
t.string :name
|
159
|
+
t.integer :school_id
|
160
|
+
end
|
161
|
+
|
162
|
+
create_table :profiles, force: true do |t|
|
163
|
+
t.integer :school_id
|
164
|
+
t.integer :building_id
|
165
|
+
end
|
166
|
+
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
class ::AcademicYear < ActiveRecord::Base ; end
|
171
|
+
class ::Building < ActiveRecord::Base ; end
|
172
|
+
class ::GradeSystem < ActiveRecord::Base ; end
|
173
|
+
class ::Profile < ActiveRecord::Base ; end
|
174
|
+
class ::School < ActiveRecord::Base ; end
|
175
|
+
|
176
|
+
ActiveRecord::Base.connection.add_foreign_key(School.table_name, GradeSystem.table_name, column: :default_grade_system_id)
|
177
|
+
ActiveRecord::Base.connection.add_foreign_key(GradeSystem.table_name, School.table_name, column: :school_id)
|
178
|
+
ActiveRecord::Base.connection.add_foreign_key(GradeSystem.table_name, GradeSystem.table_name, column: :parent_id)
|
179
|
+
ActiveRecord::Base.connection.add_foreign_key(GradeSystem.table_name, Profile.table_name, column: :profile_id)
|
180
|
+
ActiveRecord::Base.connection.add_foreign_key(Profile.table_name, Building.table_name, column: :building_id)
|
181
|
+
ActiveRecord::Base.connection.add_foreign_key(Profile.table_name, School.table_name, column: :school_id)
|
182
|
+
ActiveRecord::Base.connection.add_foreign_key(Building.table_name, School.table_name, column: :school_id)
|
183
|
+
ActiveRecord::Base.connection.add_foreign_key(AcademicYear.table_name, School.table_name, column: :school_id)
|
184
|
+
end
|
185
|
+
|
186
|
+
it "should not raise an error" do
|
187
|
+
expect { dump_schema }.to_not raise_error
|
188
|
+
end
|
189
|
+
|
190
|
+
["buildings", "grade_systems", "profiles", "schools"].each do |table|
|
191
|
+
it "should dump constraints for table #{table.inspect} after the table definition" do
|
192
|
+
expect(dump_schema =~ %r{create_table "#{table}"}).to be < (dump_schema =~ %r{foreign_key.*"#{table}"})
|
193
|
+
end
|
194
|
+
end
|
128
195
|
end
|
129
196
|
end
|
130
197
|
|
@@ -77,22 +77,22 @@ describe ActiveRecord::Schema do
|
|
77
77
|
|
78
78
|
|
79
79
|
def with_auto_index(value = true)
|
80
|
-
old_value =
|
81
|
-
|
80
|
+
old_value = SchemaPlus::ForeignKeys.config.auto_index
|
81
|
+
SchemaPlus::ForeignKeys.config.auto_index = value
|
82
82
|
begin
|
83
83
|
yield
|
84
84
|
ensure
|
85
|
-
|
85
|
+
SchemaPlus::ForeignKeys.config.auto_index = old_value
|
86
86
|
end
|
87
87
|
end
|
88
88
|
|
89
89
|
def with_auto_create(value = true)
|
90
|
-
old_value =
|
91
|
-
|
90
|
+
old_value = SchemaPlus::ForeignKeys.config.auto_create
|
91
|
+
SchemaPlus::ForeignKeys.config.auto_create = value
|
92
92
|
begin
|
93
93
|
yield
|
94
94
|
ensure
|
95
|
-
|
95
|
+
SchemaPlus::ForeignKeys.config.auto_create = old_value
|
96
96
|
end
|
97
97
|
end
|
98
98
|
|