ridgepole 0.5.3.beta4 → 0.6.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +22 -50
  3. data/bin/ridgepole +8 -18
  4. data/lib/ridgepole/client.rb +10 -16
  5. data/lib/ridgepole/delta.rb +57 -19
  6. data/lib/ridgepole/diff.rb +45 -14
  7. data/lib/ridgepole/dsl_parser.rb +38 -8
  8. data/lib/ridgepole/execute_expander.rb +0 -29
  9. data/lib/ridgepole/ext/mysql_awesome.rb +1 -5
  10. data/lib/ridgepole/version.rb +1 -1
  11. data/lib/ridgepole.rb +10 -2
  12. data/ridgepole.gemspec +3 -8
  13. data/spec/bigint_pk/bigint_pkspec.rb +2 -2
  14. data/spec/cli/ridgepole_spec.rb +3 -5
  15. data/spec/collation/collation_spec.rb +35 -35
  16. data/spec/diff/diff2_spec.rb +18 -18
  17. data/spec/diff/diff_spec.rb +18 -18
  18. data/spec/dump/dump_class_method_spec.rb +18 -18
  19. data/spec/dump/dump_some_tables_spec.rb +12 -12
  20. data/spec/dump/dump_spec.rb +18 -18
  21. data/spec/dump/dump_without_table_options_spec.rb +6 -6
  22. data/spec/migrate/check_orphan_index_spec.rb +8 -8
  23. data/spec/migrate/migrate_add_column2_spec.rb +15 -15
  24. data/spec/migrate/migrate_add_column_spec.rb +40 -40
  25. data/spec/migrate/migrate_change_column2_spec.rb +5 -5
  26. data/spec/migrate/migrate_change_column3_spec.rb +26 -26
  27. data/spec/migrate/migrate_change_column_float_spec.rb +7 -7
  28. data/spec/migrate/migrate_change_column_spec.rb +42 -42
  29. data/spec/migrate/migrate_change_index2_spec.rb +13 -13
  30. data/spec/migrate/migrate_change_index3_spec.rb +26 -26
  31. data/spec/migrate/migrate_change_index4_spec.rb +28 -28
  32. data/spec/migrate/migrate_change_index_spec.rb +46 -46
  33. data/spec/migrate/migrate_change_table_option_spec.rb +2 -2
  34. data/spec/migrate/migrate_create_index_spec.rb +18 -18
  35. data/spec/migrate/migrate_create_table_spec.rb +18 -18
  36. data/spec/migrate/migrate_create_table_with_options_spec.rb +3 -3
  37. data/spec/migrate/migrate_drop_column_and_index_2_spec.rb +35 -35
  38. data/spec/migrate/migrate_drop_column_and_index_spec.rb +33 -33
  39. data/spec/migrate/migrate_drop_column_spec.rb +36 -36
  40. data/spec/migrate/migrate_drop_index_spec.rb +18 -18
  41. data/spec/migrate/migrate_drop_table_spec.rb +24 -24
  42. data/spec/migrate/migrate_duplicate_index_spec.rb +1 -1
  43. data/spec/migrate/migrate_duplicate_table_spec.rb +2 -2
  44. data/spec/migrate/migrate_empty_spec.rb +18 -18
  45. data/spec/migrate/migrate_execute_spec.rb +58 -49
  46. data/spec/migrate/migrate_merge_mode_spec.rb +38 -38
  47. data/spec/migrate/migrate_noop_spec.rb +40 -40
  48. data/spec/migrate/migrate_rename_column_spec.rb +37 -37
  49. data/spec/migrate/migrate_rename_table_spec.rb +39 -39
  50. data/spec/migrate/migrate_same_default_null_spec.rb +4 -4
  51. data/spec/migrate/migrate_same_spec.rb +18 -18
  52. data/spec/migrate/migrate_script_error_spec.rb +8 -8
  53. data/spec/migrate/migrate_skip_rename_column_spec.rb +36 -36
  54. data/spec/migrate/migrate_skip_rename_table_spec.rb +36 -36
  55. data/spec/migrate/migrate_with_ignore_tables_spec.rb +12 -12
  56. data/spec/migrate/migrate_with_pre_post_query_spec.rb +32 -32
  57. data/spec/migrate/migrate_with_tables_spec.rb +12 -12
  58. data/spec/migrate_0/migrate_change_fk_spec.rb +11 -11
  59. data/spec/migrate_0/migrate_create_fk_spec.rb +29 -30
  60. data/spec/migrate_0/migrate_drop_fk_spec.rb +22 -23
  61. data/spec/spec_helper.rb +24 -2
  62. metadata +10 -89
  63. data/lib/ridgepole/ext/foreign_key.rb +0 -121
  64. data/spec/0_diff/dump_disable_unsigned_spec.rb +0 -75
  65. data/spec/comment/comment_spec.rb +0 -179
  66. data/spec/migrate/migrate_same_default_unsigned_spec.rb +0 -63
  67. data/spec/~pkdump/pkdump_spec.rb +0 -67
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4836232269876c37ac6d3c9bdcf2f1495d4620d4
4
- data.tar.gz: f7e856a5c6de4b9ea7ba2e66ac9c9c277e97a2fb
3
+ metadata.gz: 91b851129e798ca93f666b4942b4fa163b061d96
4
+ data.tar.gz: 19d3dd0442a52a650d2d1bfcc8adda3b6755335f
5
5
  SHA512:
6
- metadata.gz: 79e5383609e65c7847ebfcd48c76c86867a3efdb5c0274938febc79cfd73d079952b8738e81e57a3a152221277ad27d84f7b2779b7e5ef0daddbbe0659fd3ec9
7
- data.tar.gz: 87232f87adb82294b2218b8b4b112ab10f54d9293208e305c0ae337cbbf6a0120c2caa4d478e656c3e5d3350fba91bdfb77f9e7a44030ceec43128fd26e50455
6
+ metadata.gz: 5263173c6cca716198521b2c2a0802b6755a7cb9eb414756cf2d3e6fbcce61ac057d7eda74e0c3b95931d4df6ed8554933640f9d4952eea91878aa9193eb7cc4
7
+ data.tar.gz: 0b12d04adeb3b76dd298199a5a88a467a30ff8149734cf5b9d311475bc0b82f482a661d381ff271eb3ca5260c858005cf9193fcb07b30d7a6faa4854a83d80da
data/README.md CHANGED
@@ -27,6 +27,13 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
27
27
  * Add `--enable-mysql-awesome` option ([activerecord-mysql-awesome](https://github.com/kamipo/activerecord-mysql-awesome) is required `>= 0.0.3`)
28
28
  * It is not possible to enable both `--enable-mysql-awesome` and `--enable-migration-comments`, `--enable-mysql-awesome` and `--enable-mysql-unsigned`, `--enable-mysql-awesome` and `--enable-mysql-pkdump`
29
29
  * Fix foreigner version `<= 1.7.1`
30
+ * `>= 0.6.0`
31
+ * Fix rails version `~> 4.2.1`
32
+ * Disable following libraries support:
33
+ * activerecord-mysql-unsigned
34
+ * migration_comments
35
+ * foreigner
36
+ * Disable sqlite support
30
37
 
31
38
  ## Installation
32
39
 
@@ -54,6 +61,9 @@ Usage: ridgepole [options]
54
61
  --table-options OPTIONS
55
62
  --bulk-change
56
63
  --default-int-limit LIMIT
64
+ --default-float-limit LIMIT
65
+ --default-string-limit LIMIT
66
+ --default-text-limit LIMIT
57
67
  --pre-query QUERY
58
68
  --post-query QUERY
59
69
  -e, --export
@@ -65,13 +75,8 @@ Usage: ridgepole [options]
65
75
  -o, --output FILE
66
76
  -t, --tables TABLES
67
77
  --ignore-tables TABLES
68
- --enable-mysql-unsigned
69
- --enable-mysql-pkdump
70
- --enable-foreigner
71
- --enable-migration-comments
72
78
  --enable-mysql-awesome
73
79
  --mysql-awesome-unsigned-pk
74
- --normalize-mysql-float
75
80
  --dump-without-table-options
76
81
  -r, --require LIBS
77
82
  --log-file LOG_FILE
@@ -96,7 +101,7 @@ $ ridgepole -c config.yml --export -o Schemafile # or `ridgepole -c '{adapter: m
96
101
  Export Schema to `Schemafile`
97
102
 
98
103
  $ cat Schemafile
99
- create_table "articles", force: true do |t|
104
+ create_table "articles", force: :cascade do |t|
100
105
  t.string "title"
101
106
  t.text "text"
102
107
  t.datetime "created_at"
@@ -117,7 +122,7 @@ index f5848b9..c266fed 100644
117
122
  --- a/Schemafile
118
123
  +++ b/Schemafile
119
124
  @@ -1,6 +1,7 @@
120
- create_table "articles", force: true do |t|
125
+ create_table "articles", force: :cascade do |t|
121
126
  t.string "title"
122
127
  t.text "text"
123
128
  + t.text "author"
@@ -139,7 +144,7 @@ Apply `Schemafile`
139
144
 
140
145
  ## Rename
141
146
  ```ruby
142
- create_table "articles", force: true do |t|
147
+ create_table "articles", force: :cascade do |t|
143
148
  t.string "title"
144
149
  t.text "desc", renamed_from: "text"
145
150
  t.text "author"
@@ -147,7 +152,7 @@ create_table "articles", force: true do |t|
147
152
  t.datetime "updated_at"
148
153
  end
149
154
 
150
- create_table "user_comments", force: true, renamed_from: "comments" do |t|
155
+ create_table "user_comments", force: :cascade, renamed_from: "comments" do |t|
151
156
  t.string "commenter"
152
157
  t.text "body"
153
158
  t.integer "article_id"
@@ -157,39 +162,25 @@ end
157
162
  ```
158
163
 
159
164
  ## Foreign Key
160
- You can use the foreign key by passing `--enable-foreigner` ([foreigner](https://github.com/matthuhiggins/foreigner) is required)
161
-
162
165
  ```ruby
163
- create_table "parent", force: true do |t|
166
+ create_table "parent", force: :cascade do |t|
164
167
  end
165
168
 
166
- create_table "child", id: false, force: true do |t|
169
+ create_table "child", id: false, force: :cascade do |t|
167
170
  t.integer "id"
168
171
  t.integer "parent_id"
169
172
  end
170
173
 
171
174
  add_index "child", ["parent_id"], name: "par_ind", using: :btree
172
175
 
173
- add_foreign_key "child", "parent", name: "child_ibfk_1", dependent: :delete
174
- ```
175
-
176
- ## Comment
177
- You can use the table/column comment by passing `--enable-migration-comments` ([migration_comments](https://github.com/pinnymz/migration_comments) is required)
178
-
179
- ```ruby
180
- create_table "articles", force: true, comment: "table comment" do |t|
181
- t.string "title", comment: "column comment"
182
- t.text "text"
183
- t.datetime "created_at"
184
- t.datetime "updated_at"
185
- end
176
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
186
177
  ```
187
178
 
188
179
  ## Collation
189
180
  You can use the column collation by passing `--enable-mysql-awesome` ([activerecord-mysql-awesome](https://github.com/kamipo/activerecord-mysql-awesome) is required)
190
181
 
191
182
  ```ruby
192
- create_table "articles", force: true, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
183
+ create_table "articles", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
193
184
  t.string "title", collation: "ascii_bin"
194
185
  t.text "text", null: false, collation: "utf8mb4_bin"
195
186
  t.datetime "created_at"
@@ -197,32 +188,13 @@ create_table "articles", force: true, options: "ENGINE=InnoDB DEFAULT CHARSET=ut
197
188
  end
198
189
  ```
199
190
 
200
- ## bigint support
201
- Export of `bigint` PK is enabled by passing `--enable-mysql-pkdump` ([activerecord-mysql-pkdump](https://github.com/winebarrel/activerecord-mysql-pkdump) is required)
202
- ```ruby
203
- create_table "books", id: "bigint(20) PRIMARY KEY auto_increment", force: true do |t|
204
- t.string "title", null: false
205
- t.integer "author_id", null: false
206
- t.datetime "created_at"
207
- t.datetime "updated_at"
208
- end
209
- ```
210
-
211
- If you use `--enable-mysql-awesome`:
212
-
213
- ```ruby
214
- create_table "books", id: :bigint, force: true do |t|
215
- ...
216
- end
217
- ```
218
-
219
191
  ## Execute
220
192
  ```ruby
221
- create_table "authors", force: true do |t|
193
+ create_table "authors", force: :cascade do |t|
222
194
  t.string "name", null: false
223
195
  end
224
196
 
225
- create_table "books", force: true do |t|
197
+ create_table "books", force: :cascade do |t|
226
198
  t.string "title", null: false
227
199
  t.integer "author_id", unsigned: true, null: false
228
200
  end
@@ -255,7 +227,7 @@ remove_column("articles", "author")
255
227
  ### Reverse diff
256
228
  ```sh
257
229
  $ cat file1.schema
258
- create_table "articles", force: true do |t|
230
+ create_table "articles", force: :cascade do |t|
259
231
  t.string "title"
260
232
  t.text "text"
261
233
  t.datetime "created_at"
@@ -263,7 +235,7 @@ create_table "articles", force: true do |t|
263
235
  end
264
236
 
265
237
  $ cat file2.schema
266
- create_table "articles", force: true do |t|
238
+ create_table "articles", force: :cascade do |t|
267
239
  t.string "title"
268
240
  t.text "desc", renamed_from: "text"
269
241
  t.text "author"
data/bin/ridgepole CHANGED
@@ -66,9 +66,13 @@ ARGV.options do |opt|
66
66
  raise "Cannot use `bulk-change` in `merge`" if options[:merge]
67
67
  options[:bulk_change] = true
68
68
  }
69
- opt.on('', '--default-int-limit LIMIT', Integer) {|v|
70
- options[:default_int_limit] = v
71
- }
69
+
70
+ Ridgepole::DEFAULTS_LIMITS.keys.each do |column_type|
71
+ opt.on('', "--default-#{column_type == :integer ? :int : column_type}-limit LIMIT", Integer) {|v|
72
+ options[:"default_#{column_type}_limit"] = v
73
+ }
74
+ end
75
+
72
76
  opt.on('', '--pre-query QUERY') {|v| options[:pre_query] = v }
73
77
  opt.on('', '--post-query QUERY') {|v| options[:post_query] = v }
74
78
  opt.on('-e', '--export') { set_mode[:export] }
@@ -91,13 +95,8 @@ ARGV.options do |opt|
91
95
  opt.on('-o', '--output FILE') {|v| output_file = v }
92
96
  opt.on('-t', '--tables TABLES', Array) {|v| options[:tables] = v }
93
97
  opt.on('', '--ignore-tables TABLES', Array) {|v| options[:ignore_tables] = v.map {|i| Regexp.new(i) } }
94
- opt.on('', '--enable-mysql-unsigned') { options[:enable_mysql_unsigned] = true }
95
- opt.on('', '--enable-mysql-pkdump') { options[:enable_mysql_pkdump] = true }
96
- opt.on('', '--enable-foreigner') { options[:enable_foreigner] = true }
97
- opt.on('', '--enable-migration-comments') { options[:enable_migration_comments] = true }
98
98
  opt.on('', '--enable-mysql-awesome') { options[:enable_mysql_awesome] = true }
99
99
  opt.on('', '--mysql-awesome-unsigned-pk') { options[:mysql_awesome_unsigned_pk] = true }
100
- opt.on('', '--normalize-mysql-float') { options[:normalize_mysql_float] = true }
101
100
  opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = true }
102
101
  opt.on('-r' , '--require LIBS', Array) {|v| v.each {|i| require i } }
103
102
  opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
@@ -115,18 +114,9 @@ ARGV.options do |opt|
115
114
  puts opt.help
116
115
  exit 1
117
116
  end
118
-
119
- if options[:enable_mysql_awesome]
120
- if options[:enable_migration_comments]
121
- raise "It is not possible to enable both `--enable-mysql-awesome` and `--enable-migration-comments`"
122
- elsif options[:enable_mysql_unsigned]
123
- raise "It is not possible to enable both `--enable-mysql-awesome` and `--enable-mysql-unsigned`"
124
- elsif options[:enable_mysql_pkdump]
125
- raise "It is not possible to enable both `--enable-mysql-awesome` and `--enable-mysql-pkdump`"
126
- end
127
- end
128
117
  rescue => e
129
118
  $stderr.puts("[ERROR] #{e.message}")
119
+ puts e.backtrace
130
120
  exit 1
131
121
  end
132
122
  end
@@ -1,24 +1,14 @@
1
1
  class Ridgepole::Client
2
2
  def initialize(conn_spec, options = {})
3
+ Ridgepole::Client.normalize_options(options)
3
4
  @options = options
5
+
4
6
  ActiveRecord::Base.establish_connection(conn_spec)
5
7
  Ridgepole::ExecuteExpander.expand_execute(ActiveRecord::Base.connection)
6
8
  @dumper = Ridgepole::Dumper.new(@options)
7
9
  @parser = Ridgepole::DSLParser.new(@options)
8
10
  @diff = Ridgepole::Diff.new(@options)
9
11
 
10
- if @options[:enable_mysql_unsigned]
11
- require 'activerecord-mysql-unsigned/base'
12
- end
13
-
14
- if @options[:enable_mysql_pkdump]
15
- require 'activerecord-mysql-pkdump'
16
- end
17
-
18
- if @options[:enable_migration_comments]
19
- require 'migration_comments'
20
- end
21
-
22
12
  if @options[:enable_mysql_awesome]
23
13
  require 'activerecord/mysql/awesome/base'
24
14
 
@@ -26,10 +16,6 @@ class Ridgepole::Client
26
16
  require 'ridgepole/ext/mysql_awesome.rb'
27
17
  end
28
18
  end
29
-
30
- if @options[:enable_foreigner]
31
- Ridgepole::ForeignKey.init
32
- end
33
19
  end
34
20
 
35
21
  def dump(&block)
@@ -51,6 +37,7 @@ class Ridgepole::Client
51
37
 
52
38
  class << self
53
39
  def diff(dsl_or_config1, dsl_or_config2, options = {})
40
+ normalize_options(options)
54
41
  logger = Ridgepole::Logger.instance
55
42
 
56
43
  logger.verbose_info('# Parse DSL1')
@@ -64,10 +51,17 @@ class Ridgepole::Client
64
51
  end
65
52
 
66
53
  def dump(conn_spec, options = {}, &block)
54
+ normalize_options(options)
67
55
  client = self.new(conn_spec, options)
68
56
  client.dump(&block)
69
57
  end
70
58
 
59
+ def normalize_options(options)
60
+ Ridgepole::DEFAULTS_LIMITS.each do |column_type, limit|
61
+ options[:"default_#{column_type}_limit"] ||= limit
62
+ end
63
+ end
64
+
71
65
  private
72
66
 
73
67
  def load_definition(dsl_or_config, options = {})
@@ -14,6 +14,7 @@ class Ridgepole::Delta
14
14
  end
15
15
 
16
16
  open(log_file, 'wb') {|f| f.puts JSON.pretty_generate(result) }
17
+ result
17
18
  else
18
19
  migrate0(options)
19
20
  end
@@ -59,7 +60,7 @@ class Ridgepole::Delta
59
60
  buf = StringIO.new
60
61
 
61
62
  callback = proc do |sql, name|
62
- buf.puts sql if sql =~ /\A(CREATE|ALTER|DROP|RENAME|COPY)\b/i
63
+ buf.puts sql if sql =~ /\A(CREATE|ALTER|DROP|RENAME)\b/i
63
64
  end
64
65
 
65
66
  Ridgepole::ExecuteExpander.without_operation(callback) do
@@ -204,10 +205,7 @@ create_table(#{table_name.inspect}, #{options.inspect}) do |t|
204
205
  definition.each do |column_name, column_attrs|
205
206
  column_type = column_attrs.fetch(:type)
206
207
  column_options = column_attrs[:options] || {}
207
-
208
- if @options[:default_int_limit] and column_type == :integer
209
- column_options[:limit] ||= @options[:default_int_limit]
210
- end
208
+ normalize_limit(column_type, column_options)
211
209
 
212
210
  buf.puts(<<-EOS)
213
211
  t.#{column_type}(#{column_name.inspect}, #{column_options.inspect})
@@ -226,11 +224,9 @@ end
226
224
  end
227
225
  end
228
226
 
229
- if @options[:enable_foreigner] and not (foreign_keys = attrs[:foreign_keys] || {}).empty?
230
- append_change_table(table_name, buf) do
231
- foreign_keys.each do |foreign_key_name, foreign_key_attrs|
232
- Ridgepole::ForeignKey.append_add_foreign_key(table_name, foreign_key_name, foreign_key_attrs, buf, @options)
233
- end
227
+ unless (foreign_keys = attrs[:foreign_keys] || {}).empty?
228
+ foreign_keys.each do |foreign_key_name, foreign_key_attrs|
229
+ append_add_foreign_key(table_name, foreign_key_name, foreign_key_attrs, buf, @options)
234
230
  end
235
231
  end
236
232
 
@@ -254,15 +250,21 @@ drop_table(#{table_name.inspect})
254
250
  end
255
251
 
256
252
  def append_change(table_name, attrs, buf)
257
- append_change_table(table_name, buf) do
258
- append_change_definition(table_name, attrs[:definition] || {}, buf)
259
- append_change_indices(table_name, attrs[:indices] || {}, buf)
253
+ definition = attrs[:definition] || {}
254
+ indices = attrs[:indices] || {}
255
+ foreign_keys = attrs[:foreign_keys] || {}
260
256
 
261
- if @options[:enable_foreigner]
262
- Ridgepole::ForeignKey.append_change_foreign_keys(table_name, attrs[:foreign_keys] || {}, buf, @options)
257
+ if not definition.empty? or not indices.empty?
258
+ append_change_table(table_name, buf) do
259
+ append_change_definition(table_name, definition, buf)
260
+ append_change_indices(table_name, indices, buf)
263
261
  end
264
262
  end
265
263
 
264
+ unless foreign_keys.empty?
265
+ append_change_foreign_keys(table_name, foreign_keys, buf, @options)
266
+ end
267
+
266
268
  buf.puts
267
269
  end
268
270
 
@@ -293,10 +295,7 @@ drop_table(#{table_name.inspect})
293
295
  def append_add_column(table_name, column_name, attrs, buf)
294
296
  type = attrs.fetch(:type)
295
297
  options = attrs[:options] || {}
296
-
297
- if @options[:default_int_limit] and type == :integer
298
- options[:limit] ||= @options[:default_int_limit]
299
- end
298
+ normalize_limit(type, options)
300
299
 
301
300
  if @options[:bulk_change]
302
301
  buf.puts(<<-EOS)
@@ -389,7 +388,46 @@ remove_index(#{table_name.inspect}, #{target.inspect})
389
388
  end
390
389
  end
391
390
 
391
+ def append_change_foreign_keys(table_name, delta, buf, options)
392
+ (delta[:delete] || {}).each do |foreign_key_name, attrs|
393
+ append_remove_foreign_key(table_name, foreign_key_name, attrs, buf, options)
394
+ end
395
+
396
+ (delta[:add] || {}).each do |foreign_key_name, attrs|
397
+ append_add_foreign_key(table_name, foreign_key_name, attrs, buf, options)
398
+ end
399
+ end
400
+
401
+ def append_add_foreign_key(table_name, foreign_key_name, attrs, buf, options)
402
+ to_table = attrs.fetch(:to_table)
403
+ attrs_options = attrs[:options] || {}
404
+
405
+ buf.puts(<<-EOS)
406
+ add_foreign_key(#{table_name.inspect}, #{to_table.inspect}, #{attrs_options.inspect})
407
+ EOS
408
+ end
409
+
410
+ def append_remove_foreign_key(table_name, foreign_key_name, attrs, buf, options)
411
+ attrs_options = attrs[:options] || {}
412
+ target = {:name => attrs_options.fetch(:name)}
413
+
414
+ buf.puts(<<-EOS)
415
+ remove_foreign_key(#{table_name.inspect}, #{target.inspect})
416
+ EOS
417
+ end
418
+
392
419
  def delta_execute
393
420
  @delta[:execute] || []
394
421
  end
422
+
423
+ def normalize_limit(column_type, column_options)
424
+ Ridgepole::DEFAULTS_LIMITS.each do |default_column_type, limit|
425
+ default_limit = @options[:"default_#{default_column_type}_limit"]
426
+ next if default_limit <= 0
427
+
428
+ if column_type == default_column_type
429
+ column_options[:limit] ||= default_limit
430
+ end
431
+ end
432
+ end
395
433
  end
@@ -78,10 +78,7 @@ class Ridgepole::Diff
78
78
  scan_options_change(table_name, from[:options], to[:options], table_delta)
79
79
  scan_definition_change(from[:definition], to[:definition], from[:indices], table_delta)
80
80
  scan_indices_change(from[:indices], to[:indices], to[:definition], table_delta, from[:options], to[:options])
81
-
82
- if @options[:enable_foreigner]
83
- Ridgepole::ForeignKey.scan_foreign_keys_change(from[:foreign_keys], to[:foreign_keys], table_delta, @options)
84
- end
81
+ scan_foreign_keys_change(from[:foreign_keys], to[:foreign_keys], table_delta, @options)
85
82
 
86
83
  unless table_delta.empty?
87
84
  delta[:change] ||= {}
@@ -243,10 +240,13 @@ class Ridgepole::Diff
243
240
  opts = attrs[:options]
244
241
  opts[:null] = true unless opts.has_key?(:null)
245
242
 
246
- # XXX: MySQL only?
247
- case attrs[:type]
248
- when :string
249
- opts.delete(:limit) if opts[:limit] == 255
243
+ Ridgepole::DEFAULTS_LIMITS.keys.each do |column_type|
244
+ default_limit = @options[:"default_#{column_type}_limit"]
245
+ next if default_limit <= 0
246
+
247
+ if attrs[:type] == column_type and opts[:limit] == default_limit
248
+ opts.delete(:limit)
249
+ end
250
250
  end
251
251
 
252
252
  # XXX: MySQL only?
@@ -254,14 +254,9 @@ class Ridgepole::Diff
254
254
  opts[:default] = nil
255
255
  end
256
256
 
257
- # XXX: MySQL only?
258
- if @options[:enable_mysql_unsigned] or @options[:enable_mysql_awesome]
257
+ if @options[:enable_mysql_awesome]
259
258
  opts[:unsigned] = false unless opts.has_key?(:unsigned)
260
259
  end
261
-
262
- if @options[:normalize_mysql_float] and attrs[:type] == :float
263
- opts.delete(:limit) if opts[:limit] == 24
264
- end
265
260
  end
266
261
 
267
262
  def normalize_index_options!(opts)
@@ -276,4 +271,40 @@ class Ridgepole::Diff
276
271
 
277
272
  expected_columns.all? {|i| actual_columns.include?(i) }
278
273
  end
274
+
275
+ def scan_foreign_keys_change(from, to, table_delta, options)
276
+ from = (from || {}).dup
277
+ to = (to || {}).dup
278
+ foreign_keys_delta = {}
279
+
280
+ to.each do |foreign_key_name, to_attrs|
281
+ from_attrs = from.delete(foreign_key_name)
282
+
283
+ if from_attrs
284
+ if from_attrs != to_attrs
285
+ foreign_keys_delta[:add] ||= {}
286
+ foreign_keys_delta[:add][foreign_key_name] = to_attrs
287
+
288
+ unless options[:merge]
289
+ foreign_keys_delta[:delete] ||= {}
290
+ foreign_keys_delta[:delete][foreign_key_name] = from_attrs
291
+ end
292
+ end
293
+ else
294
+ foreign_keys_delta[:add] ||= {}
295
+ foreign_keys_delta[:add][foreign_key_name] = to_attrs
296
+ end
297
+ end
298
+
299
+ unless options[:merge]
300
+ from.each do |foreign_key_name, from_attrs|
301
+ foreign_keys_delta[:delete] ||= {}
302
+ foreign_keys_delta[:delete][foreign_key_name] = from_attrs
303
+ end
304
+ end
305
+
306
+ unless foreign_keys_delta.empty?
307
+ table_delta[:foreign_keys] = foreign_keys_delta
308
+ end
309
+ end
279
310
  end
@@ -22,10 +22,12 @@ class Ridgepole::DSLParser
22
22
  }
23
23
  end
24
24
 
25
+ # https://github.com/winebarrel/rails/blob/v4.2.1/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L274
25
26
  TYPES = [
26
27
  :string,
27
28
  :text,
28
29
  :integer,
30
+ :bigint,
29
31
  :float,
30
32
  :decimal,
31
33
  :datetime,
@@ -33,7 +35,7 @@ class Ridgepole::DSLParser
33
35
  :time,
34
36
  :date,
35
37
  :binary,
36
- :boolean
38
+ :boolean,
37
39
  ]
38
40
 
39
41
  TYPES.each do |column_type|
@@ -53,9 +55,11 @@ class Ridgepole::DSLParser
53
55
  def references(*args)
54
56
  options = args.extract_options!
55
57
  polymorphic = options.delete(:polymorphic)
58
+ type = options.delete(:type) || :integer
59
+
56
60
  args.each do |col|
57
- column("#{col}_id", :integer, options)
58
- column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) unless polymorphic.nil?
61
+ column("#{col}_id", type, options)
62
+ column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
59
63
  end
60
64
  end
61
65
  alias :belongs_to :references
@@ -120,6 +124,28 @@ class Ridgepole::DSLParser
120
124
  }
121
125
  end
122
126
 
127
+ def add_foreign_key(from_table, to_table, options = {})
128
+ unless options[:name]
129
+ raise "Foreign key name in `#{from_table}` is undefined"
130
+ end
131
+
132
+ from_table = from_table.to_s
133
+ to_table = to_table.to_s
134
+ options[:name] = options[:name].to_s
135
+ @__definition[from_table] ||= {}
136
+ @__definition[from_table][:foreign_keys] ||= {}
137
+ idx = options[:name]
138
+
139
+ if @__definition[from_table][:foreign_keys][idx]
140
+ raise "Foreign Key `#{from_table}(#{idx})` already defined"
141
+ end
142
+
143
+ @__definition[from_table][:foreign_keys][idx] = {
144
+ :to_table => to_table,
145
+ :options => options,
146
+ }
147
+ end
148
+
123
149
  def require(file)
124
150
  schemafile = (file =~ %r|\A/|) ? file : File.join(@__working_dir, file)
125
151
 
@@ -147,11 +173,7 @@ class Ridgepole::DSLParser
147
173
  def parse(dsl, opts = {})
148
174
  definition, execute = Context.eval(dsl, opts)
149
175
  check_orphan_index(definition)
150
-
151
- if @options[:enable_foreigner]
152
- Ridgepole::ForeignKey.check_orphan_foreign_key(definition)
153
- end
154
-
176
+ check_orphan_foreign_key(definition)
155
177
  [definition, execute]
156
178
  end
157
179
 
@@ -164,4 +186,12 @@ class Ridgepole::DSLParser
164
186
  end
165
187
  end
166
188
  end
189
+
190
+ def check_orphan_foreign_key(definition)
191
+ definition.each do |table_name, attrs|
192
+ if attrs[:foreign_keys] and not attrs[:definition]
193
+ raise "Table `#{table_name}` to create the foreign key is not defined: #{attrs[:foreign_keys].keys.join(',')}"
194
+ end
195
+ end
196
+ end
167
197
  end
@@ -69,32 +69,3 @@ module ActiveRecord::ConnectionAdapters::SchemaStatements
69
69
  end
70
70
  alias_method_chain :index_name_exists?, :noop
71
71
  end
72
-
73
- begin
74
- require 'active_record/connection_adapters/sqlite3_adapter'
75
-
76
- class ActiveRecord::ConnectionAdapters::SQLite3Adapter
77
- def exec_query_with_noop(sql, name = nil, binds = [])
78
- if Ridgepole::ExecuteExpander.noop
79
- if (callback = Ridgepole::ExecuteExpander.callback)
80
- callback.call(sql, name)
81
- end
82
- else
83
- exec_query_without_noop(sql, name, binds)
84
- end
85
- end
86
- alias_method_chain :exec_query, :noop
87
-
88
- def copy_table_with_noop(from, to, options = {})
89
- if Ridgepole::ExecuteExpander.noop
90
- if (callback = Ridgepole::ExecuteExpander.callback)
91
- callback.call("COPY TABLE #{from} TO #{to}")
92
- end
93
- else
94
- copy_table_without_noop(from, to, options)
95
- end
96
- end
97
- alias_method_chain :copy_table, :noop
98
- end
99
- rescue LoadError
100
- end
@@ -3,10 +3,6 @@ class ActiveRecord::ConnectionAdapters::TableDefinition
3
3
  alias primary_key_without_unsigned primary_key
4
4
 
5
5
  def primary_key(name, type = :primary_key, options = {})
6
- if options.has_key?(:id)
7
- primary_key_without_unsigned(name, type, options)
8
- else
9
- primary_key_without_unsigned(name, type, options.merge(primary_key: true).reverse_merge(unsigned: true))
10
- end
6
+ primary_key_without_unsigned(name, type, options.merge(primary_key: true).reverse_merge(unsigned: true))
11
7
  end
12
8
  end
@@ -1,3 +1,3 @@
1
1
  module Ridgepole
2
- VERSION = '0.5.3.beta4'
2
+ VERSION = '0.6.0.beta'
3
3
  end
data/lib/ridgepole.rb CHANGED
@@ -6,14 +6,22 @@ require 'active_record'
6
6
  require 'active_support'
7
7
  require 'active_support/core_ext'
8
8
 
9
- module Ridgepole; end
9
+ module Ridgepole
10
+ # for MySQL
11
+ DEFAULTS_LIMITS = {
12
+ :integer => 4,
13
+ :float => 24,
14
+ :string => 255,
15
+ :text => 65535,
16
+ }
17
+ end
18
+
10
19
  require 'ridgepole/client'
11
20
  require 'ridgepole/delta'
12
21
  require 'ridgepole/diff'
13
22
  require 'ridgepole/dsl_parser'
14
23
  require 'ridgepole/dumper'
15
24
  require 'ridgepole/execute_expander'
16
- require 'ridgepole/ext/foreign_key'
17
25
  require 'ridgepole/logger'
18
26
  require 'ridgepole/migration_ext'
19
27
  require 'ridgepole/schema_dumper_ext'