lca 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 62afb1561f978b69743ea334e439cc449375f0b4bc1cad29601b174a5d231a41
4
- data.tar.gz: 1f18a6226d1fee18f3e2d226616c34dd3cae9ba99389ef9ffb17cf828c586e34
3
+ metadata.gz: 3d5a7b8821030f1d77f41f6fda374ffa6179bf8f412355380dbabb17618d820f
4
+ data.tar.gz: 1a16958b8df53caa76ade2b50ab6e8a3e522267ba16ae03c19da3667a6f13ea9
5
5
  SHA512:
6
- metadata.gz: e190b6449f86a075d6582276747f6f17b1d617cf404f1db04ef7600f7ac9a84490d60535901a9b1e3fc212fb855b381fb80f78d583db391a159b27c75b63e9a3
7
- data.tar.gz: b2fc67ead55300e549192c8f1b37e2971e11bc4da9ed08956ea46a601fc6aba1596a56106822f1b085e2c82d94560975bcc301ef8df3946121a331ddcd925a7f
6
+ metadata.gz: a16076c63779cd5dd152e6c4a95a0a332274659648155ff69b61b2d9540e72f74a726155eeac7ae71287d634aceeb5227e2f174e7ff6b418ff2e72b2ec7c1704
7
+ data.tar.gz: 194fa4db64be6e462c6be6dfc17eeb4acc7e4861778fd6b292830af20ef394e3a82db564a9199bea8f7fee75e884400058809540b3bc161a54bb007f6c1b1811
@@ -3,12 +3,13 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
3
3
 
4
4
  # enable ltree extension
5
5
  begin
6
- execute "create extension ltree"
6
+ execute "create extension ltree"
7
7
  rescue
8
- p "LTREE was already enabled"
8
+ p "LTREE was already enabled"
9
9
  end
10
+
10
11
 
11
- # LCA_OPTIONS[:table_name]
12
+ # create the main table as set up within LCA_OPTIONS[:table_name]
12
13
  execute <<-SQL
13
14
  create table #{ LCA_OPTIONS[:table_name] } (
14
15
  id serial,
@@ -33,16 +34,22 @@ class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version
33
34
  metadata_inline jsonb,
34
35
 
35
36
  impact_amount decimal,
37
+ impact_amount_previous decimal,
36
38
  impact_amount_unit text,
37
39
  impact_factor decimal,
38
40
  impact_precision decimal,
41
+
42
+ created_at timestamp(6) without time zone not null,
43
+ updated_at timestamp(6) without time zone not null,
44
+
39
45
 
40
46
  primary key (id, owner_id)
41
47
  ) partition by list(owner_id)
42
48
  SQL
43
49
 
44
- # TODO do we want to also create an "others" partition?
45
- # "CREATE TABLE #{LCA_OPTIONS[:table_name]}_others PARTITION OF #{LCA_OPTIONS[:table_name]} DEFAULT"
50
+ # create an "others" partition for when the owner is undefined/unknown? just in case / may help in some edge cases
51
+ execute "CREATE TABLE #{LCA_OPTIONS[:table_name]}_others PARTITION OF #{LCA_OPTIONS[:table_name]} DEFAULT"
52
+
46
53
 
47
54
  # add indexes
48
55
  add_index LCA_OPTIONS[:table_name], :id
@@ -54,6 +61,8 @@ SQL
54
61
  add_index LCA_OPTIONS[:table_name], :path, using: :gist
55
62
  add_index LCA_OPTIONS[:table_name], [:data_provider, :data_external_id]
56
63
 
64
+
65
+ # when postgrest is enabled...
57
66
  if LCA_OPTIONS[:create_postgrest_roles]
58
67
  # create postgrest anon user with no privs
59
68
  # postgrest may pass an user's role using JWT
@@ -63,8 +72,38 @@ SQL
63
72
  end
64
73
 
65
74
 
75
+ # snapshots table
76
+ # maybe this should only store the impacts?
77
+ execute <<-SQL
78
+ create table #{ LCA_OPTIONS[:table_name] }_snapshots (
79
+ id serial,
80
+
81
+ impact_id integer,
82
+
83
+ owner_id integer,
84
+ owner_type character varying,
85
+
86
+ impact_amount decimal,
87
+ impact_amount_previous decimal,
88
+ impact_amount_unit text,
89
+ impact_factor decimal,
90
+ impact_precision decimal,
91
+
92
+ created_at timestamp(6) without time zone not null,
93
+ updated_at timestamp(6) without time zone not null,
94
+
95
+ primary key (id, owner_id)
96
+ ) partition by list(owner_id)
97
+ SQL
98
+
99
+ # index the snapshots table
100
+ add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :id
101
+ add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :owner_id
102
+ add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :impact_id
103
+ add_index "#{LCA_OPTIONS[:table_name]}_snapshots", :created_at
104
+
66
105
 
67
- #
106
+ # metadata table
68
107
  create_table "#{ LCA_OPTIONS[:table_name] }_metadata" do |t|
69
108
  t.string :model_type
70
109
  t.integer :model_id
@@ -1,15 +1,11 @@
1
1
  require "jwt"
2
2
 
3
+ # read https://www.postgresqltutorial.com/postgresql-schema/
4
+
3
5
  module Lca
4
6
  module Lcable
5
7
  extend ActiveSupport::Concern
6
8
 
7
- # def self.included(base)
8
- # ::Lca::Model.class_exec do
9
- # @@owner_class = base.name
10
- # end
11
- # end
12
-
13
9
  included do
14
10
 
15
11
  has_many :lca_cycles, class_name: "::Lca::Model", foreign_key: :owner_id, as: :owner
@@ -41,16 +37,21 @@ module Lca
41
37
  lca_sql "create table if not exists #{lca_table_name}_#{id} partition of #{lca_table_name} for values in (#{id})"
42
38
  # TODO create partition indexes
43
39
 
44
- if LCA_OPTIONS[:create_postgrest_roles]
45
- # drop role if it exists
46
- lca_sql "drop role if exists #{ lca_role }"
40
+ # create snapshots partition
41
+ lca_sql "create table if not exists #{lca_table_name}_snapshots_#{id} partition of #{lca_table_name}_snapshots for values in (#{id})"
42
+ # TODO create partition indexes
43
+
44
+ if LCA_OPTIONS[:create_postgrest_roles]
45
+ # drop role if it exists
46
+ lca_sql "drop role if exists #{ lca_role }"
47
47
 
48
- # create role
49
- lca_sql "create role #{ lca_role }"
48
+ # create role
49
+ lca_sql "create role #{ lca_role }"
50
50
 
51
- # grant privs
52
- lca_sql "grant all privileges on #{lca_table_name}_#{id} to #{ lca_role }"
53
- end
51
+ # grant privs
52
+ lca_sql "grant all privileges on #{lca_table_name}_#{id} to #{ lca_role }"
53
+ lca_sql "grant all privileges on #{lca_table_name}_snapshots_#{id} to #{ lca_role }"
54
+ end
54
55
 
55
56
  end # create_storage
56
57
 
@@ -58,21 +59,24 @@ module Lca
58
59
  # Deletes or detaches the partition and removes the role for this owner
59
60
  def lca_delete_storage
60
61
 
61
- if LCA_OPTIONS[:create_postgrest_roles]
62
+ if LCA_OPTIONS[:create_postgrest_roles]
62
63
  # revoke privs
63
64
  lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_#{id} FROM #{ lca_role }"
65
+ lca_sql "REVOKE ALL PRIVILEGES ON #{lca_table_name}_snapshots_#{id} FROM #{ lca_role }"
64
66
 
65
67
  # delete role
66
68
  lca_sql "drop role #{ lca_role }"
67
- end
69
+ end
68
70
 
69
- if LCA_OPTIONS[:destroy_partition_on_owner_destroy]
71
+ if LCA_OPTIONS[:destroy_partition_on_owner_destroy]
70
72
  # delete partition
71
73
  lca_sql "drop table if exists #{lca_table_name}_#{id}"
72
- else
73
- # detach and forget about it
74
- lca_sql "alter table #{lca_table_name} detach partition #{ lca_role }"
75
- end
74
+ lca_sql "drop table if exists #{lca_table_name}_snapshots_#{id}"
75
+ else
76
+ # detach and forget about it
77
+ lca_sql "alter table #{lca_table_name} detach partition #{ lca_role }"
78
+ lca_sql "alter table #{lca_table_name}_snapshots detach partition #{ lca_role }"
79
+ end
76
80
  end # delete_storage
77
81
 
78
82
  def lca_sql sql
@@ -1,8 +1,23 @@
1
1
  class Lca::Impact < Lca::Model
2
2
  belongs_to :exchange, class_name: "::Lca::Exchange", foreign_key: :parent_entity_id, required: true
3
3
 
4
+ has_many :snapshots, class_name: "::Lca::ImpactSnapshot", foreign_key: :impact_id, dependent: :destroy
5
+
4
6
  validates_presence_of :impact_amount
5
7
  validates_presence_of :impact_amount_unit, allow_blank: false
6
8
  validates_presence_of :impact_factor
7
9
  validates_presence_of :impact_precision
10
+
11
+ # lol?
12
+ before_update :snapshot!
13
+
14
+
15
+ def snapshot!
16
+ return ::Lca::ImpactSnapshot.create(
17
+ self.attributes
18
+ .deep_symbolize_keys
19
+ .slice(:impact_amount, :impact_amount_unit, :impact_factor, :impact_precision, :owner_id, :owner_type)
20
+ .merge( impact_id: id )
21
+ )
22
+ end # .snapshot!
8
23
  end
@@ -0,0 +1,44 @@
1
+ class Lca::ImpactSnapshot < ActiveRecord::Base
2
+
3
+ self.primary_key = :id
4
+
5
+ def self.table_name
6
+ return "#{::LCA_OPTIONS[:table_name]}_snapshots" if defined? ::LCA_OPTIONS
7
+ return "lca_models_snapshots"
8
+ end
9
+
10
+
11
+ belongs_to :impact, class_name: "::Lca::Impact", foreign_key: :impact_id, required: true
12
+ belongs_to :owner, polymorphic: true, required: true
13
+
14
+ validates_presence_of :impact_amount
15
+ validates_presence_of :impact_amount_unit, allow_blank: false
16
+ validates_presence_of :impact_factor
17
+ validates_presence_of :impact_precision
18
+
19
+ # pull data from owner's partition
20
+ def self.owned_by(owner_id)
21
+ return self if !owner_id.is_a? Integer
22
+ partition_suffix = "_#{owner_id}"
23
+ table = "#{ self.table_name }#{ partition_suffix }"
24
+
25
+ ApplicationRecord.connection.schema_cache.clear!
26
+ return self if !ApplicationRecord.connection.schema_cache.data_source_exists? table
27
+
28
+ model_class = Class.new self
29
+
30
+ original_class_name = self.name
31
+ class_name = "#{name}#{partition_suffix}"
32
+
33
+ model_class.define_singleton_method(:table_name) do
34
+ table
35
+ end
36
+
37
+ model_class.define_singleton_method(:name) do
38
+ class_name
39
+ end
40
+
41
+ model_class
42
+ end # owned_by
43
+
44
+ end
data/lib/lca/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Lca
4
- VERSION = "0.2.5"
4
+ VERSION = "0.2.6"
5
5
  end
data/lib/lca.rb CHANGED
@@ -15,6 +15,7 @@ require_relative "lca/models/stage"
15
15
  require_relative "lca/models/process"
16
16
  require_relative "lca/models/exchange"
17
17
  require_relative "lca/models/impact"
18
+ require_relative "lca/models/impact_snapshot"
18
19
  require_relative "lca/models/cycle/product"
19
20
  require_relative "lca/models/cycle/service"
20
21
  require_relative "lca/models/cycle/activity"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lca
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nick @ Earthster
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-28 00:00:00.000000000 Z
11
+ date: 2021-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -118,6 +118,7 @@ files:
118
118
  - lib/lca/models/impact/human_health/cancer.rb
119
119
  - lib/lca/models/impact/technosphere.rb
120
120
  - lib/lca/models/impact/technosphere/resource_availability.rb
121
+ - lib/lca/models/impact_snapshot.rb
121
122
  - lib/lca/models/metadata.rb
122
123
  - lib/lca/models/model.rb
123
124
  - lib/lca/models/process.rb