ridgepole 0.7.0.beta7 → 0.7.0.beta8

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b3d9d5631f7b5f23b80eab12746ba43042bbf7c9
4
- data.tar.gz: 8694a5b48a72f4b0e20ad78d50b18fc2ff150c25
3
+ metadata.gz: 6379a32db8dc48969ccf8c184534eccdea98fdd0
4
+ data.tar.gz: efbb7beeb7656df70ed62a102585f7ef2bbe17d7
5
5
  SHA512:
6
- metadata.gz: 0c95e9f942e81fd62e1c437e9a1501fb0bd95c4849c8deb0208d100c1cb3a0d326b4c2ee9d11838b58a63817413873727cb043be3195fe5c964bd2f7b007ca4a
7
- data.tar.gz: f8f021d5b87e6183438885afc1f6e5a274f5e57ec336b9dd2f2b7fbafdfba278c57cad6b630114e24347fd04a27c2224ecaf0c5b45f6b2d9741e48aaa8f9fbb6
6
+ metadata.gz: 0dfbe2a44db029662943ad2f447e4d72a26f9d970807336f0cce0806cf1758337e1f65587cd9ba0b7496887e87ebcd82d0a42cec842fd2443e90bb5b8c4cf63b
7
+ data.tar.gz: 4ba605415d1d48c00eb462f9dc6573c1ee131c456eb9cc215f471604b401f11972a4023ee6c14664a2c23a21a274c6b466c9e78564092cc63e034225f2ba54bb
data/README.md CHANGED
@@ -9,7 +9,7 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
9
9
  [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=0.6)](https://travis-ci.org/winebarrel/ridgepole)
10
10
  [![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.6)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.6)
11
11
 
12
- [![Edge Version](https://img.shields.io/badge/edge_version-0.7.0.beta7-brightgreen.svg)](https://rubygems.org/gems/ridgepole/versions/0.7.0.beta7)
12
+ [![Edge Version](https://img.shields.io/badge/edge_version-0.7.0.beta8-brightgreen.svg)](https://rubygems.org/gems/ridgepole/versions/0.7.0.beta8)
13
13
  [![Build Status](https://travis-ci.org/winebarrel/ridgepole.svg?branch=0.7)](https://travis-ci.org/winebarrel/ridgepole)
14
14
  [![Coverage Status](https://coveralls.io/repos/github/winebarrel/ridgepole/badge.svg?branch=0.7)](https://coveralls.io/github/winebarrel/ridgepole?branch=0.7)
15
15
 
@@ -75,6 +75,7 @@ It defines DB schema using [Rails DSL](http://guides.rubyonrails.org/migrations.
75
75
  * Add `--mysql-change-table-options` option
76
76
  * Pass config from env
77
77
  * Fix change fk order
78
+ * Add `--check-relation-type` option
78
79
 
79
80
  ## Installation
80
81
 
@@ -126,6 +127,7 @@ Usage: ridgepole [options]
126
127
  --index-removed-drop-column
127
128
  --skip-drop-table
128
129
  --mysql-change-table-options
130
+ --check-relation-type DEF_PK
129
131
  -r, --require LIBS
130
132
  --log-file LOG_FILE
131
133
  --verbose
@@ -350,6 +352,31 @@ Apply `Schemafile`
350
352
  -> 0.0243s
351
353
  ```
352
354
 
355
+ ## Relation column type check
356
+
357
+ ```ruby
358
+ create_table "employees", force: :cascade do |t|
359
+ t.integer "emp_no", null: false
360
+ t.string "first_name", limit: 14, null: false
361
+ t.string "last_name", limit: 16, null: false
362
+ end
363
+
364
+ create_table "dept_manager", force: :cascade do |t|
365
+ t.integer "employee_id"
366
+ t.string "dept_no", limit: 4, null: false
367
+ end
368
+ ```
369
+
370
+ ```sh
371
+ $ ridgepole -a -c database.yml --check-relation-type bigint # default primary key type (e.g. `<5.1`: integer, `>=5.1`: bigint for MySQL)
372
+ Apply `Schemafile`
373
+ ...
374
+ [WARNING] Relation column type is different.
375
+ employees.id: bigint
376
+ dept_manager.employee_id: integer
377
+ ...
378
+ ```
379
+
353
380
  ## Running tests
354
381
 
355
382
  ```sh
data/bin/ridgepole CHANGED
@@ -110,22 +110,23 @@ ARGV.options do |opt|
110
110
  ARGV.shift
111
111
  diff_files = [diff_arg1, diff_arg2]
112
112
  }
113
- opt.on('', '--reverse') { options[:reverse] = true }
114
- opt.on('', '--with-apply') { diff_with_apply = true }
115
- opt.on('-o', '--output SCHEMAFILE') {|v| output_file = v }
116
- opt.on('-t', '--tables TABLES', Array) {|v| options[:tables] = v }
117
- opt.on('', '--ignore-tables REGEX_LIST', Array) {|v| options[:ignore_tables] = v.map {|i| Regexp.new(i) } }
118
- opt.on('', '--mysql-use-alter') { options[:mysql_use_alter] = true }
119
- opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = true }
120
- opt.on('', '--dump-with-default-fk-name') { options[:dump_with_default_fk_name] = true }
121
- opt.on('', '--index-removed-drop-column') { options[:index_removed_drop_column] = true }
122
- opt.on('', '--skip-drop-table') { options[:skip_drop_table] = true }
123
- opt.on('', '--mysql-change-table-options') { options[:mysql_change_table_options] = true }
124
- opt.on('-r', '--require LIBS', Array) {|v| v.each {|i| require i } }
125
- opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
126
- opt.on('' , '--verbose') { Ridgepole::Logger.verbose = true }
127
- opt.on('' , '--debug') { options[:debug] = true }
128
- opt.on('' , '--[no-]color') {|v| options[:color] = v }
113
+ opt.on('', '--reverse') { options[:reverse] = true }
114
+ opt.on('', '--with-apply') { diff_with_apply = true }
115
+ opt.on('-o', '--output SCHEMAFILE') {|v| output_file = v }
116
+ opt.on('-t', '--tables TABLES', Array) {|v| options[:tables] = v }
117
+ opt.on('', '--ignore-tables REGEX_LIST', Array) {|v| options[:ignore_tables] = v.map {|i| Regexp.new(i) } }
118
+ opt.on('', '--mysql-use-alter') { options[:mysql_use_alter] = true }
119
+ opt.on('', '--dump-without-table-options') { options[:dump_without_table_options] = true }
120
+ opt.on('', '--dump-with-default-fk-name') { options[:dump_with_default_fk_name] = true }
121
+ opt.on('', '--index-removed-drop-column') { options[:index_removed_drop_column] = true }
122
+ opt.on('', '--skip-drop-table') { options[:skip_drop_table] = true }
123
+ opt.on('', '--mysql-change-table-options') { options[:mysql_change_table_options] = true }
124
+ opt.on('', '--check-relation-type DEF_PK') {|v| options[:check_relation_type] = v }
125
+ opt.on('-r', '--require LIBS', Array) {|v| v.each {|i| require i } }
126
+ opt.on('' , '--log-file LOG_FILE') {|v| options[:log_file] = v }
127
+ opt.on('' , '--verbose') { Ridgepole::Logger.verbose = true }
128
+ opt.on('' , '--debug') { options[:debug] = true }
129
+ opt.on('' , '--[no-]color') {|v| options[:color] = v }
129
130
 
130
131
  opt.on('-v', '--version') {
131
132
  puts opt.ver
@@ -13,11 +13,15 @@ class Ridgepole::Diff
13
13
  end
14
14
 
15
15
  delta = {}
16
+ relation_info = {}
17
+
16
18
  scan_table_rename(from, to, delta)
17
19
  # for reverse option
18
20
  scan_table_rename(to, from, delta)
19
21
 
20
22
  to.each do |table_name, to_attrs|
23
+ collect_relation_info!(table_name, to_attrs, relation_info)
24
+
21
25
  next unless target?(table_name)
22
26
 
23
27
  if (from_attrs = from.delete(table_name))
@@ -34,6 +38,8 @@ class Ridgepole::Diff
34
38
  end
35
39
  end
36
40
 
41
+ scan_relation_info(relation_info)
42
+
37
43
  unless @options[:merge] or @options[:skip_drop_table]
38
44
  from.each do |table_name, from_attrs|
39
45
  next unless target?(table_name)
@@ -419,4 +425,63 @@ class Ridgepole::Diff
419
425
  diffy.to_s(@options[:color] ? :color : :text).gsub(/\s+\z/m, '')
420
426
  end
421
427
 
428
+ def collect_relation_info!(table_name, table_attr, relation_info)
429
+ return unless @options[:check_relation_type]
430
+
431
+ attrs_by_column = {}
432
+ definition = table_attr[:definition] || {}
433
+
434
+ definition.each do |column_name, column_attrs|
435
+ if column_name =~ /\w+_id\z/
436
+ attrs_by_column[column_name] = column_attrs.dup
437
+ end
438
+ end
439
+
440
+ relation_info[table_name] = {
441
+ :options => table_attr[:options] || {},
442
+ :columns => attrs_by_column,
443
+ }
444
+ end
445
+
446
+ def scan_relation_info(relation_info)
447
+ return unless @options[:check_relation_type]
448
+
449
+ relation_info.each do |child_table, table_info|
450
+ next unless target?(child_table)
451
+
452
+ attrs_by_column = table_info.fetch(:columns)
453
+ parent_table_info = nil
454
+
455
+ attrs_by_column.each do |column_name, column_attrs|
456
+ parent_table = column_name.sub(/_id\z/, '')
457
+
458
+ [parent_table.pluralize, parent_table.singularize].each do |table_name|
459
+ parent_table_info = relation_info[table_name]
460
+
461
+ if parent_table_info
462
+ parent_table = table_name
463
+ break
464
+ end
465
+ end
466
+
467
+ next unless parent_table_info
468
+
469
+ table_options = parent_table_info.fetch(:options)
470
+ pk_type = table_options[:id] || @options[:check_relation_type].to_sym
471
+ child_column_type = column_attrs[:type]
472
+
473
+ if pk_type != child_column_type
474
+ parent_label = "#{parent_table}.id"
475
+ child_label = "#{child_table}.#{column_name}"
476
+ label_len = [parent_label.length, child_label.length].max
477
+
478
+ @logger.warn(<<-EOS % [label_len, parent_label, label_len, child_label])
479
+ [WARNING] Relation column type is different.
480
+ %*s: #{pk_type}
481
+ %*s: #{child_column_type}
482
+ EOS
483
+ end
484
+ end
485
+ end
486
+ end
422
487
  end
@@ -1,3 +1,3 @@
1
1
  module Ridgepole
2
- VERSION = '0.7.0.beta7'
2
+ VERSION = '0.7.0.beta8'
3
3
  end
@@ -46,6 +46,7 @@ describe 'ridgepole' do
46
46
  --index-removed-drop-column
47
47
  --skip-drop-table
48
48
  --mysql-change-table-options
49
+ --check-relation-type DEF_PK
49
50
  -r, --require LIBS
50
51
  --log-file LOG_FILE
51
52
  --verbose
@@ -0,0 +1,112 @@
1
+ describe 'Ridgepole::Client#diff -> migrate', 5.1 do
2
+ context 'with warning' do
3
+ let(:actual_dsl) {
4
+ erbh(<<-EOS)
5
+ create_table "dept_manager", force: :cascade do |t|
6
+ t.string "dept_no", limit: 4, null: false
7
+ t.date "from_date", null: false
8
+ t.date "to_date", null: false
9
+ end
10
+
11
+ create_table "employees", force: :cascade do |t|
12
+ t.integer "emp_no", null: false
13
+ t.date "birth_date", null: false
14
+ t.string "first_name", limit: 14, null: false
15
+ t.string "last_name", limit: 16, null: false
16
+ t.string "gender", limit: 1, null: false
17
+ t.date "hire_date", null: false
18
+ end
19
+ EOS
20
+ }
21
+
22
+ let(:expected_dsl) {
23
+ erbh(<<-EOS)
24
+ create_table "dept_manager", force: :cascade do |t|
25
+ t.integer "employee_id"
26
+ t.string "dept_no", limit: 4, null: false
27
+ t.date "from_date", null: false
28
+ t.date "to_date", null: false
29
+ end
30
+
31
+ create_table "employees", force: :cascade do |t|
32
+ t.integer "emp_no", null: false
33
+ t.date "birth_date", null: false
34
+ t.string "first_name", limit: 14, null: false
35
+ t.string "last_name", limit: 16, null: false
36
+ t.string "gender", limit: 1, null: false
37
+ t.date "hire_date", null: false
38
+ end
39
+ EOS
40
+ }
41
+
42
+ before { subject.diff(actual_dsl).migrate }
43
+ subject { client(check_relation_type: 'bigint') }
44
+
45
+ it {
46
+ expect(Ridgepole::Logger.instance).to receive(:warn).with(<<-EOS)
47
+ [WARNING] Relation column type is different.
48
+ employees.id: bigint
49
+ dept_manager.employee_id: integer
50
+ EOS
51
+
52
+ delta = subject.diff(expected_dsl)
53
+ expect(delta.differ?).to be_truthy
54
+ expect(subject.dump).to match_fuzzy actual_dsl
55
+ delta.migrate
56
+ expect(subject.dump).to match_fuzzy expected_dsl
57
+ }
58
+ end
59
+
60
+ context 'without warning' do
61
+ let(:actual_dsl) {
62
+ erbh(<<-EOS)
63
+ create_table "dept_manager", force: :cascade do |t|
64
+ t.string "dept_no", limit: 4, null: false
65
+ t.date "from_date", null: false
66
+ t.date "to_date", null: false
67
+ end
68
+
69
+ create_table "employees", force: :cascade do |t|
70
+ t.integer "emp_no", null: false
71
+ t.date "birth_date", null: false
72
+ t.string "first_name", limit: 14, null: false
73
+ t.string "last_name", limit: 16, null: false
74
+ t.string "gender", limit: 1, null: false
75
+ t.date "hire_date", null: false
76
+ end
77
+ EOS
78
+ }
79
+
80
+ let(:expected_dsl) {
81
+ erbh(<<-EOS)
82
+ create_table "dept_manager", force: :cascade do |t|
83
+ t.bigint "employee_id"
84
+ t.string "dept_no", limit: 4, null: false
85
+ t.date "from_date", null: false
86
+ t.date "to_date", null: false
87
+ end
88
+
89
+ create_table "employees", force: :cascade do |t|
90
+ t.integer "emp_no", null: false
91
+ t.date "birth_date", null: false
92
+ t.string "first_name", limit: 14, null: false
93
+ t.string "last_name", limit: 16, null: false
94
+ t.string "gender", limit: 1, null: false
95
+ t.date "hire_date", null: false
96
+ end
97
+ EOS
98
+ }
99
+
100
+ before { subject.diff(actual_dsl).migrate }
101
+ subject { client(check_relation_type: 'bigint') }
102
+
103
+ it {
104
+ expect(Ridgepole::Logger.instance).to_not receive(:warn)
105
+ delta = subject.diff(expected_dsl)
106
+ expect(delta.differ?).to be_truthy
107
+ expect(subject.dump).to match_fuzzy actual_dsl
108
+ delta.migrate
109
+ expect(subject.dump).to match_fuzzy expected_dsl
110
+ }
111
+ end
112
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ridgepole
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.0.beta7
4
+ version: 0.7.0.beta8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Genki Sugawara
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-06-27 00:00:00.000000000 Z
11
+ date: 2017-06-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -284,6 +284,7 @@ files:
284
284
  - spec/mysql/migrate/migrate_change_index7_spec.rb
285
285
  - spec/mysql/migrate/migrate_change_index_spec.rb
286
286
  - spec/mysql/migrate/migrate_change_table_option_spec.rb
287
+ - spec/mysql/migrate/migrate_check_relation_column_type_spec.rb
287
288
  - spec/mysql/migrate/migrate_create_index2_spec.rb
288
289
  - spec/mysql/migrate/migrate_create_index_spec.rb
289
290
  - spec/mysql/migrate/migrate_create_table_spec.rb
@@ -425,6 +426,7 @@ test_files:
425
426
  - spec/mysql/migrate/migrate_change_index7_spec.rb
426
427
  - spec/mysql/migrate/migrate_change_index_spec.rb
427
428
  - spec/mysql/migrate/migrate_change_table_option_spec.rb
429
+ - spec/mysql/migrate/migrate_check_relation_column_type_spec.rb
428
430
  - spec/mysql/migrate/migrate_create_index2_spec.rb
429
431
  - spec/mysql/migrate/migrate_create_index_spec.rb
430
432
  - spec/mysql/migrate/migrate_create_table_spec.rb