db_leftovers 0.9.2 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.document +0 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +8 -0
- data/LICENSE.txt +0 -0
- data/README.html +16 -9
- data/README.md +13 -8
- data/Rakefile +0 -0
- data/TODO +0 -7
- data/VERSION +1 -1
- data/db_leftovers.gemspec +12 -4
- data/lib/db_leftovers.rb +3 -1
- data/lib/db_leftovers/constraint.rb +0 -0
- data/lib/db_leftovers/definition.rb +5 -2
- data/lib/db_leftovers/dsl.rb +13 -1
- data/lib/db_leftovers/foreign_key.rb +0 -0
- data/lib/db_leftovers/generic_database_interface.rb +67 -0
- data/lib/db_leftovers/index.rb +0 -0
- data/lib/db_leftovers/mysql_database_interface.rb +83 -0
- data/lib/db_leftovers/{database_interface.rb → postgres_database_interface.rb} +15 -58
- data/lib/db_leftovers/table_dsl.rb +0 -0
- data/lib/tasks/leftovers.rake +0 -0
- data/spec/config/database.yml.sample +27 -0
- data/spec/db_leftovers_spec.rb +112 -178
- data/spec/mysql_spec.rb +32 -0
- data/spec/postgres_spec.rb +103 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/support/mock_database_interface.rb +59 -0
- data/spec/support/shared_db_tests.rb +152 -0
- data/spec/support/sql_matcher.rb +11 -0
- metadata +12 -4
File without changes
|
data/lib/tasks/leftovers.rake
CHANGED
File without changes
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# Set this up by saying:
|
2
|
+
# postgres=# create user db_leftovers_test with password 'testdb';
|
3
|
+
# postgres=# create database db_leftovers_test owner db_leftovers_test;
|
4
|
+
# postgres=# grant all privileges on database db_leftovers_test to db_leftovers_test;
|
5
|
+
|
6
|
+
postgres:
|
7
|
+
adapter: postgres
|
8
|
+
host: localhost
|
9
|
+
database: db_leftovers_test
|
10
|
+
username: db_leftovers_test
|
11
|
+
password: testdb
|
12
|
+
encoding: utf8
|
13
|
+
template: template0 # Required for UTF-8 encoding
|
14
|
+
|
15
|
+
|
16
|
+
# Set this up by saying:
|
17
|
+
# mysql> create database db_leftovers_test;
|
18
|
+
# mysql> grant all privileges on db_leftovers_test.* to db_leftovers@localhost identified by 'testdb';
|
19
|
+
|
20
|
+
mysql:
|
21
|
+
adapter: mysql2
|
22
|
+
host: localhost
|
23
|
+
database: db_leftovers_test
|
24
|
+
username: db_leftovers
|
25
|
+
password: testdb
|
26
|
+
encoding: utf8
|
27
|
+
|
data/spec/db_leftovers_spec.rb
CHANGED
@@ -1,107 +1,41 @@
|
|
1
1
|
require 'rails'
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
3
|
|
4
|
-
|
5
|
-
class DBLeftovers::DatabaseInterface
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@@sqls = []
|
9
|
-
end
|
10
|
-
|
11
|
-
def self.sqls
|
12
|
-
@@sqls
|
13
|
-
end
|
14
|
-
|
15
|
-
def execute_sql(sql)
|
16
|
-
@@sqls << DBLeftovers::DatabaseInterface.normal_whitespace(sql)
|
17
|
-
end
|
18
|
-
|
19
|
-
alias :old_execute_add_index :execute_add_index
|
20
|
-
def execute_add_index(idx)
|
21
|
-
old_execute_add_index(idx)
|
22
|
-
@@indexes[idx.index_name] = idx
|
23
|
-
end
|
24
|
-
|
25
|
-
alias :old_execute_add_constraint :execute_add_constraint
|
26
|
-
def execute_add_constraint(chk)
|
27
|
-
old_execute_add_constraint(chk)
|
28
|
-
@@constraints[chk.constraint_name] = chk
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.saw_sql(sql)
|
32
|
-
# puts sqls.join("\n\n\n")
|
33
|
-
# Don't fail if only the whitespace is different:
|
34
|
-
sqls.include?(normal_whitespace(sql))
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.starts_with(indexes=[], foreign_keys=[], constraints=[])
|
38
|
-
# Convert symbols to strings:
|
39
|
-
@@indexes = Hash[indexes.map{|idx| [idx.index_name, idx]}]
|
40
|
-
@@foreign_keys = Hash[foreign_keys.map{|fk| [fk.constraint_name, fk]}]
|
41
|
-
@@constraints = Hash[constraints.map{|chk| [chk.constraint_name, chk]}]
|
42
|
-
end
|
43
|
-
|
44
|
-
def lookup_all_indexes
|
45
|
-
@@indexes
|
46
|
-
end
|
47
|
-
|
48
|
-
def lookup_all_foreign_keys
|
49
|
-
@@foreign_keys
|
50
|
-
end
|
51
|
-
|
52
|
-
def lookup_all_constraints
|
53
|
-
@@constraints
|
54
|
-
end
|
55
|
-
|
56
|
-
private
|
57
|
-
|
58
|
-
def self.normal_whitespace(sql)
|
59
|
-
sql.gsub(/\s/m, ' ').gsub(/ +/, ' ').strip
|
60
|
-
end
|
61
|
-
|
62
|
-
end
|
63
|
-
|
64
|
-
RSpec::Matchers.define :have_seen_sql do |sql|
|
65
|
-
match do |db|
|
66
|
-
db.saw_sql(sql)
|
67
|
-
end
|
4
|
+
describe DBLeftovers do
|
68
5
|
|
69
|
-
|
70
|
-
|
6
|
+
before do
|
7
|
+
@db = DBLeftovers::MockDatabaseInterface.new
|
71
8
|
end
|
72
|
-
end
|
73
|
-
|
74
|
-
describe DBLeftovers do
|
75
9
|
|
76
10
|
it "should allow an empty definition" do
|
77
|
-
|
78
|
-
DBLeftovers::Definition.define do
|
11
|
+
@db.starts_with
|
12
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
79
13
|
end
|
80
|
-
|
14
|
+
@db.sqls.should be_empty
|
81
15
|
end
|
82
16
|
|
83
17
|
it "should allow an empty table definition" do
|
84
|
-
|
85
|
-
DBLeftovers::Definition.define do
|
18
|
+
@db.starts_with
|
19
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
86
20
|
table :books do
|
87
21
|
end
|
88
22
|
end
|
89
|
-
|
23
|
+
@db.sqls.should be_empty
|
90
24
|
end
|
91
25
|
|
92
26
|
it "should create indexes on an empty database" do
|
93
|
-
|
94
|
-
DBLeftovers::Definition.define do
|
27
|
+
@db.starts_with
|
28
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
95
29
|
index :books, :shelf_id
|
96
30
|
index :books, :publisher_id, :where => 'published'
|
97
31
|
end
|
98
|
-
|
99
|
-
|
32
|
+
@db.sqls.size.should == 2
|
33
|
+
@db.should have_seen_sql <<-EOQ
|
100
34
|
CREATE INDEX index_books_on_shelf_id
|
101
35
|
ON books
|
102
36
|
(shelf_id)
|
103
37
|
EOQ
|
104
|
-
|
38
|
+
@db.should have_seen_sql <<-EOQ
|
105
39
|
CREATE INDEX index_books_on_publisher_id
|
106
40
|
ON books
|
107
41
|
(publisher_id)
|
@@ -112,20 +46,20 @@ describe DBLeftovers do
|
|
112
46
|
|
113
47
|
|
114
48
|
it "should create table-prefixed indexes on an empty database" do
|
115
|
-
|
116
|
-
DBLeftovers::Definition.define do
|
49
|
+
@db.starts_with
|
50
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
117
51
|
table :books do
|
118
52
|
index :shelf_id
|
119
53
|
index :publisher_id, :where => 'published'
|
120
54
|
end
|
121
55
|
end
|
122
|
-
|
123
|
-
|
56
|
+
@db.sqls.size.should == 2
|
57
|
+
@db.should have_seen_sql <<-EOQ
|
124
58
|
CREATE INDEX index_books_on_shelf_id
|
125
59
|
ON books
|
126
60
|
(shelf_id)
|
127
61
|
EOQ
|
128
|
-
|
62
|
+
@db.should have_seen_sql <<-EOQ
|
129
63
|
CREATE INDEX index_books_on_publisher_id
|
130
64
|
ON books
|
131
65
|
(publisher_id)
|
@@ -136,27 +70,27 @@ describe DBLeftovers do
|
|
136
70
|
|
137
71
|
|
138
72
|
it "should create foreign keys on an empty database" do
|
139
|
-
|
140
|
-
DBLeftovers::Definition.define do
|
73
|
+
@db.starts_with
|
74
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
141
75
|
foreign_key :books, :shelf_id, :shelves
|
142
76
|
foreign_key :books, :publisher_id, :publishers, :id, :on_delete => :set_null
|
143
77
|
foreign_key :books, :author_id, :authors, :id, :on_delete => :cascade
|
144
78
|
end
|
145
|
-
|
146
|
-
|
79
|
+
@db.sqls.should have(3).items
|
80
|
+
@db.should have_seen_sql <<-EOQ
|
147
81
|
ALTER TABLE books
|
148
82
|
ADD CONSTRAINT fk_books_shelf_id
|
149
83
|
FOREIGN KEY (shelf_id)
|
150
84
|
REFERENCES shelves (id)
|
151
85
|
EOQ
|
152
|
-
|
86
|
+
@db.should have_seen_sql <<-EOQ
|
153
87
|
ALTER TABLE books
|
154
88
|
ADD CONSTRAINT fk_books_publisher_id
|
155
89
|
FOREIGN KEY (publisher_id)
|
156
90
|
REFERENCES publishers (id)
|
157
91
|
ON DELETE SET NULL
|
158
92
|
EOQ
|
159
|
-
|
93
|
+
@db.should have_seen_sql <<-EOQ
|
160
94
|
ALTER TABLE books
|
161
95
|
ADD CONSTRAINT fk_books_author_id
|
162
96
|
FOREIGN KEY (author_id)
|
@@ -168,29 +102,29 @@ describe DBLeftovers do
|
|
168
102
|
|
169
103
|
|
170
104
|
it "should create table-prefixed foreign keys on an empty database" do
|
171
|
-
|
172
|
-
DBLeftovers::Definition.define do
|
105
|
+
@db.starts_with
|
106
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
173
107
|
table :books do
|
174
108
|
foreign_key :shelf_id, :shelves
|
175
109
|
foreign_key :publisher_id, :publishers, :id, :on_delete => :set_null
|
176
110
|
foreign_key :author_id, :authors, :id, :on_delete => :cascade
|
177
111
|
end
|
178
112
|
end
|
179
|
-
|
180
|
-
|
113
|
+
@db.sqls.should have(3).items
|
114
|
+
@db.should have_seen_sql <<-EOQ
|
181
115
|
ALTER TABLE books
|
182
116
|
ADD CONSTRAINT fk_books_shelf_id
|
183
117
|
FOREIGN KEY (shelf_id)
|
184
118
|
REFERENCES shelves (id)
|
185
119
|
EOQ
|
186
|
-
|
120
|
+
@db.should have_seen_sql <<-EOQ
|
187
121
|
ALTER TABLE books
|
188
122
|
ADD CONSTRAINT fk_books_publisher_id
|
189
123
|
FOREIGN KEY (publisher_id)
|
190
124
|
REFERENCES publishers (id)
|
191
125
|
ON DELETE SET NULL
|
192
126
|
EOQ
|
193
|
-
|
127
|
+
@db.should have_seen_sql <<-EOQ
|
194
128
|
ALTER TABLE books
|
195
129
|
ADD CONSTRAINT fk_books_author_id
|
196
130
|
FOREIGN KEY (author_id)
|
@@ -200,33 +134,33 @@ describe DBLeftovers do
|
|
200
134
|
end
|
201
135
|
|
202
136
|
it "should create foreign keys with optional params inferred" do
|
203
|
-
|
204
|
-
DBLeftovers::Definition.define do
|
137
|
+
@db.starts_with
|
138
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
205
139
|
foreign_key :books, :shelves
|
206
140
|
foreign_key :books, :publishers, :on_delete => :set_null
|
207
141
|
foreign_key :books, :publication_country_id, :countries
|
208
142
|
foreign_key :books, :co_author_id, :authors, :on_delete => :cascade
|
209
143
|
end
|
210
|
-
|
211
|
-
|
144
|
+
@db.sqls.should have(4).items
|
145
|
+
@db.should have_seen_sql <<-EOQ
|
212
146
|
ALTER TABLE books ADD CONSTRAINT fk_books_shelf_id FOREIGN KEY (shelf_id) REFERENCES shelves (id)
|
213
147
|
EOQ
|
214
|
-
|
148
|
+
@db.should have_seen_sql <<-EOQ
|
215
149
|
ALTER TABLE books ADD CONSTRAINT fk_books_publisher_id FOREIGN KEY (publisher_id) REFERENCES publishers (id) ON DELETE SET NULL
|
216
150
|
EOQ
|
217
|
-
|
151
|
+
@db.should have_seen_sql <<-EOQ
|
218
152
|
ALTER TABLE books ADD CONSTRAINT fk_books_publication_country_id
|
219
153
|
FOREIGN KEY (publication_country_id) REFERENCES countries (id)
|
220
154
|
EOQ
|
221
|
-
|
155
|
+
@db.should have_seen_sql <<-EOQ
|
222
156
|
ALTER TABLE books ADD CONSTRAINT fk_books_co_author_id
|
223
157
|
FOREIGN KEY (co_author_id) REFERENCES authors (id) ON DELETE CASCADE
|
224
158
|
EOQ
|
225
159
|
end
|
226
160
|
|
227
161
|
it "should create foreign keys with optional params inferred and table block" do
|
228
|
-
|
229
|
-
DBLeftovers::Definition.define do
|
162
|
+
@db.starts_with
|
163
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
230
164
|
table :books do
|
231
165
|
foreign_key :shelves
|
232
166
|
foreign_key :publishers
|
@@ -234,94 +168,94 @@ describe DBLeftovers do
|
|
234
168
|
foreign_key :co_author_id, :authors, :on_delete => :cascade
|
235
169
|
end
|
236
170
|
end
|
237
|
-
|
171
|
+
@db.sqls.should have(4).items
|
238
172
|
end
|
239
173
|
|
240
174
|
it "should not create indexes when they already exist" do
|
241
|
-
|
175
|
+
@db.starts_with([
|
242
176
|
DBLeftovers::Index.new(:books, :shelf_id),
|
243
177
|
DBLeftovers::Index.new(:books, :publisher_id, :where => 'published')
|
244
178
|
])
|
245
|
-
DBLeftovers::Definition.define do
|
179
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
246
180
|
index :books, :shelf_id
|
247
181
|
index :books, :publisher_id, :where => 'published'
|
248
182
|
end
|
249
|
-
|
183
|
+
@db.sqls.should have(0).items
|
250
184
|
end
|
251
185
|
|
252
186
|
|
253
187
|
|
254
188
|
it "should create indexes when they have been redefined" do
|
255
|
-
|
189
|
+
@db.starts_with([
|
256
190
|
DBLeftovers::Index.new(:books, :shelf_id),
|
257
191
|
DBLeftovers::Index.new(:books, :publisher_id, :where => 'published'),
|
258
192
|
DBLeftovers::Index.new(:books, :isbn, :unique => true)
|
259
193
|
])
|
260
|
-
DBLeftovers::Definition.define do
|
194
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
261
195
|
index :books, :shelf_id, :where => 'isbn IS NOT NULL'
|
262
196
|
index :books, :publisher_id
|
263
197
|
index :books, :isbn
|
264
198
|
end
|
265
|
-
|
266
|
-
|
267
|
-
|
199
|
+
@db.sqls.should have(6).items
|
200
|
+
@db.sqls[0].should =~ /DROP INDEX index_books_on_shelf_id/
|
201
|
+
@db.sqls[1].should =~ /CREATE\s+INDEX index_books_on_shelf_id/
|
268
202
|
end
|
269
203
|
|
270
204
|
|
271
205
|
|
272
206
|
it "should not create table-prefixed indexes when they already exist" do
|
273
|
-
|
207
|
+
@db.starts_with([
|
274
208
|
DBLeftovers::Index.new(:books, :shelf_id),
|
275
209
|
DBLeftovers::Index.new(:books, :publisher_id, :where => 'published')
|
276
210
|
])
|
277
|
-
DBLeftovers::Definition.define do
|
211
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
278
212
|
table :books do
|
279
213
|
index :shelf_id
|
280
214
|
index :publisher_id, :where => 'published'
|
281
215
|
end
|
282
216
|
end
|
283
|
-
|
217
|
+
@db.sqls.should have(0).items
|
284
218
|
end
|
285
219
|
|
286
220
|
|
287
221
|
|
288
222
|
|
289
223
|
it "should not create foreign keys when they already exist" do
|
290
|
-
|
224
|
+
@db.starts_with([], [
|
291
225
|
DBLeftovers::ForeignKey.new('fk_books_shelf_id', 'books', 'shelf_id', 'shelves', 'id')
|
292
226
|
])
|
293
|
-
DBLeftovers::Definition.define do
|
227
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
294
228
|
foreign_key :books, :shelf_id, :shelves
|
295
229
|
end
|
296
|
-
|
230
|
+
@db.sqls.should have(0).items
|
297
231
|
end
|
298
232
|
|
299
233
|
|
300
234
|
|
301
235
|
it "should not create table-prefixed foreign keys when they already exist" do
|
302
|
-
|
236
|
+
@db.starts_with([], [
|
303
237
|
DBLeftovers::ForeignKey.new('fk_books_shelf_id', 'books', 'shelf_id', 'shelves', 'id')
|
304
238
|
])
|
305
|
-
DBLeftovers::Definition.define do
|
239
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
306
240
|
table :books do
|
307
241
|
foreign_key :shelf_id, :shelves
|
308
242
|
end
|
309
243
|
end
|
310
|
-
|
244
|
+
@db.sqls.should have(0).items
|
311
245
|
end
|
312
246
|
|
313
247
|
|
314
248
|
|
315
249
|
it "should drop indexes when they are removed from the definition" do
|
316
|
-
|
250
|
+
@db.starts_with([
|
317
251
|
DBLeftovers::Index.new(:books, :shelf_id),
|
318
252
|
DBLeftovers::Index.new(:books, :publisher_id, :where => 'published')
|
319
253
|
])
|
320
|
-
DBLeftovers::Definition.define do
|
254
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
321
255
|
index :books, :shelf_id
|
322
256
|
end
|
323
|
-
|
324
|
-
|
257
|
+
@db.sqls.should have(1).item
|
258
|
+
@db.should have_seen_sql <<-EOQ
|
325
259
|
DROP INDEX index_books_on_publisher_id
|
326
260
|
EOQ
|
327
261
|
end
|
@@ -329,15 +263,15 @@ describe DBLeftovers do
|
|
329
263
|
|
330
264
|
|
331
265
|
it "should drop foreign keys when they are removed from the definition" do
|
332
|
-
|
266
|
+
@db.starts_with([], [
|
333
267
|
DBLeftovers::ForeignKey.new('fk_books_shelf_id', 'books', 'shelf_id', 'shelves', 'id'),
|
334
268
|
DBLeftovers::ForeignKey.new('fk_books_author_id', 'books', 'author_id', 'authors', 'id')
|
335
269
|
])
|
336
|
-
DBLeftovers::Definition.define do
|
270
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
337
271
|
foreign_key :books, :shelf_id, :shelves
|
338
272
|
end
|
339
|
-
|
340
|
-
|
273
|
+
@db.sqls.should have(1).item
|
274
|
+
@db.should have_seen_sql <<-EOQ
|
341
275
|
ALTER TABLE books DROP CONSTRAINT fk_books_author_id
|
342
276
|
EOQ
|
343
277
|
end
|
@@ -345,32 +279,32 @@ describe DBLeftovers do
|
|
345
279
|
|
346
280
|
|
347
281
|
it "should create foreign keys when they have been redefined" do
|
348
|
-
|
282
|
+
@db.starts_with([], [
|
349
283
|
DBLeftovers::ForeignKey.new('fk_books_shelf_id', 'books', 'shelf_id', 'shelves', 'id'),
|
350
284
|
DBLeftovers::ForeignKey.new('fk_books_author_id', 'books', 'author_id', 'authors', 'id')
|
351
285
|
])
|
352
|
-
DBLeftovers::Definition.define do
|
286
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
353
287
|
table :books do
|
354
288
|
foreign_key :shelf_id, :shelves, :id, :on_delete => :cascade
|
355
289
|
foreign_key :author_id, :authors, :id, :on_delete => :set_null
|
356
290
|
end
|
357
291
|
end
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
292
|
+
@db.sqls.should have(4).items
|
293
|
+
@db.sqls[0].should =~ /ALTER TABLE books DROP CONSTRAINT fk_books_shelf_id/
|
294
|
+
@db.sqls[1].should =~ /ALTER TABLE books ADD CONSTRAINT fk_books_shelf_id/
|
295
|
+
@db.sqls[2].should =~ /ALTER TABLE books DROP CONSTRAINT fk_books_author_id/
|
296
|
+
@db.sqls[3].should =~ /ALTER TABLE books ADD CONSTRAINT fk_books_author_id/
|
363
297
|
end
|
364
298
|
|
365
299
|
|
366
300
|
|
367
301
|
it "should support creating multi-column indexes" do
|
368
|
-
|
369
|
-
DBLeftovers::Definition.define do
|
302
|
+
@db.starts_with
|
303
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
370
304
|
index :books, [:year, :title]
|
371
305
|
end
|
372
|
-
|
373
|
-
|
306
|
+
@db.sqls.should have(1).item
|
307
|
+
@db.should have_seen_sql <<-EOQ
|
374
308
|
CREATE INDEX index_books_on_year_and_title
|
375
309
|
ON books
|
376
310
|
(year, title)
|
@@ -380,13 +314,13 @@ describe DBLeftovers do
|
|
380
314
|
|
381
315
|
|
382
316
|
it "should support dropping multi-column indexes" do
|
383
|
-
|
317
|
+
@db.starts_with([
|
384
318
|
DBLeftovers::Index.new(:books, [:year, :title])
|
385
319
|
])
|
386
|
-
DBLeftovers::Definition.define do
|
320
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
387
321
|
end
|
388
|
-
|
389
|
-
|
322
|
+
@db.sqls.should have(1).item
|
323
|
+
@db.should have_seen_sql <<-EOQ
|
390
324
|
DROP INDEX index_books_on_year_and_title
|
391
325
|
EOQ
|
392
326
|
end
|
@@ -394,20 +328,20 @@ describe DBLeftovers do
|
|
394
328
|
|
395
329
|
|
396
330
|
it "should allow mixing indexes and foreign keys in the same table" do
|
397
|
-
|
398
|
-
DBLeftovers::Definition.define do
|
331
|
+
@db.starts_with
|
332
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
399
333
|
table :books do
|
400
334
|
index :author_id
|
401
335
|
foreign_key :author_id, :authors, :id
|
402
336
|
end
|
403
337
|
end
|
404
|
-
|
405
|
-
|
338
|
+
@db.sqls.should have(2).items
|
339
|
+
@db.should have_seen_sql <<-EOQ
|
406
340
|
CREATE INDEX index_books_on_author_id
|
407
341
|
ON books
|
408
342
|
(author_id)
|
409
343
|
EOQ
|
410
|
-
|
344
|
+
@db.should have_seen_sql <<-EOQ
|
411
345
|
ALTER TABLE books
|
412
346
|
ADD CONSTRAINT fk_books_author_id
|
413
347
|
FOREIGN KEY (author_id)
|
@@ -418,8 +352,8 @@ describe DBLeftovers do
|
|
418
352
|
|
419
353
|
|
420
354
|
it "should allow separating indexes and foreign keys from the same table" do
|
421
|
-
|
422
|
-
DBLeftovers::Definition.define do
|
355
|
+
@db.starts_with
|
356
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
423
357
|
table :books do
|
424
358
|
index :author_id
|
425
359
|
end
|
@@ -427,13 +361,13 @@ describe DBLeftovers do
|
|
427
361
|
foreign_key :author_id, :authors, :id
|
428
362
|
end
|
429
363
|
end
|
430
|
-
|
431
|
-
|
364
|
+
@db.sqls.should have(2).items
|
365
|
+
@db.should have_seen_sql <<-EOQ
|
432
366
|
CREATE INDEX index_books_on_author_id
|
433
367
|
ON books
|
434
368
|
(author_id)
|
435
369
|
EOQ
|
436
|
-
|
370
|
+
@db.should have_seen_sql <<-EOQ
|
437
371
|
ALTER TABLE books
|
438
372
|
ADD CONSTRAINT fk_books_author_id
|
439
373
|
FOREIGN KEY (author_id)
|
@@ -443,7 +377,7 @@ describe DBLeftovers do
|
|
443
377
|
|
444
378
|
it "should reject invalid foreign key options" do
|
445
379
|
lambda {
|
446
|
-
DBLeftovers::Definition.define do
|
380
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
447
381
|
foreign_key :books, :author_id, :authors, :id, :icky => :boo_boo
|
448
382
|
end
|
449
383
|
}.should raise_error(RuntimeError, "Unknown option: icky")
|
@@ -451,7 +385,7 @@ describe DBLeftovers do
|
|
451
385
|
|
452
386
|
it "should reject invalid foreign key on_delete values" do
|
453
387
|
lambda {
|
454
|
-
DBLeftovers::Definition.define do
|
388
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
455
389
|
foreign_key :books, :author_id, :authors, :id, :on_delete => :panic
|
456
390
|
end
|
457
391
|
}.should raise_error(RuntimeError, "Unknown on_delete option: panic")
|
@@ -459,7 +393,7 @@ describe DBLeftovers do
|
|
459
393
|
|
460
394
|
it "should give good a error message if you use the old :set_null option" do
|
461
395
|
lambda {
|
462
|
-
DBLeftovers::Definition.define do
|
396
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
463
397
|
foreign_key :books, :author_id, :authors, :id, :set_null => true
|
464
398
|
end
|
465
399
|
}.should raise_error(RuntimeError, "`:set_null => true` should now be `:on_delete => :set_null`")
|
@@ -467,60 +401,60 @@ describe DBLeftovers do
|
|
467
401
|
|
468
402
|
it "should give good a error message if you use the old :cascade option" do
|
469
403
|
lambda {
|
470
|
-
DBLeftovers::Definition.define do
|
404
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
471
405
|
foreign_key :books, :author_id, :authors, :id, :cascade => true
|
472
406
|
end
|
473
407
|
}.should raise_error(RuntimeError, "`:cascade => true` should now be `:on_delete => :cascade`")
|
474
408
|
end
|
475
409
|
|
476
410
|
it "should create CHECK constraints on an empty database" do
|
477
|
-
|
478
|
-
DBLeftovers::Definition.define do
|
411
|
+
@db.starts_with([], [], [])
|
412
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
479
413
|
check :books, :books_have_positive_pages, 'pages_count > 0'
|
480
414
|
end
|
481
|
-
|
482
|
-
|
415
|
+
@db.sqls.should have(1).item
|
416
|
+
@db.should have_seen_sql <<-EOQ
|
483
417
|
ALTER TABLE books ADD CONSTRAINT books_have_positive_pages CHECK (pages_count > 0)
|
484
418
|
EOQ
|
485
419
|
end
|
486
420
|
|
487
421
|
it "should create CHECK constraints inside a table block" do
|
488
|
-
|
489
|
-
DBLeftovers::Definition.define do
|
422
|
+
@db.starts_with([], [], [])
|
423
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
490
424
|
table :books do
|
491
425
|
check :books_have_positive_pages, 'pages_count > 0'
|
492
426
|
end
|
493
427
|
end
|
494
|
-
|
495
|
-
|
428
|
+
@db.sqls.should have(1).item
|
429
|
+
@db.should have_seen_sql <<-EOQ
|
496
430
|
ALTER TABLE books ADD CONSTRAINT books_have_positive_pages CHECK (pages_count > 0)
|
497
431
|
EOQ
|
498
432
|
end
|
499
433
|
|
500
434
|
it "should remove obsolete CHECK constraints" do
|
501
|
-
|
435
|
+
@db.starts_with([], [], [
|
502
436
|
DBLeftovers::Constraint.new(:books_have_positive_pages, :books, 'pages_count > 0')
|
503
437
|
])
|
504
|
-
DBLeftovers::Definition.define do
|
438
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
505
439
|
end
|
506
|
-
|
507
|
-
|
440
|
+
@db.sqls.should have(1).item
|
441
|
+
@db.should have_seen_sql <<-EOQ
|
508
442
|
ALTER TABLE books DROP CONSTRAINT books_have_positive_pages
|
509
443
|
EOQ
|
510
444
|
end
|
511
445
|
|
512
446
|
it "should drop and re-create changed CHECK constraints" do
|
513
|
-
|
447
|
+
@db.starts_with([], [], [
|
514
448
|
DBLeftovers::Constraint.new(:books_have_positive_pages, :books, 'pages_count > 0')
|
515
449
|
])
|
516
|
-
DBLeftovers::Definition.define do
|
450
|
+
DBLeftovers::Definition.define :db_interface => @db do
|
517
451
|
check :books, :books_have_positive_pages, 'pages_count > 12'
|
518
452
|
end
|
519
|
-
|
520
|
-
|
453
|
+
@db.sqls.should have(2).items
|
454
|
+
@db.should have_seen_sql <<-EOQ
|
521
455
|
ALTER TABLE books DROP CONSTRAINT books_have_positive_pages
|
522
456
|
EOQ
|
523
|
-
|
457
|
+
@db.should have_seen_sql <<-EOQ
|
524
458
|
ALTER TABLE books ADD CONSTRAINT books_have_positive_pages CHECK (pages_count > 12)
|
525
459
|
EOQ
|
526
460
|
end
|