chrono_model 0.9.0 → 0.9.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/chrono_model/adapter.rb +45 -5
- data/lib/chrono_model/time_machine.rb +1 -1
- data/lib/chrono_model/version.rb +1 -1
- data/spec/adapter_spec.rb +69 -21
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cee551e571642d52fea58087df5f10e7462867ed
|
4
|
+
data.tar.gz: e1e4ba037b6fdda2b322743d777e0ba5fab27109
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 105ae80bfc9669dba6eadee46d113d5c57276924cb2d702fde777a75a628c297ade0acf338bfc7fcce418676938c20bb66e371866c4f1763c81f5d7ec03926e0
|
7
|
+
data.tar.gz: 5285cbce3977b45c8580e649a859ea16c42a69871b9a8a7a23f2a6225db808fba5439754c84d24e86ec2ca920c4558a1eabffade89953104aa777da1f38d2e57
|
data/lib/chrono_model/adapter.rb
CHANGED
@@ -152,7 +152,7 @@ module ChronoModel
|
|
152
152
|
end
|
153
153
|
end
|
154
154
|
|
155
|
-
chrono_alter(table_name) { super table_name, options, &block }
|
155
|
+
chrono_alter(table_name, options) { super table_name, options, &block }
|
156
156
|
else
|
157
157
|
if options[:temporal] == false && is_chrono?(table_name)
|
158
158
|
# Remove temporal features from this table
|
@@ -567,8 +567,8 @@ module ChronoModel
|
|
567
567
|
metadata = chrono_metadata_for(table_name)
|
568
568
|
version = metadata['chronomodel']
|
569
569
|
|
570
|
-
if version.blank?
|
571
|
-
|
570
|
+
if version.blank?
|
571
|
+
upgrade_from_legacy(table_name)
|
572
572
|
end
|
573
573
|
|
574
574
|
next if version == current
|
@@ -587,6 +587,46 @@ module ChronoModel
|
|
587
587
|
$stderr.puts message
|
588
588
|
end
|
589
589
|
|
590
|
+
def upgrade_from_legacy(table_name)
|
591
|
+
# roses are red
|
592
|
+
# violets are blue
|
593
|
+
# and this is the most boring piece of code ever
|
594
|
+
history_table = "#{HISTORY_SCHEMA}.#{table_name}"
|
595
|
+
p_pkey = primary_key(table_name)
|
596
|
+
|
597
|
+
execute "ALTER TABLE #{history_table} ADD COLUMN validity tsrange;"
|
598
|
+
execute """
|
599
|
+
UPDATE #{history_table} SET validity = tsrange(valid_from,
|
600
|
+
CASE WHEN extract(year from valid_to) = 9999 THEN NULL
|
601
|
+
ELSE valid_to
|
602
|
+
END
|
603
|
+
);
|
604
|
+
"""
|
605
|
+
|
606
|
+
execute "DROP INDEX #{history_table}_temporal_on_valid_from;"
|
607
|
+
execute "DROP INDEX #{history_table}_temporal_on_valid_from_and_valid_to;"
|
608
|
+
execute "DROP INDEX #{history_table}_temporal_on_valid_to;"
|
609
|
+
execute "DROP INDEX #{history_table}_inherit_pkey"
|
610
|
+
execute "DROP INDEX #{history_table}_recorded_at"
|
611
|
+
execute "DROP INDEX #{history_table}_instance_history"
|
612
|
+
execute "ALTER TABLE #{history_table} DROP CONSTRAINT #{table_name}_valid_from_before_valid_to;"
|
613
|
+
execute "ALTER TABLE #{history_table} DROP CONSTRAINT #{table_name}_timeline_consistency;"
|
614
|
+
execute "DROP RULE #{table_name}_upd_first ON #{table_name};"
|
615
|
+
execute "DROP RULE #{table_name}_upd_next ON #{table_name};"
|
616
|
+
execute "DROP RULE #{table_name}_del ON #{table_name};"
|
617
|
+
execute "DROP RULE #{table_name}_ins ON #{table_name};"
|
618
|
+
execute "DROP TRIGGER history_ins ON #{TEMPORAL_SCHEMA}.#{table_name};"
|
619
|
+
execute "DROP FUNCTION #{TEMPORAL_SCHEMA}.#{table_name}_ins();"
|
620
|
+
execute "ALTER TABLE #{history_table} DROP COLUMN valid_from;"
|
621
|
+
execute "ALTER TABLE #{history_table} DROP COLUMN valid_to;"
|
622
|
+
|
623
|
+
execute "CREATE EXTENSION IF NOT EXISTS btree_gist;"
|
624
|
+
|
625
|
+
chrono_create_view_for(table_name)
|
626
|
+
_on_history_schema { add_history_validity_constraint(table_name, p_pkey) }
|
627
|
+
_on_history_schema { chrono_create_history_indexes_for(table_name, p_pkey) }
|
628
|
+
end
|
629
|
+
|
590
630
|
def chrono_metadata_for(table)
|
591
631
|
comment = select_value(
|
592
632
|
"SELECT obj_description(#{quote(table)}::regclass)",
|
@@ -811,9 +851,9 @@ module ChronoModel
|
|
811
851
|
# types, the view must be dropped and recreated, while the change has
|
812
852
|
# to be applied to the table in the temporal schema.
|
813
853
|
#
|
814
|
-
def chrono_alter(table_name)
|
854
|
+
def chrono_alter(table_name, opts = {})
|
815
855
|
transaction do
|
816
|
-
options = chrono_metadata_for(table_name)
|
856
|
+
options = chrono_metadata_for(table_name).merge(opts)
|
817
857
|
|
818
858
|
execute "DROP VIEW #{table_name}"
|
819
859
|
|
@@ -451,7 +451,7 @@ module ChronoModel
|
|
451
451
|
end
|
452
452
|
|
453
453
|
def past
|
454
|
-
time_query(:before, :now).where(
|
454
|
+
time_query(:before, :now).where("NOT upper_inf(#{quoted_table_name}.validity)")
|
455
455
|
end
|
456
456
|
|
457
457
|
# To identify this class as the History subclass
|
data/lib/chrono_model/version.rb
CHANGED
data/spec/adapter_spec.rb
CHANGED
@@ -572,37 +572,85 @@ describe ChronoModel::Adapter do
|
|
572
572
|
end
|
573
573
|
|
574
574
|
context 'selective journaled fields' do
|
575
|
-
|
576
|
-
|
577
|
-
|
578
|
-
|
575
|
+
describe 'basic behaviour' do
|
576
|
+
specify do
|
577
|
+
adapter.create_table table, :temporal => true, :journal => %w( foo ) do |t|
|
578
|
+
t.string 'foo'
|
579
|
+
t.string 'bar'
|
580
|
+
end
|
581
|
+
|
582
|
+
adapter.execute <<-SQL
|
583
|
+
INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
|
584
|
+
SQL
|
585
|
+
|
586
|
+
adapter.execute <<-SQL
|
587
|
+
UPDATE #{table} SET foo = 'test foo', bar = 'no history';
|
588
|
+
SQL
|
589
|
+
|
590
|
+
2.times do
|
591
|
+
adapter.execute <<-SQL
|
592
|
+
UPDATE #{table} SET bar = 'really no history';
|
593
|
+
SQL
|
594
|
+
end
|
595
|
+
|
596
|
+
expect(count(current)).to eq 1
|
597
|
+
expect(count(history)).to eq 1
|
598
|
+
|
599
|
+
adapter.drop_table table
|
579
600
|
end
|
601
|
+
end
|
580
602
|
|
581
|
-
|
582
|
-
|
583
|
-
SQL
|
603
|
+
describe 'schema changes' do
|
604
|
+
table 'journaled_things'
|
584
605
|
|
585
|
-
|
586
|
-
|
587
|
-
|
606
|
+
before do
|
607
|
+
adapter.create_table table, :temporal => true, :journal => %w( foo ) do |t|
|
608
|
+
t.string 'foo'
|
609
|
+
t.string 'bar'
|
610
|
+
t.string 'baz'
|
611
|
+
end
|
612
|
+
end
|
613
|
+
|
614
|
+
after do
|
615
|
+
adapter.drop_table table
|
616
|
+
end
|
617
|
+
|
618
|
+
it 'preserves options upon column change' do
|
619
|
+
adapter.change_table table, temporal: true, journal: %w(foo bar)
|
620
|
+
|
621
|
+
adapter.execute <<-SQL
|
622
|
+
INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
|
623
|
+
SQL
|
624
|
+
|
625
|
+
expect(count(current)).to eq 1
|
626
|
+
expect(count(history)).to eq 1
|
588
627
|
|
589
|
-
2.times do
|
590
628
|
adapter.execute <<-SQL
|
591
|
-
UPDATE #{table} SET
|
629
|
+
UPDATE #{table} SET foo = 'test foo', bar = 'chronomodel';
|
592
630
|
SQL
|
631
|
+
|
632
|
+
expect(count(current)).to eq 1
|
633
|
+
expect(count(history)).to eq 2
|
593
634
|
end
|
594
|
-
end
|
595
635
|
|
596
|
-
|
597
|
-
|
598
|
-
end
|
636
|
+
it 'changes option upon table change' do
|
637
|
+
adapter.change_table table, temporal: true, journal: %w(bar)
|
599
638
|
|
600
|
-
|
601
|
-
|
639
|
+
adapter.execute <<-SQL
|
640
|
+
INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
|
641
|
+
UPDATE #{table} SET foo = 'test foo', bar = 'no history';
|
642
|
+
SQL
|
602
643
|
|
603
|
-
|
604
|
-
|
644
|
+
expect(count(current)).to eq 1
|
645
|
+
expect(count(history)).to eq 1
|
605
646
|
|
606
|
-
|
647
|
+
adapter.execute <<-SQL
|
648
|
+
UPDATE #{table} SET foo = 'test foo again', bar = 'no history';
|
649
|
+
SQL
|
607
650
|
|
651
|
+
expect(count(current)).to eq 1
|
652
|
+
expect(count(history)).to eq 1
|
653
|
+
end
|
654
|
+
end
|
655
|
+
end
|
608
656
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chrono_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.9.
|
4
|
+
version: 0.9.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Marcello Barnaba
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2016-09-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|