ridgepole 0.6.0.beta2 → 0.6.0.beta3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (84) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -0
  3. data/README.md +2 -0
  4. data/bin/ridgepole +16 -16
  5. data/lib/ridgepole/client.rb +6 -9
  6. data/lib/ridgepole/default_limit.rb +26 -0
  7. data/lib/ridgepole/delta.rb +2 -8
  8. data/lib/ridgepole/diff.rb +14 -10
  9. data/lib/ridgepole/version.rb +1 -1
  10. data/lib/ridgepole.rb +2 -10
  11. data/ridgepole.gemspec +1 -0
  12. data/spec/{bigint_pk → mysql/bigint_pk}/bigint_pkspec.rb +0 -0
  13. data/spec/{cli → mysql/cli}/config_spec.rb +3 -1
  14. data/spec/{cli → mysql/cli}/ridgepole_spec.rb +3 -0
  15. data/spec/{collation → mysql/collation}/collation_spec.rb +2 -0
  16. data/spec/{diff → mysql/diff}/diff2_spec.rb +2 -0
  17. data/spec/{diff → mysql/diff}/diff_spec.rb +2 -0
  18. data/spec/{dump → mysql/dump}/dump_class_method_spec.rb +2 -0
  19. data/spec/{dump → mysql/dump}/dump_some_tables_spec.rb +2 -0
  20. data/spec/{dump → mysql/dump}/dump_spec.rb +2 -0
  21. data/spec/{dump → mysql/dump}/dump_without_table_options_spec.rb +2 -0
  22. data/spec/{migrate_0 → mysql/fk}/migrate_change_fk_spec.rb +5 -3
  23. data/spec/{migrate_0 → mysql/fk}/migrate_create_fk_spec.rb +7 -5
  24. data/spec/{migrate_0 → mysql/fk}/migrate_drop_fk_spec.rb +6 -4
  25. data/spec/{migrate → mysql/migrate}/check_orphan_index_spec.rb +2 -0
  26. data/spec/{migrate → mysql/migrate}/migrate_add_column2_spec.rb +2 -0
  27. data/spec/{migrate → mysql/migrate}/migrate_add_column_spec.rb +2 -0
  28. data/spec/{migrate → mysql/migrate}/migrate_change_column2_spec.rb +2 -0
  29. data/spec/{migrate → mysql/migrate}/migrate_change_column3_spec.rb +2 -0
  30. data/spec/mysql/migrate/migrate_change_column4_spec.rb +83 -0
  31. data/spec/{migrate → mysql/migrate}/migrate_change_column_float_spec.rb +2 -0
  32. data/spec/{migrate → mysql/migrate}/migrate_change_column_spec.rb +2 -0
  33. data/spec/{migrate → mysql/migrate}/migrate_change_index2_spec.rb +2 -0
  34. data/spec/{migrate → mysql/migrate}/migrate_change_index3_spec.rb +2 -0
  35. data/spec/{migrate → mysql/migrate}/migrate_change_index4_spec.rb +2 -0
  36. data/spec/mysql/migrate/migrate_change_index5_spec.rb +78 -0
  37. data/spec/{migrate → mysql/migrate}/migrate_change_index_spec.rb +2 -0
  38. data/spec/{migrate → mysql/migrate}/migrate_change_table_option_spec.rb +2 -0
  39. data/spec/{migrate → mysql/migrate}/migrate_create_index_spec.rb +2 -0
  40. data/spec/{migrate → mysql/migrate}/migrate_create_table_spec.rb +2 -0
  41. data/spec/{migrate → mysql/migrate}/migrate_create_table_with_options_spec.rb +2 -0
  42. data/spec/{migrate → mysql/migrate}/migrate_drop_column_and_index_2_spec.rb +2 -0
  43. data/spec/{migrate → mysql/migrate}/migrate_drop_column_and_index_spec.rb +2 -0
  44. data/spec/{migrate → mysql/migrate}/migrate_drop_column_spec.rb +2 -0
  45. data/spec/{migrate → mysql/migrate}/migrate_drop_index_spec.rb +2 -0
  46. data/spec/{migrate → mysql/migrate}/migrate_drop_table_spec.rb +2 -0
  47. data/spec/{migrate → mysql/migrate}/migrate_duplicate_index_spec.rb +2 -0
  48. data/spec/{migrate → mysql/migrate}/migrate_duplicate_table_spec.rb +2 -0
  49. data/spec/{migrate → mysql/migrate}/migrate_empty_spec.rb +2 -0
  50. data/spec/{migrate → mysql/migrate}/migrate_execute_spec.rb +2 -0
  51. data/spec/{migrate → mysql/migrate}/migrate_merge_mode_spec.rb +2 -0
  52. data/spec/{migrate → mysql/migrate}/migrate_noop_spec.rb +2 -0
  53. data/spec/{migrate → mysql/migrate}/migrate_rename_column_spec.rb +2 -0
  54. data/spec/{migrate → mysql/migrate}/migrate_rename_table_spec.rb +2 -0
  55. data/spec/{migrate → mysql/migrate}/migrate_same_default_null_spec.rb +2 -0
  56. data/spec/{migrate → mysql/migrate}/migrate_same_spec.rb +2 -0
  57. data/spec/{migrate → mysql/migrate}/migrate_script_error_spec.rb +2 -0
  58. data/spec/{migrate → mysql/migrate}/migrate_skip_rename_column_spec.rb +2 -0
  59. data/spec/{migrate → mysql/migrate}/migrate_skip_rename_table_spec.rb +2 -0
  60. data/spec/{migrate → mysql/migrate}/migrate_with_ignore_tables_spec.rb +2 -0
  61. data/spec/{migrate → mysql/migrate}/migrate_with_pre_post_query_spec.rb +2 -0
  62. data/spec/{migrate → mysql/migrate}/migrate_with_tables_spec.rb +2 -0
  63. data/spec/{ridgepole_test_database.sql → mysql/ridgepole_test_database.sql} +0 -0
  64. data/spec/{ridgepole_test_tables.sql → mysql/ridgepole_test_tables.sql} +0 -0
  65. data/spec/postgresql/diff/diff_spec.rb +153 -0
  66. data/spec/postgresql/dump/dump_spec.rb +76 -0
  67. data/spec/postgresql/fk/migrate_change_fk_spec.rb +62 -0
  68. data/spec/postgresql/fk/migrate_create_fk_spec.rb +173 -0
  69. data/spec/postgresql/fk/migrate_drop_fk_spec.rb +114 -0
  70. data/spec/postgresql/migrate/migrate_add_column_spec.rb +220 -0
  71. data/spec/postgresql/migrate/migrate_change_column_spec.rb +203 -0
  72. data/spec/postgresql/migrate/migrate_change_index_spec.rb +193 -0
  73. data/spec/postgresql/migrate/migrate_create_table_spec.rb +103 -0
  74. data/spec/postgresql/migrate/migrate_drop_column_spec.rb +185 -0
  75. data/spec/postgresql/migrate/migrate_drop_column_with_index_spec.rb +180 -0
  76. data/spec/postgresql/migrate/migrate_drop_index_spec.rb +125 -0
  77. data/spec/postgresql/migrate/migrate_drop_table_spec.rb +116 -0
  78. data/spec/postgresql/migrate/migrate_rename_column_spec.rb +179 -0
  79. data/spec/postgresql/migrate/migrate_rename_table_spec.rb +199 -0
  80. data/spec/postgresql/migrate/migrate_same_spec.rb +84 -0
  81. data/spec/postgresql/ridgepole_test_database.sql +15 -0
  82. data/spec/postgresql/ridgepole_test_tables.sql +74 -0
  83. data/spec/spec_helper.rb +63 -7
  84. metadata +159 -104
@@ -0,0 +1,199 @@
1
+ if postgresql?
2
+ describe 'Ridgepole::Client#diff -> migrate' do
3
+ context 'when rename table' do
4
+ let(:actual_dsl) {
5
+ <<-RUBY
6
+ create_table "clubs", force: :cascade do |t|
7
+ t.string "name", limit: 255, default: "", null: false
8
+ end
9
+
10
+ add_index "clubs", ["name"], name: "idx_name", unique: true, using: :btree
11
+
12
+ create_table "departments", primary_key: "dept_no", force: :cascade do |t|
13
+ t.string "dept_name", limit: 40, null: false
14
+ end
15
+
16
+ add_index "departments", ["dept_name"], name: "idx_dept_name", unique: true, using: :btree
17
+
18
+ create_table "dept_emp", id: false, force: :cascade do |t|
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
+ end
24
+
25
+ add_index "dept_emp", ["dept_no"], name: "idx_dept_emp_dept_no", using: :btree
26
+ add_index "dept_emp", ["emp_no"], name: "idx_dept_emp_emp_no", using: :btree
27
+
28
+ create_table "dept_manager", id: false, force: :cascade do |t|
29
+ t.string "dept_no", limit: 4, null: false
30
+ t.integer "emp_no", null: false
31
+ t.date "from_date", null: false
32
+ t.date "to_date", null: false
33
+ end
34
+
35
+ add_index "dept_manager", ["dept_no"], name: "idx_dept_manager_dept_no", using: :btree
36
+ add_index "dept_manager", ["emp_no"], name: "idx_dept_manager_emp_no", using: :btree
37
+
38
+ create_table "employee_clubs", force: :cascade do |t|
39
+ t.integer "emp_no", null: false
40
+ t.integer "club_id", null: false
41
+ end
42
+
43
+ add_index "employee_clubs", ["emp_no", "club_id"], name: "idx_employee_clubs_emp_no_club_id", using: :btree
44
+
45
+ create_table "employees", primary_key: "emp_no", force: :cascade do |t|
46
+ t.date "birth_date", null: false
47
+ t.string "first_name", limit: 14, null: false
48
+ t.string "last_name", limit: 16, null: false
49
+ t.date "hire_date", null: false
50
+ end
51
+
52
+ create_table "salaries", id: false, force: :cascade do |t|
53
+ t.integer "emp_no", null: false
54
+ t.integer "salary", null: false
55
+ t.date "from_date", null: false
56
+ t.date "to_date", null: false
57
+ end
58
+
59
+ add_index "salaries", ["emp_no"], name: "idx_salaries_emp_no", using: :btree
60
+
61
+ create_table "titles", id: false, force: :cascade do |t|
62
+ t.integer "emp_no", null: false
63
+ t.string "title", limit: 50, null: false
64
+ t.date "from_date", null: false
65
+ t.date "to_date"
66
+ end
67
+
68
+ add_index "titles", ["emp_no"], name: "idx_titles_emp_no", using: :btree
69
+ RUBY
70
+ }
71
+
72
+ let(:expected_dsl) {
73
+ <<-RUBY
74
+ create_table "clubs", force: :cascade do |t|
75
+ t.string "name", limit: 255, default: "", null: false
76
+ end
77
+
78
+ add_index "clubs", ["name"], name: "idx_name", unique: true, using: :btree
79
+
80
+ create_table "departments", primary_key: "dept_no", force: :cascade do |t|
81
+ t.string "dept_name", limit: 40, null: false
82
+ end
83
+
84
+ add_index "departments", ["dept_name"], name: "idx_dept_name", unique: true, using: :btree
85
+
86
+ create_table "dept_emp", id: false, force: :cascade do |t|
87
+ t.integer "emp_no", null: false
88
+ t.string "dept_no", limit: 4, null: false
89
+ t.date "from_date", null: false
90
+ t.date "to_date", null: false
91
+ end
92
+
93
+ add_index "dept_emp", ["dept_no"], name: "idx_dept_emp_dept_no", using: :btree
94
+ add_index "dept_emp", ["emp_no"], name: "idx_dept_emp_emp_no", using: :btree
95
+
96
+ create_table "dept_manager", id: false, force: :cascade do |t|
97
+ t.string "dept_no", limit: 4, null: false
98
+ t.integer "emp_no", null: false
99
+ t.date "from_date", null: false
100
+ t.date "to_date", null: false
101
+ end
102
+
103
+ add_index "dept_manager", ["dept_no"], name: "idx_dept_manager_dept_no", using: :btree
104
+ add_index "dept_manager", ["emp_no"], name: "idx_dept_manager_emp_no", using: :btree
105
+
106
+ create_table "employee_clubs", force: :cascade do |t|
107
+ t.integer "emp_no", null: false
108
+ t.integer "club_id", null: false
109
+ end
110
+
111
+ add_index "employee_clubs", ["emp_no", "club_id"], name: "idx_employee_clubs_emp_no_club_id", using: :btree
112
+
113
+ create_table "employees2", primary_key: "emp_no", force: :cascade, renamed_from: 'employees' do |t|
114
+ t.date "birth_date", null: false
115
+ t.string "first_name", limit: 14, null: false
116
+ t.string "last_name", limit: 16, null: false
117
+ t.date "hire_date", null: false
118
+ end
119
+
120
+ create_table "salaries", id: false, force: :cascade do |t|
121
+ t.integer "emp_no", null: false
122
+ t.integer "salary", null: false
123
+ t.date "from_date", null: false
124
+ t.date "to_date", null: false
125
+ end
126
+
127
+ add_index "salaries", ["emp_no"], name: "idx_salaries_emp_no", using: :btree
128
+
129
+ create_table "titles", id: false, force: :cascade do |t|
130
+ t.integer "emp_no", null: false
131
+ t.string "title", limit: 50, null: false
132
+ t.date "from_date", null: false
133
+ t.date "to_date"
134
+ end
135
+
136
+ add_index "titles", ["emp_no"], name: "idx_titles_emp_no", using: :btree
137
+ RUBY
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 eq actual_dsl.strip_heredoc.strip
147
+ delta.migrate
148
+ expect(subject.dump).to eq expected_dsl.strip_heredoc.strip.gsub(/, renamed_from: 'employees'/, '')
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 eq <<-RUBY.strip_heredoc.strip
155
+ rename_table("employees2", "employees")
156
+ RUBY
157
+ }
158
+ end
159
+
160
+ context 'when rename table (dry-run)' do
161
+ let(:actual_dsl) {
162
+ <<-RUBY
163
+ create_table "employees", primary_key: "emp_no", force: :cascade do |t|
164
+ t.date "birth_date", null: false
165
+ t.string "first_name", limit: 14, null: false
166
+ t.string "last_name", limit: 16, null: false
167
+ t.date "hire_date", null: false
168
+ end
169
+
170
+ add_index "employees", ["first_name"], name: "first_name", using: :btree
171
+ RUBY
172
+ }
173
+
174
+ let(:expected_dsl) {
175
+ <<-RUBY
176
+ create_table "employees2", primary_key: "emp_no", force: :cascade, renamed_from: 'employees' do |t|
177
+ t.date "birth_date", null: false
178
+ t.string "first_name", limit: 14, null: false
179
+ t.string "last_name", limit: 16, null: false
180
+ t.date "hire_date", null: false
181
+ end
182
+
183
+ add_index "employees2", ["first_name"], name: "first_name", using: :btree
184
+ RUBY
185
+ }
186
+
187
+ before { subject.diff(actual_dsl).migrate }
188
+ subject { client }
189
+
190
+ it {
191
+ delta = subject.diff(expected_dsl)
192
+ expect(delta.differ?).to be_truthy
193
+ expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
194
+ delta.migrate(noop: true)
195
+ expect(subject.dump).to eq actual_dsl.strip_heredoc.strip
196
+ }
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,84 @@
1
+ if postgresql?
2
+ describe 'Ridgepole::Client#diff -> migrate' do
3
+ context 'when database and definition are same' do
4
+ let(:dsl) {
5
+ <<-RUBY
6
+ create_table "clubs", force: :cascade do |t|
7
+ t.string "name", limit: 255, default: "", null: false
8
+ end
9
+
10
+ add_index "clubs", ["name"], name: "idx_name", unique: true, using: :btree
11
+
12
+ create_table "departments", primary_key: "dept_no", force: :cascade do |t|
13
+ t.string "dept_name", limit: 40, null: false
14
+ end
15
+
16
+ add_index "departments", ["dept_name"], name: "idx_dept_name", unique: true, using: :btree
17
+
18
+ create_table "dept_emp", id: false, force: :cascade do |t|
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
+ end
24
+
25
+ add_index "dept_emp", ["dept_no"], name: "idx_dept_emp_dept_no", using: :btree
26
+ add_index "dept_emp", ["emp_no"], name: "idx_dept_emp_emp_no", using: :btree
27
+
28
+ create_table "dept_manager", id: false, force: :cascade do |t|
29
+ t.string "dept_no", limit: 4, null: false
30
+ t.integer "emp_no", null: false
31
+ t.date "from_date", null: false
32
+ t.date "to_date", null: false
33
+ end
34
+
35
+ add_index "dept_manager", ["dept_no"], name: "idx_dept_manager_dept_no", using: :btree
36
+ add_index "dept_manager", ["emp_no"], name: "idx_dept_manager_emp_no", using: :btree
37
+
38
+ create_table "employee_clubs", force: :cascade do |t|
39
+ t.integer "emp_no", null: false
40
+ t.integer "club_id", null: false
41
+ end
42
+
43
+ add_index "employee_clubs", ["emp_no", "club_id"], name: "idx_employee_clubs_emp_no_club_id", using: :btree
44
+
45
+ create_table "employees", primary_key: "emp_no", force: :cascade do |t|
46
+ t.date "birth_date", null: false
47
+ t.string "first_name", limit: 14, null: false
48
+ t.string "last_name", limit: 16, null: false
49
+ t.date "hire_date", null: false
50
+ end
51
+
52
+ create_table "salaries", id: false, force: :cascade do |t|
53
+ t.integer "emp_no", null: false
54
+ t.integer "salary", null: false
55
+ t.date "from_date", null: false
56
+ t.date "to_date", null: false
57
+ end
58
+
59
+ add_index "salaries", ["emp_no"], name: "idx_salaries_emp_no", using: :btree
60
+
61
+ create_table "titles", id: false, force: :cascade do |t|
62
+ t.integer "emp_no", null: false
63
+ t.string "title", limit: 50, null: false
64
+ t.date "from_date", null: false
65
+ t.date "to_date"
66
+ end
67
+
68
+ add_index "titles", ["emp_no"], name: "idx_titles_emp_no", using: :btree
69
+ RUBY
70
+ }
71
+
72
+ before { restore_tables }
73
+ subject { client }
74
+
75
+ it {
76
+ delta = subject.diff(dsl)
77
+ expect(delta.differ?).to be_falsey
78
+ expect(subject.dump).to eq dsl.strip_heredoc.strip
79
+ delta.migrate
80
+ expect(subject.dump).to eq dsl.strip_heredoc.strip
81
+ }
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,15 @@
1
+ CREATE DATABASE ridgepole_test;
2
+
3
+ \c ridgepole_test;
4
+
5
+ DO $$
6
+ DECLARE
7
+ stmts CURSOR FOR
8
+ SELECT tablename FROM pg_tables
9
+ WHERE schemaname = 'public';
10
+ BEGIN
11
+ FOR stmt IN stmts LOOP
12
+ EXECUTE 'DROP TABLE ' || quote_ident(stmt.tablename) || ';';
13
+ END LOOP;
14
+ END;
15
+ $$
@@ -0,0 +1,74 @@
1
+ \c ridgepole_test;
2
+
3
+ DROP TABLE IF EXISTS clubs;
4
+ CREATE TABLE clubs (
5
+ id serial PRIMARY KEY,
6
+ name varchar(255) NOT NULL DEFAULT ''
7
+ );
8
+ CREATE UNIQUE INDEX idx_name ON clubs (name);
9
+
10
+ DROP TABLE IF EXISTS departments;
11
+ CREATE TABLE departments (
12
+ dept_no char(4) PRIMARY KEY,
13
+ dept_name varchar(40) NOT NULL
14
+ );
15
+ CREATE UNIQUE INDEX idx_dept_name ON departments (dept_name);
16
+
17
+ DROP TABLE IF EXISTS dept_emp ;
18
+ CREATE TABLE dept_emp (
19
+ emp_no int NOT NULL,
20
+ dept_no char(4) NOT NULL,
21
+ from_date date NOT NULL,
22
+ to_date date NOT NULL,
23
+ PRIMARY KEY (emp_no,dept_no)
24
+ );
25
+ CREATE INDEX idx_dept_emp_emp_no ON dept_emp (emp_no);
26
+ CREATE INDEX idx_dept_emp_dept_no ON dept_emp (dept_no);
27
+
28
+ DROP TABLE IF EXISTS dept_manager;
29
+ CREATE TABLE dept_manager (
30
+ dept_no char(4) NOT NULL,
31
+ emp_no int NOT NULL,
32
+ from_date date NOT NULL,
33
+ to_date date NOT NULL,
34
+ PRIMARY KEY (emp_no,dept_no)
35
+ );
36
+ CREATE INDEX idx_dept_manager_emp_no ON dept_manager (emp_no);
37
+ CREATE INDEX idx_dept_manager_dept_no ON dept_manager (dept_no);
38
+
39
+ DROP TABLE IF EXISTS employee_clubs;
40
+ CREATE TABLE employee_clubs (
41
+ id serial PRIMARY KEY,
42
+ emp_no int NOT NULL,
43
+ club_id int NOT NULL
44
+ );
45
+ CREATE INDEX idx_employee_clubs_emp_no_club_id ON employee_clubs (emp_no,club_id);
46
+
47
+ DROP TABLE IF EXISTS employees;
48
+ CREATE TABLE employees (
49
+ emp_no int PRIMARY KEY,
50
+ birth_date date NOT NULL,
51
+ first_name varchar(14) NOT NULL,
52
+ last_name varchar(16) NOT NULL,
53
+ hire_date date NOT NULL
54
+ );
55
+
56
+ DROP TABLE IF EXISTS salaries;
57
+ CREATE TABLE salaries (
58
+ emp_no int NOT NULL,
59
+ salary int NOT NULL,
60
+ from_date date NOT NULL,
61
+ to_date date NOT NULL,
62
+ PRIMARY KEY (emp_no,from_date)
63
+ );
64
+ CREATE INDEX idx_salaries_emp_no ON salaries (emp_no);
65
+
66
+ DROP TABLE IF EXISTS titles;
67
+ CREATE TABLE titles (
68
+ emp_no int NOT NULL,
69
+ title varchar(50) NOT NULL,
70
+ from_date date NOT NULL,
71
+ to_date date DEFAULT NULL,
72
+ PRIMARY KEY (emp_no,title,from_date)
73
+ );
74
+ CREATE INDEX idx_titles_emp_no ON titles (emp_no);
data/spec/spec_helper.rb CHANGED
@@ -1,6 +1,10 @@
1
1
  $: << File.expand_path('..', __FILE__)
2
2
 
3
- if ENV['TRAVIS']
3
+ def travis?
4
+ !!ENV['TRAVIS']
5
+ end
6
+
7
+ if travis?
4
8
  require 'simplecov'
5
9
  require 'coveralls'
6
10
 
@@ -37,15 +41,41 @@ RSpec.configure do |config|
37
41
  end
38
42
 
39
43
  def restore_database
40
- sql_file = File.expand_path('../ridgepole_test_database.sql', __FILE__)
44
+ if postgresql?
45
+ restore_database_postgresql
46
+ else
47
+ restore_database_mysql
48
+ end
49
+ end
50
+
51
+ def restore_database_mysql
52
+ sql_file = File.expand_path('../mysql/ridgepole_test_database.sql', __FILE__)
41
53
  system("mysql -uroot < #{sql_file}")
42
54
  end
43
55
 
56
+ def restore_database_postgresql
57
+ sql_file = File.expand_path('../postgresql/ridgepole_test_database.sql', __FILE__)
58
+ system("psql #{travis? ? '-U postgres' : ''} --set ON_ERROR_STOP=off -q -f #{sql_file} 2>/dev/null")
59
+ end
60
+
44
61
  def restore_tables
45
- sql_file = File.expand_path('../ridgepole_test_tables.sql', __FILE__)
62
+ if postgresql?
63
+ restore_tables_postgresql
64
+ else
65
+ restore_tables_mysql
66
+ end
67
+ end
68
+
69
+ def restore_tables_mysql
70
+ sql_file = File.expand_path('../mysql/ridgepole_test_tables.sql', __FILE__)
46
71
  system("mysql -uroot < #{sql_file}")
47
72
  end
48
73
 
74
+ def restore_tables_postgresql
75
+ sql_file = File.expand_path('../postgresql/ridgepole_test_tables.sql', __FILE__)
76
+ system("psql #{travis? ? '-U postgres' : ''} -q -f #{sql_file} 2>/dev/null")
77
+ end
78
+
49
79
  def client(options = {}, config = {})
50
80
  config = conn_spec(config)
51
81
 
@@ -65,17 +95,39 @@ def client(options = {}, config = {})
65
95
  end
66
96
 
67
97
  def conn_spec(config = {})
68
- {
69
- adapter: 'mysql2',
70
- database: TEST_SCHEMA,
71
- }.merge(config)
98
+ if postgresql?
99
+ spec = {
100
+ adapter: 'postgresql',
101
+ database: TEST_SCHEMA,
102
+ }
103
+
104
+ spec[:username] = 'postgres' if travis?
105
+ spec.merge(config)
106
+ else
107
+ {
108
+ adapter: 'mysql2',
109
+ database: TEST_SCHEMA,
110
+ }.merge(config)
111
+ end
72
112
  end
73
113
 
74
114
  def show_create_table(table_name)
115
+ if postgresql?
116
+ show_create_table_postgresql(table_name)
117
+ else
118
+ show_create_table_mysql(table_name)
119
+ end
120
+ end
121
+
122
+ def show_create_table_mysql(table_name)
75
123
  raw_conn = ActiveRecord::Base.connection.raw_connection
76
124
  raw_conn.query("SHOW CREATE TABLE `#{table_name}`").first[1]
77
125
  end
78
126
 
127
+ def show_create_table_postgresql(table_name)
128
+ `pg_dump #{travis? ? '-U postgres' : ''} --schema-only #{TEST_SCHEMA} --table=#{table_name} | awk '/^CREATE TABLE/,/);/{print} /^CREATE INDEX/{print}'`.strip
129
+ end
130
+
79
131
  def default_cli_hook
80
132
  <<-RUBY.strip_heredoc
81
133
  require 'ridgepole'
@@ -141,6 +193,10 @@ def mysql_awesome_enabled?
141
193
  ENV['ENABLE_MYSQL_AWESOME'] == '1'
142
194
  end
143
195
 
196
+ def postgresql?
197
+ ENV['POSTGRESQL'] == '1'
198
+ end
199
+
144
200
  def if_mysql_awesome_enabled(then_str, else_str = '')
145
201
  if mysql_awesome_enabled?
146
202
  then_str