masamune 0.13.0 → 0.13.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.
@@ -38,6 +38,12 @@ describe Masamune::Transform::DefineSchema do
38
38
  column 'preferences', type: :key_value, null: true
39
39
  end
40
40
 
41
+ fact 'visits', partition: 'y%Ym%m' do
42
+ partition :y
43
+ partition :m
44
+ measure 'total', type: :integer
45
+ end
46
+
41
47
  file 'user' do
42
48
  column 'tenant_id', type: :integer
43
49
  column 'user_id', type: :integer
@@ -50,13 +56,31 @@ describe Masamune::Transform::DefineSchema do
50
56
  end
51
57
  end
52
58
 
53
- subject(:result) { transform.define_schema(catalog, :postgres).to_s }
59
+ context 'without options' do
60
+ subject(:result) { transform.define_schema(catalog, :postgres).to_s }
54
61
 
55
- it 'should render combined template' do
56
- is_expected.to eq Masamune::Template.combine \
57
- Masamune::Transform::Operator.new('define_schema', source: catalog.postgres),
58
- transform.define_table(catalog.postgres.dimensions['user_account_state']),
59
- transform.define_table(catalog.postgres.dimensions['user'])
62
+ it 'should render combined template' do
63
+ is_expected.to eq Masamune::Template.combine \
64
+ Masamune::Transform::Operator.new('define_schema', source: catalog.postgres),
65
+ transform.define_table(catalog.postgres.dimensions['user_account_state']),
66
+ transform.define_table(catalog.postgres.dimensions['user']),
67
+ transform.define_table(catalog.postgres.facts['visits'])
68
+ end
69
+ end
70
+
71
+ context 'without start_date and stop_date' do
72
+ subject(:result) { transform.define_schema(catalog, :postgres, start_date: Date.civil(2015, 01, 01), stop_date: Date.civil(2015, 03, 15)).to_s }
73
+
74
+ it 'should render combined template' do
75
+ is_expected.to eq Masamune::Template.combine \
76
+ Masamune::Transform::Operator.new('define_schema', source: catalog.postgres),
77
+ transform.define_table(catalog.postgres.dimensions['user_account_state']),
78
+ transform.define_table(catalog.postgres.dimensions['user']),
79
+ transform.define_table(catalog.postgres.facts['visits']),
80
+ transform.define_table(catalog.postgres.facts['visits'].partition_table(Date.civil(2015, 01, 01))),
81
+ transform.define_table(catalog.postgres.facts['visits'].partition_table(Date.civil(2015, 02, 01))),
82
+ transform.define_table(catalog.postgres.facts['visits'].partition_table(Date.civil(2015, 03, 01)))
83
+ end
60
84
  end
61
85
  end
62
86
 
@@ -148,6 +148,17 @@ describe Masamune::Transform::DefineTable do
148
148
  end
149
149
  end
150
150
 
151
+ context 'for postgres fact partition with :post' do
152
+ let(:target) { catalog.postgres.visits_fact.partition_table(Date.civil(2015, 01, 01)) }
153
+
154
+ subject(:result) { transform.define_table(target, [], :post).to_s }
155
+
156
+ it 'should eq render table template' do
157
+ is_expected.to match /ALTER TABLE visits_fact_y2015m01 INHERIT visits_fact;/
158
+ is_expected.to match /ALTER TABLE visits_fact_y2015m01 ADD CONSTRAINT visits_fact_y2015m01_time_key_check CHECK \(time_key >= 1420070400 AND time_key < 1422748800\);/
159
+ end
160
+ end
161
+
151
162
  describe 'for fact table from file with sources files' do
152
163
  let(:files) { (1..3).map { |i| double(path: "output_#{i}.csv") } }
153
164
  let(:target) { catalog.postgres.visits_fact }
@@ -24,9 +24,9 @@ require 'spec_helper'
24
24
 
25
25
  describe Masamune::Transform::DefineTable do
26
26
  let(:files) { [] }
27
- let(:options) { {} }
27
+ let(:section) { :all }
28
28
 
29
- subject { transform.define_table(target, files, options).to_s }
29
+ subject { transform.define_table(target, files, section).to_s }
30
30
 
31
31
  context 'for postgres table with columns' do
32
32
  before do
@@ -794,7 +794,7 @@ describe Masamune::Transform::DefineTable do
794
794
  end
795
795
  end
796
796
 
797
- context 'for postgres table with index columns and with_index: false' do
797
+ context 'for postgres table with index columns and section :pre' do
798
798
  before do
799
799
  catalog.schema :postgres do
800
800
  table 'user' do
@@ -804,7 +804,7 @@ describe Masamune::Transform::DefineTable do
804
804
  end
805
805
  end
806
806
 
807
- let(:options) { { with_index: false } }
807
+ let(:section) { :pre }
808
808
  let(:target) { catalog.postgres.user_table }
809
809
 
810
810
  it 'should render table template' do
@@ -815,16 +815,11 @@ describe Masamune::Transform::DefineTable do
815
815
  tenant_id INTEGER NOT NULL,
816
816
  user_id INTEGER NOT NULL
817
817
  );
818
-
819
- DO $$ BEGIN
820
- IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_pkey') THEN
821
- ALTER TABLE user_table ADD PRIMARY KEY (id);
822
- END IF; END $$;
823
818
  EOS
824
819
  end
825
820
  end
826
821
 
827
- context 'for postgres table with referenced tables and with_foreign_key: false' do
822
+ context 'for postgres table with referenced tables and section :pre' do
828
823
  before do
829
824
  catalog.schema :postgres do
830
825
  table 'user_account_state' do
@@ -842,7 +837,7 @@ describe Masamune::Transform::DefineTable do
842
837
  end
843
838
  end
844
839
 
845
- let(:options) { { with_foreign_key: false } }
840
+ let(:section) { :pre }
846
841
  let(:target) { catalog.postgres.user_table }
847
842
 
848
843
  it 'should render table template' do
@@ -853,16 +848,11 @@ describe Masamune::Transform::DefineTable do
853
848
  user_account_state_table_id INTEGER NOT NULL DEFAULT default_user_account_state_table_id(),
854
849
  name VARCHAR NOT NULL
855
850
  );
856
-
857
- DO $$ BEGIN
858
- IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_pkey') THEN
859
- ALTER TABLE user_table ADD PRIMARY KEY (id);
860
- END IF; END $$;
861
851
  EOS
862
852
  end
863
853
  end
864
854
 
865
- context 'for postgres table with unique columns and with_unique_constraint: false' do
855
+ context 'for postgres table with unique columns and section :pre' do
866
856
  before do
867
857
  catalog.schema :postgres do
868
858
  table 'user' do
@@ -872,7 +862,7 @@ describe Masamune::Transform::DefineTable do
872
862
  end
873
863
  end
874
864
 
875
- let(:options) { { with_unique_constraint: false } }
865
+ let(:section) { :pre }
876
866
  let(:target) { catalog.postgres.user_table }
877
867
 
878
868
  it 'should render table template' do
@@ -883,11 +873,109 @@ describe Masamune::Transform::DefineTable do
883
873
  tenant_id INTEGER NOT NULL,
884
874
  user_id INTEGER NOT NULL
885
875
  );
876
+ EOS
877
+ end
878
+ end
879
+
880
+ context 'for postgres table with unique columns and section :post' do
881
+ before do
882
+ catalog.schema :postgres do
883
+ table 'user' do
884
+ column 'tenant_id', unique: true
885
+ column 'user_id'
886
+ end
887
+ end
888
+ end
889
+
890
+ let(:section) { :post }
891
+ let(:target) { catalog.postgres.user_table }
892
+
893
+ it 'should render table template' do
894
+ is_expected.to eq <<-EOS.strip_heredoc
895
+ DO $$ BEGIN
896
+ IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_pkey') THEN
897
+ ALTER TABLE user_table ADD PRIMARY KEY (id);
898
+ END IF; END $$;
899
+
900
+ DO $$ BEGIN
901
+ IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_3854361_key') THEN
902
+ ALTER TABLE user_table ADD CONSTRAINT user_table_3854361_key UNIQUE(tenant_id);
903
+ END IF; END $$;
904
+
905
+ ANALYZE user_table;
906
+ EOS
907
+ end
908
+ end
909
+
910
+ context 'for postgres table with index columns and section :post' do
911
+ before do
912
+ catalog.schema :postgres do
913
+ table 'user' do
914
+ column 'tenant_id', index: true
915
+ column 'user_id', index: true
916
+ end
917
+ end
918
+ end
919
+
920
+ let(:section) { :post }
921
+ let(:target) { catalog.postgres.user_table }
886
922
 
923
+ it 'should render table template' do
924
+ is_expected.to eq <<-EOS.strip_heredoc
925
+ DO $$ BEGIN
926
+ IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_pkey') THEN
927
+ ALTER TABLE user_table ADD PRIMARY KEY (id);
928
+ END IF; END $$;
929
+
930
+ DO $$ BEGIN
931
+ IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_3854361_index') THEN
932
+ CREATE INDEX user_table_3854361_index ON user_table (tenant_id);
933
+ END IF; END $$;
934
+
935
+ DO $$ BEGIN
936
+ IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_e8701ad_index') THEN
937
+ CREATE INDEX user_table_e8701ad_index ON user_table (user_id);
938
+ END IF; END $$;
939
+
940
+ ANALYZE user_table;
941
+ EOS
942
+ end
943
+ end
944
+
945
+ context 'for postgres table with referenced tables and section :post' do
946
+ before do
947
+ catalog.schema :postgres do
948
+ table 'user_account_state' do
949
+ column 'name', type: :string, unique: true
950
+ column 'description', type: :string
951
+ row name: 'registered', description: 'Registered'
952
+ row name: 'active', description: 'Active', attributes: { default: true }
953
+ row name: 'inactive', description: 'Inactive'
954
+ end
955
+
956
+ table 'user' do
957
+ references :user_account_state
958
+ column 'name', type: :string
959
+ end
960
+ end
961
+ end
962
+
963
+ let(:section) { :post }
964
+ let(:target) { catalog.postgres.user_table }
965
+
966
+ it 'should render table template' do
967
+ is_expected.to eq <<-EOS.strip_heredoc
887
968
  DO $$ BEGIN
888
969
  IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_pkey') THEN
889
970
  ALTER TABLE user_table ADD PRIMARY KEY (id);
890
971
  END IF; END $$;
972
+
973
+ DO $$ BEGIN
974
+ IF NOT EXISTS (SELECT 1 FROM pg_constraint c WHERE c.conname = 'user_table_bd2027e_fkey') THEN
975
+ ALTER TABLE user_table ADD CONSTRAINT user_table_bd2027e_fkey FOREIGN KEY (user_account_state_table_id) REFERENCES user_account_state_table(id);
976
+ END IF; END $$;
977
+
978
+ ANALYZE user_table;
891
979
  EOS
892
980
  end
893
981
  end
@@ -37,16 +37,19 @@ describe Masamune::Transform::DenormalizeTable do
37
37
  end
38
38
 
39
39
  dimension 'tenant', type: :two do
40
+ references :cluster, natural_key: true
40
41
  column 'tenant_id', type: :integer, index: true, natural_key: true
41
42
  end
42
43
 
43
44
  dimension 'user', type: :two do
45
+ references :cluster
44
46
  column 'tenant_id', type: :integer, index: true, natural_key: true
45
47
  column 'user_id', type: :integer, index: true, natural_key: true
46
48
  column 'name', type: :string
47
49
  end
48
50
 
49
51
  dimension 'user_agent', type: :mini do
52
+ references :cluster
50
53
  column 'name', type: :string, unique: true, index: 'shared'
51
54
  column 'version', type: :string, unique: true, index: 'shared', default: 'Unknown'
52
55
  column 'mobile', type: :boolean, unique: true, index: 'shared', default: false
@@ -60,6 +63,7 @@ describe Masamune::Transform::DenormalizeTable do
60
63
  references :user, label: 'manager'
61
64
  references :user
62
65
  references :user_agent
66
+ references :session, degenerate: true
63
67
  measure 'total', type: :integer
64
68
  end
65
69
  end
@@ -69,6 +73,70 @@ describe Masamune::Transform::DenormalizeTable do
69
73
 
70
74
  subject(:result) { transform.denormalize_table(target, options).to_s }
71
75
 
76
+ context 'with postgres fact without :columns' do
77
+ let(:target) { catalog.postgres.visits_fact }
78
+ let(:options) { { } }
79
+
80
+ it 'should eq render denormalize_table template' do
81
+ is_expected.to eq <<-EOS.strip_heredoc
82
+ SELECT
83
+ cluster_type.name AS cluster_type_name,
84
+ date_dimension.date_id AS date_dimension_date_id,
85
+ tenant_dimension.tenant_id AS tenant_dimension_tenant_id,
86
+ manager_user_dimension.tenant_id AS manager_user_dimension_tenant_id,
87
+ manager_user_dimension.user_id AS manager_user_dimension_user_id,
88
+ user_dimension.tenant_id AS user_dimension_tenant_id,
89
+ user_dimension.user_id AS user_dimension_user_id,
90
+ user_agent_type.name AS user_agent_type_name,
91
+ user_agent_type.version AS user_agent_type_version,
92
+ user_agent_type.mobile AS user_agent_type_mobile,
93
+ session_type_id AS session_type_id,
94
+ visits_fact.total,
95
+ visits_fact.time_key
96
+ FROM
97
+ visits_fact
98
+ LEFT JOIN
99
+ cluster_type
100
+ ON
101
+ cluster_type.id = visits_fact.cluster_type_id
102
+ LEFT JOIN
103
+ date_dimension
104
+ ON
105
+ date_dimension.id = visits_fact.date_dimension_id
106
+ LEFT JOIN
107
+ tenant_dimension
108
+ ON
109
+ tenant_dimension.id = visits_fact.tenant_dimension_id
110
+ LEFT JOIN
111
+ user_dimension AS manager_user_dimension
112
+ ON
113
+ manager_user_dimension.id = visits_fact.manager_user_dimension_id
114
+ LEFT JOIN
115
+ user_dimension
116
+ ON
117
+ user_dimension.id = visits_fact.user_dimension_id
118
+ LEFT JOIN
119
+ user_agent_type
120
+ ON
121
+ user_agent_type.id = visits_fact.user_agent_type_id
122
+ ORDER BY
123
+ cluster_type_name,
124
+ date_dimension_date_id,
125
+ tenant_dimension_tenant_id,
126
+ manager_user_dimension_tenant_id,
127
+ manager_user_dimension_user_id,
128
+ user_dimension_tenant_id,
129
+ user_dimension_user_id,
130
+ user_agent_type_name,
131
+ user_agent_type_version,
132
+ user_agent_type_mobile,
133
+ session_type_id,
134
+ total,
135
+ time_key
136
+ ;
137
+ EOS
138
+ end
139
+ end
72
140
  context 'with postgres fact with :columns' do
73
141
  let(:target) { catalog.postgres.visits_fact }
74
142
  let(:options) do
@@ -144,8 +212,7 @@ describe Masamune::Transform::DenormalizeTable do
144
212
  let(:options) do
145
213
  {
146
214
  except: [
147
- 'cluster.name',
148
- 'last_modified_at'
215
+ 'cluster.name'
149
216
  ]
150
217
  }
151
218
  end
@@ -162,6 +229,7 @@ describe Masamune::Transform::DenormalizeTable do
162
229
  user_agent_type.name AS user_agent_type_name,
163
230
  user_agent_type.version AS user_agent_type_version,
164
231
  user_agent_type.mobile AS user_agent_type_mobile,
232
+ session_type_id AS session_type_id,
165
233
  visits_fact.total,
166
234
  visits_fact.time_key
167
235
  FROM
@@ -196,6 +264,7 @@ describe Masamune::Transform::DenormalizeTable do
196
264
  user_agent_type_name,
197
265
  user_agent_type_version,
198
266
  user_agent_type_mobile,
267
+ session_type_id,
199
268
  total,
200
269
  time_key
201
270
  ;
@@ -156,6 +156,7 @@ describe Masamune::Transform::RollupFact do
156
156
 
157
157
  ALTER TABLE visits_hourly_fact_y2014m08 INHERIT visits_hourly_fact;
158
158
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600);
159
+
159
160
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_d6b9b38_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
160
161
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_0a531a8_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
161
162
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_d3950d9_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
@@ -257,6 +258,7 @@ describe Masamune::Transform::RollupFact do
257
258
 
258
259
  ALTER TABLE visits_daily_fact_y2014m08 INHERIT visits_daily_fact;
259
260
  ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600);
261
+
260
262
  ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_d6b9b38_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
261
263
  ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_0a531a8_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
262
264
  ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_d3950d9_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
@@ -358,6 +360,7 @@ describe Masamune::Transform::RollupFact do
358
360
 
359
361
  ALTER TABLE visits_monthly_fact_y2014m08 INHERIT visits_monthly_fact;
360
362
  ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600);
363
+
361
364
  ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_d6b9b38_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
362
365
  ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_0a531a8_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
363
366
  ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_d3950d9_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
@@ -211,6 +211,7 @@ describe Masamune::Transform::StageFact do
211
211
 
212
212
  ALTER TABLE visits_hourly_fact_y2014m08 INHERIT visits_hourly_fact;
213
213
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600);
214
+
214
215
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_d6b9b38_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
215
216
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_0a531a8_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
216
217
  ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_ff74c56_fkey FOREIGN KEY (cluster_type_id, tenant_dimension_id) REFERENCES tenant_dimension(cluster_type_id, id) NOT VALID DEFERRABLE INITIALLY DEFERRED;
@@ -52,6 +52,23 @@ module TaskExampleGroup
52
52
  end
53
53
  end
54
54
 
55
+ shared_examples 'executes with success' do
56
+ it 'exits with status code 0' do
57
+ expect { cli_invocation }.to raise_error { |e|
58
+ expect(e).to be_a(SystemExit)
59
+ expect(e.status).to eq(0)
60
+ }
61
+ end
62
+ end
63
+
64
+ shared_examples 'raises Thor::MalformattedArgumentError' do |message|
65
+ it { expect { cli_invocation }.to raise_error Thor::MalformattedArgumentError, message }
66
+ end
67
+
68
+ shared_examples 'raises Thor::RequiredArgumentMissingError' do |message|
69
+ it { expect { cli_invocation }.to raise_error Thor::RequiredArgumentMissingError, message }
70
+ end
71
+
55
72
  def self.included(base)
56
73
  base.before :all do
57
74
  ENV['THOR_DEBUG'] = '1'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: masamune
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.13.0
4
+ version: 0.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Andrews
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-11 00:00:00.000000000 Z
11
+ date: 2015-09-28 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor
@@ -272,6 +272,7 @@ files:
272
272
  - lib/masamune/transform/deduplicate_dimension.rb
273
273
  - lib/masamune/transform/define_foreign_key.psql.erb
274
274
  - lib/masamune/transform/define_index.psql.erb
275
+ - lib/masamune/transform/define_inheritance.psql.erb
275
276
  - lib/masamune/transform/define_schema.hql.erb
276
277
  - lib/masamune/transform/define_schema.psql.erb
277
278
  - lib/masamune/transform/define_schema.rb