masamune 0.11.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (185) hide show
  1. checksums.yaml +7 -0
  2. data/LICENSE.txt +21 -0
  3. data/README.md +54 -0
  4. data/Rakefile +15 -0
  5. data/bin/masamune-elastic-mapreduce +4 -0
  6. data/bin/masamune-hive +4 -0
  7. data/bin/masamune-psql +4 -0
  8. data/bin/masamune-shell +4 -0
  9. data/lib/masamune.rb +56 -0
  10. data/lib/masamune/accumulate.rb +60 -0
  11. data/lib/masamune/actions.rb +38 -0
  12. data/lib/masamune/actions/data_flow.rb +131 -0
  13. data/lib/masamune/actions/date_parse.rb +75 -0
  14. data/lib/masamune/actions/elastic_mapreduce.rb +68 -0
  15. data/lib/masamune/actions/execute.rb +52 -0
  16. data/lib/masamune/actions/filesystem.rb +37 -0
  17. data/lib/masamune/actions/hadoop_filesystem.rb +40 -0
  18. data/lib/masamune/actions/hadoop_streaming.rb +41 -0
  19. data/lib/masamune/actions/hive.rb +74 -0
  20. data/lib/masamune/actions/postgres.rb +76 -0
  21. data/lib/masamune/actions/postgres_admin.rb +34 -0
  22. data/lib/masamune/actions/s3cmd.rb +44 -0
  23. data/lib/masamune/actions/transform.rb +89 -0
  24. data/lib/masamune/after_initialize_callbacks.rb +55 -0
  25. data/lib/masamune/cached_filesystem.rb +110 -0
  26. data/lib/masamune/commands.rb +37 -0
  27. data/lib/masamune/commands/elastic_mapreduce.rb +119 -0
  28. data/lib/masamune/commands/hadoop_filesystem.rb +57 -0
  29. data/lib/masamune/commands/hadoop_streaming.rb +116 -0
  30. data/lib/masamune/commands/hive.rb +178 -0
  31. data/lib/masamune/commands/interactive.rb +37 -0
  32. data/lib/masamune/commands/postgres.rb +128 -0
  33. data/lib/masamune/commands/postgres_admin.rb +72 -0
  34. data/lib/masamune/commands/postgres_common.rb +33 -0
  35. data/lib/masamune/commands/retry_with_backoff.rb +60 -0
  36. data/lib/masamune/commands/s3cmd.rb +70 -0
  37. data/lib/masamune/commands/shell.rb +202 -0
  38. data/lib/masamune/configuration.rb +195 -0
  39. data/lib/masamune/data_plan.rb +31 -0
  40. data/lib/masamune/data_plan/builder.rb +66 -0
  41. data/lib/masamune/data_plan/elem.rb +190 -0
  42. data/lib/masamune/data_plan/engine.rb +162 -0
  43. data/lib/masamune/data_plan/rule.rb +292 -0
  44. data/lib/masamune/data_plan/set.rb +176 -0
  45. data/lib/masamune/environment.rb +164 -0
  46. data/lib/masamune/filesystem.rb +567 -0
  47. data/lib/masamune/has_environment.rb +40 -0
  48. data/lib/masamune/helpers.rb +27 -0
  49. data/lib/masamune/helpers/postgres.rb +84 -0
  50. data/lib/masamune/io.rb +33 -0
  51. data/lib/masamune/last_element.rb +53 -0
  52. data/lib/masamune/method_logger.rb +41 -0
  53. data/lib/masamune/multi_io.rb +39 -0
  54. data/lib/masamune/schema.rb +36 -0
  55. data/lib/masamune/schema/catalog.rb +233 -0
  56. data/lib/masamune/schema/column.rb +527 -0
  57. data/lib/masamune/schema/dimension.rb +133 -0
  58. data/lib/masamune/schema/event.rb +121 -0
  59. data/lib/masamune/schema/fact.rb +133 -0
  60. data/lib/masamune/schema/map.rb +265 -0
  61. data/lib/masamune/schema/row.rb +133 -0
  62. data/lib/masamune/schema/store.rb +115 -0
  63. data/lib/masamune/schema/table.rb +308 -0
  64. data/lib/masamune/schema/table_reference.rb +76 -0
  65. data/lib/masamune/spec_helper.rb +23 -0
  66. data/lib/masamune/string_format.rb +34 -0
  67. data/lib/masamune/tasks/elastic_mapreduce_thor.rb +60 -0
  68. data/lib/masamune/tasks/hive_thor.rb +55 -0
  69. data/lib/masamune/tasks/postgres_thor.rb +47 -0
  70. data/lib/masamune/tasks/shell_thor.rb +63 -0
  71. data/lib/masamune/template.rb +77 -0
  72. data/lib/masamune/thor.rb +186 -0
  73. data/lib/masamune/thor_loader.rb +38 -0
  74. data/lib/masamune/topological_hash.rb +34 -0
  75. data/lib/masamune/transform.rb +47 -0
  76. data/lib/masamune/transform/bulk_upsert.psql.erb +64 -0
  77. data/lib/masamune/transform/bulk_upsert.rb +52 -0
  78. data/lib/masamune/transform/consolidate_dimension.rb +54 -0
  79. data/lib/masamune/transform/deduplicate_dimension.psql.erb +52 -0
  80. data/lib/masamune/transform/deduplicate_dimension.rb +53 -0
  81. data/lib/masamune/transform/define_event_view.hql.erb +51 -0
  82. data/lib/masamune/transform/define_event_view.rb +60 -0
  83. data/lib/masamune/transform/define_index.psql.erb +34 -0
  84. data/lib/masamune/transform/define_schema.hql.erb +23 -0
  85. data/lib/masamune/transform/define_schema.psql.erb +79 -0
  86. data/lib/masamune/transform/define_schema.rb +56 -0
  87. data/lib/masamune/transform/define_table.hql.erb +34 -0
  88. data/lib/masamune/transform/define_table.psql.erb +95 -0
  89. data/lib/masamune/transform/define_table.rb +40 -0
  90. data/lib/masamune/transform/define_unique.psql.erb +30 -0
  91. data/lib/masamune/transform/insert_reference_values.psql.erb +43 -0
  92. data/lib/masamune/transform/insert_reference_values.rb +64 -0
  93. data/lib/masamune/transform/load_dimension.rb +47 -0
  94. data/lib/masamune/transform/load_fact.rb +45 -0
  95. data/lib/masamune/transform/operator.rb +96 -0
  96. data/lib/masamune/transform/relabel_dimension.psql.erb +76 -0
  97. data/lib/masamune/transform/relabel_dimension.rb +39 -0
  98. data/lib/masamune/transform/rollup_fact.psql.erb +79 -0
  99. data/lib/masamune/transform/rollup_fact.rb +149 -0
  100. data/lib/masamune/transform/snapshot_dimension.psql.erb +75 -0
  101. data/lib/masamune/transform/snapshot_dimension.rb +74 -0
  102. data/lib/masamune/transform/stage_dimension.psql.erb +39 -0
  103. data/lib/masamune/transform/stage_dimension.rb +83 -0
  104. data/lib/masamune/transform/stage_fact.psql.erb +80 -0
  105. data/lib/masamune/transform/stage_fact.rb +111 -0
  106. data/lib/masamune/version.rb +25 -0
  107. data/spec/fixtures/aggregate.sql.erb +25 -0
  108. data/spec/fixtures/comment.sql.erb +27 -0
  109. data/spec/fixtures/invalid.sql.erb +23 -0
  110. data/spec/fixtures/relative.sql.erb +23 -0
  111. data/spec/fixtures/simple.sql.erb +28 -0
  112. data/spec/fixtures/whitespace.sql.erb +30 -0
  113. data/spec/masamune/actions/elastic_mapreduce_spec.rb +108 -0
  114. data/spec/masamune/actions/execute_spec.rb +50 -0
  115. data/spec/masamune/actions/hadoop_filesystem_spec.rb +44 -0
  116. data/spec/masamune/actions/hadoop_streaming_spec.rb +74 -0
  117. data/spec/masamune/actions/hive_spec.rb +117 -0
  118. data/spec/masamune/actions/postgres_admin_spec.rb +58 -0
  119. data/spec/masamune/actions/postgres_spec.rb +134 -0
  120. data/spec/masamune/actions/s3cmd_spec.rb +44 -0
  121. data/spec/masamune/actions/transform_spec.rb +144 -0
  122. data/spec/masamune/after_initialization_callbacks_spec.rb +61 -0
  123. data/spec/masamune/cached_filesystem_spec.rb +167 -0
  124. data/spec/masamune/commands/hadoop_filesystem_spec.rb +50 -0
  125. data/spec/masamune/commands/hadoop_streaming_spec.rb +106 -0
  126. data/spec/masamune/commands/hive_spec.rb +117 -0
  127. data/spec/masamune/commands/postgres_admin_spec.rb +69 -0
  128. data/spec/masamune/commands/postgres_spec.rb +100 -0
  129. data/spec/masamune/commands/retry_with_backoff_spec.rb +116 -0
  130. data/spec/masamune/commands/s3cmd_spec.rb +50 -0
  131. data/spec/masamune/commands/shell_spec.rb +101 -0
  132. data/spec/masamune/configuration_spec.rb +102 -0
  133. data/spec/masamune/data_plan/builder_spec.rb +91 -0
  134. data/spec/masamune/data_plan/elem_spec.rb +102 -0
  135. data/spec/masamune/data_plan/engine_spec.rb +356 -0
  136. data/spec/masamune/data_plan/rule_spec.rb +407 -0
  137. data/spec/masamune/data_plan/set_spec.rb +517 -0
  138. data/spec/masamune/environment_spec.rb +65 -0
  139. data/spec/masamune/filesystem_spec.rb +1421 -0
  140. data/spec/masamune/helpers/postgres_spec.rb +95 -0
  141. data/spec/masamune/schema/catalog_spec.rb +613 -0
  142. data/spec/masamune/schema/column_spec.rb +696 -0
  143. data/spec/masamune/schema/dimension_spec.rb +137 -0
  144. data/spec/masamune/schema/event_spec.rb +75 -0
  145. data/spec/masamune/schema/fact_spec.rb +117 -0
  146. data/spec/masamune/schema/map_spec.rb +593 -0
  147. data/spec/masamune/schema/row_spec.rb +28 -0
  148. data/spec/masamune/schema/store_spec.rb +49 -0
  149. data/spec/masamune/schema/table_spec.rb +395 -0
  150. data/spec/masamune/string_format_spec.rb +60 -0
  151. data/spec/masamune/tasks/elastic_mapreduce_thor_spec.rb +57 -0
  152. data/spec/masamune/tasks/hive_thor_spec.rb +75 -0
  153. data/spec/masamune/tasks/postgres_thor_spec.rb +42 -0
  154. data/spec/masamune/tasks/shell_thor_spec.rb +51 -0
  155. data/spec/masamune/template_spec.rb +77 -0
  156. data/spec/masamune/thor_spec.rb +238 -0
  157. data/spec/masamune/transform/bulk_upsert.dimension_spec.rb +200 -0
  158. data/spec/masamune/transform/consolidate_dimension_spec.rb +62 -0
  159. data/spec/masamune/transform/deduplicate_dimension_spec.rb +84 -0
  160. data/spec/masamune/transform/define_event_view_spec.rb +84 -0
  161. data/spec/masamune/transform/define_schema_spec.rb +83 -0
  162. data/spec/masamune/transform/define_table.dimension_spec.rb +306 -0
  163. data/spec/masamune/transform/define_table.fact_spec.rb +291 -0
  164. data/spec/masamune/transform/define_table.table_spec.rb +525 -0
  165. data/spec/masamune/transform/insert_reference_values.dimension_spec.rb +111 -0
  166. data/spec/masamune/transform/insert_reference_values.fact_spec.rb +149 -0
  167. data/spec/masamune/transform/load_dimension_spec.rb +76 -0
  168. data/spec/masamune/transform/load_fact_spec.rb +89 -0
  169. data/spec/masamune/transform/relabel_dimension_spec.rb +102 -0
  170. data/spec/masamune/transform/rollup_fact_spec.rb +333 -0
  171. data/spec/masamune/transform/snapshot_dimension_spec.rb +103 -0
  172. data/spec/masamune/transform/stage_dimension_spec.rb +115 -0
  173. data/spec/masamune/transform/stage_fact_spec.rb +204 -0
  174. data/spec/masamune_spec.rb +32 -0
  175. data/spec/spec_helper.rb +41 -0
  176. data/spec/support/masamune/example_group.rb +36 -0
  177. data/spec/support/masamune/mock_command.rb +99 -0
  178. data/spec/support/masamune/mock_delegate.rb +51 -0
  179. data/spec/support/masamune/mock_filesystem.rb +96 -0
  180. data/spec/support/masamune/thor_mute.rb +35 -0
  181. data/spec/support/rspec/example/action_example_group.rb +34 -0
  182. data/spec/support/rspec/example/task_example_group.rb +80 -0
  183. data/spec/support/rspec/example/transform_example_group.rb +36 -0
  184. data/spec/support/shared_examples/postgres_common_examples.rb +53 -0
  185. metadata +462 -0
@@ -0,0 +1,28 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Schema::Row do
26
+ subject(:row) { described_class.new }
27
+ it { expect(row).to_not be_nil }
28
+ end
@@ -0,0 +1,49 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Schema::Store do
26
+ let(:environment) { double }
27
+
28
+ context 'without type' do
29
+ subject(:store) { described_class.new(environment) }
30
+ it { expect { store }.to raise_error ArgumentError, 'required parameter type: missing' }
31
+ end
32
+
33
+ context 'with type :unknown' do
34
+ subject(:store) { described_class.new(environment, type: :unknown) }
35
+ it { expect { store }.to raise_error ArgumentError, "unknown type: 'unknown'" }
36
+ end
37
+
38
+ context 'with type :postgres' do
39
+ subject(:store) { described_class.new(environment, type: :postgres) }
40
+ it { expect(store.format).to eq(:csv) }
41
+ it { expect(store.headers).to be_truthy }
42
+ end
43
+
44
+ context 'with type :hive' do
45
+ subject(:store) { described_class.new(environment, type: :hive) }
46
+ it { expect(store.format).to eq(:tsv) }
47
+ it { expect(store.headers).to be_falsey }
48
+ end
49
+ end
@@ -0,0 +1,395 @@
1
+ # The MIT License (MIT)
2
+ #
3
+ # Copyright (c) 2014-2015, VMware, Inc. All Rights Reserved.
4
+ #
5
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ # of this software and associated documentation files (the "Software"), to deal
7
+ # in the Software without restriction, including without limitation the rights
8
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ # copies of the Software, and to permit persons to whom the Software is
10
+ # furnished to do so, subject to the following conditions:
11
+ #
12
+ # The above copyright notice and this permission notice shall be included in
13
+ # all copies or substantial portions of the Software.
14
+ #
15
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ # THE SOFTWARE.
22
+
23
+ require 'spec_helper'
24
+
25
+ describe Masamune::Schema::Table do
26
+ context 'without id' do
27
+ subject(:table) { described_class.new }
28
+ it { expect { table }.to raise_error ArgumentError }
29
+ end
30
+
31
+ context 'with name' do
32
+ let(:table) do
33
+ described_class.new id: 'user', name: 'account_table'
34
+ end
35
+
36
+ it { expect(table.name).to eq('account_table') }
37
+ end
38
+
39
+ context 'with columns' do
40
+ let(:table) do
41
+ described_class.new id: 'user',
42
+ columns: [
43
+ Masamune::Schema::Column.new(id: 'tenant_id'),
44
+ Masamune::Schema::Column.new(id: 'user_id')
45
+ ]
46
+ end
47
+
48
+ it { expect(table.name).to eq('user_table') }
49
+ end
50
+
51
+ context 'with index columns' do
52
+ let(:table) do
53
+ described_class.new id: 'user',
54
+ columns: [
55
+ Masamune::Schema::Column.new(id: 'tenant_id', index: true),
56
+ Masamune::Schema::Column.new(id: 'user_id', index: true)
57
+ ]
58
+ end
59
+
60
+ it { expect(table.name).to eq('user_table') }
61
+ end
62
+
63
+ context 'with multiple index columns' do
64
+ let(:table) do
65
+ described_class.new id: 'user',
66
+ columns: [
67
+ Masamune::Schema::Column.new(id: 'tenant_id', index: ['tenant_id', 'shared']),
68
+ Masamune::Schema::Column.new(id: 'user_id', index: ['user_id', 'shared'])
69
+ ]
70
+ end
71
+
72
+ it { expect(table.name).to eq('user_table') }
73
+ end
74
+
75
+ context 'with multiple unique columns' do
76
+ let(:table) do
77
+ described_class.new id: 'user',
78
+ columns: [
79
+ Masamune::Schema::Column.new(id: 'tenant_id', unique: ['shared']),
80
+ Masamune::Schema::Column.new(id: 'user_id', unique: ['user_id', 'shared'])
81
+ ]
82
+ end
83
+
84
+ it { expect(table.name).to eq('user_table') }
85
+ end
86
+
87
+ context 'with enum column' do
88
+ let(:table) do
89
+ described_class.new id: 'user',
90
+ columns: [
91
+ Masamune::Schema::Column.new(id: 'tenant_id'),
92
+ Masamune::Schema::Column.new(id: 'user_id'),
93
+ Masamune::Schema::Column.new(id: 'state', type: :enum, sub_type: :user_state, values: %w(active inactive terminated), default: 'active')
94
+ ]
95
+ end
96
+
97
+ it { expect(table.name).to eq('user_table') }
98
+ it { expect(table.stage_table.name).to eq('user_table_stage') }
99
+ end
100
+
101
+ context 'with surrogate_key columns override' do
102
+ let(:table) do
103
+ described_class.new id: 'user',
104
+ columns: [
105
+ Masamune::Schema::Column.new(id: 'identifier', type: :uuid, surrogate_key: true),
106
+ Masamune::Schema::Column.new(id: 'name', type: :string)
107
+ ]
108
+ end
109
+
110
+ it { expect(table.name).to eq('user_table') }
111
+ end
112
+
113
+ context 'with invalid values' do
114
+ let(:table) do
115
+ described_class.new id: 'user',
116
+ columns: [
117
+ Masamune::Schema::Column.new(id: 'name', type: :string, unique: true),
118
+ Masamune::Schema::Column.new(id: 'description', type: :string)
119
+ ],
120
+ rows: [
121
+ Masamune::Schema::Row.new(values: {
122
+ name: 'active',
123
+ description: 'Active',
124
+ missing_column: true
125
+ })
126
+ ]
127
+ end
128
+
129
+ it { expect { table }.to raise_error ArgumentError, /contains undefined columns/ }
130
+ end
131
+
132
+ context 'with partial values' do
133
+ let(:table) do
134
+ described_class.new id: 'user',
135
+ columns: [
136
+ Masamune::Schema::Column.new(id: 'name', type: :string),
137
+ Masamune::Schema::Column.new(id: 'description', type: :string)
138
+ ],
139
+ rows: [
140
+ Masamune::Schema::Row.new(values: {
141
+ name: 'registered',
142
+ description: 'Registered'
143
+ }),
144
+ Masamune::Schema::Row.new(values: {
145
+ name: 'active'
146
+ })
147
+ ]
148
+ end
149
+
150
+ it { expect(table.name).to eq('user_table') }
151
+ end
152
+
153
+ context 'with shared unique index' do
154
+ let(:table) do
155
+ described_class.new id: 'user',
156
+ columns: [
157
+ Masamune::Schema::Column.new(id: 'tenant_id', type: :integer, unique: 'tenant_and_user', index: 'tenant_and_user'),
158
+ Masamune::Schema::Column.new(id: 'user_id', type: :integer, unique: 'tenant_and_user', index: 'tenant_and_user')
159
+ ]
160
+ end
161
+
162
+ it { expect(table.name).to eq('user_table') }
163
+ end
164
+
165
+ context 'with multiple default and named rows' do
166
+ let(:table) do
167
+ described_class.new id: 'user',
168
+ columns: [
169
+ Masamune::Schema::Column.new(id: 'uuid', type: :uuid, surrogate_key: true),
170
+ Masamune::Schema::Column.new(id: 'tenant_id', type: :integer, natural_key: true),
171
+ Masamune::Schema::Column.new(id: 'user_id', type: :integer, natural_key: true)
172
+ ],
173
+ rows: [
174
+ Masamune::Schema::Row.new(values: {
175
+ tenant_id: 'default_tenant_id()',
176
+ user_id: -1,
177
+ }, default: true),
178
+ Masamune::Schema::Row.new(values: {
179
+ tenant_id: 'default_tenant_id()',
180
+ user_id: -2,
181
+ }, id: 'unknown')
182
+ ]
183
+ end
184
+
185
+ it { expect(table.name).to eq('user_table') }
186
+ end
187
+
188
+ context 'with referenced tables with default values' do
189
+ let(:mini_table) do
190
+ described_class.new id: 'user_account_state',
191
+ columns: [
192
+ Masamune::Schema::Column.new(id: 'name', type: :string, unique: true),
193
+ Masamune::Schema::Column.new(id: 'description', type: :string)
194
+ ],
195
+ rows: [
196
+ Masamune::Schema::Row.new(values: {
197
+ name: 'registered',
198
+ description: 'Registered'
199
+ }),
200
+ Masamune::Schema::Row.new(values: {
201
+ name: 'active',
202
+ description: 'Active',
203
+ }, default: true),
204
+ Masamune::Schema::Row.new(values: {
205
+ name: 'inactive',
206
+ description: 'Inactive'
207
+ })
208
+ ]
209
+ end
210
+
211
+ let(:table) do
212
+ described_class.new id: 'user', references: [Masamune::Schema::TableReference.new(mini_table)],
213
+ columns: [
214
+ Masamune::Schema::Column.new(id: 'name', type: :string)
215
+ ]
216
+ end
217
+
218
+ it { expect(table.name).to eq('user_table') }
219
+ it { expect(table.columns[:user_account_state_table_id].required_value?).to eq(false) }
220
+ end
221
+
222
+ context 'with labeled referenced table' do
223
+ let(:mini_table) do
224
+ described_class.new id: 'user_account_state',
225
+ columns: [
226
+ Masamune::Schema::Column.new(id: 'name', type: :string, unique: true),
227
+ Masamune::Schema::Column.new(id: 'description', type: :string)
228
+ ],
229
+ rows: [
230
+ Masamune::Schema::Row.new(values: {name: 'active'}, default: true)
231
+ ]
232
+ end
233
+
234
+ let(:table) do
235
+ described_class.new id: 'user', references: [
236
+ Masamune::Schema::TableReference.new(mini_table),
237
+ Masamune::Schema::TableReference.new(mini_table, label: 'actor', null: true, default: :null)
238
+ ],
239
+ columns: [
240
+ Masamune::Schema::Column.new(id: 'name', type: :string)
241
+ ]
242
+ end
243
+
244
+ it { expect(table.name).to eq('user_table') }
245
+ it { expect(table.columns[:user_account_state_table_id].required_value?).to eq(false) }
246
+ it { expect(table.columns[:actor_user_account_state_table_id].required_value?).to eq(false) }
247
+ end
248
+
249
+ context 'with referenced tables without default values' do
250
+ let(:mini_table) do
251
+ described_class.new id: 'user_account_state',
252
+ columns: [
253
+ Masamune::Schema::Column.new(id: 'name', type: :string, unique: true),
254
+ Masamune::Schema::Column.new(id: 'description', type: :string)
255
+ ]
256
+ end
257
+
258
+ let(:table) do
259
+ described_class.new id: 'user', references: [Masamune::Schema::TableReference.new(mini_table)],
260
+ columns: [
261
+ Masamune::Schema::Column.new(id: 'name', type: :string)
262
+ ]
263
+ end
264
+
265
+ it { expect(table.name).to eq('user_table') }
266
+ it { expect(table.columns[:user_account_state_table_id].required_value?).to eq(true) }
267
+ end
268
+
269
+ context '#stage_table' do
270
+ let(:mini_table) do
271
+ described_class.new id: 'user_account_state',
272
+ columns: [
273
+ Masamune::Schema::Column.new(id: 'name', type: :string, unique: true),
274
+ Masamune::Schema::Column.new(id: 'description', type: :string)
275
+ ]
276
+ end
277
+
278
+ let(:table) do
279
+ described_class.new id: 'user', references: [
280
+ Masamune::Schema::TableReference.new(mini_table),
281
+ Masamune::Schema::TableReference.new(mini_table, label: 'hr')
282
+ ],
283
+ columns: [
284
+ Masamune::Schema::Column.new(id: 'user_id', type: :integer),
285
+ Masamune::Schema::Column.new(id: 'name', type: :string),
286
+ Masamune::Schema::Column.new(id: 'last_modified_at', type: :timestamp)
287
+ ]
288
+ end
289
+
290
+ context 'without suffix or selected columns' do
291
+ let!(:stage_table) { table.stage_table }
292
+
293
+ it 'should duplicate columns' do
294
+ expect(table.parent).to be_nil
295
+ expect(table.columns[:name].parent).to eq(table)
296
+ expect(stage_table.parent).to eq(table)
297
+ expect(stage_table.columns[:name].parent).to eq(stage_table)
298
+ end
299
+ end
300
+
301
+ context 'with optional suffix' do
302
+ let!(:stage_table) { table.stage_table(suffix: 'actor') }
303
+
304
+ it 'should append suffix to id' do
305
+ expect(stage_table.id).to eq(:user_actor)
306
+ expect(stage_table.name).to eq('user_actor_table_stage')
307
+ end
308
+
309
+ it 'should duplicate columns' do
310
+ expect(table.parent).to be_nil
311
+ expect(table.columns[:name].parent).to eq(table)
312
+ expect(stage_table.parent).to eq(table)
313
+ expect(stage_table.columns[:name].parent).to eq(stage_table)
314
+ end
315
+ end
316
+
317
+ context 'with specified columns' do
318
+ subject(:stage_table) { table.stage_table(columns: %w(id name user_account_state.id hr_user_account_state.id)) }
319
+
320
+ it 'should stage table' do
321
+ expect(stage_table.name).to eq('user_table_stage')
322
+ expect(stage_table.columns.keys).to eq([:name, :user_account_state_table_id, :hr_user_account_state_table_id])
323
+ expect(stage_table.references.keys).to eq([:user_account_state, :hr_user_account_state])
324
+ end
325
+ end
326
+
327
+ context 'with specified columns (denormalized)' do
328
+ subject(:stage_table) { table.stage_table(columns: %w(id name user_account_state.name hr_user_account_state.name)) }
329
+
330
+ it 'should stage table' do
331
+ expect(stage_table.name).to eq('user_table_stage')
332
+ expect(stage_table.columns.keys).to eq([:name, :user_account_state_table_name, :hr_user_account_state_table_name])
333
+ expect(stage_table.references.keys).to eq([:user_account_state, :hr_user_account_state])
334
+ end
335
+ end
336
+
337
+ context 'with specified target table' do
338
+ let(:target) do
339
+ described_class.new id: 'user',
340
+ columns: [
341
+ Masamune::Schema::Column.new(id: 'name', type: :string),
342
+ Masamune::Schema::Column.new(id: 'user_account_state.name', type: :string),
343
+ Masamune::Schema::Column.new(id: 'hr_user_account_state.name', type: :string)
344
+ ]
345
+ end
346
+
347
+ subject(:stage_table) { table.stage_table(target: target) }
348
+
349
+ it 'should stage table' do
350
+ expect(stage_table.name).to eq('user_table_stage')
351
+ expect(stage_table.columns.keys).to eq([:name, :user_account_state_table_name, :hr_user_account_state_table_name])
352
+ expect(stage_table.references.keys).to eq([:user_account_state, :hr_user_account_state])
353
+ end
354
+ end
355
+
356
+ context 'with specified target table (referenced columns)' do
357
+ let(:target) do
358
+ described_class.new id: 'user', type: :stage,
359
+ columns: [
360
+ Masamune::Schema::Column.new(id: 'user_id', type: :integer),
361
+ Masamune::Schema::Column.new(id: 'name', type: :string),
362
+ Masamune::Schema::Column.new(id: 'name', type: :string, reference: Masamune::Schema::TableReference.new(mini_table, label: 'hr'))
363
+ ]
364
+ end
365
+
366
+ subject(:stage_table) { table.stage_table(target: target) }
367
+
368
+ it 'should stage table' do
369
+ expect(stage_table.name).to eq('user_table_stage')
370
+ expect(stage_table.columns.keys).to eq([:user_id, :name, :hr_user_account_state_table_name])
371
+ expect(stage_table.references.keys).to eq([:hr_user_account_state])
372
+ end
373
+ end
374
+
375
+ context 'with specified target table (referenced tables)' do
376
+ let(:target) do
377
+ described_class.new id: 'user_data', references: [
378
+ Masamune::Schema::TableReference.new(mini_table, label: 'hr')
379
+ ],
380
+ columns: [
381
+ Masamune::Schema::Column.new(id: 'user_id', type: :integer),
382
+ Masamune::Schema::Column.new(id: 'name', type: :string)
383
+ ]
384
+ end
385
+
386
+ subject(:stage_table) { table.stage_table(target: target) }
387
+
388
+ it 'should stage table' do
389
+ expect(stage_table.name).to eq('user_table_stage')
390
+ expect(stage_table.columns.keys).to eq([:hr_user_account_state_table_id, :user_id, :name])
391
+ expect(stage_table.references.keys).to eq([:hr_user_account_state])
392
+ end
393
+ end
394
+ end
395
+ end