miguel 0.1.0.pre3 → 0.1.0.pre4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.travis.gemfile +16 -1
- data/.travis.yml +8 -0
- data/README.md +3 -1
- data/lib/miguel/importer.rb +7 -4
- data/lib/miguel/migrator.rb +23 -18
- data/lib/miguel/schema.rb +7 -3
- data/miguel.gemspec +1 -1
- data/test/data/schema.rb +26 -4
- data/test/data/schema.txt +35 -20
- data/test/helper.rb +11 -0
- data/test/test_dumper.rb +1 -0
- data/test/test_schema.rb +1 -0
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: dbf13dd299009fb0beefd95d64b4a1b3bfba5be5
|
4
|
+
data.tar.gz: 0f4558c6faa5826150e998fb900706ea84bb65a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 30b5da0b99408f21db2c16658c1451f19c0e76dc3398c8edce4881d9339eb07a2ee113a4fcb8cd0aca1e16cd23d03432842169f780399c94580a0a92cf7ecee1
|
7
|
+
data.tar.gz: c6bf941afb95fa42763aa62c0d49376aacca8b32fe6d044d753fffbad2184f7a811edafc107ae46eb7572f44ba2b677cf901e5635572e848db0650c3ba23a2e1
|
data/.travis.gemfile
CHANGED
@@ -5,6 +5,21 @@ source 'https://rubygems.org'
|
|
5
5
|
gem 'rake'
|
6
6
|
gem 'bacon', '~> 1.2'
|
7
7
|
gem 'sequel', '~> 4.0'
|
8
|
-
|
8
|
+
|
9
|
+
platforms :ruby do
|
10
|
+
gem 'sqlite3'
|
11
|
+
gem 'mysql2'
|
12
|
+
gem 'pg'
|
13
|
+
end
|
14
|
+
|
15
|
+
platforms :jruby do
|
16
|
+
gem 'jdbc-sqlite3'
|
17
|
+
gem 'jdbc-mysql'
|
18
|
+
gem 'jdbc-postgres'
|
19
|
+
end
|
20
|
+
|
21
|
+
if ENV[ 'CODECLIMATE_REPO_TOKEN' ]
|
22
|
+
gem 'codeclimate-test-reporter'
|
23
|
+
end
|
9
24
|
|
10
25
|
# EOF #
|
data/.travis.yml
CHANGED
@@ -1,3 +1,5 @@
|
|
1
|
+
# TravisCI config.
|
2
|
+
|
1
3
|
language: ruby
|
2
4
|
rvm:
|
3
5
|
- ruby-head
|
@@ -9,8 +11,14 @@ rvm:
|
|
9
11
|
- jruby-head
|
10
12
|
- jruby-19mode
|
11
13
|
gemfile: .travis.gemfile
|
14
|
+
env:
|
15
|
+
global:
|
16
|
+
# travis encrypt CODECLIMATE_REPO_TOKEN=???
|
17
|
+
secure: WjkizgQWf8gx1KIfPsX99TR6uJgnF9ybHUtu/kz0JHWYNif8IJ7qsO+kCXzdGtTiwMgQL4cHzYqbGa6VAiiPQMmG9NPLZ1Q4STLW9W4afg2lOqQmyGk0iSapEu/YXlWM6kZW4IcY/4BG8DzgfJnzy6+4yWBGZFdMwcpJEVZpy02c3Y6RBsrFJ6wz1BpWZR8bHaFXrLzfpHeS8t8h1OYetf1RLg9MvPvankkoIBBxw6ahV9QhG1O2cXyzlfH30wQA90stTFOybUkJ/hVLlCXyxEbjuhUKTWeU4s/Uw5XhA3k5ll0GWyaTUQU3aSWwTDv2Z/dncwR2y8RMuGJqHVLcTz2jtb+NINncrYq5ybGUaCNpdJea7/OS4R6M9t2VS666j5dPN4tVsTgyAONzgHHEmTYwHk3s9s4EKyoHAl398Kv/dazwSmBasvc1kIDzsi6n83k4MHMSNFTBlA4Jz0LaQ7Qx0hxS7MIsGhDmVeeEOQOw1idof6hQdBCpcLfO9NCxqRFWYAgK99U5mXjqRWBeQGyEwrtgbvyweanDrFfHtVEQpUrW1Do4zNz9jZ+MS43hKViu3loH3y20w8Vi96UXWoDp/RreLGMpM33V3xzCC+U3Ex1Dckoq0TgQ8RFD3U/d0KdQJ02B8C3iRmNqD43jGJEJl5USnT58gwgPumLwoCs=
|
12
18
|
matrix:
|
13
19
|
allow_failures:
|
14
20
|
- rvm: ruby-head
|
15
21
|
- rvm: jruby-19mode
|
16
22
|
- rvm: jruby-head
|
23
|
+
|
24
|
+
# EOF #
|
data/README.md
CHANGED
@@ -129,6 +129,8 @@ set_defaults :foreign_key, key: :id, type: :integer, unsigned: false
|
|
129
129
|
|
130
130
|
If you prefer unsigned keys instead and your database engine supports it,
|
131
131
|
you can pass the `unsigned_keys: true` option to `define` to make it happen.
|
132
|
+
If you don't want any of these defaults set up for you,
|
133
|
+
pass the `use_defaults: false` option to define instead.
|
132
134
|
|
133
135
|
Finally, the `timestamps` helper can be used to create the
|
134
136
|
`create_time` and `update_time` timestamps for you.
|
@@ -190,7 +192,7 @@ leaving dozens of piecewise migration files finally behind.
|
|
190
192
|
The database specific type support is geared towards [MySQL][] and [SQLite][].
|
191
193
|
Generic types should work with any database, but your mileage may vary.
|
192
194
|
|
193
|
-
Changing
|
195
|
+
Changing primary keys can be as problematic as with normal Sequel migrations,
|
194
196
|
so it's best to set them once and stick with them.
|
195
197
|
|
196
198
|
It is currently not possible to describe renaming of columns or tables.
|
data/lib/miguel/importer.rb
CHANGED
@@ -103,15 +103,18 @@ module Miguel
|
|
103
103
|
[ type, opts ]
|
104
104
|
end
|
105
105
|
|
106
|
-
# Types we support for default values. Anything else is converted to String.
|
107
|
-
DEFAULT_TYPES = [ String, Numeric, TrueClass, FalseClass ]
|
108
|
-
|
109
106
|
# Convert given database default of given type to default used by our schema definitions.
|
110
107
|
def revert_default( type, default, ruby_default )
|
111
108
|
default = ruby_default unless ruby_default.nil?
|
112
109
|
return if default.nil?
|
113
110
|
|
114
|
-
|
111
|
+
case default
|
112
|
+
when String, Numeric, TrueClass, FalseClass
|
113
|
+
when DateTime
|
114
|
+
default = default.strftime( '%F %T' )
|
115
|
+
else
|
116
|
+
default = default.to_s
|
117
|
+
end
|
115
118
|
|
116
119
|
if type.to_s =~ /date|time/
|
117
120
|
case default
|
data/lib/miguel/migrator.rb
CHANGED
@@ -21,6 +21,14 @@ module Miguel
|
|
21
21
|
]
|
22
22
|
end
|
23
23
|
|
24
|
+
# Iterate over matching pairs of named items.
|
25
|
+
def each_pair( name, from_items, to_items )
|
26
|
+
for from, to in from_items.zip( to_items )
|
27
|
+
fail "invalid #{name} pair #{from.name} -> #{to.name}" unless from.name == to.name
|
28
|
+
yield from, to
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
24
32
|
# Convert foreign keys from given tables into [ table name, foreign key ] pairs for easier comparison.
|
25
33
|
def prepare_keys( tables )
|
26
34
|
result = []
|
@@ -41,25 +49,26 @@ module Miguel
|
|
41
49
|
result
|
42
50
|
end
|
43
51
|
|
44
|
-
# Generate code for
|
45
|
-
def
|
52
|
+
# Generate code for altering given foreign keys.
|
53
|
+
def dump_foreign_keys( out, table_keys, &block )
|
46
54
|
for name, keys in split_keys( table_keys )
|
47
55
|
out.dump "alter_table #{name.inspect}" do
|
48
|
-
|
49
|
-
out << "add_foreign_key #{key.out_columns}, #{key.out_table_name}#{key.out_canonic_opts}"
|
50
|
-
end
|
56
|
+
keys.each &block
|
51
57
|
end
|
52
58
|
end
|
53
59
|
end
|
54
60
|
|
61
|
+
# Generate code for adding given foreign keys.
|
62
|
+
def dump_add_foreign_keys( out, table_keys )
|
63
|
+
dump_foreign_keys( out, table_keys ) do |key|
|
64
|
+
out << "add_foreign_key #{key.out_columns}, #{key.out_table_name}#{key.out_canonic_opts}"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
55
68
|
# Generate code for dropping given foreign keys.
|
56
69
|
def dump_drop_foreign_keys( out, table_keys )
|
57
|
-
|
58
|
-
out
|
59
|
-
for key in keys
|
60
|
-
out << "drop_foreign_key #{key.out_columns} # #{key.out_table_name}#{key.out_canonic_opts}"
|
61
|
-
end
|
62
|
-
end
|
70
|
+
dump_foreign_keys( out, table_keys ) do |key|
|
71
|
+
out << "drop_foreign_key #{key.out_columns} # #{key.out_table_name}#{key.out_canonic_opts}"
|
63
72
|
end
|
64
73
|
end
|
65
74
|
|
@@ -126,7 +135,7 @@ module Miguel
|
|
126
135
|
if from.allow_null != to.allow_null && to.allow_null
|
127
136
|
out << "set_column_allow_null #{to.out_name}"
|
128
137
|
end
|
129
|
-
if from.type_opts != to.type_opts
|
138
|
+
if from.canonic_type != to.canonic_type || from.type_opts != to.type_opts
|
130
139
|
out << "set_column_type #{to.out_name}, #{to.out_type}#{to.out_opts}"
|
131
140
|
end
|
132
141
|
if from.default != to.default
|
@@ -139,9 +148,7 @@ module Miguel
|
|
139
148
|
|
140
149
|
# Generate code for altering given columns.
|
141
150
|
def dump_alter_columns( out, from_columns, to_columns )
|
142
|
-
|
143
|
-
for from, to in pairs
|
144
|
-
fail "invalid column pair #{from.name} -> #{to.name}" unless from.name == to.name
|
151
|
+
each_pair( :column, from_columns, to_columns ) do |from, to|
|
145
152
|
dump_alter_column( out, from, to )
|
146
153
|
end
|
147
154
|
end
|
@@ -176,9 +183,7 @@ module Miguel
|
|
176
183
|
|
177
184
|
# Generate code for altering given tables.
|
178
185
|
def dump_alter_tables( out, from_tables, to_tables )
|
179
|
-
|
180
|
-
for from, to in pairs
|
181
|
-
fail "invalid table pair #{from.name} -> #{to.name}" unless from.name == to.name
|
186
|
+
each_pair( :table, from_tables, to_tables ) do |from, to|
|
182
187
|
dump_alter_table( out, from, to )
|
183
188
|
end
|
184
189
|
end
|
data/lib/miguel/schema.rb
CHANGED
@@ -9,6 +9,9 @@ module Miguel
|
|
9
9
|
# Class for defining database schema.
|
10
10
|
class Schema
|
11
11
|
|
12
|
+
# String denoting zero time.
|
13
|
+
ZERO_TIME = '0000-00-00 00:00:00'.freeze
|
14
|
+
|
12
15
|
# Module for pretty printing of names, types, and especially options.
|
13
16
|
module Output
|
14
17
|
|
@@ -146,6 +149,7 @@ module Miguel
|
|
146
149
|
:bigint => { :size => 20 },
|
147
150
|
:decimal => { :size => [ 10, 0 ] },
|
148
151
|
:integer => { :unsigned => false },
|
152
|
+
:time => { :only_time => true },
|
149
153
|
:primary_key => { :unsigned => false, :type => :integer },
|
150
154
|
}
|
151
155
|
|
@@ -156,7 +160,7 @@ module Miguel
|
|
156
160
|
# Get the column options in a canonic way.
|
157
161
|
def canonic_opts
|
158
162
|
return {} if type == :primary_key && name.is_a?( Array )
|
159
|
-
o = { :type => canonic_type, :default => default }
|
163
|
+
o = { :type => canonic_type, :default => default, :null => true }
|
160
164
|
o.merge!( DEFAULT_OPTS[ canonic_type ] || {} )
|
161
165
|
o.merge!( opts )
|
162
166
|
o[ :size ] = canonic_size( o[ :size ] )
|
@@ -296,7 +300,7 @@ module Miguel
|
|
296
300
|
# automatically updated, and let the create one to be set manually.
|
297
301
|
# Also, Sequel doesn't currently honor :on_update for column definitions,
|
298
302
|
# so we have to use default literal to make it work. Sigh.
|
299
|
-
timestamp :create_time, :null => false, :default =>
|
303
|
+
timestamp :create_time, :null => false, :default => ZERO_TIME
|
300
304
|
timestamp :update_time, :null => false, :default => Sequel.lit( 'CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP' )
|
301
305
|
else
|
302
306
|
Time :create_time
|
@@ -553,7 +557,7 @@ module Miguel
|
|
553
557
|
# we have to be careful to turn off the MySQL autoupdate behavior.
|
554
558
|
# That's why we have to set defaults explicitly.
|
555
559
|
|
556
|
-
set_defaults :Time, :timestamp, :default =>
|
560
|
+
set_defaults :Time, :timestamp, :default => ZERO_TIME
|
557
561
|
set_defaults :Time?, :timestamp, :default => nil
|
558
562
|
|
559
563
|
self
|
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 + '.pre4'
|
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
|
data/test/data/schema.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Test schema.
|
2
2
|
|
3
|
-
Miguel::Schema.define do
|
3
|
+
Miguel::Schema.define( use_defaults: false ) do
|
4
4
|
|
5
5
|
table :sequel_types do
|
6
6
|
Integer :a0 # integer
|
@@ -17,13 +17,17 @@ Miguel::Schema.define do
|
|
17
17
|
BigDecimal :f2, :size=>10 # numeric(10)
|
18
18
|
BigDecimal :f3, :size=>[10, 2] # numeric(10, 2)
|
19
19
|
Date :g # date
|
20
|
-
DateTime :h
|
21
|
-
Time :i
|
20
|
+
#DateTime :h # timestamp or datetime
|
21
|
+
#Time :i # timestamp or datetime
|
22
|
+
Time :i2, :only_time=>true # time
|
22
23
|
Numeric :j # numeric
|
23
24
|
TrueClass :k # boolean
|
24
25
|
FalseClass :l # boolean
|
25
26
|
end
|
26
27
|
|
28
|
+
set_standard_defaults
|
29
|
+
set_defaults :Custom, :String, fixed: true, size: 3
|
30
|
+
|
27
31
|
table :miguel_types do
|
28
32
|
String :string
|
29
33
|
Text :text
|
@@ -35,7 +39,15 @@ Miguel::Schema.define do
|
|
35
39
|
Bool :bool
|
36
40
|
True :true
|
37
41
|
False :false
|
38
|
-
Time :
|
42
|
+
Time :time
|
43
|
+
Custom :custom
|
44
|
+
end
|
45
|
+
|
46
|
+
table :native_types do
|
47
|
+
date :date, default: '2000-12-31'
|
48
|
+
time :time, default: '23:59:59'
|
49
|
+
datetime :datetime, default: '2037-12-31 23:59:59'
|
50
|
+
timestamp :timestamp, default: '1970-01-02 00:00:00'
|
39
51
|
end
|
40
52
|
|
41
53
|
table :timestamps do
|
@@ -88,6 +100,16 @@ Miguel::Schema.define do
|
|
88
100
|
foreign_key? :user_id, :users
|
89
101
|
end
|
90
102
|
|
103
|
+
table :defaults do
|
104
|
+
String :string, default: 'abc'
|
105
|
+
Integer :int, default: 10
|
106
|
+
Signed :signed, default: -1
|
107
|
+
Unsigned :unsigned, default: 1000
|
108
|
+
Float :float, default: 3.14
|
109
|
+
Bool :bool, default: true
|
110
|
+
Time :time, default: '2037-12-31 23:59:59'
|
111
|
+
end
|
112
|
+
|
91
113
|
join_table :user_id, :users, :simple_id, :simple
|
92
114
|
|
93
115
|
join_table :left_id, :users, :right_id, :users, :self_join do
|
data/test/data/schema.txt
CHANGED
@@ -1,23 +1,22 @@
|
|
1
1
|
table :sequel_types do
|
2
|
-
Integer :a0
|
3
|
-
String :a1
|
4
|
-
String :a2, :
|
5
|
-
String :a3, :
|
6
|
-
String :a4, :
|
7
|
-
String :a5, :
|
8
|
-
File :b
|
9
|
-
Fixnum :c
|
10
|
-
Bignum :d
|
11
|
-
Float :e
|
12
|
-
BigDecimal :f
|
13
|
-
BigDecimal :f2, :
|
14
|
-
BigDecimal :f3, :
|
15
|
-
Date :g
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
FalseClass :l, :null => false
|
2
|
+
Integer :a0
|
3
|
+
String :a1
|
4
|
+
String :a2, :size => 50
|
5
|
+
String :a3, :fixed => true
|
6
|
+
String :a4, :fixed => true, :size => 50
|
7
|
+
String :a5, :text => true
|
8
|
+
File :b
|
9
|
+
Fixnum :c
|
10
|
+
Bignum :d
|
11
|
+
Float :e
|
12
|
+
BigDecimal :f
|
13
|
+
BigDecimal :f2, :size => 10
|
14
|
+
BigDecimal :f3, :size => [10, 2]
|
15
|
+
Date :g
|
16
|
+
Time :i2, :only_time => true
|
17
|
+
Numeric :j
|
18
|
+
TrueClass :k
|
19
|
+
FalseClass :l
|
21
20
|
end
|
22
21
|
table :miguel_types do
|
23
22
|
String :string, :null => false
|
@@ -30,7 +29,14 @@ table :miguel_types do
|
|
30
29
|
TrueClass :bool, :null => false
|
31
30
|
TrueClass :true, :null => false, :default => true
|
32
31
|
TrueClass :false, :null => false, :default => false
|
33
|
-
timestamp :
|
32
|
+
timestamp :time, :null => false, :default => "0000-00-00 00:00:00"
|
33
|
+
String :custom, :null => false, :fixed => true, :size => 3
|
34
|
+
end
|
35
|
+
table :native_types do
|
36
|
+
date :date, :null => false, :default => "2000-12-31"
|
37
|
+
time :time, :null => false, :default => "23:59:59"
|
38
|
+
datetime :datetime, :null => false, :default => "2037-12-31 23:59:59"
|
39
|
+
timestamp :timestamp, :null => false, :default => "1970-01-02 00:00:00"
|
34
40
|
end
|
35
41
|
table :timestamps do
|
36
42
|
timestamp :t1, :null => false, :default => "0000-00-00 00:00:00"
|
@@ -79,6 +85,15 @@ table :null do
|
|
79
85
|
integer :user_id, :null => true, :key => [:id], :unsigned => false, :type => :integer
|
80
86
|
foreign_key [:user_id], :users, :null => true, :key => [:id], :unsigned => false, :type => :integer
|
81
87
|
end
|
88
|
+
table :defaults do
|
89
|
+
String :string, :null => false, :default => "abc"
|
90
|
+
Integer :int, :null => false, :default => 10
|
91
|
+
integer :signed, :null => false, :unsigned => false, :default => -1
|
92
|
+
integer :unsigned, :null => false, :unsigned => true, :default => 1000
|
93
|
+
Float :float, :null => false, :default => 3.14
|
94
|
+
TrueClass :bool, :null => false, :default => true
|
95
|
+
timestamp :time, :null => false, :default => "2037-12-31 23:59:59"
|
96
|
+
end
|
82
97
|
table :simple_users do
|
83
98
|
integer :user_id, :null => false, :key => [:id], :unsigned => false, :type => :integer
|
84
99
|
integer :simple_id, :null => false, :key => [:id], :unsigned => false, :type => :integer
|
data/test/helper.rb
ADDED
data/test/test_dumper.rb
CHANGED
data/test/test_schema.rb
CHANGED
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.pre4
|
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-09-
|
11
|
+
date: 2015-09-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: sequel
|
@@ -69,6 +69,7 @@ files:
|
|
69
69
|
- test/data/simple.rb
|
70
70
|
- test/data/simple.txt
|
71
71
|
- test/data/simple_mysql.txt
|
72
|
+
- test/helper.rb
|
72
73
|
- test/test_dumper.rb
|
73
74
|
- test/test_schema.rb
|
74
75
|
homepage: http://rubygems.org/gems/miguel
|