chrono_model 0.9.0 → 0.9.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 46c5c3d3c7a91b99e025dbf9dbd961962cc36914
4
- data.tar.gz: 0d120a3f68b711f77031e3e3df5d203efd812a23
3
+ metadata.gz: cee551e571642d52fea58087df5f10e7462867ed
4
+ data.tar.gz: e1e4ba037b6fdda2b322743d777e0ba5fab27109
5
5
  SHA512:
6
- metadata.gz: 17f0b8f514b94579d9cd5f4b32fad9c5d8bed4d95282c361acf10f930d7f1fd64d503139b6eb6f5bde6e9c2c51134f3bcfafdb5034b0dde95af1814bdae563c4
7
- data.tar.gz: 4f7d2ee5109d774d3217cec430bf74964b269e9fff6c8b790a6a328fd7bcb043c3e18c87defc3601a4c85e2c2a597bf314c4e0a0251054ca923d4250cc89b6c2
6
+ metadata.gz: 105ae80bfc9669dba6eadee46d113d5c57276924cb2d702fde777a75a628c297ade0acf338bfc7fcce418676938c20bb66e371866c4f1763c81f5d7ec03926e0
7
+ data.tar.gz: 5285cbce3977b45c8580e649a859ea16c42a69871b9a8a7a23f2a6225db808fba5439754c84d24e86ec2ca920c4558a1eabffade89953104aa777da1f38d2e57
@@ -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? # FIXME
571
- raise Error, "ChronoModel found structures created by a too old version. Cannot upgrade right now."
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('NOT upper_inf(validity)')
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
@@ -1,3 +1,3 @@
1
1
  module ChronoModel
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.1"
3
3
  end
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
- before :all do
576
- adapter.create_table table, :temporal => true, :journal => %w( foo ) do |t|
577
- t.string 'foo'
578
- t.string 'bar'
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
- adapter.execute <<-SQL
582
- INSERT INTO #{table} (foo, bar) VALUES ('test foo', 'test bar');
583
- SQL
603
+ describe 'schema changes' do
604
+ table 'journaled_things'
584
605
 
585
- adapter.execute <<-SQL
586
- UPDATE #{table} SET foo = 'test foo', bar = 'no history';
587
- SQL
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 bar = 'really no history';
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
- after :all do
597
- adapter.drop_table table
598
- end
636
+ it 'changes option upon table change' do
637
+ adapter.change_table table, temporal: true, journal: %w(bar)
599
638
 
600
- it { expect(count(current)).to eq 1 }
601
- it { expect(count(history)).to eq 1 }
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
- it 'preserves options upon column change'
604
- it 'changes option upon table change'
644
+ expect(count(current)).to eq 1
645
+ expect(count(history)).to eq 1
605
646
 
606
- end
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.0
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: 2015-10-29 00:00:00.000000000 Z
12
+ date: 2016-09-20 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord