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.
Files changed (78) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +3 -3
  3. data/Gemfile +2 -0
  4. data/README.md +4 -4
  5. data/TODO.md +3 -10
  6. data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.base +1 -1
  7. data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.mysql2 +0 -0
  8. data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.postgresql +0 -0
  9. data/gemfiles/{rails-4.2 → activerecord-4.2}/Gemfile.sqlite3 +0 -0
  10. data/lib/schema_plus.rb +13 -121
  11. data/lib/schema_plus/version.rb +1 -1
  12. data/lib/schema_plus_columns.rb +7 -0
  13. data/lib/{schema_column_plus → schema_plus_columns}/active_record/connection_adapters/column.rb +1 -1
  14. data/lib/schema_plus_columns/middleware/model.rb +16 -0
  15. data/lib/schema_plus_db_default.rb +13 -0
  16. data/lib/{schema_db_default → schema_plus_db_default}/active_record/attribute.rb +4 -4
  17. data/lib/{schema_db_default → schema_plus_db_default}/db_default.rb +1 -1
  18. data/lib/schema_plus_db_default/middleware.rb +30 -0
  19. data/lib/{schema_default_expr.rb → schema_plus_default_expr.rb} +6 -6
  20. data/lib/{schema_default_expr → schema_plus_default_expr}/active_record/connection_adapters/mysql2_adapter.rb +1 -1
  21. data/lib/{schema_default_expr → schema_plus_default_expr}/active_record/connection_adapters/postgresql_adapter.rb +1 -1
  22. data/lib/{schema_default_expr → schema_plus_default_expr}/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
  23. data/lib/{schema_default_expr → schema_plus_default_expr}/middleware.rb +21 -19
  24. data/lib/schema_plus_enums.rb +6 -0
  25. data/lib/{schema_pg_enums → schema_plus_enums}/active_record.rb +1 -1
  26. data/lib/schema_plus_enums/middleware.rb +22 -0
  27. data/lib/schema_plus_foreign_keys.rb +96 -0
  28. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/base.rb +2 -2
  29. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/abstract_adapter.rb +7 -7
  30. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/foreign_key_definition.rb +4 -4
  31. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/mysql2_adapter.rb +9 -9
  32. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/postgresql_adapter.rb +5 -5
  33. data/lib/schema_plus_foreign_keys/active_record/connection_adapters/schema_statements.rb +33 -0
  34. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/sqlite3_adapter.rb +5 -5
  35. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/connection_adapters/table_definition.rb +8 -8
  36. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/migration/command_recorder.rb +5 -5
  37. data/lib/{schema_plus → schema_plus_foreign_keys}/active_record/schema.rb +7 -7
  38. data/lib/{schema_plus → schema_plus_foreign_keys}/middleware/dumper.rb +11 -26
  39. data/lib/{schema_plus → schema_plus_foreign_keys}/middleware/migration.rb +14 -15
  40. data/lib/schema_plus_foreign_keys/middleware/model.rb +15 -0
  41. data/lib/schema_plus_foreign_keys/version.rb +3 -0
  42. data/lib/schema_plus_views.rb +16 -0
  43. data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/abstract_adapter.rb +1 -1
  44. data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/mysql2_adapter.rb +1 -1
  45. data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/postgresql_adapter.rb +1 -1
  46. data/lib/{schema_views → schema_plus_views}/active_record/connection_adapters/sqlite3_adapter.rb +1 -1
  47. data/lib/schema_plus_views/middleware.rb +49 -0
  48. data/schema_dev.yml +1 -1
  49. data/schema_plus.gemspec +3 -2
  50. data/spec/{schema_column_plus → schema_plus_columns}/column_spec.rb +0 -0
  51. data/spec/{schema_db_default → schema_plus_db_default}/column_spec.rb +1 -1
  52. data/spec/{schema_default_expr → schema_plus_default_expr}/column_default_spec.rb +0 -0
  53. data/spec/schema_plus_default_expr/migration_spec.rb +44 -0
  54. data/spec/{schema_default_expr → schema_plus_default_expr}/schema_dumper_spec.rb +2 -2
  55. data/spec/{schema_pg_enums → schema_plus_enums}/enum_spec.rb +0 -0
  56. data/spec/{schema_pg_enums → schema_plus_enums}/schema_dumper_spec.rb +0 -0
  57. data/spec/{foreign_key_definition_spec.rb → schema_plus_foreign_keys/foreign_key_definition_spec.rb} +0 -0
  58. data/spec/{foreign_key_spec.rb → schema_plus_foreign_keys/foreign_key_spec.rb} +2 -2
  59. data/spec/{migration_spec.rb → schema_plus_foreign_keys/migration_spec.rb} +13 -29
  60. data/spec/{named_schemas_spec.rb → schema_plus_foreign_keys/named_schemas_spec.rb} +0 -0
  61. data/spec/{schema_dumper_spec.rb → schema_plus_foreign_keys/schema_dumper_spec.rb} +2 -2
  62. data/spec/{schema_spec.rb → schema_plus_foreign_keys/schema_spec.rb} +7 -7
  63. data/spec/{schema_views → schema_plus_views}/named_schemas_spec.rb +7 -7
  64. data/spec/{schema_views → schema_plus_views}/views_spec.rb +0 -0
  65. data/spec/spec_helper.rb +3 -3
  66. metadata +102 -81
  67. data/init.rb +0 -1
  68. data/lib/schema_column_plus.rb +0 -7
  69. data/lib/schema_column_plus/middleware/model.rb +0 -22
  70. data/lib/schema_db_default.rb +0 -13
  71. data/lib/schema_db_default/middleware.rb +0 -30
  72. data/lib/schema_pg_enums.rb +0 -6
  73. data/lib/schema_pg_enums/middleware.rb +0 -23
  74. data/lib/schema_plus/active_record/connection_adapters/schema_statements.rb +0 -34
  75. data/lib/schema_plus/middleware/model.rb +0 -17
  76. data/lib/schema_views.rb +0 -16
  77. data/lib/schema_views/middleware.rb +0 -47
  78. data/spec/connection_spec.rb +0 -10
@@ -1,25 +1,16 @@
1
- module SchemaPlus
1
+ module SchemaPlusForeignKeys
2
2
  module Middleware
3
3
  module Dumper
4
4
 
5
- def self.insert
6
- SchemaMonkey::Middleware::Dumper::Tables.prepend FkDependencies
7
- SchemaMonkey::Middleware::Dumper::Tables.append IgnoreActiveRecordFkDumps
8
- SchemaMonkey::Middleware::Dumper::Table.append ForeignKeys
9
- end
10
-
11
5
  # index and foreign key constraint definitions are dumped
12
6
  # inline in the create_table block. (This is done for elegance, but
13
7
  # also because Sqlite3 does not allow foreign key constraints to be
14
8
  # added to a table after it has been defined.)
15
9
 
16
- #
17
- # Middleware for the collection of tables
18
- #
10
+ module Tables
19
11
 
20
- class FkDependencies < SchemaMonkey::Middleware::Base
12
+ def before(env)
21
13
 
22
- def call(env)
23
14
  @inline_fks = Hash.new{ |h, k| h[k] = [] }
24
15
  @backref_fks = Hash.new{ |h, k| h[k] = [] }
25
16
 
@@ -39,10 +30,15 @@ module SchemaPlus
39
30
 
40
31
  env.dump.data.inline_fks = @inline_fks
41
32
  env.dump.data.backref_fks = @backref_fks
33
+ end
42
34
 
43
- continue env
35
+ # Ignore the foreign key dumps at the end of the schema; we'll put them in/near their tables
36
+ def after(env)
37
+ env.dump.foreign_keys = []
44
38
  end
45
39
 
40
+ private
41
+
46
42
  def break_fk_cycles(env) #:nodoc:
47
43
  env.dump.strongly_connected_components.select{|component| component.size > 1}.each do |tables|
48
44
  table = tables.sort.first
@@ -54,22 +50,11 @@ module SchemaPlus
54
50
  end
55
51
  end
56
52
  end
57
- end
58
53
 
59
- class IgnoreActiveRecordFkDumps < SchemaMonkey::Middleware::Base
60
- # Ignore the foreign key dumps at the end of the schema; we'll put them in/near their tables
61
- def call(env)
62
- continue env
63
- env.dump.foreign_keys = []
64
- end
65
54
  end
66
55
 
67
- #
68
- # Middleware for individual tables
69
- #
70
- class ForeignKeys < SchemaMonkey::Middleware::Base
71
- def call(env)
72
- continue env
56
+ module Table
57
+ def after(env)
73
58
  dumped = {}
74
59
  env.table.columns.each do |column|
75
60
  if (foreign_key = env.dump.data.inline_fks[env.table.name].find(&its.column.to_s == column.name))
@@ -1,14 +1,13 @@
1
- module SchemaPlus
1
+ module SchemaPlusForeignKeys
2
2
  module Middleware
3
3
  module Migration
4
4
 
5
- def self.insert
6
- SchemaMonkey::Middleware::Migration::Column.prepend Shortcuts
7
- SchemaMonkey::Middleware::Migration::Column.append AddForeignKeys
8
- end
5
+ module Column
9
6
 
10
- class Shortcuts < SchemaMonkey::Middleware::Base
11
- def call(env)
7
+ #
8
+ # Column option shortcuts
9
+ #
10
+ def before(env)
12
11
  fk_options = env.options[:foreign_key]
13
12
 
14
13
  case fk_options
@@ -37,14 +36,12 @@ module SchemaPlus
37
36
  fk_options = false if fk_options and fk_options.has_key?(:references) and not fk_options[:references]
38
37
 
39
38
  env.options[:foreign_key] = fk_options
40
-
41
- continue env
42
-
43
39
  end
44
- end
45
40
 
46
- class AddForeignKeys < SchemaMonkey::Middleware::Base
47
- def call(env)
41
+ #
42
+ # Add the foreign keys
43
+ #
44
+ def around(env)
48
45
  options = env.options
49
46
  original_options = options.dup
50
47
 
@@ -56,7 +53,7 @@ module SchemaPlus
56
53
  index = options.delete(:index) unless is_polymorphic
57
54
  options[:foreign_key] = false if is_reference
58
55
 
59
- continue env
56
+ yield env
60
57
 
61
58
  return if is_polymorphic
62
59
 
@@ -66,13 +63,15 @@ module SchemaPlus
66
63
 
67
64
  end
68
65
 
66
+ private
67
+
69
68
  def add_foreign_keys_and_auto_index(env)
70
69
 
71
70
  if (reverting = env.caller.is_a?(::ActiveRecord::Migration::CommandRecorder) && env.caller.reverting)
72
71
  commands_length = env.caller.commands.length
73
72
  end
74
73
 
75
- config = (env.caller.try(:schema_plus_config) || SchemaPlus.config).foreign_keys
74
+ config = (env.caller.try(:schema_plus_config) || SchemaPlusForeignKeys.config)
76
75
  fk_args = get_fk_args(env, config)
77
76
 
78
77
  # remove existing fk and auto-generated index in case of change of fk on existing column
@@ -0,0 +1,15 @@
1
+ module SchemaPlusForeignKeys
2
+ module Middleware
3
+
4
+ module Model
5
+ module ResetColumnInformation
6
+
7
+ def after(env)
8
+ env.model.reset_foreign_key_information
9
+ end
10
+
11
+ end
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ module SchemaPlusForeignKeys
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,16 @@
1
+ require 'schema_monkey'
2
+
3
+ require_relative 'schema_plus_views/active_record/connection_adapters/abstract_adapter'
4
+ require_relative 'schema_plus_views/middleware'
5
+
6
+ module SchemaPlusViews
7
+ module ActiveRecord
8
+ module ConnectionAdapters
9
+ autoload :Mysql2Adapter, 'schema_plus_views/active_record/connection_adapters/mysql2_adapter'
10
+ autoload :PostgresqlAdapter, 'schema_plus_views/active_record/connection_adapters/postgresql_adapter'
11
+ autoload :Sqlite3Adapter, 'schema_plus_views/active_record/connection_adapters/sqlite3_adapter'
12
+ end
13
+ end
14
+ end
15
+
16
+ SchemaMonkey.register(SchemaPlusViews)
@@ -1,4 +1,4 @@
1
- module SchemaViews
1
+ module SchemaPlusViews
2
2
  module ActiveRecord
3
3
  module ConnectionAdapters
4
4
  module AbstractAdapter
@@ -1,4 +1,4 @@
1
- module SchemaViews
1
+ module SchemaPlusViews
2
2
  module ActiveRecord
3
3
  module ConnectionAdapters
4
4
  module Mysql2Adapter
@@ -1,4 +1,4 @@
1
- module SchemaViews
1
+ module SchemaPlusViews
2
2
  module ActiveRecord
3
3
  module ConnectionAdapters
4
4
  module PostgresqlAdapter
@@ -1,4 +1,4 @@
1
- module SchemaViews
1
+ module SchemaPlusViews
2
2
  module ActiveRecord
3
3
  module ConnectionAdapters
4
4
  module Sqlite3Adapter
@@ -0,0 +1,49 @@
1
+ module SchemaPlusViews
2
+ module Middleware
3
+
4
+ module Dumper
5
+ module Tables
6
+
7
+ # Dump views
8
+ def after(env)
9
+ re_view_referent = %r{(?:(?i)FROM|JOIN) \S*\b(\S+)\b}
10
+ env.connection.views.each do |view_name|
11
+ next if env.dumper.ignored?(view_name)
12
+ view = View.new(name: view_name, definition: env.connection.view_definition(view_name))
13
+ env.dump.tables[view.name] = view
14
+ env.dump.depends(view.name, view.definition.scan(re_view_referent).flatten)
15
+ end
16
+ end
17
+
18
+ # quacks like a SchemaMonkey Dump::Table
19
+ class View < KeyStruct[:name, :definition]
20
+ def assemble(stream)
21
+ stream.puts(" create_view #{name.inspect}, #{definition.inspect}, :force => true\n")
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ module Query
28
+ module Tables
29
+
30
+ module Mysql
31
+ def after(env)
32
+ Tables.filter_out_views(env)
33
+ end
34
+ end
35
+
36
+ module Sqlite3
37
+ def after(env)
38
+ Tables.filter_out_views(env)
39
+ end
40
+ end
41
+
42
+ def self.filter_out_views(env)
43
+ env.tables -= env.connection.views(env.query_name)
44
+ end
45
+ end
46
+ end
47
+ end
48
+
49
+ end
data/schema_dev.yml CHANGED
@@ -1,7 +1,7 @@
1
1
  ruby:
2
2
  - 1.9.3
3
3
  - 2.1.5
4
- rails:
4
+ activerecord:
5
5
  - 4.2
6
6
  db:
7
7
  - mysql2
data/schema_plus.gemspec CHANGED
@@ -23,11 +23,12 @@ Gem::Specification.new do |s|
23
23
 
24
24
  s.add_dependency "activerecord", "~> 4.2"
25
25
  s.add_dependency "valuable"
26
- s.add_dependency "schema_monkey", "~> 0.4"
26
+ s.add_dependency "schema_monkey", "~> 1.0", ">= 1.0.1"
27
+ s.add_dependency "schema_monkey_rails", "~> 0.1", ">= 0.1.1"
27
28
  s.add_dependency "schema_plus_indexes", "~> 0.1"
28
29
  s.add_dependency "schema_plus_pg_indexes", "~> 0.1"
29
30
 
30
- s.add_development_dependency "schema_dev", "~> 2.0", ">= 2.0.3"
31
+ s.add_development_dependency "schema_dev", "~> 3.0"
31
32
  s.add_development_dependency "rake"
32
33
  s.add_development_dependency "rspec", "~> 3.0.0"
33
34
  s.add_development_dependency "rdoc"
@@ -1,6 +1,6 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe SchemaDbDefault do
3
+ describe SchemaPlusDbDefault do
4
4
 
5
5
  let(:migration) { ::ActiveRecord::Migration }
6
6
 
@@ -0,0 +1,44 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveRecord::Migration do
4
+
5
+ before(:each) do
6
+ define_schema(:auto_create => true) do
7
+ create_table :posts, :force => true do |t|
8
+ t.string :content
9
+ end
10
+ end
11
+ class Post < ::ActiveRecord::Base ; end
12
+ end
13
+
14
+ context "when table is created" do
15
+
16
+ before(:each) do
17
+ @model = Post
18
+ end
19
+
20
+ it "should properly handle default values for booleans" do
21
+ expect {
22
+ recreate_table(@model) do |t|
23
+ t.boolean :bool, :default => true
24
+ end
25
+ }.to_not raise_error
26
+ expect(@model.create.reload.bool).to be true
27
+ end
28
+
29
+ it "should properly handle default values for json (#195)", :postgresql => :only do
30
+ recreate_table(@model) do |t|
31
+ t.json :json, :default => {}
32
+ end
33
+ expect(@model.create.reload.json).to eq({})
34
+ end
35
+
36
+ end
37
+
38
+ def recreate_table(model, opts={}, &block)
39
+ ActiveRecord::Migration.suppress_messages do
40
+ ActiveRecord::Migration.create_table model.table_name, opts.merge(:force => true), &block
41
+ end
42
+ model.reset_column_information
43
+ end
44
+ end
@@ -4,8 +4,8 @@ require 'stringio'
4
4
  describe "Schema dump" do
5
5
 
6
6
  before(:all) do
7
- SchemaPlus.setup do |config|
8
- config.foreign_keys.auto_create = false
7
+ SchemaPlusForeignKeys.setup do |config|
8
+ config.auto_create = false
9
9
  end
10
10
  ActiveRecord::Migration.suppress_messages do
11
11
  ActiveRecord::Schema.define do
@@ -1,4 +1,4 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
1
+ require 'spec_helper'
2
2
 
3
3
  describe "Foreign Key" do
4
4
 
@@ -32,7 +32,7 @@ describe "Foreign Key" do
32
32
  context "with modifications to SQL generated by upstream visit_TableDefinition" do
33
33
  before(:each) do
34
34
  allow_any_instance_of(ActiveRecord::Base.connection.class.const_get(:SchemaCreation))
35
- .to receive(:visit_TableDefinition_without_schema_plus)
35
+ .to receive(:visit_TableDefinition_without_schema_plus_foreign_keys)
36
36
  .and_return('this is unexpected')
37
37
  end
38
38
 
@@ -40,22 +40,6 @@ describe ActiveRecord::Migration do
40
40
  @model = Post
41
41
  end
42
42
 
43
- it "should properly handle default values for booleans" do
44
- expect {
45
- recreate_table(@model) do |t|
46
- t.boolean :bool, :default => true
47
- end
48
- }.to_not raise_error
49
- expect(@model.create.reload.bool).to be true
50
- end
51
-
52
- it "should properly handle default values for json (#195)", :postgresql => :only do
53
- recreate_table(@model) do |t|
54
- t.json :json, :default => {}
55
- end
56
- expect(@model.create.reload.json).to eq({})
57
- end if ActiveRecord::VERSION::MAJOR >= 4
58
-
59
43
  it "should create auto foreign keys" do
60
44
  recreate_table(@model) do |t|
61
45
  t.integer :user_id
@@ -520,53 +504,53 @@ describe ActiveRecord::Migration do
520
504
  end
521
505
 
522
506
  it "should auto-index if specified in global options" do
523
- SchemaPlus.config.foreign_keys.auto_index = true
507
+ SchemaPlusForeignKeys.config.auto_index = true
524
508
  add_column(:post_id, :integer) do
525
509
  expect(@model).to have_index.on(:post_id)
526
510
  end
527
- SchemaPlus.config.foreign_keys.auto_index = false
511
+ SchemaPlusForeignKeys.config.auto_index = false
528
512
  end
529
513
 
530
514
  it "should auto-index foreign keys only" do
531
- SchemaPlus.config.foreign_keys.auto_index = true
515
+ SchemaPlusForeignKeys.config.auto_index = true
532
516
  add_column(:state, :integer) do
533
517
  expect(@model).not_to have_index.on(:state)
534
518
  end
535
- SchemaPlus.config.foreign_keys.auto_index = false
519
+ SchemaPlusForeignKeys.config.auto_index = false
536
520
  end
537
521
 
538
522
  # MySQL creates an index on foreign key and we can't override that
539
523
  it "should allow to overwrite auto_index options in column definition", :mysql => :skip do
540
- SchemaPlus.config.foreign_keys.auto_index = true
524
+ SchemaPlusForeignKeys.config.auto_index = true
541
525
  add_column(:post_id, :integer, :index => false) do
542
526
  expect(@model).not_to have_index.on(:post_id)
543
527
  end
544
- SchemaPlus.config.foreign_keys.auto_index = false
528
+ SchemaPlusForeignKeys.config.auto_index = false
545
529
  end
546
530
 
547
531
  it "should use default on_update action" do
548
- SchemaPlus.config.foreign_keys.on_update = :cascade
532
+ SchemaPlusForeignKeys.config.on_update = :cascade
549
533
  add_column(:post_id, :integer) do
550
534
  expect(@model).to reference.on(:post_id).on_update(:cascade)
551
535
  end
552
- SchemaPlus.config.foreign_keys.on_update = nil
536
+ SchemaPlusForeignKeys.config.on_update = nil
553
537
  end
554
538
 
555
539
  it "should use default on_delete action" do
556
- SchemaPlus.config.foreign_keys.on_delete = :cascade
540
+ SchemaPlusForeignKeys.config.on_delete = :cascade
557
541
  add_column(:post_id, :integer) do
558
542
  expect(@model).to reference.on(:post_id).on_delete(:cascade)
559
543
  end
560
- SchemaPlus.config.foreign_keys.on_delete = nil
544
+ SchemaPlusForeignKeys.config.on_delete = nil
561
545
  end
562
546
 
563
547
  it "should allow to overwrite default actions" do
564
- SchemaPlus.config.foreign_keys.on_delete = :cascade
565
- SchemaPlus.config.foreign_keys.on_update = :restrict
548
+ SchemaPlusForeignKeys.config.on_delete = :cascade
549
+ SchemaPlusForeignKeys.config.on_update = :restrict
566
550
  add_column(:post_id, :integer, :foreign_key => { :on_update => :nullify, :on_delete => :nullify}) do
567
551
  expect(@model).to reference.on(:post_id).on_delete(:nullify).on_update(:nullify)
568
552
  end
569
- SchemaPlus.config.foreign_keys.on_delete = nil
553
+ SchemaPlusForeignKeys.config.on_delete = nil
570
554
  end
571
555
 
572
556
  protected