From 114960e356114310820e0db86a744edbf84cf7c4 Mon Sep 17 00:00:00 2001 From: Michael Andrews Date: Thu, 19 Mar 2015 17:09:17 -0700 Subject: [PATCH 1/4] Use integer as default surrogate_key --- lib/masamune/schema/dimension.rb | 11 +- lib/masamune/schema/table.rb | 2 +- .../transform/deduplicate_dimension.psql.erb | 10 +- .../transform/relabel_dimension.psql.erb | 12 +- .../transform/snapshot_dimension.psql.erb | 14 +-- lib/masamune/transform/stage_dimension.rb | 4 +- spec/masamune/schema/table_spec.rb | 8 +- .../transform/bulk_upsert.dimension_spec.rb | 17 ++- .../transform/deduplicate_dimension_spec.rb | 10 +- .../transform/define_table.dimension_spec.rb | 16 +-- .../transform/define_table.fact_spec.rb | 24 ++-- .../transform/define_table.table_spec.rb | 69 ++++++----- .../transform/relabel_dimension_spec.rb | 12 +- spec/masamune/transform/rollup_fact_spec.rb | 114 +++++++++--------- .../transform/snapshot_dimension_spec.rb | 14 +-- .../transform/stage_dimension_spec.rb | 5 +- spec/masamune/transform/stage_fact_spec.rb | 42 +++---- 17 files changed, 190 insertions(+), 194 deletions(-) diff --git a/lib/masamune/schema/dimension.rb b/lib/masamune/schema/dimension.rb index 277db68..a2d8a1a 100644 --- a/lib/masamune/schema/dimension.rb +++ b/lib/masamune/schema/dimension.rb @@ -62,7 +62,7 @@ def reserved_column_ids when :two [:start_at, :end_at, :version, :last_modified_at] when :four - [:parent_uuid, :record_uuid, :start_at, :end_at, :version, :last_modified_at] + [:parent_id, :record_id, :start_at, :end_at, :version, :last_modified_at] when :ledger [:source_kind, :source_uuid, :start_at, :last_modified_at, :delta] else @@ -92,10 +92,8 @@ def ledger_table_columns def initialize_surrogate_key_column! case type - when :mini + when :mini, :one, :two, :four, :ledger, :date initialize_column! id: 'id', type: :integer, surrogate_key: true - when :one, :two, :four, :ledger, :date - initialize_column! id: 'uuid', type: :uuid, surrogate_key: true end end @@ -110,8 +108,9 @@ def initialize_dimension_columns! initialize_column! id: 'last_modified_at', type: :timestamp, default: 'NOW()' when :four children << ledger_table - initialize_column! id: 'parent_uuid', type: :uuid, null: true, reference: ledger_table - initialize_column! id: 'record_uuid', type: :uuid, null: true, reference: ledger_table + # FIXME derive type from from parent + initialize_column! id: 'parent_id', type: :integer, null: true, reference: ledger_table + initialize_column! id: 'record_id', type: :integer, null: true, reference: ledger_table initialize_column! id: 'start_at', type: :timestamp, default: 'TO_TIMESTAMP(0)', index: true, unique: 'natural' initialize_column! id: 'end_at', type: :timestamp, null: true, index: true initialize_column! id: 'version', type: :integer, default: 1, null: true, index: true diff --git a/lib/masamune/schema/table.rb b/lib/masamune/schema/table.rb index 6aa8403..fe941de 100644 --- a/lib/masamune/schema/table.rb +++ b/lib/masamune/schema/table.rb @@ -244,7 +244,7 @@ def stage_table_references(parent, selected = []) def initialize_surrogate_key_column! case type when :table - initialize_column! id: 'uuid', type: :uuid, surrogate_key: true + initialize_column! id: 'id', type: :integer, surrogate_key: true end end diff --git a/lib/masamune/transform/deduplicate_dimension.psql.erb b/lib/masamune/transform/deduplicate_dimension.psql.erb index d63cac3..f2ee276 100644 --- a/lib/masamune/transform/deduplicate_dimension.psql.erb +++ b/lib/masamune/transform/deduplicate_dimension.psql.erb @@ -21,21 +21,21 @@ -- THE SOFTWARE. INSERT INTO - <%= target.name %> (<%= target.insert_columns.join(', ') %>, parent_uuid, record_uuid, start_at) + <%= target.name %> (<%= target.insert_columns.join(', ') %>, parent_id, record_id, start_at) SELECT DISTINCT <%- target.insert_view_values.each do |value| -%> <%= value %><%= ',' %> <%- end -%> - parent_uuid, - record_uuid, + parent_id, + record_id, start_at FROM ( SELECT <%- target.insert_view_values.each do |value| -%> <%= value %><%= ',' %> <%- end -%> - parent_uuid, - record_uuid, + parent_id, + record_id, start_at, CASE WHEN <%= target.insert_view_values.map { |value| "(LAG(#{value}) OVER w = #{value})" }.join(' AND ') %> THEN diff --git a/lib/masamune/transform/relabel_dimension.psql.erb b/lib/masamune/transform/relabel_dimension.psql.erb index 6a3e231..0ec92a2 100644 --- a/lib/masamune/transform/relabel_dimension.psql.erb +++ b/lib/masamune/transform/relabel_dimension.psql.erb @@ -33,7 +33,7 @@ SET FROM ( SELECT - uuid, + id, <%- target.window.each do |name| -%> <%= name %>, <%- end -%> @@ -42,10 +42,10 @@ FROM FROM <%= target.name %> GROUP BY - <%= target.window(:uuid, :start_at).join(', ') %> + <%= target.window(:id, :start_at).join(', ') %> ) AS tmp WHERE - <%= target.name %>.uuid = tmp.uuid + <%= target.name %>.id = tmp.id ; <%-# Relabel end_at column -%> @@ -58,7 +58,7 @@ SET FROM ( SELECT - uuid, + id, start_at, <%- target.window.each do |name| -%> <%= name %>, @@ -67,10 +67,10 @@ FROM FROM <%= target.name %> GROUP BY - <%= target.window(:uuid, :start_at).join(', ') %> + <%= target.window(:id, :start_at).join(', ') %> ) AS tmp WHERE - <%= target.name %>.uuid = tmp.uuid + <%= target.name %>.id = tmp.id ; COMMIT; diff --git a/lib/masamune/transform/snapshot_dimension.psql.erb b/lib/masamune/transform/snapshot_dimension.psql.erb index e9941e3..cd50276 100644 --- a/lib/masamune/transform/snapshot_dimension.psql.erb +++ b/lib/masamune/transform/snapshot_dimension.psql.erb @@ -39,14 +39,14 @@ WITH ranges AS ( <%- target.insert_view_values.each do |value| -%> consolidated.<%= value %><%= ',' %> <%- end -%> - consolidated.parent_uuid, - consolidated.record_uuid, + consolidated.parent_id, + consolidated.record_id, consolidated.start_at FROM ( SELECT DISTINCT ON (<%= target.window('start_at').join(', ') %>) - FIRST_VALUE(uuid) OVER w AS parent_uuid, + FIRST_VALUE(id) OVER w AS parent_id, FIRST_VALUE(start_at) OVER w AS parent_start_at, - uuid AS record_uuid, + id AS record_id, <%- target.insert_values(window: 'w').each do |value, last| -%> <%= value %><%= ',' %> <%- end -%> @@ -62,13 +62,13 @@ WITH ranges AS ( <%- end -%> ) INSERT INTO - <%= target.name %> (<%= target.insert_columns.join(', ') %>, parent_uuid, record_uuid, start_at) + <%= target.name %> (<%= target.insert_columns.join(', ') %>, parent_id, record_id, start_at) SELECT <%- target.insert_view_values.each do |value| -%> <%= value %><%= ',' %> <%- end -%> - parent_uuid, - record_uuid, + parent_id, + record_id, start_at FROM snapshot diff --git a/lib/masamune/transform/stage_dimension.rb b/lib/masamune/transform/stage_dimension.rb index 414f4a8..feb68d6 100644 --- a/lib/masamune/transform/stage_dimension.rb +++ b/lib/masamune/transform/stage_dimension.rb @@ -41,7 +41,7 @@ def insert_columns(source) else column.name end - end + end.compact end def insert_values(source) @@ -54,7 +54,7 @@ def insert_values(source) else column.qualified_name end - end + end.compact end method_with_last_element :insert_values diff --git a/spec/masamune/schema/table_spec.rb b/spec/masamune/schema/table_spec.rb index 543fab6..db5b0fa 100644 --- a/spec/masamune/schema/table_spec.rb +++ b/spec/masamune/schema/table_spec.rb @@ -292,17 +292,17 @@ end context 'with specified columns' do - subject(:stage_table) { table.stage_table(columns: %w(uuid name user_account_state.uuid hr_user_account_state.uuid)) } + subject(:stage_table) { table.stage_table(columns: %w(id name user_account_state.id hr_user_account_state.id)) } it 'should stage table' do expect(stage_table.name).to eq('user_table_stage') - expect(stage_table.columns.keys).to eq([:name, :user_account_state_table_uuid, :hr_user_account_state_table_uuid]) + expect(stage_table.columns.keys).to eq([:name, :user_account_state_table_id, :hr_user_account_state_table_id]) expect(stage_table.references.keys).to eq([:user_account_state, :hr_user_account_state]) end end context 'with specified columns (denormalized)' do - subject(:stage_table) { table.stage_table(columns: %w(uuid name user_account_state.name hr_user_account_state.name)) } + subject(:stage_table) { table.stage_table(columns: %w(id name user_account_state.name hr_user_account_state.name)) } it 'should stage table' do expect(stage_table.name).to eq('user_table_stage') @@ -364,7 +364,7 @@ it 'should stage table' do expect(stage_table.name).to eq('user_table_stage') - expect(stage_table.columns.keys).to eq([:hr_user_account_state_table_uuid, :user_id, :name]) + expect(stage_table.columns.keys).to eq([:hr_user_account_state_table_id, :user_id, :name]) expect(stage_table.references.keys).to eq([:hr_user_account_state]) end end diff --git a/spec/masamune/transform/bulk_upsert.dimension_spec.rb b/spec/masamune/transform/bulk_upsert.dimension_spec.rb index 630c482..c25f563 100644 --- a/spec/masamune/transform/bulk_upsert.dimension_spec.rb +++ b/spec/masamune/transform/bulk_upsert.dimension_spec.rb @@ -41,7 +41,6 @@ dimension 'department', type: :mini do references :cluster - column 'uuid', type: :uuid, surrogate_key: true column 'tenant_id', type: :integer, unique: true, natural_key: true column 'department_id', type: :integer, unique: true, natural_key: true row tenant_id: -1, department_id: -1, attributes: {default: true} @@ -85,7 +84,7 @@ UPDATE user_dimension SET - department_type_uuid = user_dimension_stage.department_type_uuid, + department_type_id = user_dimension_stage.department_type_id, user_account_state_type_id = user_dimension_stage.user_account_state_type_id, hr_user_account_state_type_id = user_dimension_stage.hr_user_account_state_type_id, name = user_dimension_stage.name, @@ -99,17 +98,17 @@ ; INSERT INTO - user_dimension (department_type_uuid, user_account_state_type_id, hr_user_account_state_type_id, tenant_id, user_id, name, preferences, parent_uuid, record_uuid, start_at, end_at, version, last_modified_at) + user_dimension (department_type_id, user_account_state_type_id, hr_user_account_state_type_id, tenant_id, user_id, name, preferences, parent_id, record_id, start_at, end_at, version, last_modified_at) SELECT - user_dimension_stage.department_type_uuid, + user_dimension_stage.department_type_id, user_dimension_stage.user_account_state_type_id, user_dimension_stage.hr_user_account_state_type_id, user_dimension_stage.tenant_id, user_dimension_stage.user_id, user_dimension_stage.name, user_dimension_stage.preferences, - user_dimension_stage.parent_uuid, - user_dimension_stage.record_uuid, + user_dimension_stage.parent_id, + user_dimension_stage.record_id, user_dimension_stage.start_at, user_dimension_stage.end_at, user_dimension_stage.version, @@ -144,7 +143,7 @@ UPDATE user_dimension_ledger SET - department_type_uuid = user_dimension_ledger_stage.department_type_uuid, + department_type_id = user_dimension_ledger_stage.department_type_id, user_account_state_type_id = user_dimension_ledger_stage.user_account_state_type_id, hr_user_account_state_type_id = user_dimension_ledger_stage.hr_user_account_state_type_id, name = user_dimension_ledger_stage.name, @@ -161,9 +160,9 @@ ; INSERT INTO - user_dimension_ledger (department_type_uuid, user_account_state_type_id, hr_user_account_state_type_id, tenant_id, user_id, name, preferences_now, preferences_was, source_kind, source_uuid, start_at, last_modified_at, delta) + user_dimension_ledger (department_type_id, user_account_state_type_id, hr_user_account_state_type_id, tenant_id, user_id, name, preferences_now, preferences_was, source_kind, source_uuid, start_at, last_modified_at, delta) SELECT - user_dimension_ledger_stage.department_type_uuid, + user_dimension_ledger_stage.department_type_id, user_dimension_ledger_stage.user_account_state_type_id, user_dimension_ledger_stage.hr_user_account_state_type_id, user_dimension_ledger_stage.tenant_id, diff --git a/spec/masamune/transform/deduplicate_dimension_spec.rb b/spec/masamune/transform/deduplicate_dimension_spec.rb index 95827f3..6affc52 100644 --- a/spec/masamune/transform/deduplicate_dimension_spec.rb +++ b/spec/masamune/transform/deduplicate_dimension_spec.rb @@ -47,14 +47,14 @@ it 'should render deduplicate_dimension template' do is_expected.to eq <<-EOS.strip_heredoc INSERT INTO - user_deduplicated_dimension_stage (user_account_state_type_id, tenant_id, user_id, preferences, parent_uuid, record_uuid, start_at) + user_deduplicated_dimension_stage (user_account_state_type_id, tenant_id, user_id, preferences, parent_id, record_id, start_at) SELECT DISTINCT user_account_state_type_id, tenant_id, user_id, preferences, - parent_uuid, - record_uuid, + parent_id, + record_id, start_at FROM ( SELECT @@ -62,8 +62,8 @@ tenant_id, user_id, preferences, - parent_uuid, - record_uuid, + parent_id, + record_id, start_at, CASE WHEN (LAG(user_account_state_type_id) OVER w = user_account_state_type_id) AND (LAG(tenant_id) OVER w = tenant_id) AND (LAG(user_id) OVER w = user_id) AND (LAG(preferences) OVER w = preferences) THEN diff --git a/spec/masamune/transform/define_table.dimension_spec.rb b/spec/masamune/transform/define_table.dimension_spec.rb index 1cd5586..181089a 100644 --- a/spec/masamune/transform/define_table.dimension_spec.rb +++ b/spec/masamune/transform/define_table.dimension_spec.rb @@ -57,7 +57,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_dimension ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL, last_modified_at TIMESTAMP NOT NULL DEFAULT NOW() @@ -82,7 +82,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_dimension ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL, start_at TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP(0), @@ -148,7 +148,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_dimension_ledger ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, user_account_state_type_id INTEGER REFERENCES user_account_state_type(id) DEFAULT default_user_account_state_type_id(), tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL, @@ -188,13 +188,13 @@ CREATE TABLE IF NOT EXISTS user_dimension ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, user_account_state_type_id INTEGER NOT NULL REFERENCES user_account_state_type(id) DEFAULT default_user_account_state_type_id(), tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL, preferences HSTORE, - parent_uuid UUID REFERENCES user_dimension_ledger(uuid), - record_uuid UUID REFERENCES user_dimension_ledger(uuid), + parent_id INTEGER REFERENCES user_dimension_ledger(id), + record_id INTEGER REFERENCES user_dimension_ledger(id), start_at TIMESTAMP NOT NULL DEFAULT TO_TIMESTAMP(0), end_at TIMESTAMP, version INTEGER DEFAULT 1, @@ -267,8 +267,8 @@ tenant_id INTEGER, user_id INTEGER, preferences HSTORE, - parent_uuid UUID, - record_uuid UUID, + parent_id INTEGER, + record_id INTEGER, start_at TIMESTAMP DEFAULT TO_TIMESTAMP(0), end_at TIMESTAMP, version INTEGER DEFAULT 1, diff --git a/spec/masamune/transform/define_table.fact_spec.rb b/spec/masamune/transform/define_table.fact_spec.rb index d3b4efa..1197faf 100644 --- a/spec/masamune/transform/define_table.fact_spec.rb +++ b/spec/masamune/transform/define_table.fact_spec.rb @@ -115,10 +115,10 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS visits_fact ( - date_dimension_uuid UUID NOT NULL REFERENCES date_dimension(uuid), - tenant_dimension_uuid UUID NOT NULL REFERENCES tenant_dimension(uuid), - user_dimension_uuid UUID NOT NULL REFERENCES user_dimension(uuid), - group_dimension_uuid UUID[] NOT NULL, + date_dimension_id INTEGER NOT NULL REFERENCES date_dimension(id), + tenant_dimension_id INTEGER NOT NULL REFERENCES tenant_dimension(id), + user_dimension_id INTEGER NOT NULL REFERENCES user_dimension(id), + group_dimension_id INTEGER[] NOT NULL, user_agent_type_id INTEGER NOT NULL REFERENCES user_agent_type(id), feature_type_id INTEGER NOT NULL REFERENCES feature_type(id), total INTEGER NOT NULL, @@ -127,23 +127,23 @@ ); DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_date_dimension_uuid_index') THEN - CREATE INDEX visits_fact_date_dimension_uuid_index ON visits_fact (date_dimension_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_date_dimension_id_index') THEN + CREATE INDEX visits_fact_date_dimension_id_index ON visits_fact (date_dimension_id); END IF; END $$; DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_tenant_dimension_uuid_index') THEN - CREATE INDEX visits_fact_tenant_dimension_uuid_index ON visits_fact (tenant_dimension_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_tenant_dimension_id_index') THEN + CREATE INDEX visits_fact_tenant_dimension_id_index ON visits_fact (tenant_dimension_id); END IF; END $$; DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_user_dimension_uuid_index') THEN - CREATE INDEX visits_fact_user_dimension_uuid_index ON visits_fact (user_dimension_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_user_dimension_id_index') THEN + CREATE INDEX visits_fact_user_dimension_id_index ON visits_fact (user_dimension_id); END IF; END $$; DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_group_dimension_uuid_index') THEN - CREATE INDEX visits_fact_group_dimension_uuid_index ON visits_fact (group_dimension_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_group_dimension_id_index') THEN + CREATE INDEX visits_fact_group_dimension_id_index ON visits_fact (group_dimension_id); END IF; END $$; DO $$ BEGIN diff --git a/spec/masamune/transform/define_table.table_spec.rb b/spec/masamune/transform/define_table.table_spec.rb index 0d9c6b5..4cbd491 100644 --- a/spec/masamune/transform/define_table.table_spec.rb +++ b/spec/masamune/transform/define_table.table_spec.rb @@ -41,7 +41,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL ); @@ -65,7 +65,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL ); @@ -99,7 +99,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL ); @@ -138,7 +138,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL ); @@ -178,7 +178,7 @@ CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL, state USER_STATE_TYPE NOT NULL DEFAULT 'active' @@ -228,7 +228,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, name VARCHAR NOT NULL, description VARCHAR NOT NULL ); @@ -260,7 +260,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL ); @@ -282,7 +282,6 @@ before do catalog.schema :postgres do table 'user' do - column 'uuid', type: :uuid, surrogate_key: true column 'tenant_id', type: :integer, natural_key: true column 'user_id', type: :integer, natural_key: true row tenant_id: 'default_tenant_id()', user_id: -1, attributes: {default: true} @@ -297,7 +296,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), + id SERIAL PRIMARY KEY, tenant_id INTEGER NOT NULL, user_id INTEGER NOT NULL ); @@ -320,9 +319,9 @@ SELECT -1; $$ LANGUAGE SQL; - CREATE OR REPLACE FUNCTION default_user_table_uuid() - RETURNS UUID IMMUTABLE AS $$ - SELECT uuid FROM user_table WHERE tenant_id = default_tenant_id() AND user_id = -1; + CREATE OR REPLACE FUNCTION default_user_table_id() + RETURNS INTEGER IMMUTABLE AS $$ + SELECT id FROM user_table WHERE tenant_id = default_tenant_id() AND user_id = -1; $$ LANGUAGE SQL; CREATE OR REPLACE FUNCTION unknown_user_id() @@ -330,9 +329,9 @@ SELECT -2; $$ LANGUAGE SQL; - CREATE OR REPLACE FUNCTION unknown_user_table_uuid() - RETURNS UUID IMMUTABLE AS $$ - SELECT uuid FROM user_table WHERE tenant_id = default_tenant_id() AND user_id = -2; + CREATE OR REPLACE FUNCTION unknown_user_table_id() + RETURNS INTEGER IMMUTABLE AS $$ + SELECT id FROM user_table WHERE tenant_id = default_tenant_id() AND user_id = -2; $$ LANGUAGE SQL; EOS end @@ -362,14 +361,14 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - user_account_state_table_uuid UUID NOT NULL REFERENCES user_account_state_table(uuid) DEFAULT default_user_account_state_table_uuid(), + id SERIAL PRIMARY KEY, + user_account_state_table_id INTEGER NOT NULL REFERENCES user_account_state_table(id) DEFAULT default_user_account_state_table_id(), name VARCHAR NOT NULL ); DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_user_account_state_table_uuid_index') THEN - CREATE INDEX user_table_user_account_state_table_uuid_index ON user_table (user_account_state_table_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_user_account_state_table_id_index') THEN + CREATE INDEX user_table_user_account_state_table_id_index ON user_table (user_account_state_table_id); END IF; END $$; EOS end @@ -398,20 +397,20 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS user_table ( - uuid UUID PRIMARY KEY DEFAULT uuid_generate_v4(), - user_account_state_table_uuid UUID NOT NULL REFERENCES user_account_state_table(uuid) DEFAULT default_user_account_state_table_uuid(), - hr_user_account_state_table_uuid UUID REFERENCES user_account_state_table(uuid), + id SERIAL PRIMARY KEY, + user_account_state_table_id INTEGER NOT NULL REFERENCES user_account_state_table(id) DEFAULT default_user_account_state_table_id(), + hr_user_account_state_table_id INTEGER REFERENCES user_account_state_table(id), name VARCHAR NOT NULL ); DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_user_account_state_table_uuid_index') THEN - CREATE INDEX user_table_user_account_state_table_uuid_index ON user_table (user_account_state_table_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_user_account_state_table_id_index') THEN + CREATE INDEX user_table_user_account_state_table_id_index ON user_table (user_account_state_table_id); END IF; END $$; DO $$ BEGIN - IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_hr_user_account_state_table_uuid_index') THEN - CREATE INDEX user_table_hr_user_account_state_table_uuid_index ON user_table (hr_user_account_state_table_uuid); + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_hr_user_account_state_table_id_index') THEN + CREATE INDEX user_table_hr_user_account_state_table_id_index ON user_table (hr_user_account_state_table_id); END IF; END $$; EOS end @@ -443,31 +442,31 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TEMPORARY TABLE IF NOT EXISTS user_table_stage ( - user_account_state_table_uuid UUID, - hr_user_account_state_table_uuid UUID, + user_account_state_table_id INTEGER, + hr_user_account_state_table_id INTEGER, name VARCHAR ); - CREATE INDEX user_table_stage_user_account_state_table_uuid_index ON user_table_stage (user_account_state_table_uuid); - CREATE INDEX user_table_stage_hr_user_account_state_table_uuid_index ON user_table_stage (hr_user_account_state_table_uuid); + CREATE INDEX user_table_stage_user_account_state_table_id_index ON user_table_stage (user_account_state_table_id); + CREATE INDEX user_table_stage_hr_user_account_state_table_id_index ON user_table_stage (hr_user_account_state_table_id); EOS end end context 'for postgres table with all specified columns' do - let(:columns) { %w(hr_user_account_state_table_uuid user_account_state_table_uuid name) } + let(:columns) { %w(hr_user_account_state_table_id user_account_state_table_id name) } it 'should render table template' do is_expected.to eq <<-EOS.strip_heredoc CREATE TEMPORARY TABLE IF NOT EXISTS user_table_stage ( - hr_user_account_state_table_uuid UUID, - user_account_state_table_uuid UUID, + hr_user_account_state_table_id INTEGER, + user_account_state_table_id INTEGER, name VARCHAR ); - CREATE INDEX user_table_stage_hr_user_account_state_table_uuid_index ON user_table_stage (hr_user_account_state_table_uuid); - CREATE INDEX user_table_stage_user_account_state_table_uuid_index ON user_table_stage (user_account_state_table_uuid); + CREATE INDEX user_table_stage_hr_user_account_state_table_id_index ON user_table_stage (hr_user_account_state_table_id); + CREATE INDEX user_table_stage_user_account_state_table_id_index ON user_table_stage (user_account_state_table_id); EOS end end diff --git a/spec/masamune/transform/relabel_dimension_spec.rb b/spec/masamune/transform/relabel_dimension_spec.rb index 4aea7e7..fb0bddf 100644 --- a/spec/masamune/transform/relabel_dimension_spec.rb +++ b/spec/masamune/transform/relabel_dimension_spec.rb @@ -58,7 +58,7 @@ FROM ( SELECT - uuid, + id, tenant_id, user_id, start_at, @@ -66,10 +66,10 @@ FROM user_dimension GROUP BY - uuid, tenant_id, user_id, start_at + id, tenant_id, user_id, start_at ) AS tmp WHERE - user_dimension.uuid = tmp.uuid + user_dimension.id = tmp.id ; UPDATE user_dimension SET end_at = NULL; @@ -81,7 +81,7 @@ FROM ( SELECT - uuid, + id, start_at, tenant_id, user_id, @@ -89,10 +89,10 @@ FROM user_dimension GROUP BY - uuid, tenant_id, user_id, start_at + id, tenant_id, user_id, start_at ) AS tmp WHERE - user_dimension.uuid = tmp.uuid + user_dimension.id = tmp.id ; COMMIT; diff --git a/spec/masamune/transform/rollup_fact_spec.rb b/spec/masamune/transform/rollup_fact_spec.rb index 6d01218..7dc41c5 100644 --- a/spec/masamune/transform/rollup_fact_spec.rb +++ b/spec/masamune/transform/rollup_fact_spec.rb @@ -87,18 +87,18 @@ CREATE TABLE IF NOT EXISTS visits_hourly_fact_y2014m08_stage (LIKE visits_hourly_fact INCLUDING ALL); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id); INSERT INTO - visits_hourly_fact_y2014m08_stage (date_dimension_uuid, tenant_dimension_uuid, user_dimension_uuid, user_agent_type_id, feature_type_id, total, time_key) + visits_hourly_fact_y2014m08_stage (date_dimension_id, tenant_dimension_id, user_dimension_id, user_agent_type_id, feature_type_id, total, time_key) SELECT - (SELECT uuid FROM date_dimension d WHERE d.date_epoch = date_dimension.date_epoch ORDER BY d.date_id LIMIT 1), - visits_transaction_fact_y2014m08.tenant_dimension_uuid, - visits_transaction_fact_y2014m08.user_dimension_uuid, + (SELECT id FROM date_dimension d WHERE d.date_epoch = date_dimension.date_epoch ORDER BY d.date_id LIMIT 1), + visits_transaction_fact_y2014m08.tenant_dimension_id, + visits_transaction_fact_y2014m08.user_dimension_id, visits_transaction_fact_y2014m08.user_agent_type_id, visits_transaction_fact_y2014m08.feature_type_id, SUM(visits_transaction_fact_y2014m08.total), @@ -108,19 +108,19 @@ JOIN date_dimension ON - date_dimension.uuid = visits_transaction_fact_y2014m08.date_dimension_uuid + date_dimension.id = visits_transaction_fact_y2014m08.date_dimension_id GROUP BY date_dimension.date_epoch, - visits_transaction_fact_y2014m08.tenant_dimension_uuid, - visits_transaction_fact_y2014m08.user_dimension_uuid, + visits_transaction_fact_y2014m08.tenant_dimension_id, + visits_transaction_fact_y2014m08.user_dimension_id, visits_transaction_fact_y2014m08.user_agent_type_id, visits_transaction_fact_y2014m08.feature_type_id, (visits_transaction_fact_y2014m08.time_key - (visits_transaction_fact_y2014m08.time_key % 3600)) ; - CREATE INDEX visits_hourly_fact_y2014m08_stage_date_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (date_dimension_uuid); - CREATE INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (tenant_dimension_uuid); - CREATE INDEX visits_hourly_fact_y2014m08_stage_user_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (user_dimension_uuid); + CREATE INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index ON visits_hourly_fact_y2014m08_stage (date_dimension_id); + CREATE INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index ON visits_hourly_fact_y2014m08_stage (tenant_dimension_id); + CREATE INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index ON visits_hourly_fact_y2014m08_stage (user_dimension_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_user_agent_type_id_index ON visits_hourly_fact_y2014m08_stage (user_agent_type_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_feature_type_id_index ON visits_hourly_fact_y2014m08_stage (feature_type_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_time_key_index ON visits_hourly_fact_y2014m08_stage (time_key); @@ -134,15 +134,15 @@ ALTER TABLE visits_hourly_fact_y2014m08 INHERIT visits_hourly_fact; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; - ALTER INDEX visits_hourly_fact_y2014m08_stage_date_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_date_dimension_uuid_index; - ALTER INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_tenant_dimension_uuid_index; - ALTER INDEX visits_hourly_fact_y2014m08_stage_user_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_user_dimension_uuid_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_date_dimension_id_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_tenant_dimension_id_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_user_dimension_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_user_agent_type_id_index RENAME TO visits_hourly_fact_y2014m08_user_agent_type_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_feature_type_id_index RENAME TO visits_hourly_fact_y2014m08_feature_type_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_time_key_index RENAME TO visits_hourly_fact_y2014m08_time_key_index; @@ -167,18 +167,18 @@ CREATE TABLE IF NOT EXISTS visits_daily_fact_y2014m08_stage (LIKE visits_daily_fact INCLUDING ALL); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); - ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid); - ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid); - ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid); + ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); + ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); + ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id); INSERT INTO - visits_daily_fact_y2014m08_stage (date_dimension_uuid, tenant_dimension_uuid, user_dimension_uuid, user_agent_type_id, feature_type_id, total, time_key) + visits_daily_fact_y2014m08_stage (date_dimension_id, tenant_dimension_id, user_dimension_id, user_agent_type_id, feature_type_id, total, time_key) SELECT - (SELECT uuid FROM date_dimension d WHERE d.date_epoch = date_dimension.date_epoch ORDER BY d.date_id LIMIT 1), - visits_hourly_fact_y2014m08.tenant_dimension_uuid, - visits_hourly_fact_y2014m08.user_dimension_uuid, + (SELECT id FROM date_dimension d WHERE d.date_epoch = date_dimension.date_epoch ORDER BY d.date_id LIMIT 1), + visits_hourly_fact_y2014m08.tenant_dimension_id, + visits_hourly_fact_y2014m08.user_dimension_id, visits_hourly_fact_y2014m08.user_agent_type_id, visits_hourly_fact_y2014m08.feature_type_id, SUM(visits_hourly_fact_y2014m08.total), @@ -188,18 +188,18 @@ JOIN date_dimension ON - date_dimension.uuid = visits_hourly_fact_y2014m08.date_dimension_uuid + date_dimension.id = visits_hourly_fact_y2014m08.date_dimension_id GROUP BY date_dimension.date_epoch, - visits_hourly_fact_y2014m08.tenant_dimension_uuid, - visits_hourly_fact_y2014m08.user_dimension_uuid, + visits_hourly_fact_y2014m08.tenant_dimension_id, + visits_hourly_fact_y2014m08.user_dimension_id, visits_hourly_fact_y2014m08.user_agent_type_id, visits_hourly_fact_y2014m08.feature_type_id ; - CREATE INDEX visits_daily_fact_y2014m08_stage_date_dimension_uuid_index ON visits_daily_fact_y2014m08_stage (date_dimension_uuid); - CREATE INDEX visits_daily_fact_y2014m08_stage_tenant_dimension_uuid_index ON visits_daily_fact_y2014m08_stage (tenant_dimension_uuid); - CREATE INDEX visits_daily_fact_y2014m08_stage_user_dimension_uuid_index ON visits_daily_fact_y2014m08_stage (user_dimension_uuid); + CREATE INDEX visits_daily_fact_y2014m08_stage_date_dimension_id_index ON visits_daily_fact_y2014m08_stage (date_dimension_id); + CREATE INDEX visits_daily_fact_y2014m08_stage_tenant_dimension_id_index ON visits_daily_fact_y2014m08_stage (tenant_dimension_id); + CREATE INDEX visits_daily_fact_y2014m08_stage_user_dimension_id_index ON visits_daily_fact_y2014m08_stage (user_dimension_id); CREATE INDEX visits_daily_fact_y2014m08_stage_user_agent_type_id_index ON visits_daily_fact_y2014m08_stage (user_agent_type_id); CREATE INDEX visits_daily_fact_y2014m08_stage_feature_type_id_index ON visits_daily_fact_y2014m08_stage (feature_type_id); CREATE INDEX visits_daily_fact_y2014m08_stage_time_key_index ON visits_daily_fact_y2014m08_stage (time_key); @@ -213,15 +213,15 @@ ALTER TABLE visits_daily_fact_y2014m08 INHERIT visits_daily_fact; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; - ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid) NOT VALID; - ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid) NOT VALID; - ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid) NOT VALID; + ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; + ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; + ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; - ALTER INDEX visits_daily_fact_y2014m08_stage_date_dimension_uuid_index RENAME TO visits_daily_fact_y2014m08_date_dimension_uuid_index; - ALTER INDEX visits_daily_fact_y2014m08_stage_tenant_dimension_uuid_index RENAME TO visits_daily_fact_y2014m08_tenant_dimension_uuid_index; - ALTER INDEX visits_daily_fact_y2014m08_stage_user_dimension_uuid_index RENAME TO visits_daily_fact_y2014m08_user_dimension_uuid_index; + ALTER INDEX visits_daily_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_daily_fact_y2014m08_date_dimension_id_index; + ALTER INDEX visits_daily_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_daily_fact_y2014m08_tenant_dimension_id_index; + ALTER INDEX visits_daily_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_daily_fact_y2014m08_user_dimension_id_index; ALTER INDEX visits_daily_fact_y2014m08_stage_user_agent_type_id_index RENAME TO visits_daily_fact_y2014m08_user_agent_type_id_index; ALTER INDEX visits_daily_fact_y2014m08_stage_feature_type_id_index RENAME TO visits_daily_fact_y2014m08_feature_type_id_index; ALTER INDEX visits_daily_fact_y2014m08_stage_time_key_index RENAME TO visits_daily_fact_y2014m08_time_key_index; @@ -246,18 +246,18 @@ CREATE TABLE IF NOT EXISTS visits_monthly_fact_y2014m08_stage (LIKE visits_monthly_fact INCLUDING ALL); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); - ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid); - ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid); - ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid); + ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); + ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); + ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id); INSERT INTO - visits_monthly_fact_y2014m08_stage (date_dimension_uuid, tenant_dimension_uuid, user_dimension_uuid, user_agent_type_id, feature_type_id, total, time_key) + visits_monthly_fact_y2014m08_stage (date_dimension_id, tenant_dimension_id, user_dimension_id, user_agent_type_id, feature_type_id, total, time_key) SELECT - (SELECT uuid FROM date_dimension d WHERE d.month_epoch = date_dimension.month_epoch ORDER BY d.date_id LIMIT 1), - visits_daily_fact_y2014m08.tenant_dimension_uuid, - visits_daily_fact_y2014m08.user_dimension_uuid, + (SELECT id FROM date_dimension d WHERE d.month_epoch = date_dimension.month_epoch ORDER BY d.date_id LIMIT 1), + visits_daily_fact_y2014m08.tenant_dimension_id, + visits_daily_fact_y2014m08.user_dimension_id, visits_daily_fact_y2014m08.user_agent_type_id, visits_daily_fact_y2014m08.feature_type_id, SUM(visits_daily_fact_y2014m08.total), @@ -267,18 +267,18 @@ JOIN date_dimension ON - date_dimension.uuid = visits_daily_fact_y2014m08.date_dimension_uuid + date_dimension.id = visits_daily_fact_y2014m08.date_dimension_id GROUP BY date_dimension.month_epoch, - visits_daily_fact_y2014m08.tenant_dimension_uuid, - visits_daily_fact_y2014m08.user_dimension_uuid, + visits_daily_fact_y2014m08.tenant_dimension_id, + visits_daily_fact_y2014m08.user_dimension_id, visits_daily_fact_y2014m08.user_agent_type_id, visits_daily_fact_y2014m08.feature_type_id ; - CREATE INDEX visits_monthly_fact_y2014m08_stage_date_dimension_uuid_index ON visits_monthly_fact_y2014m08_stage (date_dimension_uuid); - CREATE INDEX visits_monthly_fact_y2014m08_stage_tenant_dimension_uuid_index ON visits_monthly_fact_y2014m08_stage (tenant_dimension_uuid); - CREATE INDEX visits_monthly_fact_y2014m08_stage_user_dimension_uuid_index ON visits_monthly_fact_y2014m08_stage (user_dimension_uuid); + CREATE INDEX visits_monthly_fact_y2014m08_stage_date_dimension_id_index ON visits_monthly_fact_y2014m08_stage (date_dimension_id); + CREATE INDEX visits_monthly_fact_y2014m08_stage_tenant_dimension_id_index ON visits_monthly_fact_y2014m08_stage (tenant_dimension_id); + CREATE INDEX visits_monthly_fact_y2014m08_stage_user_dimension_id_index ON visits_monthly_fact_y2014m08_stage (user_dimension_id); CREATE INDEX visits_monthly_fact_y2014m08_stage_user_agent_type_id_index ON visits_monthly_fact_y2014m08_stage (user_agent_type_id); CREATE INDEX visits_monthly_fact_y2014m08_stage_feature_type_id_index ON visits_monthly_fact_y2014m08_stage (feature_type_id); CREATE INDEX visits_monthly_fact_y2014m08_stage_time_key_index ON visits_monthly_fact_y2014m08_stage (time_key); @@ -292,15 +292,15 @@ ALTER TABLE visits_monthly_fact_y2014m08 INHERIT visits_monthly_fact; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; - ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid) NOT VALID; - ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid) NOT VALID; - ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid) NOT VALID; + ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; + ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; + ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; - ALTER INDEX visits_monthly_fact_y2014m08_stage_date_dimension_uuid_index RENAME TO visits_monthly_fact_y2014m08_date_dimension_uuid_index; - ALTER INDEX visits_monthly_fact_y2014m08_stage_tenant_dimension_uuid_index RENAME TO visits_monthly_fact_y2014m08_tenant_dimension_uuid_index; - ALTER INDEX visits_monthly_fact_y2014m08_stage_user_dimension_uuid_index RENAME TO visits_monthly_fact_y2014m08_user_dimension_uuid_index; + ALTER INDEX visits_monthly_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_monthly_fact_y2014m08_date_dimension_id_index; + ALTER INDEX visits_monthly_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_monthly_fact_y2014m08_tenant_dimension_id_index; + ALTER INDEX visits_monthly_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_monthly_fact_y2014m08_user_dimension_id_index; ALTER INDEX visits_monthly_fact_y2014m08_stage_user_agent_type_id_index RENAME TO visits_monthly_fact_y2014m08_user_agent_type_id_index; ALTER INDEX visits_monthly_fact_y2014m08_stage_feature_type_id_index RENAME TO visits_monthly_fact_y2014m08_feature_type_id_index; ALTER INDEX visits_monthly_fact_y2014m08_stage_time_key_index RENAME TO visits_monthly_fact_y2014m08_time_key_index; diff --git a/spec/masamune/transform/snapshot_dimension_spec.rb b/spec/masamune/transform/snapshot_dimension_spec.rb index 092df08..6cac201 100644 --- a/spec/masamune/transform/snapshot_dimension_spec.rb +++ b/spec/masamune/transform/snapshot_dimension_spec.rb @@ -61,14 +61,14 @@ consolidated.tenant_id, consolidated.user_id, consolidated.preferences, - consolidated.parent_uuid, - consolidated.record_uuid, + consolidated.parent_id, + consolidated.record_id, consolidated.start_at FROM ( SELECT DISTINCT ON (tenant_id, user_id, start_at) - FIRST_VALUE(uuid) OVER w AS parent_uuid, + FIRST_VALUE(id) OVER w AS parent_id, FIRST_VALUE(start_at) OVER w AS parent_start_at, - uuid AS record_uuid, + id AS record_id, coalesce_merge(user_account_state_type_id) OVER w AS user_account_state_type_id, tenant_id AS tenant_id, user_id AS user_id, @@ -85,14 +85,14 @@ consolidated.user_id IS NOT NULL ) INSERT INTO - user_dimension_stage (user_account_state_type_id, tenant_id, user_id, preferences, parent_uuid, record_uuid, start_at) + user_dimension_stage (user_account_state_type_id, tenant_id, user_id, preferences, parent_id, record_id, start_at) SELECT user_account_state_type_id, tenant_id, user_id, preferences, - parent_uuid, - record_uuid, + parent_id, + record_id, start_at FROM snapshot diff --git a/spec/masamune/transform/stage_dimension_spec.rb b/spec/masamune/transform/stage_dimension_spec.rb index a91bfb1..967e482 100644 --- a/spec/masamune/transform/stage_dimension_spec.rb +++ b/spec/masamune/transform/stage_dimension_spec.rb @@ -41,7 +41,6 @@ dimension 'department', type: :mini do references :cluster - column 'uuid', type: :uuid, surrogate_key: true column 'tenant_id', type: :integer, unique: true, natural_key: true column 'department_id', type: :integer, unique: true, natural_key: true row tenant_id: -1, department_id: -1, attributes: {default: true} @@ -83,9 +82,9 @@ CREATE TEMPORARY TABLE IF NOT EXISTS user_dimension_ledger_stage (LIKE user_dimension_ledger INCLUDING ALL); INSERT INTO - user_dimension_ledger_stage (department_type_uuid, user_account_state_type_id, hr_user_account_state_type_id, tenant_id, user_id, preferences_now, source_kind, start_at, delta) + user_dimension_ledger_stage (department_type_id, user_account_state_type_id, hr_user_account_state_type_id, tenant_id, user_id, preferences_now, source_kind, start_at, delta) SELECT - department_type.uuid, + department_type.id, user_account_state_type.id, hr_user_account_state_type.id, user_file_dimension_ledger_stage.tenant_id, diff --git a/spec/masamune/transform/stage_fact_spec.rb b/spec/masamune/transform/stage_fact_spec.rb index 39e7913..21b541d 100644 --- a/spec/masamune/transform/stage_fact_spec.rb +++ b/spec/masamune/transform/stage_fact_spec.rb @@ -94,20 +94,20 @@ CREATE TABLE IF NOT EXISTS visits_hourly_fact_y2014m08_stage (LIKE visits_hourly_fact INCLUDING ALL); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid); - ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_group_dimension_uuid_fkey FOREIGN KEY (group_dimension_uuid) REFERENCES group_dimension(uuid); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_group_dimension_id_fkey FOREIGN KEY (group_dimension_id) REFERENCES group_dimension(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id); INSERT INTO - visits_hourly_fact_y2014m08_stage (date_dimension_uuid, tenant_dimension_uuid, user_dimension_uuid, group_dimension_uuid, user_agent_type_id, feature_type_id, session_type_id, total, time_key) + visits_hourly_fact_y2014m08_stage (date_dimension_id, tenant_dimension_id, user_dimension_id, group_dimension_id, user_agent_type_id, feature_type_id, session_type_id, total, time_key) SELECT - date_dimension.uuid, - tenant_dimension.uuid, - user_dimension.uuid, - group_dimension.uuid, + date_dimension.id, + tenant_dimension.id, + user_dimension.id, + group_dimension.id, user_agent_type.id, feature_type.id, visits_hourly_file_fact_stage.session_type_id, @@ -145,10 +145,10 @@ feature_type.name = visits_hourly_file_fact_stage.feature_type_name ; - CREATE INDEX visits_hourly_fact_y2014m08_stage_date_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (date_dimension_uuid); - CREATE INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (tenant_dimension_uuid); - CREATE INDEX visits_hourly_fact_y2014m08_stage_user_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (user_dimension_uuid); - CREATE INDEX visits_hourly_fact_y2014m08_stage_group_dimension_uuid_index ON visits_hourly_fact_y2014m08_stage (group_dimension_uuid); + CREATE INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index ON visits_hourly_fact_y2014m08_stage (date_dimension_id); + CREATE INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index ON visits_hourly_fact_y2014m08_stage (tenant_dimension_id); + CREATE INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index ON visits_hourly_fact_y2014m08_stage (user_dimension_id); + CREATE INDEX visits_hourly_fact_y2014m08_stage_group_dimension_id_index ON visits_hourly_fact_y2014m08_stage (group_dimension_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_user_agent_type_id_index ON visits_hourly_fact_y2014m08_stage (user_agent_type_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_feature_type_id_index ON visits_hourly_fact_y2014m08_stage (feature_type_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_session_type_id_index ON visits_hourly_fact_y2014m08_stage (session_type_id); @@ -163,17 +163,17 @@ ALTER TABLE visits_hourly_fact_y2014m08 INHERIT visits_hourly_fact; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_date_dimension_uuid_fkey FOREIGN KEY (date_dimension_uuid) REFERENCES date_dimension(uuid) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_tenant_dimension_uuid_fkey FOREIGN KEY (tenant_dimension_uuid) REFERENCES tenant_dimension(uuid) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_dimension_uuid_fkey FOREIGN KEY (user_dimension_uuid) REFERENCES user_dimension(uuid) NOT VALID; - ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_group_dimension_uuid_fkey FOREIGN KEY (group_dimension_uuid) REFERENCES group_dimension(uuid) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_group_dimension_id_fkey FOREIGN KEY (group_dimension_id) REFERENCES group_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; - ALTER INDEX visits_hourly_fact_y2014m08_stage_date_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_date_dimension_uuid_index; - ALTER INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_tenant_dimension_uuid_index; - ALTER INDEX visits_hourly_fact_y2014m08_stage_user_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_user_dimension_uuid_index; - ALTER INDEX visits_hourly_fact_y2014m08_stage_group_dimension_uuid_index RENAME TO visits_hourly_fact_y2014m08_group_dimension_uuid_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_date_dimension_id_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_tenant_dimension_id_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_user_dimension_id_index; + ALTER INDEX visits_hourly_fact_y2014m08_stage_group_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_group_dimension_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_user_agent_type_id_index RENAME TO visits_hourly_fact_y2014m08_user_agent_type_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_feature_type_id_index RENAME TO visits_hourly_fact_y2014m08_feature_type_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_session_type_id_index RENAME TO visits_hourly_fact_y2014m08_session_type_id_index; From 4129710881b0191a74eaa926952d6f78bada2184 Mon Sep 17 00:00:00 2001 From: Michael Andrews Date: Fri, 20 Mar 2015 10:21:12 -0700 Subject: [PATCH 2/4] Add string_to_integer function --- lib/masamune/transform/define_schema.psql.erb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/lib/masamune/transform/define_schema.psql.erb b/lib/masamune/transform/define_schema.psql.erb index d88571c..3957d45 100644 --- a/lib/masamune/transform/define_schema.psql.erb +++ b/lib/masamune/transform/define_schema.psql.erb @@ -25,6 +25,10 @@ CREATE EXTENSION IF NOT EXISTS hstore; SET TIMEZONE TO 'UTC'; +CREATE OR REPLACE FUNCTION string_to_integer(string VARCHAR) RETURNS INTEGER AS $$ + SELECT SUM(ascii(a))::INTEGER FROM (SELECT unnest(string_to_array(string, NULL)) as a) s; +$$ LANGUAGE SQL; + CREATE OR REPLACE FUNCTION json_to_hstore(JSON) RETURNS HSTORE AS $$ SELECT HSTORE(RTRIM(LTRIM(REPLACE($1::TEXT, ':', '=>'), '{'), '}')) From 29426ba6bfdf51f2feb5abdbe01a76f558b7aca7 Mon Sep 17 00:00:00 2001 From: Michael Andrews Date: Fri, 20 Mar 2015 12:10:14 -0700 Subject: [PATCH 3/4] Add sequence Column#type --- lib/masamune/schema/column.rb | 31 ++++++++++++----- lib/masamune/schema/table.rb | 5 +++ lib/masamune/transform/define_schema.psql.erb | 4 --- lib/masamune/transform/define_table.psql.erb | 12 +++++++ lib/masamune/transform/rollup_fact.rb | 3 ++ spec/masamune/schema/column_spec.rb | 16 +++++++++ .../transform/define_table.fact_spec.rb | 14 ++++++++ .../transform/define_table.table_spec.rb | 33 +++++++++++++++++++ .../insert_reference_values.dimension_spec.rb | 1 - .../masamune/transform/load_dimension_spec.rb | 1 - spec/masamune/transform/rollup_fact_spec.rb | 21 ++++++++++++ spec/masamune/transform/stage_fact_spec.rb | 12 +++++++ 12 files changed, 139 insertions(+), 14 deletions(-) diff --git a/lib/masamune/schema/column.rb b/lib/masamune/schema/column.rb index 33938d2..16bf9a9 100644 --- a/lib/masamune/schema/column.rb +++ b/lib/masamune/schema/column.rb @@ -38,6 +38,7 @@ class Column index: Set.new, unique: Set.new, ignore: false, + sequence_offset: 1, surrogate_key: false, natural_key: false, measure: false, @@ -58,8 +59,6 @@ def initialize(opts = {}) DEFAULT_ATTRIBUTES.merge(opts).each do |name, value| public_send("#{name}=", value) end - - initialize_default_attributes! end def id=(id) @@ -74,6 +73,16 @@ def name end end + def default + return @default unless @default.nil? + case type + when :uuid + 'uuid_generate_v4()' + when :sequence + "nextval('#{sequence_id}')" + end + end + def index=(value) @index ||= Set.new @index.clear @@ -92,6 +101,11 @@ def index=(value) end end + def unique + self.unique = 'natural' if natural_key + @unique + end + def unique=(value) @unique ||= Set.new @unique.clear @@ -152,6 +166,8 @@ def sql_type(for_surrogate_key = false) 'TIMESTAMP' when :boolean 'BOOLEAN' + when :sequence + 'INTEGER' when :enum "#{sub_type}_TYPE".upcase when :key_value @@ -436,12 +452,16 @@ def adjacent reference.columns[id] end + def sequence_id + "#{reference_name}_seq" if type == :sequence + end + private def sql_constraints [].tap do |constraints| constraints << 'NOT NULL' unless null || surrogate_key || !strict || parent.temporary? || degenerate? - constraints << 'PRIMARY KEY' if surrogate_key + constraints << "PRIMARY KEY" if surrogate_key end end @@ -449,11 +469,6 @@ def sql_default "DEFAULT #{sql_value(default)}" unless default.nil? end - def initialize_default_attributes! - self.default = 'uuid_generate_v4()' if surrogate_key && type == :uuid - self.unique = 'natural' if natural_key - end - def ruby_array(value) case value when Array diff --git a/lib/masamune/schema/table.rb b/lib/masamune/schema/table.rb index fe941de..e95f623 100644 --- a/lib/masamune/schema/table.rb +++ b/lib/masamune/schema/table.rb @@ -140,6 +140,11 @@ def enum_columns columns.select { |_, column| column.type == :enum } end + def sequence_columns + return {} if temporary? + columns.select { |_, column| column.reference.nil? && column.type == :sequence } + end + def reference_columns columns.values.select { | column| column.reference } end diff --git a/lib/masamune/transform/define_schema.psql.erb b/lib/masamune/transform/define_schema.psql.erb index 3957d45..d88571c 100644 --- a/lib/masamune/transform/define_schema.psql.erb +++ b/lib/masamune/transform/define_schema.psql.erb @@ -25,10 +25,6 @@ CREATE EXTENSION IF NOT EXISTS hstore; SET TIMEZONE TO 'UTC'; -CREATE OR REPLACE FUNCTION string_to_integer(string VARCHAR) RETURNS INTEGER AS $$ - SELECT SUM(ascii(a))::INTEGER FROM (SELECT unnest(string_to_array(string, NULL)) as a) s; -$$ LANGUAGE SQL; - CREATE OR REPLACE FUNCTION json_to_hstore(JSON) RETURNS HSTORE AS $$ SELECT HSTORE(RTRIM(LTRIM(REPLACE($1::TEXT, ':', '=>'), '{'), '}')) diff --git a/lib/masamune/transform/define_table.psql.erb b/lib/masamune/transform/define_table.psql.erb index 5b49187..7c47595 100644 --- a/lib/masamune/transform/define_table.psql.erb +++ b/lib/masamune/transform/define_table.psql.erb @@ -33,6 +33,14 @@ CREATE TYPE <%= column.sql_type %> AS ENUM (<%= column.values.map { |value| "'#{ END IF; END $$; <%- end -%> +<%- target.sequence_columns.each do |_, column| -%> +DO $$ BEGIN +IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = '<%= column.sequence_id %>') THEN +CREATE SEQUENCE <%= column.sequence_id %>; +ALTER SEQUENCE <%= column.sequence_id %> RESTART <%= column.sequence_offset %>; +END IF; END $$; +<%- end -%> + <%- if target.temporary? -%> CREATE TEMPORARY TABLE IF NOT EXISTS <%= target.name %> <%- else -%> @@ -44,6 +52,10 @@ CREATE TABLE IF NOT EXISTS <%= target.name %> <%- end -%> ); +<%- target.sequence_columns.each do |_, column| -%> +ALTER SEQUENCE <%= column.sequence_id %> OWNED BY <%= column.qualified_name %>; +<%- end -%> + <%- files.each do |file| -%> <%- copy_options = [] diff --git a/lib/masamune/transform/rollup_fact.rb b/lib/masamune/transform/rollup_fact.rb index 513ea33..dd694c9 100644 --- a/lib/masamune/transform/rollup_fact.rb +++ b/lib/masamune/transform/rollup_fact.rb @@ -40,6 +40,7 @@ def insert_columns(source) shared_columns(source).values.map do |columns| column = columns.first next if column.id == :last_modified_at + next if column.auto_reference values << column.name end measures.each do |_ ,measure| @@ -56,6 +57,7 @@ def insert_values(source) column = columns.first next unless column.reference next if column.reference.type == :date + next if column.auto_reference values << column.qualified_name end source.measures.each do |_ ,measure| @@ -81,6 +83,7 @@ def group_by(source) column = columns.first next unless column.reference next if column.reference.type == :date + next if column.auto_reference group_by << column.qualified_name end group_by << "(#{floor_time_key(source)})" if grain == :hourly diff --git a/spec/masamune/schema/column_spec.rb b/spec/masamune/schema/column_spec.rb index 6102435..803207c 100644 --- a/spec/masamune/schema/column_spec.rb +++ b/spec/masamune/schema/column_spec.rb @@ -44,6 +44,22 @@ it { expect { column }.to raise_error ArgumentError } end + context 'with default: true' do + subject(:column) { described_class.new(id: 'boolean', type: :boolean, default: true) } + context '#default' do + subject { column.default } + it { is_expected.to eq(true) } + end + end + + context 'with default: false' do + subject(:column) { described_class.new(id: 'boolean', type: :boolean, default: false) } + context '#default' do + subject { column.default } + it { is_expected.to eq(false) } + end + end + context 'with index: false' do subject(:column) { described_class.new(id: 'id', index: false) } context '#index' do diff --git a/spec/masamune/transform/define_table.fact_spec.rb b/spec/masamune/transform/define_table.fact_spec.rb index 1197faf..d822a67 100644 --- a/spec/masamune/transform/define_table.fact_spec.rb +++ b/spec/masamune/transform/define_table.fact_spec.rb @@ -25,6 +25,13 @@ describe Masamune::Transform::DefineTable do before do catalog.schema :postgres do + dimension 'cluster', type: :mini do + column 'id', type: :sequence, surrogate_key: true, auto: true + column 'name', type: :string + + row name: 'current_database()', attributes: {default: true} + end + dimension 'date', type: :date do column 'date_id', type: :integer, unique: true, index: true, natural_key: true end @@ -53,6 +60,7 @@ end fact 'visits', partition: 'y%Ym%m' do + references :cluster references :date references :tenant references :user @@ -115,6 +123,7 @@ is_expected.to eq <<-EOS.strip_heredoc CREATE TABLE IF NOT EXISTS visits_fact ( + cluster_type_id INTEGER NOT NULL REFERENCES cluster_type(id) DEFAULT default_cluster_type_id(), date_dimension_id INTEGER NOT NULL REFERENCES date_dimension(id), tenant_dimension_id INTEGER NOT NULL REFERENCES tenant_dimension(id), user_dimension_id INTEGER NOT NULL REFERENCES user_dimension(id), @@ -126,6 +135,11 @@ last_modified_at TIMESTAMP NOT NULL DEFAULT NOW() ); + DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_cluster_type_id_index') THEN + CREATE INDEX visits_fact_cluster_type_id_index ON visits_fact (cluster_type_id); + END IF; END $$; + DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'visits_fact_date_dimension_id_index') THEN CREATE INDEX visits_fact_date_dimension_id_index ON visits_fact (date_dimension_id); diff --git a/spec/masamune/transform/define_table.table_spec.rb b/spec/masamune/transform/define_table.table_spec.rb index 4cbd491..4e5c0b9 100644 --- a/spec/masamune/transform/define_table.table_spec.rb +++ b/spec/masamune/transform/define_table.table_spec.rb @@ -486,4 +486,37 @@ end end end + + context 'for postgres table with sequence column' do + before do + catalog.schema :postgres do + table 'user' do + column 'id', type: :sequence, surrogate_key: true, sequence_offset: 1024 + column 'tenant_id' + column 'user_id' + end + end + end + + let(:target) { catalog.postgres.user_table } + + it 'should render table template' do + is_expected.to eq <<-EOS.strip_heredoc + DO $$ BEGIN + IF NOT EXISTS (SELECT 1 FROM pg_class c WHERE c.relname = 'user_table_id_seq') THEN + CREATE SEQUENCE user_table_id_seq; + ALTER SEQUENCE user_table_id_seq RESTART 1024; + END IF; END $$; + + CREATE TABLE IF NOT EXISTS user_table + ( + id INTEGER PRIMARY KEY DEFAULT nextval('user_table_id_seq'), + tenant_id INTEGER NOT NULL, + user_id INTEGER NOT NULL + ); + + ALTER SEQUENCE user_table_id_seq OWNED BY user_table.id; + EOS + end + end end diff --git a/spec/masamune/transform/insert_reference_values.dimension_spec.rb b/spec/masamune/transform/insert_reference_values.dimension_spec.rb index b4637e5..42049e1 100644 --- a/spec/masamune/transform/insert_reference_values.dimension_spec.rb +++ b/spec/masamune/transform/insert_reference_values.dimension_spec.rb @@ -26,7 +26,6 @@ before do catalog.schema :postgres do dimension 'department', type: :mini do - column 'uuid', type: :uuid, surrogate_key: true column 'tenant_id', type: :integer, unique: true, natural_key: true column 'department_id', type: :integer, unique: true, natural_key: true row tenant_id: -1, department_id: -1, attributes: {default: true} diff --git a/spec/masamune/transform/load_dimension_spec.rb b/spec/masamune/transform/load_dimension_spec.rb index f91cd6b..5f7af80 100644 --- a/spec/masamune/transform/load_dimension_spec.rb +++ b/spec/masamune/transform/load_dimension_spec.rb @@ -31,7 +31,6 @@ end dimension 'department', type: :mini do - column 'uuid', type: :uuid, surrogate_key: true column 'tenant_id', type: :integer, unique: true, natural_key: true column 'department_id', type: :integer, unique: true, natural_key: true row tenant_id: -1, department_id: -1, attributes: {default: true} diff --git a/spec/masamune/transform/rollup_fact_spec.rb b/spec/masamune/transform/rollup_fact_spec.rb index 7dc41c5..8496e9f 100644 --- a/spec/masamune/transform/rollup_fact_spec.rb +++ b/spec/masamune/transform/rollup_fact_spec.rb @@ -25,6 +25,13 @@ describe Masamune::Transform::RollupFact do before do catalog.schema :postgres do + dimension 'cluster', type: :mini do + column 'id', type: :sequence, surrogate_key: true, auto: true + column 'name', type: :string + + row name: 'current_database()', attributes: {default: true} + end + dimension 'date', type: :date do column 'date_id', type: :integer, unique: true, index: true, natural_key: true column 'date_epoch', type: :integer @@ -52,6 +59,7 @@ end fact 'visits', partition: 'y%Ym%m', grain: 'transaction' do + references :cluster references :date references :tenant references :user @@ -62,6 +70,7 @@ end fact 'visits', partition: 'y%Ym%m', grain: %w(hourly daily monthly) do + references :cluster references :date references :tenant references :user @@ -87,6 +96,7 @@ CREATE TABLE IF NOT EXISTS visits_hourly_fact_y2014m08_stage (LIKE visits_hourly_fact INCLUDING ALL); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); @@ -118,6 +128,7 @@ (visits_transaction_fact_y2014m08.time_key - (visits_transaction_fact_y2014m08.time_key % 3600)) ; + CREATE INDEX visits_hourly_fact_y2014m08_stage_cluster_type_id_index ON visits_hourly_fact_y2014m08_stage (cluster_type_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index ON visits_hourly_fact_y2014m08_stage (date_dimension_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index ON visits_hourly_fact_y2014m08_stage (tenant_dimension_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index ON visits_hourly_fact_y2014m08_stage (user_dimension_id); @@ -134,12 +145,14 @@ ALTER TABLE visits_hourly_fact_y2014m08 INHERIT visits_hourly_fact; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; + ALTER INDEX visits_hourly_fact_y2014m08_stage_cluster_type_id_index RENAME TO visits_hourly_fact_y2014m08_cluster_type_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_date_dimension_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_tenant_dimension_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_user_dimension_id_index; @@ -167,6 +180,7 @@ CREATE TABLE IF NOT EXISTS visits_daily_fact_y2014m08_stage (LIKE visits_daily_fact INCLUDING ALL); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); + ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); ALTER TABLE visits_daily_fact_y2014m08_stage ADD CONSTRAINT visits_daily_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); @@ -197,6 +211,7 @@ visits_hourly_fact_y2014m08.feature_type_id ; + CREATE INDEX visits_daily_fact_y2014m08_stage_cluster_type_id_index ON visits_daily_fact_y2014m08_stage (cluster_type_id); CREATE INDEX visits_daily_fact_y2014m08_stage_date_dimension_id_index ON visits_daily_fact_y2014m08_stage (date_dimension_id); CREATE INDEX visits_daily_fact_y2014m08_stage_tenant_dimension_id_index ON visits_daily_fact_y2014m08_stage (tenant_dimension_id); CREATE INDEX visits_daily_fact_y2014m08_stage_user_dimension_id_index ON visits_daily_fact_y2014m08_stage (user_dimension_id); @@ -213,12 +228,14 @@ ALTER TABLE visits_daily_fact_y2014m08 INHERIT visits_daily_fact; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; + ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_daily_fact_y2014m08 ADD CONSTRAINT visits_daily_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; + ALTER INDEX visits_daily_fact_y2014m08_stage_cluster_type_id_index RENAME TO visits_daily_fact_y2014m08_cluster_type_id_index; ALTER INDEX visits_daily_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_daily_fact_y2014m08_date_dimension_id_index; ALTER INDEX visits_daily_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_daily_fact_y2014m08_tenant_dimension_id_index; ALTER INDEX visits_daily_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_daily_fact_y2014m08_user_dimension_id_index; @@ -246,6 +263,7 @@ CREATE TABLE IF NOT EXISTS visits_monthly_fact_y2014m08_stage (LIKE visits_monthly_fact INCLUDING ALL); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); + ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); ALTER TABLE visits_monthly_fact_y2014m08_stage ADD CONSTRAINT visits_monthly_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); @@ -276,6 +294,7 @@ visits_daily_fact_y2014m08.feature_type_id ; + CREATE INDEX visits_monthly_fact_y2014m08_stage_cluster_type_id_index ON visits_monthly_fact_y2014m08_stage (cluster_type_id); CREATE INDEX visits_monthly_fact_y2014m08_stage_date_dimension_id_index ON visits_monthly_fact_y2014m08_stage (date_dimension_id); CREATE INDEX visits_monthly_fact_y2014m08_stage_tenant_dimension_id_index ON visits_monthly_fact_y2014m08_stage (tenant_dimension_id); CREATE INDEX visits_monthly_fact_y2014m08_stage_user_dimension_id_index ON visits_monthly_fact_y2014m08_stage (user_dimension_id); @@ -292,12 +311,14 @@ ALTER TABLE visits_monthly_fact_y2014m08 INHERIT visits_monthly_fact; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; + ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_monthly_fact_y2014m08 ADD CONSTRAINT visits_monthly_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; + ALTER INDEX visits_monthly_fact_y2014m08_stage_cluster_type_id_index RENAME TO visits_monthly_fact_y2014m08_cluster_type_id_index; ALTER INDEX visits_monthly_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_monthly_fact_y2014m08_date_dimension_id_index; ALTER INDEX visits_monthly_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_monthly_fact_y2014m08_tenant_dimension_id_index; ALTER INDEX visits_monthly_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_monthly_fact_y2014m08_user_dimension_id_index; diff --git a/spec/masamune/transform/stage_fact_spec.rb b/spec/masamune/transform/stage_fact_spec.rb index 21b541d..2f9bb3c 100644 --- a/spec/masamune/transform/stage_fact_spec.rb +++ b/spec/masamune/transform/stage_fact_spec.rb @@ -25,6 +25,13 @@ describe Masamune::Transform::StageFact do before do catalog.schema :postgres do + dimension 'cluster', type: :mini do + column 'id', type: :sequence, surrogate_key: true, auto: true + column 'name', type: :string + + row name: 'current_database()', attributes: {default: true} + end + dimension 'date', type: :date do column 'date_id', type: :integer, unique: true, index: true, natural_key: true end @@ -54,6 +61,7 @@ end fact 'visits', partition: 'y%Ym%m', grain: %w(hourly daily monthly) do + references :cluster references :date references :tenant references :user @@ -94,6 +102,7 @@ CREATE TABLE IF NOT EXISTS visits_hourly_fact_y2014m08_stage (LIKE visits_hourly_fact INCLUDING ALL); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600); + ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id); ALTER TABLE visits_hourly_fact_y2014m08_stage ADD CONSTRAINT visits_hourly_fact_y2014m08_stage_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id); @@ -145,6 +154,7 @@ feature_type.name = visits_hourly_file_fact_stage.feature_type_name ; + CREATE INDEX visits_hourly_fact_y2014m08_stage_cluster_type_id_index ON visits_hourly_fact_y2014m08_stage (cluster_type_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index ON visits_hourly_fact_y2014m08_stage (date_dimension_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index ON visits_hourly_fact_y2014m08_stage (tenant_dimension_id); CREATE INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index ON visits_hourly_fact_y2014m08_stage (user_dimension_id); @@ -163,6 +173,7 @@ ALTER TABLE visits_hourly_fact_y2014m08 INHERIT visits_hourly_fact; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_time_key_check CHECK (time_key >= 1406851200 AND time_key < 1409529600) NOT VALID; + ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_cluster_type_id_fkey FOREIGN KEY (cluster_type_id) REFERENCES cluster_type(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_date_dimension_id_fkey FOREIGN KEY (date_dimension_id) REFERENCES date_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_tenant_dimension_id_fkey FOREIGN KEY (tenant_dimension_id) REFERENCES tenant_dimension(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_dimension_id_fkey FOREIGN KEY (user_dimension_id) REFERENCES user_dimension(id) NOT VALID; @@ -170,6 +181,7 @@ ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_user_agent_type_id_fkey FOREIGN KEY (user_agent_type_id) REFERENCES user_agent_type(id) NOT VALID; ALTER TABLE visits_hourly_fact_y2014m08 ADD CONSTRAINT visits_hourly_fact_y2014m08_feature_type_id_fkey FOREIGN KEY (feature_type_id) REFERENCES feature_type(id) NOT VALID; + ALTER INDEX visits_hourly_fact_y2014m08_stage_cluster_type_id_index RENAME TO visits_hourly_fact_y2014m08_cluster_type_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_date_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_date_dimension_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_tenant_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_tenant_dimension_id_index; ALTER INDEX visits_hourly_fact_y2014m08_stage_user_dimension_id_index RENAME TO visits_hourly_fact_y2014m08_user_dimension_id_index; From 5aba5608a32d2ac44ceb6866f3bb9413db89e4c6 Mon Sep 17 00:00:00 2001 From: Michael Andrews Date: Mon, 23 Mar 2015 16:34:00 -0700 Subject: [PATCH 4/4] Bump version --- lib/masamune/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/masamune/version.rb b/lib/masamune/version.rb index 0d649b1..e57a69d 100644 --- a/lib/masamune/version.rb +++ b/lib/masamune/version.rb @@ -21,5 +21,5 @@ # THE SOFTWARE. module Masamune - VERSION = '0.9.0' + VERSION = '0.10.0.rc1' end