schema_plus_core 0.3.1 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/schema_plus/core/active_record/connection_adapters/abstract_adapter.rb +3 -2
- data/lib/schema_plus/core/active_record/connection_adapters/table_definition.rb +8 -5
- data/lib/schema_plus/core/middleware.rb +1 -1
- data/lib/schema_plus/core/version.rb +1 -1
- data/spec/column_spec.rb +100 -0
- data/spec/support/enableable.rb +6 -2
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c31ad40eecda53151aef2e6b0faac2a6f99385e
|
4
|
+
data.tar.gz: eb7b4fa4146a8d2b6746391564b011f77f366b9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 90aad35397d7224e1ea32e712f27f4c1597b562245e9b6cdddb7d15add80e772d61bfbdfb7584b41e63192794ee110c354798cc119237870ac3dd3972bcf42dd
|
7
|
+
data.tar.gz: 2fbdbb53cd67cf6830caba374d039c7e4e1419cb1986008005f04363566ad794b47146d8aa86c139454b4177c69e611680fff25d5678245d6fc5646b8a5b82a2
|
data/README.md
CHANGED
@@ -192,6 +192,7 @@ Stacks for operations that change the schema. In some cases the operation immed
|
|
192
192
|
`:table_name` | The name of the table | *arg*
|
193
193
|
`:column_name` | The name of the column | *arg*
|
194
194
|
`:type` | The ActiveRecord column type (`:integer`, `:datetime`, etc.) | *arg*
|
195
|
+
`:implements_reference` | This implements a `migration.add_reference`, `t.references` or `t.belongs_to` [See below]| *context*
|
195
196
|
`:options` | The column options | *arg*, default `{}`
|
196
197
|
|
197
198
|
The base implementation performs the column operation. No value is returned.
|
@@ -207,7 +208,9 @@ Stacks for operations that change the schema. In some cases the operation immed
|
|
207
208
|
|
208
209
|
2. In the case of a table definition using `t.references` or `t.belongs_to`, the `:type` field will be set to `:reference` and the `:column_name` will include the `"_id"` suffix
|
209
210
|
|
210
|
-
3. ActiveRecord's base implementation
|
211
|
+
3. ActiveRecord's base implementation handles `migration.add_reference`, `t.references` and `t.belongs_to` by making nested calls to `migration.add_column` or `t.column` to create the resulting column (or two columns, for polymorphic references). SchemaPlus::Core invokes the `Migration::Column` stack for both the outer `migration.add_reference`, `t.references` or `t.belongs_to` call, as well as for the nested `migration.add_column` or `t.column` call; in the nested call, `env.implements_reference` will be truthy.
|
212
|
+
|
213
|
+
4. Sqlite3 implements `change_column` by a creating a new table. This will result in nested calls to `add_column`, invoking the `Migration::Column` stack for each; SchemaPlus::Core does not currently provide a way to distinguish those calls from explicit top-level calls.
|
211
214
|
|
212
215
|
* `Migration::CreateTable`
|
213
216
|
|
@@ -435,6 +438,7 @@ SchemaPlus::Core provides a state object and of callbacks to various phases of t
|
|
435
438
|
|
436
439
|
## History
|
437
440
|
|
441
|
+
* 0.4.0 Add `implements_reference` to `Migration::Column` stack env
|
438
442
|
* 0.3.1 Pass along (undocumented) return values from association declarations
|
439
443
|
* 0.3.0 Added `Model::Association::Declaration`
|
440
444
|
* 0.2.1 Added `Migration::CreateTable` and `Schema::Define`; removed dependency on (defunct) `schema_monkey_rails` gem. [Oops, this should have been a minor version bump]
|
@@ -5,7 +5,8 @@ module SchemaPlus
|
|
5
5
|
module AbstractAdapter
|
6
6
|
|
7
7
|
def add_column(table_name, name, type, options = {})
|
8
|
-
|
8
|
+
options = options.deep_dup
|
9
|
+
SchemaMonkey::Middleware::Migration::Column.start(caller: self, operation: :add, table_name: table_name, column_name: name, type: type, implements_reference: options.delete(:_implements_reference), options: options) do |env|
|
9
10
|
super env.table_name, env.column_name, env.type, env.options
|
10
11
|
end
|
11
12
|
end
|
@@ -18,7 +19,7 @@ module SchemaPlus
|
|
18
19
|
|
19
20
|
def add_reference(table_name, name, options = {})
|
20
21
|
SchemaMonkey::Middleware::Migration::Column.start(caller: self, operation: :add, table_name: table_name, column_name: "#{name}_id", type: :reference, options: options.deep_dup) do |env|
|
21
|
-
super env.table_name, env.column_name.sub(/_id$/, ''), env.options
|
22
|
+
super env.table_name, env.column_name.sub(/_id$/, ''), env.options.merge(_implements_reference: true)
|
22
23
|
end
|
23
24
|
end
|
24
25
|
|
@@ -5,20 +5,23 @@ module SchemaPlus
|
|
5
5
|
module TableDefinition
|
6
6
|
|
7
7
|
def column(name, type, options = {})
|
8
|
-
|
8
|
+
options = options.deep_dup
|
9
|
+
SchemaMonkey::Middleware::Migration::Column.start(caller: self, operation: :define, table_name: self.name, column_name: name, type: type, implements_reference: options.delete(:_implements_reference), options: options) do |env|
|
9
10
|
super env.column_name, env.type, env.options
|
10
11
|
end
|
11
12
|
end
|
12
13
|
|
13
14
|
def references(name, options = {})
|
14
|
-
|
15
|
-
|
15
|
+
options = options.deep_dup
|
16
|
+
SchemaMonkey::Middleware::Migration::Column.start(caller: self, operation: :define, table_name: self.name, column_name: "#{name}_id", type: :reference, options: options) do |env|
|
17
|
+
super env.column_name.sub(/_id$/, ''), env.options.merge(_implements_reference: true)
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
21
|
def belongs_to(name, options = {})
|
20
|
-
|
21
|
-
|
22
|
+
options = options.deep_dup
|
23
|
+
SchemaMonkey::Middleware::Migration::Column.start(caller: self, operation: :define, table_name: self.name, column_name: "#{name}_id", type: :reference, options: options) do |env|
|
24
|
+
super env.column_name.sub(/_id$/, ''), env.options.merge(_implements_reference: true)
|
22
25
|
end
|
23
26
|
end
|
24
27
|
|
@@ -26,7 +26,7 @@ module SchemaPlus
|
|
26
26
|
|
27
27
|
module Migration
|
28
28
|
module Column
|
29
|
-
ENV = [:caller, :operation, :table_name, :column_name, :type, :options]
|
29
|
+
ENV = [:caller, :operation, :table_name, :column_name, :type, :implements_reference, :options]
|
30
30
|
end
|
31
31
|
|
32
32
|
module CreateTable
|
data/spec/column_spec.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module TestImplementsReference
|
4
|
+
module Middleware
|
5
|
+
module Migration
|
6
|
+
module Column
|
7
|
+
SPY = []
|
8
|
+
|
9
|
+
include Enableable
|
10
|
+
|
11
|
+
def before(env)
|
12
|
+
return unless middleware = enabled_middleware(TestImplementsReference, env)
|
13
|
+
SPY << env.to_hash.slice(:column_name, :type, :implements_reference)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
SchemaMonkey.register TestImplementsReference
|
21
|
+
|
22
|
+
describe SchemaMonkey::Middleware::Migration::Column do
|
23
|
+
|
24
|
+
let(:migration) { ::ActiveRecord::Migration }
|
25
|
+
|
26
|
+
context TestImplementsReference::Middleware::Migration::Column do
|
27
|
+
let (:spy) { described_class.const_get(:SPY) }
|
28
|
+
|
29
|
+
around(:each) do |example|
|
30
|
+
middleware = described_class
|
31
|
+
begin
|
32
|
+
spy.clear
|
33
|
+
middleware.enable once: false
|
34
|
+
example.run
|
35
|
+
ensure
|
36
|
+
middleware.disable
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
context "when add ordinary column" do
|
41
|
+
When { migration.create_table("things") { |t| t.integer "test_column" } }
|
42
|
+
Then { expect(spy).to eq [
|
43
|
+
{ column_name: "id", type: :primary_key, implements_reference: nil },
|
44
|
+
{ column_name: "test_column", type: :integer, implements_reference: nil }
|
45
|
+
] }
|
46
|
+
end
|
47
|
+
|
48
|
+
[:references, :belongs_to].each do |method|
|
49
|
+
|
50
|
+
context "when add reference using t.#{method}" do
|
51
|
+
When { migration.create_table("things") { |t| t.send method, "test_reference" } }
|
52
|
+
Then { expect(spy).to eq [
|
53
|
+
{ column_name: "id", type: :primary_key, implements_reference: nil },
|
54
|
+
{ column_name: "test_reference_id", type: :reference, implements_reference: nil },
|
55
|
+
{ column_name: "test_reference_id", type: :integer, implements_reference: true }
|
56
|
+
] }
|
57
|
+
end
|
58
|
+
|
59
|
+
context "when add polymorphic reference using t.#{method}" do
|
60
|
+
When { migration.create_table("things") { |t| t.send method, "test_reference", polymorphic: true } }
|
61
|
+
Then { expect(spy).to eq [
|
62
|
+
{ column_name: "id", type: :primary_key, implements_reference: nil },
|
63
|
+
{ column_name: "test_reference_id", type: :reference, implements_reference: nil },
|
64
|
+
{ column_name: "test_reference_id", type: :integer, implements_reference: true },
|
65
|
+
{ column_name: "test_reference_type", type: :string, implements_reference: true }
|
66
|
+
] }
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
context "with an existing table" do
|
71
|
+
Given {
|
72
|
+
migration.create_table("things") { |t| }
|
73
|
+
spy.clear
|
74
|
+
}
|
75
|
+
|
76
|
+
context "when add reference using migration.add_reference" do
|
77
|
+
|
78
|
+
When { migration.add_reference("things", "test_reference") }
|
79
|
+
Then { expect(spy).to eq [
|
80
|
+
{ column_name: "test_reference_id", type: :reference, implements_reference: nil },
|
81
|
+
{ column_name: "test_reference_id", type: :integer, implements_reference: true }
|
82
|
+
] }
|
83
|
+
end
|
84
|
+
|
85
|
+
context "when add polymorphic reference using migration.add_reference" do
|
86
|
+
|
87
|
+
When {
|
88
|
+
migration.add_reference("things", "test_reference", polymorphic: true)
|
89
|
+
}
|
90
|
+
Then { expect(spy).to eq [
|
91
|
+
{ column_name: "test_reference_id", type: :reference, implements_reference: nil },
|
92
|
+
{ column_name: "test_reference_id", type: :integer, implements_reference: true },
|
93
|
+
{ column_name: "test_reference_type", type: :string, implements_reference: true }
|
94
|
+
] }
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
100
|
+
end
|
data/spec/support/enableable.rb
CHANGED
@@ -3,14 +3,15 @@ module Enableable
|
|
3
3
|
def enabled_middleware(root, env)
|
4
4
|
middleware = self.singleton_class.ancestors.find(&it.to_s.start_with?("#{root}::Middleware"))
|
5
5
|
return nil unless middleware.enabled?(env)
|
6
|
-
middleware.disable
|
6
|
+
middleware.disable if middleware.once?(env)
|
7
7
|
middleware
|
8
8
|
end
|
9
9
|
|
10
10
|
def self.included(base)
|
11
11
|
base.module_eval do
|
12
|
-
def self.enable(condition = true)
|
12
|
+
def self.enable(condition = true, once:true)
|
13
13
|
@enabled = condition
|
14
|
+
@once = once
|
14
15
|
end
|
15
16
|
def self.enabled?(env)
|
16
17
|
case @enabled
|
@@ -18,6 +19,9 @@ module Enableable
|
|
18
19
|
else @enabled
|
19
20
|
end
|
20
21
|
end
|
22
|
+
def self.once?(env)
|
23
|
+
@once
|
24
|
+
end
|
21
25
|
def self.disable
|
22
26
|
@enabled = false
|
23
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_plus_core
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ronen barzel
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-05-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -172,6 +172,7 @@ files:
|
|
172
172
|
- lib/schema_plus/core/version.rb
|
173
173
|
- schema_dev.yml
|
174
174
|
- schema_plus_core.gemspec
|
175
|
+
- spec/column_spec.rb
|
175
176
|
- spec/dumper_spec.rb
|
176
177
|
- spec/middleware_spec.rb
|
177
178
|
- spec/spec_helper.rb
|
@@ -204,6 +205,7 @@ signing_key:
|
|
204
205
|
specification_version: 4
|
205
206
|
summary: Provides an internal extension API to ActiveRecord
|
206
207
|
test_files:
|
208
|
+
- spec/column_spec.rb
|
207
209
|
- spec/dumper_spec.rb
|
208
210
|
- spec/middleware_spec.rb
|
209
211
|
- spec/spec_helper.rb
|