schema_plus 2.0.0.pre11 → 2.0.0.pre12
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -31
- data/lib/schema_plus/version.rb +1 -1
- data/schema_plus.gemspec +1 -0
- metadata +16 -14
- data/lib/schema_plus/default_expr.rb +0 -33
- data/lib/schema_plus/default_expr/active_record/connection_adapters/column.rb +0 -14
- data/lib/schema_plus/default_expr/active_record/connection_adapters/mysql2_adapter.rb +0 -17
- data/lib/schema_plus/default_expr/active_record/connection_adapters/postgresql_adapter.rb +0 -18
- data/lib/schema_plus/default_expr/active_record/connection_adapters/sqlite3_adapter.rb +0 -19
- data/lib/schema_plus/default_expr/middleware.rb +0 -55
- data/spec/schema_plus_default_expr/column_default_spec.rb +0 -149
- data/spec/schema_plus_default_expr/migration_spec.rb +0 -44
- data/spec/schema_plus_default_expr/schema_dumper_spec.rb +0 -116
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6212de06ef0629d376189de1d623b4ad52969b56
|
4
|
+
data.tar.gz: e7440ee39d1624b786e39da1245312f7ca61497f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 58105ec47fdea7b6460c2a5f1dad67f5e0fa16f0fc615d6a9e42def7cc83600b2ce334c76f043cb667d13f9defa27cdfdff94bf277399b99611ad30a5f98c45e
|
7
|
+
data.tar.gz: 88d0d8a42cfc9a95697816490acb3471e8ebdaa7f14dd956e18306ee67df90f4ec3d387696e488d669d2e1fa84415c8dd6bedcb9cf8725f26d50d6b32021fbed
|
data/README.md
CHANGED
@@ -13,7 +13,7 @@ Gem | Description | Included In `schema_plus` gem?
|
|
13
13
|
<p style="color:grey">schema_auto_foreign_keys</p> | Automatically creates foreign keys on referencing columns | Y
|
14
14
|
[schema_plus_columns](https://github.com/SchemaPlus/schema_plus_columns) | Column attributes including `column.indexes` and `column.unique?` | Y
|
15
15
|
[schema_plus_db_default](https://github.com/SchemaPlus/schema_plus_db_default) | Use `ActiveRecord::DB_DEFAULT` to set an attribute to the database default | Y
|
16
|
-
|
16
|
+
[schema_plus_default_expr](https://github.com/SchemaPlus/schema_plus_default_expr) | Use SQL expressions for database default values | Y
|
17
17
|
[schema_plus_enums](https://github.com/SchemaPlus/schema_plus_enums) | Define enum types in migrations | Y
|
18
18
|
<p style="color:grey">schema_plus_foreign_keys | Extended support for foreign keys, including creation as column options, `:deferrable`, and SQLite3 support | Y
|
19
19
|
[schema_plus_indexes](https://github.com/SchemaPlus/schema_plus_indexes) | Convenience and consistency in using indexes | Y
|
@@ -186,36 +186,6 @@ has some information that may be of assistance in resolving these issues.
|
|
186
186
|
|
187
187
|
SchemaPlus extends `create_table ... force: true` to use `:cascade`
|
188
188
|
|
189
|
-
### Column Defaults: Expressions
|
190
|
-
|
191
|
-
SchemaPlus allows defaults to be set using expressions or constant values:
|
192
|
-
|
193
|
-
t.datetime :seen_at, default: { expr: 'NOW()' }
|
194
|
-
t.datetime :seen_at, default: { value: "2011-12-11 00:00:00" }
|
195
|
-
|
196
|
-
Note that in MySQL only the TIMESTAMP column data type accepts SQL column
|
197
|
-
defaults and Rails uses DATETIME, so expressions can't be used with MySQL.
|
198
|
-
|
199
|
-
The standard syntax will still work as usual:
|
200
|
-
|
201
|
-
t.datetime :seen_at, default: "2011-12-11 00:00:00"
|
202
|
-
|
203
|
-
Also, as a convenience
|
204
|
-
|
205
|
-
t.datetime :seen_at, default: :now
|
206
|
-
|
207
|
-
resolves to:
|
208
|
-
|
209
|
-
NOW() # PostgreSQL
|
210
|
-
(DATETIME('now')) # SQLite3
|
211
|
-
invalid # MySQL
|
212
|
-
|
213
|
-
If you are using Postgresql with a `json` column, the default value may be an unadorned hash. A hash having just one key `:expr` or `:value` will be taken as schema_plus syntax; i.e, these two are equivalent:
|
214
|
-
|
215
|
-
t.json :fields, default: { field1: 'a', field2: 'b' }
|
216
|
-
t.json :fields, default: { value: { field1: 'a', field2: 'b' } }
|
217
|
-
|
218
|
-
|
219
189
|
### Schema Dump and Load (schema.rb)
|
220
190
|
|
221
191
|
When dumping `schema.rb`, SchemaPlus orders the views and tables in the schema
|
data/lib/schema_plus/version.rb
CHANGED
data/schema_plus.gemspec
CHANGED
@@ -28,6 +28,7 @@ Gem::Specification.new do |gem|
|
|
28
28
|
gem.add_dependency "schema_plus_columns", "~> 0.1"
|
29
29
|
gem.add_dependency "schema_plus_enums", "~> 0.1"
|
30
30
|
gem.add_dependency "schema_plus_db_default", "~> 0.1"
|
31
|
+
gem.add_dependency "schema_plus_default_expr", "~> 0.1"
|
31
32
|
gem.add_dependency "schema_plus_indexes", "~> 0.1", ">= 0.1.3"
|
32
33
|
gem.add_dependency "schema_plus_pg_indexes", "~> 0.1", ">= 0.1.3"
|
33
34
|
gem.add_dependency "schema_plus_tables", "~> 0.1"
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: schema_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.0.0.
|
4
|
+
version: 2.0.0.pre12
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronen Barzel
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2015-
|
12
|
+
date: 2015-04-01 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -109,6 +109,20 @@ dependencies:
|
|
109
109
|
- - "~>"
|
110
110
|
- !ruby/object:Gem::Version
|
111
111
|
version: '0.1'
|
112
|
+
- !ruby/object:Gem::Dependency
|
113
|
+
name: schema_plus_default_expr
|
114
|
+
requirement: !ruby/object:Gem::Requirement
|
115
|
+
requirements:
|
116
|
+
- - "~>"
|
117
|
+
- !ruby/object:Gem::Version
|
118
|
+
version: '0.1'
|
119
|
+
type: :runtime
|
120
|
+
prerelease: false
|
121
|
+
version_requirements: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - "~>"
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '0.1'
|
112
126
|
- !ruby/object:Gem::Dependency
|
113
127
|
name: schema_plus_indexes
|
114
128
|
requirement: !ruby/object:Gem::Requirement
|
@@ -290,12 +304,6 @@ files:
|
|
290
304
|
- gemfiles/activerecord-4.2.1/Gemfile.postgresql
|
291
305
|
- gemfiles/activerecord-4.2.1/Gemfile.sqlite3
|
292
306
|
- lib/schema_plus.rb
|
293
|
-
- lib/schema_plus/default_expr.rb
|
294
|
-
- lib/schema_plus/default_expr/active_record/connection_adapters/column.rb
|
295
|
-
- lib/schema_plus/default_expr/active_record/connection_adapters/mysql2_adapter.rb
|
296
|
-
- lib/schema_plus/default_expr/active_record/connection_adapters/postgresql_adapter.rb
|
297
|
-
- lib/schema_plus/default_expr/active_record/connection_adapters/sqlite3_adapter.rb
|
298
|
-
- lib/schema_plus/default_expr/middleware.rb
|
299
307
|
- lib/schema_plus/foreign_keys.rb
|
300
308
|
- lib/schema_plus/foreign_keys/active_record/base.rb
|
301
309
|
- lib/schema_plus/foreign_keys/active_record/connection_adapters/abstract_adapter.rb
|
@@ -317,9 +325,6 @@ files:
|
|
317
325
|
- schema_plus.gemspec
|
318
326
|
- spec/schema/auto_schema.rb
|
319
327
|
- spec/schema/core_schema.rb
|
320
|
-
- spec/schema_plus_default_expr/column_default_spec.rb
|
321
|
-
- spec/schema_plus_default_expr/migration_spec.rb
|
322
|
-
- spec/schema_plus_default_expr/schema_dumper_spec.rb
|
323
328
|
- spec/schema_plus_foreign_keys/foreign_key_definition_spec.rb
|
324
329
|
- spec/schema_plus_foreign_keys/foreign_key_spec.rb
|
325
330
|
- spec/schema_plus_foreign_keys/migration_spec.rb
|
@@ -359,9 +364,6 @@ summary: Enhances ActiveRecord schema mechanism, including more DRY index creati
|
|
359
364
|
test_files:
|
360
365
|
- spec/schema/auto_schema.rb
|
361
366
|
- spec/schema/core_schema.rb
|
362
|
-
- spec/schema_plus_default_expr/column_default_spec.rb
|
363
|
-
- spec/schema_plus_default_expr/migration_spec.rb
|
364
|
-
- spec/schema_plus_default_expr/schema_dumper_spec.rb
|
365
367
|
- spec/schema_plus_foreign_keys/foreign_key_definition_spec.rb
|
366
368
|
- spec/schema_plus_foreign_keys/foreign_key_spec.rb
|
367
369
|
- spec/schema_plus_foreign_keys/migration_spec.rb
|
@@ -1,33 +0,0 @@
|
|
1
|
-
require 'schema_plus/core'
|
2
|
-
|
3
|
-
require_relative 'default_expr/middleware'
|
4
|
-
require_relative 'default_expr/active_record/connection_adapters/column'
|
5
|
-
|
6
|
-
module SchemaPlus::DefaultExpr
|
7
|
-
module ActiveRecord
|
8
|
-
module ConnectionAdapters
|
9
|
-
#
|
10
|
-
# Each adapter needs to define these two functions:
|
11
|
-
#
|
12
|
-
# default_expr_valid?(expr)
|
13
|
-
#
|
14
|
-
# Return true if the passed expression can be used as a column
|
15
|
-
# default value. (For most databases the specific expression
|
16
|
-
# doesn't matter, and the adapter's function would return a
|
17
|
-
# constant true if default expressions are supported or false if
|
18
|
-
# they're not.)
|
19
|
-
#
|
20
|
-
# sql_for_function(function_name)
|
21
|
-
#
|
22
|
-
# Return SQL definition for a given canonical function_name symbol.
|
23
|
-
# Currently, the only function to support is :now, which should
|
24
|
-
# return a DATETIME object for the current time.
|
25
|
-
#
|
26
|
-
autoload :Mysql2Adapter, 'schema_plus/default_expr/active_record/connection_adapters/mysql2_adapter'
|
27
|
-
autoload :PostgresqlAdapter, 'schema_plus/default_expr/active_record/connection_adapters/postgresql_adapter'
|
28
|
-
autoload :Sqlite3Adapter, 'schema_plus/default_expr/active_record/connection_adapters/sqlite3_adapter'
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
SchemaMonkey.register SchemaPlus::DefaultExpr
|
@@ -1,17 +0,0 @@
|
|
1
|
-
module SchemaPlus::DefaultExpr
|
2
|
-
module ActiveRecord
|
3
|
-
module ConnectionAdapters
|
4
|
-
module Mysql2Adapter
|
5
|
-
def default_expr_valid?(expr)
|
6
|
-
false # only the TIMESTAMP column accepts SQL column defaults and rails uses DATETIME
|
7
|
-
end
|
8
|
-
|
9
|
-
def sql_for_function(function)
|
10
|
-
case function
|
11
|
-
when :now then 'CURRENT_TIMESTAMP'
|
12
|
-
end
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
@@ -1,18 +0,0 @@
|
|
1
|
-
module SchemaPlus::DefaultExpr
|
2
|
-
module ActiveRecord
|
3
|
-
module ConnectionAdapters
|
4
|
-
module PostgresqlAdapter
|
5
|
-
def default_expr_valid?(expr)
|
6
|
-
true # arbitrary sql is okay in PostgreSQL
|
7
|
-
end
|
8
|
-
|
9
|
-
def sql_for_function(function)
|
10
|
-
case function
|
11
|
-
when :now
|
12
|
-
"NOW()"
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
@@ -1,19 +0,0 @@
|
|
1
|
-
module SchemaPlus::DefaultExpr
|
2
|
-
module ActiveRecord
|
3
|
-
module ConnectionAdapters
|
4
|
-
module Sqlite3Adapter
|
5
|
-
|
6
|
-
def default_expr_valid?(expr)
|
7
|
-
true # arbitrary sql is okay
|
8
|
-
end
|
9
|
-
|
10
|
-
def sql_for_function(function)
|
11
|
-
case function
|
12
|
-
when :now
|
13
|
-
"(DATETIME('now'))"
|
14
|
-
end
|
15
|
-
end
|
16
|
-
end
|
17
|
-
end
|
18
|
-
end
|
19
|
-
end
|
@@ -1,55 +0,0 @@
|
|
1
|
-
module SchemaPlus::DefaultExpr
|
2
|
-
module Middleware
|
3
|
-
|
4
|
-
module Sql
|
5
|
-
module ColumnOptions
|
6
|
-
|
7
|
-
# Add options for default expressions
|
8
|
-
def before(env)
|
9
|
-
options = env.options
|
10
|
-
|
11
|
-
return unless (default = options[:default])
|
12
|
-
|
13
|
-
if default.is_a? Hash and [[:expr], [:value]].include?(default.keys)
|
14
|
-
value = default[:value]
|
15
|
-
expr = env.connection.sql_for_function(default[:expr]) || default[:expr] if default[:expr]
|
16
|
-
else
|
17
|
-
value = default
|
18
|
-
expr = env.connection.sql_for_function(default)
|
19
|
-
end
|
20
|
-
|
21
|
-
if expr
|
22
|
-
raise ArgumentError, "Invalid default expression" unless env.connection.default_expr_valid?(expr)
|
23
|
-
env.sql << " DEFAULT #{expr}"
|
24
|
-
# must explicitly check for :null to allow change_column to work on migrations
|
25
|
-
if options[:null] == false
|
26
|
-
env.sql << " NOT NULL"
|
27
|
-
end
|
28
|
-
options.delete(:default)
|
29
|
-
options.delete(:null)
|
30
|
-
else
|
31
|
-
options[:default] = value
|
32
|
-
end
|
33
|
-
end
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
module Dumper
|
38
|
-
module Table
|
39
|
-
|
40
|
-
# Emit default expression options in dump
|
41
|
-
def after(env)
|
42
|
-
env.connection.columns(env.table.name).each do |column|
|
43
|
-
if !column.default_function.nil?
|
44
|
-
if col = env.table.columns.find{|col| col.name == column.name}
|
45
|
-
options = "default: { expr: #{column.default_function.inspect} }"
|
46
|
-
options += ", #{col.options}" unless col.options.blank?
|
47
|
-
col.options = options
|
48
|
-
end
|
49
|
-
end
|
50
|
-
end
|
51
|
-
end
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
@@ -1,149 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe "Column definition" do
|
4
|
-
before(:each) do
|
5
|
-
define_schema(:auto_create => true) do
|
6
|
-
create_table :models, :force => true do |t|
|
7
|
-
end
|
8
|
-
end
|
9
|
-
class Model < ::ActiveRecord::Base ; end
|
10
|
-
end
|
11
|
-
|
12
|
-
subject {
|
13
|
-
Model.connection.execute("INSERT INTO models (dummy) values (1)")
|
14
|
-
Model.last.reload.test_column
|
15
|
-
}
|
16
|
-
|
17
|
-
context "text columns" do
|
18
|
-
|
19
|
-
before(:each) do
|
20
|
-
@nowish = /(#{Time.now.utc.to_s.sub(/:[^:]+$/, '')}|#{Time.now.to_s.sub(/:[^:]+$/,'')}).*/
|
21
|
-
end
|
22
|
-
|
23
|
-
context "just default passed" do
|
24
|
-
before(:each) do
|
25
|
-
define_test_column(:string, :default => "2011-12-11 00:00:00")
|
26
|
-
end
|
27
|
-
|
28
|
-
it "should use the normal default" do
|
29
|
-
is_expected.to eq "2011-12-11 00:00:00"
|
30
|
-
end
|
31
|
-
end
|
32
|
-
|
33
|
-
context "just default passed in hash" do
|
34
|
-
before(:each) do
|
35
|
-
define_test_column(:string, :default => { :value => "2011-12-11 00:00:00" })
|
36
|
-
end
|
37
|
-
|
38
|
-
it "should use the normal default" do
|
39
|
-
is_expected.to eq "2011-12-11 00:00:00"
|
40
|
-
end
|
41
|
-
end
|
42
|
-
|
43
|
-
context "default passed with no nulls" do
|
44
|
-
before(:each) do
|
45
|
-
define_test_column(:string, :default => "2011-12-11 00:00:00", null: false)
|
46
|
-
end
|
47
|
-
|
48
|
-
it "should use the normal default" do
|
49
|
-
is_expected.to eq "2011-12-11 00:00:00"
|
50
|
-
end
|
51
|
-
end
|
52
|
-
|
53
|
-
context "default passed in hash with no nulls" do
|
54
|
-
before(:each) do
|
55
|
-
define_test_column(:string, :default => { :value => "2011-12-11 00:00:00" }, null: false)
|
56
|
-
end
|
57
|
-
|
58
|
-
it "should use the normal default" do
|
59
|
-
is_expected.to eq "2011-12-11 00:00:00"
|
60
|
-
end
|
61
|
-
end
|
62
|
-
|
63
|
-
context "default function passed as :now" do
|
64
|
-
before(:each) do
|
65
|
-
begin
|
66
|
-
define_test_column(:string, :default => :now)
|
67
|
-
rescue ArgumentError => e
|
68
|
-
@raised_argument_error = e
|
69
|
-
end
|
70
|
-
end
|
71
|
-
|
72
|
-
it "should use NOW() as the default", :mysql => :skip do
|
73
|
-
is_expected.to match @nowish
|
74
|
-
end
|
75
|
-
|
76
|
-
it "should raise an error", :mysql => :only do
|
77
|
-
expect(@raised_argument_error).to be_a ArgumentError
|
78
|
-
end
|
79
|
-
end
|
80
|
-
|
81
|
-
context "default function passed as now with no nulls" do
|
82
|
-
before(:each) do
|
83
|
-
begin
|
84
|
-
define_test_column(:string, :default => :now, null: false)
|
85
|
-
rescue ArgumentError => e
|
86
|
-
@raised_argument_error = e
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
it "should use NOW() as the default", :mysql => :skip do
|
91
|
-
is_expected.to match @nowish
|
92
|
-
end
|
93
|
-
|
94
|
-
it "should raise an error", :mysql => :only do
|
95
|
-
expect(@raised_argument_error).to be_a ArgumentError
|
96
|
-
end
|
97
|
-
end
|
98
|
-
|
99
|
-
context "valid expr passed as default" do
|
100
|
-
it "uses the expression", :mysql => :skip do
|
101
|
-
define_test_column(:string, :default => { :expr => "(replace('THIS IS A TEST', 'TEST', 'DOG'))" })
|
102
|
-
is_expected.to eq "THIS IS A DOG"
|
103
|
-
end
|
104
|
-
|
105
|
-
it "raises an error", :mysql => :only do
|
106
|
-
expect {
|
107
|
-
define_test_column(:string, :default => { :expr => "(replace('THIS IS A TEST', 'TEST', 'DOG'))" })
|
108
|
-
}.to raise_error ArgumentError
|
109
|
-
end
|
110
|
-
end
|
111
|
-
|
112
|
-
end
|
113
|
-
|
114
|
-
context "boolean column" do
|
115
|
-
|
116
|
-
context "passed as boolean false" do
|
117
|
-
before(:each) do
|
118
|
-
define_test_column :boolean, :default => false
|
119
|
-
end
|
120
|
-
|
121
|
-
it "should give the default as false" do
|
122
|
-
is_expected.to eq false
|
123
|
-
end
|
124
|
-
end
|
125
|
-
|
126
|
-
context "passed as boolean true" do
|
127
|
-
before(:each) do
|
128
|
-
define_test_column :boolean, :default => true
|
129
|
-
end
|
130
|
-
|
131
|
-
it "should give the default as true" do
|
132
|
-
is_expected.to eq true
|
133
|
-
end
|
134
|
-
end
|
135
|
-
end
|
136
|
-
|
137
|
-
private
|
138
|
-
|
139
|
-
def define_test_column(type, *args)
|
140
|
-
ActiveRecord::Migration.suppress_messages do
|
141
|
-
ActiveRecord::Migration.create_table Model.table_name, :force => true do |t|
|
142
|
-
t.send type, :test_column, *args
|
143
|
-
t.integer :dummy
|
144
|
-
end
|
145
|
-
end
|
146
|
-
Model.reset_column_information
|
147
|
-
@column = Model.columns.first()
|
148
|
-
end
|
149
|
-
end
|
@@ -1,44 +0,0 @@
|
|
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
|
@@ -1,116 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'stringio'
|
3
|
-
|
4
|
-
describe "Schema dump" do
|
5
|
-
|
6
|
-
before(:all) do
|
7
|
-
SchemaPlus::ForeignKeys.setup do |config|
|
8
|
-
config.auto_create = false
|
9
|
-
end
|
10
|
-
ActiveRecord::Migration.suppress_messages do
|
11
|
-
ActiveRecord::Schema.define do
|
12
|
-
connection.tables.each do |table| drop_table table, force: :cascade end
|
13
|
-
|
14
|
-
create_table :posts, :force => true do |t|
|
15
|
-
t.text :body
|
16
|
-
t.integer :user_id
|
17
|
-
t.integer :first_comment_id
|
18
|
-
t.string :string_no_default
|
19
|
-
t.integer :short_id
|
20
|
-
t.string :str_short
|
21
|
-
t.integer :integer_col
|
22
|
-
t.float :float_col
|
23
|
-
t.decimal :decimal_col
|
24
|
-
t.datetime :datetime_col
|
25
|
-
t.timestamp :timestamp_col
|
26
|
-
t.time :time_col
|
27
|
-
t.date :date_col
|
28
|
-
t.binary :binary_col
|
29
|
-
t.boolean :boolean_col
|
30
|
-
end
|
31
|
-
|
32
|
-
end
|
33
|
-
end
|
34
|
-
class ::Post < ActiveRecord::Base ; end
|
35
|
-
end
|
36
|
-
|
37
|
-
context "with date default", :postgresql => :only do
|
38
|
-
it "should dump the default hash expr as now()" do
|
39
|
-
with_additional_column Post, :posted_at, :datetime, :default => :now do
|
40
|
-
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr\s*=>)\s*"now\(\)"\s*\}})
|
41
|
-
end
|
42
|
-
end
|
43
|
-
|
44
|
-
it "should dump the default hash expr as CURRENT_TIMESTAMP" do
|
45
|
-
with_additional_column Post, :posted_at, :datetime, :default => {:expr => 'date \'2001-09-28\''} do
|
46
|
-
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>).*2001-09-28.*})
|
47
|
-
end
|
48
|
-
end
|
49
|
-
|
50
|
-
it "can dump a complex default expression" do
|
51
|
-
with_additional_column Post, :name, :string, :default => {:expr => 'substring(random()::text from 3 for 6)'} do
|
52
|
-
expect(dump_posts).to match(%r{t\.string\s+"name",\s*(?:default:|:default\s*=>)\s*{\s*(?:expr:|:expr\s*=>)\s*"\\"substring\\"\(\(random\(\)\)::text, 3, 6\)"\s*}})
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
context "with date default", :sqlite3 => :only do
|
58
|
-
it "should dump the default hash expr as now" do
|
59
|
-
with_additional_column Post, :posted_at, :datetime, :default => :now do
|
60
|
-
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"\(DATETIME\('now'\)\)"\s*\}})
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
it "should dump the default hash expr string as now" do
|
65
|
-
with_additional_column Post, :posted_at, :datetime, :default => { :expr => "(DATETIME('now'))" } do
|
66
|
-
expect(dump_posts).to match(%r{t\.datetime\s+"posted_at",\s*(?:default:|:default =>)\s*\{\s*(?:expr:|:expr =>)\s*"\(DATETIME\('now'\)\)"\s*\}})
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
70
|
-
it "should dump the default value normally" do
|
71
|
-
with_additional_column Post, :posted_at, :string, :default => "now" do
|
72
|
-
expect(dump_posts).to match(%r{t\.string\s*"posted_at",\s*(?:default:|:default =>)\s*"now"})
|
73
|
-
end
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
it "should leave out :default when default was changed to null" do
|
78
|
-
ActiveRecord::Migration.suppress_messages do
|
79
|
-
ActiveRecord::Migration.change_column_default :posts, :string_no_default, nil
|
80
|
-
end
|
81
|
-
# mysql2 includes 'limit: 255' in the output. that's OK, just want to
|
82
|
-
# make sure the full line doesn't have 'default' in it.
|
83
|
-
expect(dump_posts).to match(%r{t\.string\s+"string_no_default"\s*(,\s*limit:\s*\d+)?$})
|
84
|
-
end
|
85
|
-
|
86
|
-
protected
|
87
|
-
def to_regexp(string)
|
88
|
-
Regexp.new(Regexp.escape(string))
|
89
|
-
end
|
90
|
-
|
91
|
-
def with_additional_column(model, column_name, column_type, options)
|
92
|
-
table_columns = model.columns.reject{|column| column.name == 'id'}
|
93
|
-
ActiveRecord::Migration.suppress_messages do
|
94
|
-
ActiveRecord::Migration.create_table model.table_name, :force => true do |t|
|
95
|
-
table_columns.each do |column|
|
96
|
-
t.column column.name, column.type, :default => column.default
|
97
|
-
end
|
98
|
-
t.column column_name, column_type, options
|
99
|
-
end
|
100
|
-
end
|
101
|
-
yield
|
102
|
-
end
|
103
|
-
|
104
|
-
def dump_schema(opts={})
|
105
|
-
stream = StringIO.new
|
106
|
-
ActiveRecord::SchemaDumper.ignore_tables = Array.wrap(opts[:ignore]) || []
|
107
|
-
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
108
|
-
stream.string
|
109
|
-
end
|
110
|
-
|
111
|
-
def dump_posts
|
112
|
-
dump_schema(:ignore => %w[users comments])
|
113
|
-
end
|
114
|
-
|
115
|
-
end
|
116
|
-
|