ridgepole 0.7.0.alpha → 0.7.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (113) hide show
  1. checksums.yaml +4 -4
  2. data/.rspec +1 -0
  3. data/.travis.yml +1 -0
  4. data/README.md +7 -5
  5. data/bin/ridgepole +14 -14
  6. data/lib/ridgepole/cli/config.rb +1 -1
  7. data/lib/ridgepole/client.rb +0 -4
  8. data/lib/ridgepole/delta.rb +15 -9
  9. data/lib/ridgepole/diff.rb +13 -9
  10. data/lib/ridgepole/dsl_parser/context.rb +2 -6
  11. data/lib/ridgepole/version.rb +1 -1
  12. data/ridgepole.gemspec +3 -1
  13. data/spec/erb_helper.rb +0 -39
  14. data/spec/hide_pending_formatter.rb +6 -0
  15. data/spec/mysql/bigint_pk/bigint_pk_spec.rb +9 -9
  16. data/spec/mysql/bigint_pk/int_pk_spec.rb +9 -9
  17. data/spec/mysql/cli/ridgepole_spec.rb +2 -2
  18. data/spec/mysql/collation/collation_spec.rb +28 -28
  19. data/spec/mysql/comment/comment_spec.rb +36 -36
  20. data/spec/mysql/default_lambda/default_lambda_spec.rb +1 -1
  21. data/spec/mysql/diff/diff2_spec.rb +41 -41
  22. data/spec/mysql/diff/diff_spec.rb +41 -41
  23. data/spec/mysql/dump/dump_class_method_spec.rb +26 -33
  24. data/spec/mysql/dump/dump_some_tables_spec.rb +16 -18
  25. data/spec/mysql/dump/dump_spec.rb +27 -34
  26. data/spec/mysql/dump/dump_without_table_options_spec.rb +5 -5
  27. data/spec/mysql/fk/migrate_change_fk_spec.rb +76 -23
  28. data/spec/mysql/fk/migrate_create_fk_spec.rb +70 -50
  29. data/spec/mysql/fk/migrate_drop_fk_spec.rb +131 -32
  30. data/spec/mysql/migrate/check_orphan_index_spec.rb +19 -19
  31. data/spec/mysql/migrate/migrate_add_column2_spec.rb +33 -45
  32. data/spec/mysql/migrate/migrate_add_column_order_spec.rb +47 -47
  33. data/spec/mysql/migrate/migrate_add_column_spec.rb +75 -89
  34. data/spec/mysql/migrate/migrate_add_column_with_alter_extra_spec.rb +72 -86
  35. data/spec/mysql/migrate/migrate_add_column_with_script_spec.rb +72 -86
  36. data/spec/mysql/migrate/migrate_change_column2_spec.rb +27 -32
  37. data/spec/mysql/migrate/migrate_change_column3_spec.rb +78 -78
  38. data/spec/mysql/migrate/migrate_change_column4_spec.rb +24 -24
  39. data/spec/mysql/migrate/migrate_change_column5_spec.rb +42 -42
  40. data/spec/mysql/migrate/migrate_change_column6_spec.rb +60 -60
  41. data/spec/mysql/migrate/migrate_change_column_default_spec.rb +47 -47
  42. data/spec/mysql/migrate/migrate_change_column_float_spec.rb +16 -16
  43. data/spec/mysql/migrate/migrate_change_column_spec.rb +65 -79
  44. data/spec/mysql/migrate/migrate_change_index2_spec.rb +22 -27
  45. data/spec/mysql/migrate/migrate_change_index3_spec.rb +52 -62
  46. data/spec/mysql/migrate/migrate_change_index4_spec.rb +32 -38
  47. data/spec/mysql/migrate/migrate_change_index5_spec.rb +24 -28
  48. data/spec/mysql/migrate/migrate_change_index6_spec.rb +105 -119
  49. data/spec/mysql/migrate/migrate_change_index7_spec.rb +27 -30
  50. data/spec/mysql/migrate/migrate_change_index_spec.rb +99 -120
  51. data/spec/mysql/migrate/migrate_change_table_option_spec.rb +14 -14
  52. data/spec/mysql/migrate/migrate_create_index2_spec.rb +101 -112
  53. data/spec/mysql/migrate/migrate_create_index_spec.rb +55 -66
  54. data/spec/mysql/migrate/migrate_create_table_spec.rb +68 -91
  55. data/spec/mysql/migrate/migrate_create_table_with_options_spec.rb +1 -1
  56. data/spec/mysql/migrate/migrate_create_table_with_script_spec.rb +58 -70
  57. data/spec/mysql/migrate/migrate_drop_column_and_index2_spec.rb +62 -76
  58. data/spec/mysql/migrate/migrate_drop_column_and_index_spec.rb +58 -72
  59. data/spec/mysql/migrate/migrate_drop_column_spec.rb +63 -77
  60. data/spec/mysql/migrate/migrate_drop_index_spec.rb +55 -66
  61. data/spec/mysql/migrate/migrate_drop_table_spec.rb +51 -63
  62. data/spec/mysql/migrate/migrate_duplicate_index_spec.rb +3 -3
  63. data/spec/mysql/migrate/migrate_duplicate_table_spec.rb +12 -12
  64. data/spec/mysql/migrate/migrate_empty_spec.rb +35 -42
  65. data/spec/mysql/migrate/migrate_execute_spec.rb +16 -24
  66. data/spec/mysql/migrate/migrate_log_file_spec.rb +58 -70
  67. data/spec/mysql/migrate/migrate_merge_mode_spec.rb +75 -99
  68. data/spec/mysql/migrate/migrate_noop_spec.rb +38 -38
  69. data/spec/mysql/migrate/migrate_rename_column_spec.rb +74 -88
  70. data/spec/mysql/migrate/migrate_rename_table_spec.rb +100 -97
  71. data/spec/mysql/migrate/migrate_same_default_null_spec.rb +18 -18
  72. data/spec/mysql/migrate/migrate_same_spec.rb +29 -37
  73. data/spec/mysql/migrate/migrate_script_error_spec.rb +19 -19
  74. data/spec/mysql/migrate/migrate_skip_drop_table_spec.rb +148 -0
  75. data/spec/mysql/migrate/migrate_skip_rename_column_spec.rb +70 -84
  76. data/spec/mysql/migrate/migrate_skip_rename_table_spec.rb +70 -84
  77. data/spec/mysql/migrate/migrate_with_ignore_tables_spec.rb +87 -87
  78. data/spec/mysql/migrate/migrate_with_pre_post_query_spec.rb +61 -75
  79. data/spec/mysql/migrate/migrate_with_tables_spec.rb +41 -41
  80. data/spec/mysql/migrate/migrate_with_verbose_log_spec.rb +10 -10
  81. data/spec/mysql/migrate_/migrate_create_index_with_alter_spec.rb +67 -78
  82. data/spec/mysql/migrate_/migrate_drop_index_with_alter_spec.rb +67 -78
  83. data/spec/mysql/text_blob_types/text_blob_types_spec.rb +9 -9
  84. data/spec/mysql/~default_name_fk/migrate_change_fk_spec.rb +21 -24
  85. data/spec/mysql/~default_name_fk/migrate_create_fk_spec.rb +35 -67
  86. data/spec/mysql/~default_name_fk/migrate_drop_fk_spec.rb +28 -34
  87. data/spec/postgresql/diff/diff_spec.rb +37 -37
  88. data/spec/postgresql/dump/dump_spec.rb +27 -34
  89. data/spec/postgresql/fk/migrate_change_fk_spec.rb +76 -23
  90. data/spec/postgresql/fk/migrate_create_fk_spec.rb +73 -54
  91. data/spec/postgresql/fk/migrate_drop_fk_spec.rb +131 -32
  92. data/spec/postgresql/migrate/migrate_add_column_spec.rb +63 -75
  93. data/spec/postgresql/migrate/migrate_add_expression_index_spec.rb +1 -1
  94. data/spec/postgresql/migrate/migrate_change_column_default_spec.rb +7 -7
  95. data/spec/postgresql/migrate/migrate_change_column_spec.rb +53 -65
  96. data/spec/postgresql/migrate/migrate_change_index_spec.rb +54 -68
  97. data/spec/postgresql/migrate/migrate_create_table_spec.rb +56 -77
  98. data/spec/postgresql/migrate/migrate_drop_column_spec.rb +46 -60
  99. data/spec/postgresql/migrate/migrate_drop_column_with_index_spec.rb +41 -55
  100. data/spec/postgresql/migrate/migrate_drop_expression_index_spec.rb +3 -3
  101. data/spec/postgresql/migrate/migrate_drop_index_spec.rb +51 -62
  102. data/spec/postgresql/migrate/migrate_drop_table_spec.rb +48 -60
  103. data/spec/postgresql/migrate/migrate_ext_cols_spec.rb +6 -6
  104. data/spec/postgresql/migrate/migrate_rename_column_spec.rb +54 -68
  105. data/spec/postgresql/migrate/migrate_rename_table_spec.rb +62 -78
  106. data/spec/postgresql/migrate/migrate_same_spec.rb +27 -34
  107. data/spec/postgresql/~default_name_fk/migrate_change_fk_spec.rb +21 -24
  108. data/spec/postgresql/~default_name_fk/migrate_create_fk_spec.rb +35 -66
  109. data/spec/postgresql/~default_name_fk/migrate_drop_fk_spec.rb +28 -34
  110. data/spec/spec_helper.rb +1 -1
  111. metadata +9 -8
  112. data/ridgepole.iml +0 -9
  113. data/spec/string_ext.rb +0 -40
@@ -7,67 +7,60 @@ describe 'Ridgepole::Client#dump' do
7
7
  expect(subject.dump).to match_fuzzy erbh(<<-EOS)
8
8
  create_table "clubs", <%= i cond(5.1, id: :serial) + {force: :cascade} %> do |t|
9
9
  t.string "name", limit: 255, default: "", null: false
10
+ t.index ["name"], name: "idx_name", unique: true, <%= i cond(5.0, using: :btree) %>
10
11
  end
11
12
 
12
- <%= add_index "clubs", ["name"], name: "idx_name", unique: true, using: :btree %>
13
-
14
13
  create_table "departments", primary_key: "dept_no", id: :string, limit: 4, force: :cascade do |t|
15
14
  t.string "dept_name", limit: 40, null: false
15
+ t.index ["dept_name"], name: "idx_dept_name", unique: true, <%= i cond(5.0, using: :btree) %>
16
16
  end
17
17
 
18
- <%= add_index "departments", ["dept_name"], name: "idx_dept_name", unique: true, using: :btree %>
19
-
20
18
  create_table "dept_emp", primary_key: ["emp_no", "dept_no"], force: :cascade do |t|
21
- t.integer "emp_no", null: false
22
- t.string "dept_no", limit: 4, null: false
23
- t.date "from_date", null: false
24
- t.date "to_date", null: false
19
+ t.integer "emp_no", null: false
20
+ t.string "dept_no", limit: 4, null: false
21
+ t.date "from_date", null: false
22
+ t.date "to_date", null: false
23
+ t.index ["dept_no"], name: "idx_dept_emp_dept_no", <%= i cond(5.0, using: :btree) %>
24
+ t.index ["emp_no"], name: "idx_dept_emp_emp_no", <%= i cond(5.0, using: :btree) %>
25
25
  end
26
26
 
27
- <%= add_index "dept_emp", ["dept_no"], name: "idx_dept_emp_dept_no", using: :btree %>
28
- <%= add_index "dept_emp", ["emp_no"], name: "idx_dept_emp_emp_no", using: :btree %>
29
-
30
27
  create_table "dept_manager", primary_key: ["emp_no", "dept_no"], force: :cascade do |t|
31
- t.string "dept_no", limit: 4, null: false
32
- t.integer "emp_no", null: false
33
- t.date "from_date", null: false
34
- t.date "to_date", null: false
28
+ t.string "dept_no", limit: 4, null: false
29
+ t.integer "emp_no", null: false
30
+ t.date "from_date", null: false
31
+ t.date "to_date", null: false
32
+ t.index ["dept_no"], name: "idx_dept_manager_dept_no", <%= i cond(5.0, using: :btree) %>
33
+ t.index ["emp_no"], name: "idx_dept_manager_emp_no", <%= i cond(5.0, using: :btree) %>
35
34
  end
36
35
 
37
- <%= add_index "dept_manager", ["dept_no"], name: "idx_dept_manager_dept_no", using: :btree %>
38
- <%= add_index "dept_manager", ["emp_no"], name: "idx_dept_manager_emp_no", using: :btree %>
39
-
40
36
  create_table "employee_clubs", <%= i cond(5.1, id: :serial) + {force: :cascade} %> do |t|
41
- t.integer "emp_no", null: false
37
+ t.integer "emp_no", null: false
42
38
  t.integer "club_id", null: false
39
+ t.index ["emp_no", "club_id"], name: "idx_employee_clubs_emp_no_club_id", <%= i cond(5.0, using: :btree) %>
43
40
  end
44
41
 
45
- <%= add_index "employee_clubs", ["emp_no", "club_id"], name: "idx_employee_clubs_emp_no_club_id", using: :btree %>
46
-
47
42
  create_table "employees", primary_key: "emp_no", id: :integer, <%= i cond(5.1, default: nil) %>, force: :cascade do |t|
48
- t.date "birth_date", null: false
43
+ t.date "birth_date", null: false
49
44
  t.string "first_name", limit: 14, null: false
50
- t.string "last_name", limit: 16, null: false
51
- t.date "hire_date", null: false
45
+ t.string "last_name", limit: 16, null: false
46
+ t.date "hire_date", null: false
52
47
  end
53
48
 
54
49
  create_table "salaries", primary_key: ["emp_no", "from_date"], force: :cascade do |t|
55
- t.integer "emp_no", null: false
56
- t.integer "salary", null: false
50
+ t.integer "emp_no", null: false
51
+ t.integer "salary", null: false
57
52
  t.date "from_date", null: false
58
- t.date "to_date", null: false
53
+ t.date "to_date", null: false
54
+ t.index ["emp_no"], name: "idx_salaries_emp_no", <%= i cond(5.0, using: :btree) %>
59
55
  end
60
56
 
61
- <%= add_index "salaries", ["emp_no"], name: "idx_salaries_emp_no", using: :btree %>
62
-
63
57
  create_table "titles", primary_key: ["emp_no", "title", "from_date"], force: :cascade do |t|
64
- t.integer "emp_no", null: false
65
- t.string "title", limit: 50, null: false
66
- t.date "from_date", null: false
58
+ t.integer "emp_no", null: false
59
+ t.string "title", limit: 50, null: false
60
+ t.date "from_date", null: false
67
61
  t.date "to_date"
62
+ t.index ["emp_no"], name: "idx_titles_emp_no", <%= i cond(5.0, using: :btree) %>
68
63
  end
69
-
70
- <%= add_index "titles", ["emp_no"], name: "idx_titles_emp_no", using: :btree %>
71
64
  EOS
72
65
  }
73
66
  end
@@ -2,46 +2,99 @@ describe 'Ridgepole::Client#diff -> migrate' do
2
2
  context 'when change fk' do
3
3
  let(:actual_dsl) {
4
4
  erbh(<<-EOS)
5
- create_table "parent", force: :cascade do |t|
6
- end
7
-
8
- create_table "child", force: :cascade do |t|
9
- t.integer "parent_id"
10
- end
5
+ create_table "parent", force: :cascade do |t|
6
+ end
11
7
 
12
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
8
+ create_table "child", force: :cascade do |t|
9
+ t.integer "parent_id"
10
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
11
+ end
13
12
 
14
- add_foreign_key "child", "parent", name: "child_ibfk_1", on_delete: :cascade
13
+ add_foreign_key "child", "parent", name: "child_ibfk_1", on_delete: :cascade
15
14
  EOS
16
15
  }
17
16
 
18
17
  let(:sorted_actual_dsl) {
19
18
  erbh(<<-EOS)
20
- create_table "child", force: :cascade do |t|
21
- t.integer "parent_id"
22
- end
23
-
24
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
19
+ create_table "child", force: :cascade do |t|
20
+ t.integer "parent_id"
21
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
22
+ end
25
23
 
26
- create_table "parent", force: :cascade do |t|
27
- end
24
+ create_table "parent", force: :cascade do |t|
25
+ end
28
26
 
29
- add_foreign_key "child", "parent", name: "child_ibfk_1", on_delete: :cascade
27
+ add_foreign_key "child", "parent", name: "child_ibfk_1", on_delete: :cascade
30
28
  EOS
31
29
  }
32
30
 
33
31
  let(:expected_dsl) {
34
32
  erbh(<<-EOS)
35
- create_table "child", force: :cascade do |t|
36
- t.integer "parent_id"
37
- end
33
+ create_table "child", force: :cascade do |t|
34
+ t.integer "parent_id"
35
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
36
+ end
38
37
 
39
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
38
+ create_table "parent", force: :cascade do |t|
39
+ end
40
40
 
41
- create_table "parent", force: :cascade do |t|
42
- end
41
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
42
+ EOS
43
+ }
44
+
45
+ before { subject.diff(actual_dsl).migrate }
46
+
47
+ subject { client }
48
+
49
+ it {
50
+ delta = subject.diff(expected_dsl)
51
+ expect(delta.differ?).to be_truthy
52
+ expect(subject.dump).to match_fuzzy sorted_actual_dsl
53
+ delta.migrate
54
+ expect(subject.dump).to match_fuzzy expected_dsl
55
+ }
56
+ end
57
+
58
+ context 'when change fk without name' do
59
+ let(:actual_dsl) {
60
+ erbh(<<-EOS)
61
+ create_table "parent", force: :cascade do |t|
62
+ end
63
+
64
+ create_table "child", force: :cascade do |t|
65
+ t.integer "parent_id"
66
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
67
+ end
68
+
69
+ add_foreign_key "child", "parent", on_delete: :cascade
70
+ EOS
71
+ }
72
+
73
+ let(:sorted_actual_dsl) {
74
+ erbh(<<-EOS)
75
+ create_table "child", force: :cascade do |t|
76
+ t.integer "parent_id"
77
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
78
+ end
79
+
80
+ create_table "parent", force: :cascade do |t|
81
+ end
82
+
83
+ add_foreign_key "child", "parent", on_delete: :cascade
84
+ EOS
85
+ }
86
+
87
+ let(:expected_dsl) {
88
+ erbh(<<-EOS)
89
+ create_table "child", force: :cascade do |t|
90
+ t.integer "parent_id"
91
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
92
+ end
93
+
94
+ create_table "parent", force: :cascade do |t|
95
+ end
43
96
 
44
- add_foreign_key "child", "parent", name: "child_ibfk_1"
97
+ add_foreign_key "child", "parent"
45
98
  EOS
46
99
  }
47
100
 
@@ -2,21 +2,19 @@ describe 'Ridgepole::Client#diff -> migrate' do
2
2
  context 'when create fk' do
3
3
  let(:actual_dsl) {
4
4
  erbh(<<-EOS)
5
- create_table "child", force: :cascade do |t|
6
- t.integer "parent_id"
7
- end
8
-
9
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
5
+ create_table "child", force: :cascade do |t|
6
+ t.integer "parent_id"
7
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
8
+ end
10
9
 
11
- create_table "parent", force: :cascade do |t|
12
- end
10
+ create_table "parent", force: :cascade do |t|
11
+ end
13
12
  EOS
14
13
  }
15
14
 
16
15
  let(:expected_dsl) {
17
16
  actual_dsl + (<<-EOS)
18
-
19
- add_foreign_key "child", "parent", name: "child_ibfk_1"
17
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
20
18
  EOS
21
19
  }
22
20
 
@@ -54,32 +52,30 @@ add_foreign_key "child", "parent", name: "child_ibfk_1"
54
52
  context 'when create fk when create table' do
55
53
  let(:dsl) {
56
54
  erbh(<<-EOS)
57
- # Define parent before child
58
- create_table "parent", force: :cascade do |t|
59
- end
60
-
61
- create_table "child", force: :cascade do |t|
62
- t.integer "parent_id", unsigned: true
63
- end
55
+ # Define parent before child
56
+ create_table "parent", force: :cascade do |t|
57
+ end
64
58
 
65
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
59
+ create_table "child", force: :cascade do |t|
60
+ t.integer "parent_id", unsigned: true
61
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
62
+ end
66
63
 
67
- add_foreign_key "child", "parent", name: "child_ibfk_1"
64
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
68
65
  EOS
69
66
  }
70
67
 
71
68
  let(:sorted_dsl) {
72
69
  erbh(<<-EOS)
73
- create_table "child", force: :cascade do |t|
74
- t.integer "parent_id"
75
- end
76
-
77
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
70
+ create_table "child", force: :cascade do |t|
71
+ t.integer "parent_id"
72
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
73
+ end
78
74
 
79
- create_table "parent", force: :cascade do |t|
80
- end
75
+ create_table "parent", force: :cascade do |t|
76
+ end
81
77
 
82
- add_foreign_key "child", "parent", name: "child_ibfk_1"
78
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
83
79
  EOS
84
80
  }
85
81
 
@@ -98,19 +94,18 @@ add_foreign_key "child", "parent", name: "child_ibfk_1"
98
94
  context 'already defined' do
99
95
  let(:dsl) {
100
96
  erbh(<<-EOS)
101
- # Define parent before child
102
- create_table "parent", force: :cascade do |t|
103
- end
104
-
105
- create_table "child", force: :cascade do |t|
106
- t.integer "parent_id", unsigned: true
107
- end
97
+ # Define parent before child
98
+ create_table "parent", force: :cascade do |t|
99
+ end
108
100
 
109
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
101
+ create_table "child", force: :cascade do |t|
102
+ t.integer "parent_id", unsigned: true
103
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
104
+ end
110
105
 
111
- add_foreign_key "child", "parent", name: "child_ibfk_1"
106
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
112
107
 
113
- add_foreign_key "child", "parent", name: "child_ibfk_1"
108
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
114
109
  EOS
115
110
  }
116
111
 
@@ -123,40 +118,64 @@ add_foreign_key "child", "parent", name: "child_ibfk_1"
123
118
  }
124
119
  end
125
120
 
126
- context 'no name' do
127
- let(:dsl) {
121
+ context 'when create fk without name' do
122
+ let(:actual_dsl) {
128
123
  erbh(<<-EOS)
129
- # Define parent before child
130
- create_table "parent", force: :cascade do |t|
131
- end
132
-
133
- create_table "child", force: :cascade do |t|
134
- t.integer "parent_id", unsigned: true
135
- end
124
+ create_table "child", force: :cascade do |t|
125
+ t.integer "parent_id"
126
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
127
+ end
136
128
 
137
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
129
+ create_table "parent", force: :cascade do |t|
130
+ end
131
+ EOS
132
+ }
138
133
 
139
- add_foreign_key "child", "parent"
134
+ let(:expected_dsl) {
135
+ actual_dsl + (<<-EOS)
136
+ add_foreign_key "child", "parent"
140
137
  EOS
141
138
  }
142
139
 
140
+ before { subject.diff(actual_dsl).migrate }
143
141
  subject { client }
144
142
 
145
143
  it {
146
- expect {
147
- subject.diff(dsl)
148
- }.to raise_error('Foreign key name in `child` is undefined')
144
+ delta = subject.diff(expected_dsl)
145
+ expect(delta.differ?).to be_truthy
146
+ expect(subject.dump).to match_fuzzy actual_dsl
147
+ delta.migrate
148
+ expect(subject.dump).to match_fuzzy expected_dsl
149
+ }
150
+
151
+ it {
152
+ delta = Ridgepole::Client.diff(actual_dsl, expected_dsl, reverse: true, default_int_limit: 4)
153
+ expect(delta.differ?).to be_truthy
154
+ expect(delta.script).to match_fuzzy <<-EOS
155
+ remove_foreign_key("child", "parent")
156
+ EOS
157
+ }
158
+
159
+ it {
160
+ delta = client(bulk_change: true).diff(expected_dsl)
161
+ expect(delta.differ?).to be_truthy
162
+ expect(subject.dump).to match_fuzzy actual_dsl
163
+ expect(delta.script).to match_fuzzy <<-EOS
164
+ add_foreign_key("child", "parent", {})
165
+ EOS
166
+ delta.migrate
167
+ expect(subject.dump).to match_fuzzy expected_dsl
149
168
  }
150
169
  end
151
170
 
152
171
  context 'orphan fk' do
153
172
  let(:dsl) {
154
173
  erbh(<<-EOS)
155
- # Define parent before child
156
- create_table "parent", force: :cascade do |t|
157
- end
174
+ # Define parent before child
175
+ create_table "parent", force: :cascade do |t|
176
+ end
158
177
 
159
- add_foreign_key "child", "parent", name: "child_ibfk_1"
178
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
160
179
  EOS
161
180
  }
162
181
 
@@ -2,36 +2,33 @@ describe 'Ridgepole::Client#diff -> migrate' do
2
2
  context 'when drop fk' do
3
3
  let(:actual_dsl) {
4
4
  erbh(<<-EOS)
5
- create_table "parent", force: :cascade do |t|
6
- end
7
-
8
- create_table "child", force: :cascade do |t|
9
- t.integer "parent_id"
10
- end
5
+ create_table "parent", force: :cascade do |t|
6
+ end
11
7
 
12
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
8
+ create_table "child", force: :cascade do |t|
9
+ t.integer "parent_id"
10
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
11
+ end
13
12
 
14
- add_foreign_key "child", "parent", name: "child_ibfk_1"
13
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
15
14
  EOS
16
15
  }
17
16
 
18
17
  let(:sorted_actual_dsl) {
19
18
  expected_dsl + (<<-EOS)
20
-
21
- add_foreign_key "child", "parent", name: "child_ibfk_1"
19
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
22
20
  EOS
23
21
  }
24
22
 
25
23
  let(:expected_dsl) {
26
24
  erbh(<<-EOS)
27
- create_table "child", force: :cascade do |t|
28
- t.integer "parent_id"
29
- end
30
-
31
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
25
+ create_table "child", force: :cascade do |t|
26
+ t.integer "parent_id"
27
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
28
+ end
32
29
 
33
- create_table "parent", force: :cascade do |t|
34
- end
30
+ create_table "parent", force: :cascade do |t|
31
+ end
35
32
  EOS
36
33
  }
37
34
 
@@ -69,32 +66,134 @@ end
69
66
  context 'when drop fk when drop table' do
70
67
  let(:dsl) {
71
68
  erbh(<<-EOS)
72
- create_table "parent", force: :cascade do |t|
73
- end
69
+ create_table "parent", force: :cascade do |t|
70
+ end
74
71
 
72
+ create_table "child", force: :cascade do |t|
73
+ t.integer "parent_id", unsigned: true
74
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
75
+ end
75
76
 
76
- create_table "child", force: :cascade do |t|
77
- t.integer "parent_id", unsigned: true
78
- end
77
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
78
+ EOS
79
+ }
79
80
 
80
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
81
+ let(:sorted_dsl) {
82
+ erbh(<<-EOS)
83
+ create_table "child", force: :cascade do |t|
84
+ t.integer "parent_id"
85
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
86
+ end
87
+
88
+ create_table "parent", force: :cascade do |t|
89
+ end
81
90
 
82
- add_foreign_key "child", "parent", name: "child_ibfk_1"
91
+ add_foreign_key "child", "parent", name: "child_ibfk_1"
83
92
  EOS
84
93
  }
85
94
 
86
- let(:sorted_dsl) {
95
+ before { subject.diff(dsl).migrate }
96
+ subject { client }
97
+
98
+ it {
99
+ delta = subject.diff('')
100
+ expect(delta.differ?).to be_truthy
101
+ expect(subject.dump).to match_fuzzy sorted_dsl
102
+ delta.migrate
103
+ expect(subject.dump).to match_fuzzy ''
104
+ }
105
+ end
106
+
107
+ context 'when drop fk without name' do
108
+ let(:actual_dsl) {
87
109
  erbh(<<-EOS)
88
- create_table "child", force: :cascade do |t|
89
- t.integer "parent_id"
90
- end
110
+ create_table "parent", force: :cascade do |t|
111
+ end
91
112
 
92
- <%= add_index "child", ["parent_id"], name: "par_id", using: :btree %>
113
+ create_table "child", force: :cascade do |t|
114
+ t.integer "parent_id"
115
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
116
+ end
93
117
 
94
- create_table "parent", force: :cascade do |t|
95
- end
118
+ add_foreign_key "child", "parent"
119
+ EOS
120
+ }
121
+
122
+ let(:sorted_actual_dsl) {
123
+ expected_dsl + (<<-EOS)
124
+ add_foreign_key "child", "parent"
125
+ EOS
126
+ }
127
+
128
+ let(:expected_dsl) {
129
+ erbh(<<-EOS)
130
+ create_table "child", force: :cascade do |t|
131
+ t.integer "parent_id"
132
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
133
+ end
134
+
135
+ create_table "parent", force: :cascade do |t|
136
+ end
137
+ EOS
138
+ }
139
+
140
+ before { subject.diff(actual_dsl).migrate }
141
+ subject { client }
142
+
143
+ it {
144
+ delta = subject.diff(expected_dsl)
145
+ expect(delta.differ?).to be_truthy
146
+ expect(subject.dump).to match_fuzzy sorted_actual_dsl
147
+ delta.migrate
148
+ expect(subject.dump).to match_fuzzy expected_dsl
149
+ }
150
+
151
+ it {
152
+ delta = Ridgepole::Client.diff(actual_dsl, expected_dsl, reverse: true)
153
+ expect(delta.differ?).to be_truthy
154
+ expect(delta.script).to match_fuzzy <<-EOS
155
+ add_foreign_key("child", "parent", {})
156
+ EOS
157
+ }
158
+
159
+ it {
160
+ delta = client(bulk_change: true).diff(expected_dsl)
161
+ expect(delta.differ?).to be_truthy
162
+ expect(subject.dump).to match_fuzzy sorted_actual_dsl
163
+ expect(delta.script).to match_fuzzy <<-EOS
164
+ remove_foreign_key("child", "parent")
165
+ EOS
166
+ delta.migrate
167
+ expect(subject.dump).to match_fuzzy expected_dsl
168
+ }
169
+ end
170
+
171
+ context 'when drop fk when drop table without name' do
172
+ let(:dsl) {
173
+ erbh(<<-EOS)
174
+ create_table "parent", force: :cascade do |t|
175
+ end
176
+
177
+ create_table "child", force: :cascade do |t|
178
+ t.integer "parent_id", unsigned: true
179
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
180
+ end
181
+
182
+ add_foreign_key "child", "parent"
183
+ EOS
184
+ }
185
+
186
+ let(:sorted_dsl) {
187
+ erbh(<<-EOS)
188
+ create_table "child", force: :cascade do |t|
189
+ t.integer "parent_id"
190
+ t.index ["parent_id"], name: "par_id", <%= i cond(5.0, using: :btree) %>
191
+ end
192
+
193
+ create_table "parent", force: :cascade do |t|
194
+ end
96
195
 
97
- add_foreign_key "child", "parent", name: "child_ibfk_1"
196
+ add_foreign_key "child", "parent"
98
197
  EOS
99
198
  }
100
199