miguel 0.1.0.pre5 → 0.1.0.pre6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.gemfile +1 -1
- data/README.md +7 -2
- data/lib/miguel/command.rb +2 -2
- data/lib/miguel/importer.rb +17 -1
- data/lib/miguel/schema.rb +7 -4
- data/miguel.gemspec +2 -2
- data/test/data/db.yml +5 -1
- data/test/data/schema.rb +9 -2
- data/test/data/schema.txt +6 -7
- data/test/data/schema_bare.txt +6 -7
- data/test/data/schema_change.txt +6 -7
- data/test/data/schema_full.txt +6 -7
- data/test/data/seq_0.txt +1 -1
- data/test/data/seq_1.txt +2 -2
- data/test/data/simple.txt +1 -1
- data/test/data/simple_mysql.txt +1 -1
- data/test/test_command.rb +215 -0
- data/test/test_schema.rb +5 -5
- metadata +5 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 46b9b8bd87e9bbcb4d68e8a4b0687709dd477b4a
|
4
|
+
data.tar.gz: d5386b82cf60542e68e3bacfce52407d4242dd25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9aae54cd7492a54b558931ccecb008d07559db82d3864b690bd27dde855864eaac4e3d72d9dfaff22a5d7de0f2fc732a65a63e5eed7b291e0e2a68296fa2e7b3
|
7
|
+
data.tar.gz: 59015a5b44a5f50c107ddda26d7f5369f08b732c8f0c8baaa64406c5dd6d9feb834655d3fde72a72d1d9098813460c6d2e3956cd92d4e67b6d2dd105c324fb78
|
data/.travis.gemfile
CHANGED
data/README.md
CHANGED
@@ -117,6 +117,7 @@ set_defaults :True, :TrueClass, default: true
|
|
117
117
|
set_defaults :False, :TrueClass, default: false
|
118
118
|
set_defaults :Signed, :integer, unsigned: false
|
119
119
|
set_defaults :Unsigned, :integer, unsigned: true
|
120
|
+
set_defaults :String, text: false
|
120
121
|
set_defaults :Text, :String, text: true
|
121
122
|
set_defaults :Time, :timestamp, default: '2000-01-01 00:00:00'
|
122
123
|
set_defaults :Time?, :timestamp, default: nil
|
@@ -165,7 +166,7 @@ or by the common database `.yml` config file:
|
|
165
166
|
``` yaml
|
166
167
|
# Example db.yml.
|
167
168
|
adapter: mysql2
|
168
|
-
user:
|
169
|
+
user: dev
|
169
170
|
password: sup3rsecr3t
|
170
171
|
host: localhost
|
171
172
|
database: main
|
@@ -192,7 +193,10 @@ leaving dozens of piecewise migration files finally behind.
|
|
192
193
|
## Limitations
|
193
194
|
|
194
195
|
The database specific type support is geared towards [MySQL][] and [SQLite][].
|
195
|
-
|
196
|
+
[Postgres][] is supported as well,
|
197
|
+
but note that it lacks support for some common types (e.g., unsigned integers)
|
198
|
+
compared to other databases.
|
199
|
+
Generic types should however work with any database, even though your mileage may vary.
|
196
200
|
|
197
201
|
Changing primary keys can be as problematic as with normal Sequel migrations,
|
198
202
|
so it's best to set them once and stick with them.
|
@@ -211,3 +215,4 @@ Miguel is released under the MIT license.
|
|
211
215
|
[Sequel]: http://sequel.jeremyevans.net/
|
212
216
|
[MySQL]: https://www.mysql.com/
|
213
217
|
[SQLite]: https://www.sqlite.org/
|
218
|
+
[Postgres]: http://www.postgresql.org/
|
data/lib/miguel/command.rb
CHANGED
@@ -136,8 +136,8 @@ module Miguel
|
|
136
136
|
|
137
137
|
# Make sure the argument count is as expected.
|
138
138
|
def check_args( args, count )
|
139
|
-
fail "Not enough arguments present
|
140
|
-
fail "Extra arguments present
|
139
|
+
fail "Not enough arguments present, use -h to see usage." if args.count < count
|
140
|
+
fail "Extra arguments present, use -h to see usage." if args.count > count
|
141
141
|
end
|
142
142
|
|
143
143
|
# Import schema from given database.
|
data/lib/miguel/importer.rb
CHANGED
@@ -61,6 +61,19 @@ module Miguel
|
|
61
61
|
when /\Ainteger UNSIGNED\z/
|
62
62
|
return :integer, :unsigned => true
|
63
63
|
end
|
64
|
+
when :postgres
|
65
|
+
case type
|
66
|
+
when /\Acharacter varying/
|
67
|
+
return :String, :default_size => 255
|
68
|
+
when /\Acharacter/
|
69
|
+
return :String, :fixed => true, :default_size => 255
|
70
|
+
when /\Atext\z/
|
71
|
+
return :String, :text => true
|
72
|
+
when /\Abytea\z/
|
73
|
+
return :blob
|
74
|
+
when /\Atimestamp/
|
75
|
+
return :timestamp
|
76
|
+
end
|
64
77
|
end
|
65
78
|
|
66
79
|
case type
|
@@ -134,6 +147,7 @@ module Miguel
|
|
134
147
|
opts = opts.dup
|
135
148
|
columns = opts.delete( :columns )
|
136
149
|
next if ( ! opts[ :unique ] ) && foreign_key_indexes.include?( columns ) && name == columns.first
|
150
|
+
opts.delete( :deferrable ) unless opts[ :deferrable ]
|
137
151
|
table.add_index( columns, opts )
|
138
152
|
end
|
139
153
|
end
|
@@ -145,13 +159,14 @@ module Miguel
|
|
145
159
|
name = opts.delete( :name )
|
146
160
|
columns = opts.delete( :columns )
|
147
161
|
table_name = opts.delete( :table )
|
162
|
+
opts.delete( :deferrable ) unless opts[ :deferrable ]
|
148
163
|
table.add_foreign_key( columns, table_name, opts )
|
149
164
|
end
|
150
165
|
end
|
151
166
|
|
152
167
|
# Options which are ignored for columns.
|
153
168
|
# These are usually just schema hints which the user normally doesn't specify.
|
154
|
-
IGNORED_OPTS = [ :max_length ]
|
169
|
+
IGNORED_OPTS = [ :max_length, :oid ]
|
155
170
|
|
156
171
|
# Import column type and options.
|
157
172
|
def import_column_type_and_options( opts )
|
@@ -210,6 +225,7 @@ module Miguel
|
|
210
225
|
|
211
226
|
if primary_key && ! multi_primary_key
|
212
227
|
if auto_increment
|
228
|
+
opts.delete( :default ) if opts[ :default ].to_s =~ /\Anextval/
|
213
229
|
table.add_column( :primary_key, name, opts.merge( :type => type ) )
|
214
230
|
next
|
215
231
|
end
|
data/lib/miguel/schema.rb
CHANGED
@@ -157,7 +157,7 @@ module Miguel
|
|
157
157
|
|
158
158
|
# Default options implied for certain types.
|
159
159
|
DEFAULT_OPTS = {
|
160
|
-
:string => { :size => 255 },
|
160
|
+
:string => { :size => 255, :text => false },
|
161
161
|
:bigint => { :size => 20 },
|
162
162
|
:decimal => { :size => [ 10, 0 ] },
|
163
163
|
:integer => { :unsigned => false },
|
@@ -571,8 +571,9 @@ module Miguel
|
|
571
571
|
set_defaults :False, :TrueClass, :default => false
|
572
572
|
|
573
573
|
set_defaults :Signed, :integer, :unsigned => false
|
574
|
-
set_defaults :Unsigned, :integer, :unsigned =>
|
574
|
+
set_defaults :Unsigned, :integer, :unsigned => ! opts[ :signed_unsigned ]
|
575
575
|
|
576
|
+
set_defaults :String, :text => false
|
576
577
|
set_defaults :Text, :String, :text => true
|
577
578
|
|
578
579
|
# We want times to be stored as 4 byte timestamps, however
|
@@ -645,7 +646,9 @@ module Miguel
|
|
645
646
|
|
646
647
|
# Define schema with provided block.
|
647
648
|
def define( opts = {}, &block )
|
648
|
-
|
649
|
+
sync do
|
650
|
+
set_schema( new( opts ).define( &block ) )
|
651
|
+
end
|
649
652
|
end
|
650
653
|
|
651
654
|
# Load schema from given file.
|
@@ -671,7 +674,7 @@ module Miguel
|
|
671
674
|
|
672
675
|
# Store given schema for later if requested.
|
673
676
|
def set_schema( schema )
|
674
|
-
|
677
|
+
@schema = schema if @schema == self
|
675
678
|
schema
|
676
679
|
end
|
677
680
|
|
data/miguel.gemspec
CHANGED
@@ -4,7 +4,7 @@ require File.expand_path( '../lib/miguel/version', __FILE__ )
|
|
4
4
|
|
5
5
|
Gem::Specification.new do |s|
|
6
6
|
s.name = 'miguel'
|
7
|
-
s.version = Miguel::VERSION + '.
|
7
|
+
s.version = Miguel::VERSION + '.pre6'
|
8
8
|
s.summary = 'Database migrator and migration generator for Sequel.'
|
9
9
|
s.description = <<EOT
|
10
10
|
This gem makes it easy to create and maintain an up-to-date database schema
|
@@ -20,7 +20,7 @@ EOT
|
|
20
20
|
s.executables = `git ls-files -- bin/*`.split( "\n" ).map{ |f| File.basename( f ) }
|
21
21
|
|
22
22
|
s.required_ruby_version = '>= 1.9.3'
|
23
|
-
s.add_runtime_dependency 'sequel', '~> 4.
|
23
|
+
s.add_runtime_dependency 'sequel', '~> 4.27'
|
24
24
|
s.add_development_dependency 'bacon', '~> 1.2'
|
25
25
|
s.add_development_dependency 'sqlite3'
|
26
26
|
s.add_development_dependency 'mysql2'
|
data/test/data/db.yml
CHANGED
data/test/data/schema.rb
CHANGED
@@ -2,9 +2,11 @@
|
|
2
2
|
|
3
3
|
Miguel::Schema.define( use_defaults: false ) do
|
4
4
|
|
5
|
+
mysql = opts[ :mysql_timestamps ]
|
6
|
+
|
5
7
|
table :sequel_types do
|
6
8
|
Integer :a0 # integer
|
7
|
-
String :a1
|
9
|
+
String :a1, :text=>false # varchar(255)
|
8
10
|
String :a2, :size=>50 # varchar(50)
|
9
11
|
String :a3, :fixed=>true # char(255)
|
10
12
|
String :a4, :fixed=>true, :size=>50 # char(50)
|
@@ -47,8 +49,13 @@ Miguel::Schema.define( use_defaults: false ) do
|
|
47
49
|
table :native_types do
|
48
50
|
date :date, default: '2000-12-31'
|
49
51
|
time :time, default: '23:59:59'
|
50
|
-
datetime :datetime, default: '2037-12-31 23:59:59'
|
51
52
|
timestamp :timestamp, default: '1970-01-02 00:00:00'
|
53
|
+
if mysql
|
54
|
+
datetime :datetime, default: '2037-12-31 23:59:59'
|
55
|
+
enum :enum, elements: %w[ none invalid expired declined other ], default: 'none'
|
56
|
+
set :set, elements: %w[ read write create delete ], default: 'read'
|
57
|
+
set :tricky, elements: [ "\n", "\t", "\\", "r'n'r" ]
|
58
|
+
end
|
52
59
|
end
|
53
60
|
|
54
61
|
table :timestamps do
|
data/test/data/schema.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
table :sequel_types do
|
2
2
|
Integer :a0
|
3
|
-
String :a1
|
3
|
+
String :a1, :text => false
|
4
4
|
String :a2, :size => 50
|
5
5
|
String :a3, :fixed => true
|
6
6
|
String :a4, :fixed => true, :size => 50
|
@@ -20,7 +20,7 @@ table :sequel_types do
|
|
20
20
|
end
|
21
21
|
table :miguel_types do
|
22
22
|
integer :key, :null => false, :unsigned => false
|
23
|
-
String :string, :null => false
|
23
|
+
String :string, :null => false, :text => false
|
24
24
|
String :text, :null => false, :text => true
|
25
25
|
File :blob, :null => false
|
26
26
|
Integer :int, :null => false
|
@@ -36,7 +36,6 @@ end
|
|
36
36
|
table :native_types do
|
37
37
|
date :date, :null => false, :default => "2000-12-31"
|
38
38
|
time :time, :null => false, :default => "23:59:59"
|
39
|
-
datetime :datetime, :null => false, :default => "2037-12-31 23:59:59"
|
40
39
|
timestamp :timestamp, :null => false, :default => "1970-01-02 00:00:00"
|
41
40
|
end
|
42
41
|
table :timestamps do
|
@@ -47,7 +46,7 @@ table :timestamps do
|
|
47
46
|
end
|
48
47
|
table :users do
|
49
48
|
primary_key :id, :null => false, :unsigned => false
|
50
|
-
String :name, :null => false
|
49
|
+
String :name, :null => false, :text => false
|
51
50
|
index [:name], :null => false, :unique => true
|
52
51
|
end
|
53
52
|
table :simple do
|
@@ -64,7 +63,7 @@ table :compound do
|
|
64
63
|
Integer :a, :null => false
|
65
64
|
Integer :b, :null => false
|
66
65
|
primary_key [:a, :b], :null => false, :unsigned => false
|
67
|
-
String :c, :null => false
|
66
|
+
String :c, :null => false, :text => false
|
68
67
|
integer :d, :null => false, :unsigned => false
|
69
68
|
index [:a, :c], :null => false, :unique => true
|
70
69
|
index [:b, :c, :d], :null => false
|
@@ -72,7 +71,7 @@ table :compound do
|
|
72
71
|
foreign_key [:b, :a], :compound, :null => false, :key => [:a, :b], :unsigned => false
|
73
72
|
end
|
74
73
|
table :null do
|
75
|
-
String :string, :null => true
|
74
|
+
String :string, :null => true, :text => false
|
76
75
|
String :text, :null => true, :text => true
|
77
76
|
File :blob, :null => true
|
78
77
|
Integer :int, :null => true
|
@@ -87,7 +86,7 @@ table :null do
|
|
87
86
|
foreign_key [:user_id], :users, :null => true, :key => [:id], :unsigned => false
|
88
87
|
end
|
89
88
|
table :defaults do
|
90
|
-
String :string, :null => false, :default => "abc"
|
89
|
+
String :string, :null => false, :text => false, :default => "abc"
|
91
90
|
Integer :int, :null => false, :default => 10
|
92
91
|
integer :signed, :null => false, :unsigned => false, :default => -1
|
93
92
|
integer :unsigned, :null => false, :unsigned => true, :default => 1000
|
data/test/data/schema_bare.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
create_table :sequel_types do
|
2
2
|
Integer :a0
|
3
|
-
String :a1
|
3
|
+
String :a1, :text => false
|
4
4
|
String :a2, :size => 50
|
5
5
|
String :a3, :fixed => true
|
6
6
|
String :a4, :fixed => true, :size => 50
|
@@ -20,7 +20,7 @@ create_table :sequel_types do
|
|
20
20
|
end
|
21
21
|
create_table :miguel_types do
|
22
22
|
integer :key, :null => false, :unsigned => false
|
23
|
-
String :string, :null => false
|
23
|
+
String :string, :null => false, :text => false
|
24
24
|
String :text, :null => false, :text => true
|
25
25
|
File :blob, :null => false
|
26
26
|
Integer :int, :null => false
|
@@ -36,7 +36,6 @@ end
|
|
36
36
|
create_table :native_types do
|
37
37
|
date :date, :null => false, :default => "2000-12-31"
|
38
38
|
time :time, :null => false, :default => "23:59:59"
|
39
|
-
datetime :datetime, :null => false, :default => "2037-12-31 23:59:59"
|
40
39
|
timestamp :timestamp, :null => false, :default => "1970-01-02 00:00:00"
|
41
40
|
end
|
42
41
|
create_table :timestamps do
|
@@ -47,7 +46,7 @@ create_table :timestamps do
|
|
47
46
|
end
|
48
47
|
create_table :users do
|
49
48
|
primary_key :id, :null => false, :unsigned => false
|
50
|
-
String :name, :null => false
|
49
|
+
String :name, :null => false, :text => false
|
51
50
|
index [:name], :null => false, :unique => true
|
52
51
|
end
|
53
52
|
create_table :simple do
|
@@ -62,14 +61,14 @@ create_table :compound do
|
|
62
61
|
Integer :a, :null => false
|
63
62
|
Integer :b, :null => false
|
64
63
|
primary_key [:a, :b], :null => false, :unsigned => false
|
65
|
-
String :c, :null => false
|
64
|
+
String :c, :null => false, :text => false
|
66
65
|
integer :d, :null => false, :unsigned => false
|
67
66
|
index [:a, :c], :null => false, :unique => true
|
68
67
|
index [:b, :c, :d], :null => false
|
69
68
|
index [:b, :a], :null => false
|
70
69
|
end
|
71
70
|
create_table :null do
|
72
|
-
String :string, :null => true
|
71
|
+
String :string, :null => true, :text => false
|
73
72
|
String :text, :null => true, :text => true
|
74
73
|
File :blob, :null => true
|
75
74
|
Integer :int, :null => true
|
@@ -83,7 +82,7 @@ create_table :null do
|
|
83
82
|
integer :user_id, :null => true, :key => [:id], :unsigned => false
|
84
83
|
end
|
85
84
|
create_table :defaults do
|
86
|
-
String :string, :null => false, :default => "abc"
|
85
|
+
String :string, :null => false, :text => false, :default => "abc"
|
87
86
|
Integer :int, :null => false, :default => 10
|
88
87
|
integer :signed, :null => false, :unsigned => false, :default => -1
|
89
88
|
integer :unsigned, :null => false, :unsigned => true, :default => 1000
|
data/test/data/schema_change.txt
CHANGED
@@ -2,7 +2,7 @@ Sequel.migration do
|
|
2
2
|
change do
|
3
3
|
create_table :sequel_types do
|
4
4
|
Integer :a0
|
5
|
-
String :a1
|
5
|
+
String :a1, :text => false
|
6
6
|
String :a2, :size => 50
|
7
7
|
String :a3, :fixed => true
|
8
8
|
String :a4, :fixed => true, :size => 50
|
@@ -22,7 +22,7 @@ Sequel.migration do
|
|
22
22
|
end
|
23
23
|
create_table :miguel_types do
|
24
24
|
integer :key, :null => false, :unsigned => false
|
25
|
-
String :string, :null => false
|
25
|
+
String :string, :null => false, :text => false
|
26
26
|
String :text, :null => false, :text => true
|
27
27
|
File :blob, :null => false
|
28
28
|
Integer :int, :null => false
|
@@ -38,7 +38,6 @@ Sequel.migration do
|
|
38
38
|
create_table :native_types do
|
39
39
|
date :date, :null => false, :default => "2000-12-31"
|
40
40
|
time :time, :null => false, :default => "23:59:59"
|
41
|
-
datetime :datetime, :null => false, :default => "2037-12-31 23:59:59"
|
42
41
|
timestamp :timestamp, :null => false, :default => "1970-01-02 00:00:00"
|
43
42
|
end
|
44
43
|
create_table :timestamps do
|
@@ -49,7 +48,7 @@ Sequel.migration do
|
|
49
48
|
end
|
50
49
|
create_table :users do
|
51
50
|
primary_key :id, :null => false, :unsigned => false
|
52
|
-
String :name, :null => false
|
51
|
+
String :name, :null => false, :text => false
|
53
52
|
index [:name], :null => false, :unique => true
|
54
53
|
end
|
55
54
|
create_table :simple do
|
@@ -64,14 +63,14 @@ Sequel.migration do
|
|
64
63
|
Integer :a, :null => false
|
65
64
|
Integer :b, :null => false
|
66
65
|
primary_key [:a, :b], :null => false, :unsigned => false
|
67
|
-
String :c, :null => false
|
66
|
+
String :c, :null => false, :text => false
|
68
67
|
integer :d, :null => false, :unsigned => false
|
69
68
|
index [:a, :c], :null => false, :unique => true
|
70
69
|
index [:b, :c, :d], :null => false
|
71
70
|
index [:b, :a], :null => false
|
72
71
|
end
|
73
72
|
create_table :null do
|
74
|
-
String :string, :null => true
|
73
|
+
String :string, :null => true, :text => false
|
75
74
|
String :text, :null => true, :text => true
|
76
75
|
File :blob, :null => true
|
77
76
|
Integer :int, :null => true
|
@@ -85,7 +84,7 @@ Sequel.migration do
|
|
85
84
|
integer :user_id, :null => true, :key => [:id], :unsigned => false
|
86
85
|
end
|
87
86
|
create_table :defaults do
|
88
|
-
String :string, :null => false, :default => "abc"
|
87
|
+
String :string, :null => false, :text => false, :default => "abc"
|
89
88
|
Integer :int, :null => false, :default => 10
|
90
89
|
integer :signed, :null => false, :unsigned => false, :default => -1
|
91
90
|
integer :unsigned, :null => false, :unsigned => true, :default => 1000
|
data/test/data/schema_full.txt
CHANGED
@@ -2,7 +2,7 @@ Sequel.migration do
|
|
2
2
|
up do
|
3
3
|
create_table :sequel_types do
|
4
4
|
Integer :a0
|
5
|
-
String :a1
|
5
|
+
String :a1, :text => false
|
6
6
|
String :a2, :size => 50
|
7
7
|
String :a3, :fixed => true
|
8
8
|
String :a4, :fixed => true, :size => 50
|
@@ -22,7 +22,7 @@ Sequel.migration do
|
|
22
22
|
end
|
23
23
|
create_table :miguel_types do
|
24
24
|
integer :key, :null => false, :unsigned => false
|
25
|
-
String :string, :null => false
|
25
|
+
String :string, :null => false, :text => false
|
26
26
|
String :text, :null => false, :text => true
|
27
27
|
File :blob, :null => false
|
28
28
|
Integer :int, :null => false
|
@@ -38,7 +38,6 @@ Sequel.migration do
|
|
38
38
|
create_table :native_types do
|
39
39
|
date :date, :null => false, :default => "2000-12-31"
|
40
40
|
time :time, :null => false, :default => "23:59:59"
|
41
|
-
datetime :datetime, :null => false, :default => "2037-12-31 23:59:59"
|
42
41
|
timestamp :timestamp, :null => false, :default => "1970-01-02 00:00:00"
|
43
42
|
end
|
44
43
|
create_table :timestamps do
|
@@ -49,7 +48,7 @@ Sequel.migration do
|
|
49
48
|
end
|
50
49
|
create_table :users do
|
51
50
|
primary_key :id, :null => false, :unsigned => false
|
52
|
-
String :name, :null => false
|
51
|
+
String :name, :null => false, :text => false
|
53
52
|
index [:name], :null => false, :unique => true
|
54
53
|
end
|
55
54
|
create_table :simple do
|
@@ -64,14 +63,14 @@ Sequel.migration do
|
|
64
63
|
Integer :a, :null => false
|
65
64
|
Integer :b, :null => false
|
66
65
|
primary_key [:a, :b], :null => false, :unsigned => false
|
67
|
-
String :c, :null => false
|
66
|
+
String :c, :null => false, :text => false
|
68
67
|
integer :d, :null => false, :unsigned => false
|
69
68
|
index [:a, :c], :null => false, :unique => true
|
70
69
|
index [:b, :c, :d], :null => false
|
71
70
|
index [:b, :a], :null => false
|
72
71
|
end
|
73
72
|
create_table :null do
|
74
|
-
String :string, :null => true
|
73
|
+
String :string, :null => true, :text => false
|
75
74
|
String :text, :null => true, :text => true
|
76
75
|
File :blob, :null => true
|
77
76
|
Integer :int, :null => true
|
@@ -85,7 +84,7 @@ Sequel.migration do
|
|
85
84
|
integer :user_id, :null => true, :key => [:id], :unsigned => false
|
86
85
|
end
|
87
86
|
create_table :defaults do
|
88
|
-
String :string, :null => false, :default => "abc"
|
87
|
+
String :string, :null => false, :text => false, :default => "abc"
|
89
88
|
Integer :int, :null => false, :default => 10
|
90
89
|
integer :signed, :null => false, :unsigned => false, :default => -1
|
91
90
|
integer :unsigned, :null => false, :unsigned => true, :default => 1000
|
data/test/data/seq_0.txt
CHANGED
@@ -5,7 +5,7 @@ Sequel.migration do
|
|
5
5
|
end
|
6
6
|
create_table :b do
|
7
7
|
primary_key :id, :null => false, :unsigned => false
|
8
|
-
String :t, :null => false
|
8
|
+
String :t, :null => false, :text => false
|
9
9
|
integer :u, :null => true, :unsigned => true
|
10
10
|
integer :s, :null => false, :unsigned => false
|
11
11
|
end
|
data/test/data/seq_1.txt
CHANGED
@@ -18,7 +18,7 @@ Sequel.migration do
|
|
18
18
|
end
|
19
19
|
create_table :c do
|
20
20
|
primary_key :id, :null => false, :unsigned => false
|
21
|
-
String :s, :null => true
|
21
|
+
String :s, :null => true, :text => false
|
22
22
|
end
|
23
23
|
create_table :d do
|
24
24
|
Integer :a, :null => false
|
@@ -50,7 +50,7 @@ Sequel.migration do
|
|
50
50
|
drop_column :create_time # :timestamp, :null => false, :default => "2000-01-01 00:00:00"
|
51
51
|
drop_column :update_time # :timestamp, :null => false, :default => "2000-01-01 00:00:00"
|
52
52
|
drop_column :fk # :integer, :null => false, :key => [:id], :unsigned => false
|
53
|
-
set_column_type :t, String, :null => false
|
53
|
+
set_column_type :t, String, :null => false, :text => false
|
54
54
|
set_column_allow_null :u
|
55
55
|
set_column_default :u, nil
|
56
56
|
set_column_default :s, 0
|
data/test/data/simple.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
table :items do
|
2
2
|
primary_key :id, :null => false, :unsigned => false
|
3
|
-
String :name, :null => false
|
3
|
+
String :name, :null => false, :text => false
|
4
4
|
integer :parent_id, :null => false, :key => [:id], :unsigned => false
|
5
5
|
timestamp :create_time, :null => false, :default => "2000-01-01 00:00:00"
|
6
6
|
timestamp :update_time, :null => false, :default => "2000-01-01 00:00:00"
|
data/test/data/simple_mysql.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
table :items do
|
2
2
|
primary_key :id, :null => false, :unsigned => true, :type => :integer
|
3
|
-
String :name, :null => false
|
3
|
+
String :name, :null => false, :text => false
|
4
4
|
integer :parent_id, :null => false, :key => [:id], :unsigned => true, :type => :integer
|
5
5
|
timestamp :create_time, :null => false, :default => "0000-00-00 00:00:00"
|
6
6
|
timestamp :update_time, :null => false, :default => Sequel.lit("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")
|
@@ -0,0 +1,215 @@
|
|
1
|
+
# Test Command.
|
2
|
+
|
3
|
+
require_relative 'helper'
|
4
|
+
require 'miguel/command'
|
5
|
+
|
6
|
+
require 'open3'
|
7
|
+
require 'tempfile'
|
8
|
+
|
9
|
+
describe Miguel::Command do
|
10
|
+
|
11
|
+
EXCEPTION_SCHEMA = <<-EOT
|
12
|
+
Miguel::Schema.define do
|
13
|
+
raise NotImplementedError
|
14
|
+
end
|
15
|
+
EOT
|
16
|
+
|
17
|
+
def match_file( data, name )
|
18
|
+
match( data, File.read( data( name ) ) )
|
19
|
+
end
|
20
|
+
|
21
|
+
def with_tempfile( content = nil, extension = 'rb' )
|
22
|
+
f = Tempfile.new( [ 'miguel', ".#{extension}" ] )
|
23
|
+
if content
|
24
|
+
f.write( content )
|
25
|
+
f.flush
|
26
|
+
f.rewind
|
27
|
+
end
|
28
|
+
yield f.path
|
29
|
+
ensure
|
30
|
+
f.close
|
31
|
+
f.unlink
|
32
|
+
end
|
33
|
+
|
34
|
+
def run( *args )
|
35
|
+
out = err = nil
|
36
|
+
Open3.popen3( 'ruby', 'bin/miguel', *args ) do |i, o, e, t|
|
37
|
+
yield i if block_given?
|
38
|
+
i.close
|
39
|
+
out = o.read
|
40
|
+
err = e.read
|
41
|
+
end
|
42
|
+
[ out, err ]
|
43
|
+
end
|
44
|
+
|
45
|
+
def test( *args )
|
46
|
+
out, err = run( *args )
|
47
|
+
err.should.be.empty
|
48
|
+
out
|
49
|
+
end
|
50
|
+
|
51
|
+
should 'provide help' do
|
52
|
+
test( '--help' ).should.match /Show this message/
|
53
|
+
end
|
54
|
+
|
55
|
+
should 'show version' do
|
56
|
+
test( '--version' ).should.match /\Amiguel #{Miguel::VERSION}\Z/
|
57
|
+
end
|
58
|
+
|
59
|
+
should 'show schema' do
|
60
|
+
out = test( 'show', data( 'schema.rb' ) )
|
61
|
+
match_file( out, 'schema.txt' )
|
62
|
+
end
|
63
|
+
|
64
|
+
should 'show schema changes' do
|
65
|
+
out = test( 'dump', data( 'schema.rb' ) )
|
66
|
+
match_file( out, 'schema_bare.txt' )
|
67
|
+
end
|
68
|
+
|
69
|
+
should 'show schema changes in various formats' do
|
70
|
+
for format in %w[ bare change full ]
|
71
|
+
out = test( 'dump', '--migration', format, data( 'schema.rb' ) )
|
72
|
+
match_file( out, "schema_#{format}.txt" )
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
should 'show changes needed to remove schema' do
|
77
|
+
out = test( 'down', data( 'schema.rb' ) )
|
78
|
+
match_file( out, 'schema_down.txt' )
|
79
|
+
end
|
80
|
+
|
81
|
+
should 'show changes needed to migrate from one schema to another' do
|
82
|
+
schema = 'sqlite://'
|
83
|
+
SEQ_COUNT.times do |i|
|
84
|
+
new_schema = data( "seq_#{i}.rb" )
|
85
|
+
out = test( 'diff', '-m', 'full', schema, new_schema )
|
86
|
+
match_file( out, "seq_#{i}.txt" )
|
87
|
+
schema = new_schema
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
should 'apply schema to the database' do
|
92
|
+
test( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ), '--force' ).should.not.be.empty
|
93
|
+
test( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ), '--force' ).should.match /\ANo changes are necessary\.\Z/
|
94
|
+
end
|
95
|
+
|
96
|
+
should 'be able to clear the entire database' do
|
97
|
+
test( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ), '--force' )
|
98
|
+
test( 'clear', '--env', 'mysql', data( 'db.yml' ), '--force' ).should.not.be.empty
|
99
|
+
test( 'clear', '--env', 'mysql', data( 'db.yml' ), '--force' ).should.match /\ANo changes are necessary\.\Z/
|
100
|
+
end
|
101
|
+
|
102
|
+
should 'require confirmation before changing the database' do
|
103
|
+
out, err = run( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ) )
|
104
|
+
out.should.match /^Confirm \(yes or no\)\?/
|
105
|
+
err.should.match /\AOK, aborting\.\Z/
|
106
|
+
|
107
|
+
out, err = run( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ) ) do |input|
|
108
|
+
input.write 'blah'
|
109
|
+
end
|
110
|
+
out.should.match /^Confirm \(yes or no\)\?/
|
111
|
+
out.should.match /Please answer 'yes' or 'no'\.$/
|
112
|
+
err.should.match /\AOK, aborting\.\Z/
|
113
|
+
|
114
|
+
out, err = run( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ) ) do |input|
|
115
|
+
input.write 'no'
|
116
|
+
end
|
117
|
+
out.should.match /^Confirm \(yes or no\)\?/
|
118
|
+
out.should.not.match /Please answer 'yes' or 'no'\.$/
|
119
|
+
err.should.match /\AOK, aborting\.\Z/
|
120
|
+
|
121
|
+
out, err = run( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ) ) do |input|
|
122
|
+
input.write 'yes'
|
123
|
+
end
|
124
|
+
out.should.match /^Confirm \(yes or no\)\?/
|
125
|
+
out.should.match /OK, those changes were applied\./
|
126
|
+
err.should.be.empty
|
127
|
+
|
128
|
+
out, err = run( 'clear', '--env', 'mysql', data( 'db.yml' ) )
|
129
|
+
out.should.match /^Confirm \(yes or no\)\?/
|
130
|
+
err.should.match /\AOK, aborting\.\Z/
|
131
|
+
|
132
|
+
out, err = run( 'clear', '--env', 'mysql', data( 'db.yml' ) ) do |input|
|
133
|
+
input.write 'yes'
|
134
|
+
end
|
135
|
+
out.should.match /^Confirm \(yes or no\)\?/
|
136
|
+
out.should.match /OK, those changes were applied\./
|
137
|
+
err.should.be.empty
|
138
|
+
end
|
139
|
+
|
140
|
+
should 'show no changes when told so' do
|
141
|
+
test( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ), '--force', '--quiet' ).should.be.empty
|
142
|
+
test( 'apply', '--env', 'mysql', data( 'db.yml' ), data( 'schema.rb' ), '--force', '--quiet' ).should.be.empty
|
143
|
+
test( 'clear', '--env', 'mysql', data( 'db.yml' ), '--force', '--quiet' ).should.be.empty
|
144
|
+
test( 'clear', '--env', 'mysql', data( 'db.yml' ), '--force', '--quiet' ).should.be.empty
|
145
|
+
end
|
146
|
+
|
147
|
+
should 'log SQL commands to stdout when requested' do
|
148
|
+
test( 'show', '--env', 'mysql', data( 'db.yml' ), '--echo' ).should.match /SHOW FULL TABLES/
|
149
|
+
end
|
150
|
+
|
151
|
+
should 'log SQL commands to given file when requested' do
|
152
|
+
with_tempfile do |path|
|
153
|
+
test( 'show', '--env', 'mysql', data( 'db.yml' ), '--log', path )
|
154
|
+
File.read( path ).should.match /SHOW FULL TABLES/
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
should 'report errors in loaded schema' do
|
159
|
+
with_tempfile( EXCEPTION_SCHEMA ) do |path|
|
160
|
+
out, err = run( 'show', path )
|
161
|
+
out.should.be.empty
|
162
|
+
err.should.match /NotImplementedError: NotImplementedError/
|
163
|
+
err.should.not.match /bin\/miguel/
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
should 'show full trace when requested' do
|
168
|
+
with_tempfile( EXCEPTION_SCHEMA ) do |path|
|
169
|
+
out, err = run( 'show', path, '--trace' )
|
170
|
+
out.should.be.empty
|
171
|
+
err.should.match /bin\/miguel/
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
should 'report invalid command' do
|
176
|
+
out, err = run( 'blah' )
|
177
|
+
out.should.be.empty
|
178
|
+
err.should.match /\AInvalid command, use -h to see usage\.\Z/
|
179
|
+
end
|
180
|
+
|
181
|
+
should 'report invalid number of arguments' do
|
182
|
+
out, err = run( 'show' )
|
183
|
+
out.should.be.empty
|
184
|
+
err.should.match /\ANot enough arguments present, use -h to see usage\.\Z/
|
185
|
+
|
186
|
+
out, err = run( 'show', 'arg1', 'arg2' )
|
187
|
+
out.should.be.empty
|
188
|
+
err.should.match /\AExtra arguments present, use -h to see usage\.\Z/
|
189
|
+
end
|
190
|
+
|
191
|
+
should 'report invalid arguments' do
|
192
|
+
out, err = run( 'show', '' )
|
193
|
+
out.should.be.empty
|
194
|
+
err.should.match /\AMissing database or schema name\.\Z/
|
195
|
+
|
196
|
+
out, err = run( 'clear', '' )
|
197
|
+
out.should.be.empty
|
198
|
+
err.should.match /\AMissing database name\.\Z/
|
199
|
+
|
200
|
+
out, err = run( 'clear', data( 'nonexistent.rb' ) )
|
201
|
+
out.should.be.empty
|
202
|
+
err.should.match /\ADatabase config \S+\/nonexistent\.rb not found\.\Z/
|
203
|
+
end
|
204
|
+
|
205
|
+
should 'report empty schema' do
|
206
|
+
with_tempfile( '' ) do |path|
|
207
|
+
out, err = run( 'show', path )
|
208
|
+
out.should.be.empty
|
209
|
+
err.should.match /\ANo schema loaded from file '\S+\.rb'\.\Z/
|
210
|
+
end
|
211
|
+
end
|
212
|
+
|
213
|
+
end
|
214
|
+
|
215
|
+
# EOF #
|
data/test/test_schema.rb
CHANGED
@@ -104,7 +104,7 @@ describe Miguel::Schema do
|
|
104
104
|
match_schema <<-EOT do
|
105
105
|
table :miguel_types do
|
106
106
|
integer :key, :null => false, :unsigned => false
|
107
|
-
String :string, :null => false
|
107
|
+
String :string, :null => false, :text => false
|
108
108
|
String :text, :null => false, :text => true
|
109
109
|
File :blob, :null => false
|
110
110
|
Integer :int, :null => false
|
@@ -370,7 +370,7 @@ describe Miguel::Schema do
|
|
370
370
|
table :index do
|
371
371
|
Integer :a, :null => false
|
372
372
|
Integer :b, :null => false
|
373
|
-
String :s, :null => false
|
373
|
+
String :s, :null => false, :text => false
|
374
374
|
index [:a], :null => false
|
375
375
|
index [:b], :null => false, :unique => true
|
376
376
|
index [:a, :b], :null => false
|
@@ -392,7 +392,7 @@ describe Miguel::Schema do
|
|
392
392
|
should 'support null columns' do
|
393
393
|
match_schema <<-EOT do
|
394
394
|
table :null do
|
395
|
-
String :string, :null => true
|
395
|
+
String :string, :null => true, :text => false
|
396
396
|
String :text, :null => true, :text => true
|
397
397
|
File :blob, :null => true
|
398
398
|
Integer :int, :null => true
|
@@ -427,7 +427,7 @@ describe Miguel::Schema do
|
|
427
427
|
should 'support default values' do
|
428
428
|
match_schema <<-EOT do
|
429
429
|
table :defaults do
|
430
|
-
String :string, :null => false, :default => "abc"
|
430
|
+
String :string, :null => false, :text => false, :default => "abc"
|
431
431
|
Integer :int, :null => false, :default => 10
|
432
432
|
integer :signed, :null => false, :unsigned => false, :default => -1
|
433
433
|
integer :unsigned, :null => false, :unsigned => true, :default => 1000
|
@@ -496,7 +496,7 @@ describe Miguel::Schema do
|
|
496
496
|
integer :left_id, :null => false, :key => [:id], :unsigned => false
|
497
497
|
integer :right_id, :null => false, :key => [:id], :unsigned => false
|
498
498
|
primary_key [:left_id, :right_id], :null => false, :unsigned => false
|
499
|
-
String :name, :null => false
|
499
|
+
String :name, :null => false, :text => false
|
500
500
|
index [:right_id, :left_id], :null => false, :unique => true
|
501
501
|
foreign_key [:left_id], :left, :null => false, :key => [:id], :unsigned => false
|
502
502
|
foreign_key [:right_id], :right, :null => false, :key => [:id], :unsigned => false
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: miguel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.0.
|
4
|
+
version: 0.1.0.pre6
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Patrik Rak
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-10-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - "~>"
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '4.
|
19
|
+
version: '4.27'
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - "~>"
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: '4.
|
26
|
+
version: '4.27'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bacon
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -123,6 +123,7 @@ files:
|
|
123
123
|
- test/data/simple.txt
|
124
124
|
- test/data/simple_mysql.txt
|
125
125
|
- test/helper.rb
|
126
|
+
- test/test_command.rb
|
126
127
|
- test/test_dumper.rb
|
127
128
|
- test/test_importer.rb
|
128
129
|
- test/test_migrator.rb
|