sequel-inline_schema 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +0 -0
- data/History.md +7 -0
- data/lib/sequel/inline_schema.rb +1 -1
- data/lib/sequel/plugins/inline_schema.rb +19 -4
- data/spec/sequel/plugins/inline_schema_spec.rb +174 -12
- metadata +1 -1
- metadata.gz.sig +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 698af5cd91bac6d8f22da1c5b27c1fe9bee6de7783c50e8797e4d3b1c006b8f2
|
4
|
+
data.tar.gz: 82405a67152c84ce7f7deb29136d5be145032fc835b3f91f8b46c08c85a1d007
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 76370fa88d1c87fb2b3a2537c621c1b34ac3a15fa353b12db305e33af42e8c240ffa81eb97ab1b3a573d6aee3bf9f4aabccb554920fb832aa187208c926b2307
|
7
|
+
data.tar.gz: 50b13fe397a824b3d799b7d17fd4624cbb5b794ecfa01f75a4d7232a73aee349bba809f49f5a4092a9077f2630dea8f83dfa0428398b778670c3f14b668199a1
|
checksums.yaml.gz.sig
CHANGED
Binary file
|
data.tar.gz.sig
CHANGED
Binary file
|
data/History.md
CHANGED
data/lib/sequel/inline_schema.rb
CHANGED
@@ -210,8 +210,11 @@ module Sequel::Plugins::InlineSchema
|
|
210
210
|
dataset = self.view_dataset or raise "No view declared for this model."
|
211
211
|
options = self.view_options.merge( options )
|
212
212
|
|
213
|
+
self.before_create_view
|
213
214
|
self.db.log_info "Creating view %s(%p): %s" % [ self.table_name, options, dataset.sql ]
|
214
215
|
self.db.create_view( self.table_name, dataset, options )
|
216
|
+
@db_schema = get_db_schema( true )
|
217
|
+
self.after_create_view
|
215
218
|
end
|
216
219
|
|
217
220
|
|
@@ -236,16 +239,16 @@ module Sequel::Plugins::InlineSchema
|
|
236
239
|
|
237
240
|
|
238
241
|
### Drop the view backing this model.
|
239
|
-
def drop_view
|
242
|
+
def drop_view( options={} )
|
240
243
|
self.before_drop_view
|
241
|
-
self.db.drop_view( self.table_name )
|
244
|
+
self.db.drop_view( self.table_name, self.view_options.merge(options) )
|
242
245
|
self.after_drop_view
|
243
246
|
end
|
244
247
|
|
245
248
|
|
246
249
|
### Drop the view if it already exists, otherwise do nothing.
|
247
|
-
def drop_view?
|
248
|
-
self.drop_view if self.view_exists?
|
250
|
+
def drop_view?( options={} )
|
251
|
+
self.drop_view( options ) if self.view_exists?
|
249
252
|
end
|
250
253
|
|
251
254
|
|
@@ -320,6 +323,18 @@ module Sequel::Plugins::InlineSchema
|
|
320
323
|
end
|
321
324
|
|
322
325
|
|
326
|
+
### View-drop hook; called before the backing view is dropped.
|
327
|
+
def before_drop_view
|
328
|
+
return true
|
329
|
+
end
|
330
|
+
|
331
|
+
|
332
|
+
### View-drop hook; called after the backing view is dropped.
|
333
|
+
def after_drop_view
|
334
|
+
return true
|
335
|
+
end
|
336
|
+
|
337
|
+
|
323
338
|
#
|
324
339
|
# Schema-state introspection
|
325
340
|
#
|
@@ -34,7 +34,23 @@ describe Sequel::Plugins::InlineSchema do
|
|
34
34
|
mclass
|
35
35
|
end
|
36
36
|
|
37
|
-
let
|
37
|
+
let( :view_dataset ) { model_class.dataset.group_and_count( :age ) }
|
38
|
+
|
39
|
+
let( :view_class ) do
|
40
|
+
view_class = Class.new( Sequel::Model )
|
41
|
+
view_class.plugin( :inline_schema )
|
42
|
+
view_class.set_dataset( db[view] )
|
43
|
+
view_class.set_view_dataset { view_dataset.naked }
|
44
|
+
return view_class
|
45
|
+
end
|
46
|
+
|
47
|
+
let( :materialized_view_class ) do
|
48
|
+
materialized_view_class = Class.new( Sequel::Model )
|
49
|
+
materialized_view_class.plugin( :inline_schema )
|
50
|
+
materialized_view_class.set_dataset( db[view] )
|
51
|
+
materialized_view_class.set_view_dataset( materialized: true ) { view_dataset.naked }
|
52
|
+
return materialized_view_class
|
53
|
+
end
|
38
54
|
|
39
55
|
let( :valid_pg_attributes ) do
|
40
56
|
[
|
@@ -187,11 +203,6 @@ describe Sequel::Plugins::InlineSchema do
|
|
187
203
|
|
188
204
|
|
189
205
|
it "allows a model to create a view instead of a table" do
|
190
|
-
view_class = Class.new( Sequel::Model )
|
191
|
-
view_class.plugin( :inline_schema )
|
192
|
-
view_class.set_dataset( db[view] )
|
193
|
-
view_class.set_view_dataset { view_dataset.naked }
|
194
|
-
|
195
206
|
db.fetch = fake_db_fetcher
|
196
207
|
db.sqls.clear
|
197
208
|
|
@@ -204,11 +215,6 @@ describe Sequel::Plugins::InlineSchema do
|
|
204
215
|
|
205
216
|
|
206
217
|
it "allows a model to craete a materialized view instead of a table" do
|
207
|
-
materialized_view_class = Class.new( Sequel::Model )
|
208
|
-
materialized_view_class.plugin( :inline_schema )
|
209
|
-
materialized_view_class.set_dataset( db[view] )
|
210
|
-
materialized_view_class.set_view_dataset( materialized: true ) { view_dataset.naked }
|
211
|
-
|
212
218
|
db.fetch = fake_db_fetcher
|
213
219
|
db.sqls.clear
|
214
220
|
|
@@ -221,6 +227,30 @@ describe Sequel::Plugins::InlineSchema do
|
|
221
227
|
end
|
222
228
|
|
223
229
|
|
230
|
+
it "allows a model to drop its view" do
|
231
|
+
db.fetch = fake_db_fetcher
|
232
|
+
db.sqls.clear
|
233
|
+
|
234
|
+
view_class.drop_view
|
235
|
+
|
236
|
+
expect( db.sqls ).to include(
|
237
|
+
%{DROP VIEW "#{view}"}
|
238
|
+
)
|
239
|
+
end
|
240
|
+
|
241
|
+
|
242
|
+
it "allows a model to drop its materialized view" do
|
243
|
+
db.fetch = fake_db_fetcher
|
244
|
+
db.sqls.clear
|
245
|
+
|
246
|
+
materialized_view_class.drop_view
|
247
|
+
|
248
|
+
expect( db.sqls ).to include(
|
249
|
+
%{DROP MATERIALIZED VIEW "#{view}"}
|
250
|
+
)
|
251
|
+
end
|
252
|
+
|
253
|
+
|
224
254
|
describe "table-creation ordering" do
|
225
255
|
|
226
256
|
let( :fake_db_fetcher ) do
|
@@ -308,7 +338,7 @@ describe Sequel::Plugins::InlineSchema do
|
|
308
338
|
end
|
309
339
|
|
310
340
|
|
311
|
-
describe "hooks" do
|
341
|
+
describe "table hooks" do
|
312
342
|
|
313
343
|
let( :model_class ) do
|
314
344
|
class_obj = super()
|
@@ -439,5 +469,137 @@ describe Sequel::Plugins::InlineSchema do
|
|
439
469
|
|
440
470
|
end
|
441
471
|
|
472
|
+
|
473
|
+
describe "view hooks" do
|
474
|
+
|
475
|
+
let( :view_class ) do
|
476
|
+
class_obj = super()
|
477
|
+
class_obj.singleton_class.send( :attr_accessor, :called )
|
478
|
+
class_obj.called = {}
|
479
|
+
class_obj
|
480
|
+
end
|
481
|
+
|
482
|
+
|
483
|
+
it "calls a hook before creating the model's view" do
|
484
|
+
def view_class.before_create_view
|
485
|
+
self.called[ :before_create_view ] = true
|
486
|
+
super
|
487
|
+
end
|
488
|
+
|
489
|
+
view_class.create_view
|
490
|
+
|
491
|
+
expect( view_class.called ).to include( :before_create_view )
|
492
|
+
end
|
493
|
+
|
494
|
+
|
495
|
+
it "allows cancellation of create_view from the before_create_view hook" do
|
496
|
+
def view_class.before_create_view
|
497
|
+
self.called[ :before_create_view ] = true
|
498
|
+
cancel_action
|
499
|
+
end
|
500
|
+
|
501
|
+
expect {
|
502
|
+
view_class.create_view
|
503
|
+
}.to raise_error( Sequel::HookFailed, /hook failed/i )
|
504
|
+
end
|
505
|
+
|
506
|
+
|
507
|
+
it "allows cancellation of create_view with a message from the before_create_view hook" do
|
508
|
+
def view_class.before_create_view
|
509
|
+
self.called[ :before_create_view ] = true
|
510
|
+
cancel_action( "Wait, don't create yet!" )
|
511
|
+
end
|
512
|
+
|
513
|
+
expect {
|
514
|
+
view_class.create_view
|
515
|
+
}.to raise_error( Sequel::HookFailed, "Wait, don't create yet!" )
|
516
|
+
end
|
517
|
+
|
518
|
+
|
519
|
+
it "allows cancellation of create_view with a Symbol from the before_create_view hook" do
|
520
|
+
def view_class.before_create_view
|
521
|
+
self.called[ :before_create_view ] = true
|
522
|
+
cancel_action( :before_create_view )
|
523
|
+
end
|
524
|
+
|
525
|
+
expect {
|
526
|
+
view_class.create_view
|
527
|
+
}.to raise_error( Sequel::HookFailed, /before_create_view/ )
|
528
|
+
end
|
529
|
+
|
530
|
+
|
531
|
+
it "calls a hook after view creation" do
|
532
|
+
def view_class.after_create_view
|
533
|
+
super
|
534
|
+
self.called[ :after_create_view ] = true
|
535
|
+
end
|
536
|
+
|
537
|
+
view_class.create_view
|
538
|
+
|
539
|
+
expect( view_class.called ).to include( :after_create_view )
|
540
|
+
end
|
541
|
+
|
542
|
+
|
543
|
+
it "calls a hook before dropping the model's view" do
|
544
|
+
def view_class.before_drop_view
|
545
|
+
self.called[ :before_drop_view ] = true
|
546
|
+
super
|
547
|
+
end
|
548
|
+
|
549
|
+
view_class.drop_view
|
550
|
+
|
551
|
+
expect( view_class.called ).to include( :before_drop_view )
|
552
|
+
end
|
553
|
+
|
554
|
+
|
555
|
+
it "allows cancellation of drop_view from the before_drop_view hook" do
|
556
|
+
def view_class.before_drop_view
|
557
|
+
self.called[ :before_drop_view ] = true
|
558
|
+
cancel_action
|
559
|
+
end
|
560
|
+
|
561
|
+
expect {
|
562
|
+
view_class.drop_view
|
563
|
+
}.to raise_error( Sequel::HookFailed, /hook failed/i )
|
564
|
+
end
|
565
|
+
|
566
|
+
|
567
|
+
it "allows cancellation of drop_view with a message from the before_drop_view hook" do
|
568
|
+
def view_class.before_drop_view
|
569
|
+
self.called[ :before_drop_view ] = true
|
570
|
+
cancel_action( "Wait, don't drop yet!" )
|
571
|
+
end
|
572
|
+
|
573
|
+
expect {
|
574
|
+
view_class.drop_view
|
575
|
+
}.to raise_error( Sequel::HookFailed, "Wait, don't drop yet!" )
|
576
|
+
end
|
577
|
+
|
578
|
+
|
579
|
+
it "allows cancellation of drop_view with a Symbol from the before_drop_view hook" do
|
580
|
+
def view_class.before_drop_view
|
581
|
+
self.called[ :before_drop_view ] = true
|
582
|
+
cancel_action( :before_drop_view )
|
583
|
+
end
|
584
|
+
|
585
|
+
expect {
|
586
|
+
view_class.drop_view
|
587
|
+
}.to raise_error( Sequel::HookFailed, /before_drop_view/ )
|
588
|
+
end
|
589
|
+
|
590
|
+
|
591
|
+
it "calls a hook after a class's view is dropped" do
|
592
|
+
def view_class.after_drop_view
|
593
|
+
super
|
594
|
+
self.called[ :after_drop_view ] = true
|
595
|
+
end
|
596
|
+
|
597
|
+
view_class.drop_view
|
598
|
+
|
599
|
+
expect( view_class.called ).to include( :after_drop_view )
|
600
|
+
end
|
601
|
+
|
602
|
+
end
|
603
|
+
|
442
604
|
end
|
443
605
|
|
metadata
CHANGED
metadata.gz.sig
CHANGED
Binary file
|