chicagowarehouse 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (63) hide show
  1. data/.document +5 -0
  2. data/.rspec +1 -0
  3. data/Gemfile +18 -0
  4. data/LICENSE +20 -0
  5. data/README +11 -0
  6. data/Rakefile +50 -0
  7. data/chicagowarehouse.gemspec +134 -0
  8. data/lib/chicago.rb +32 -0
  9. data/lib/chicago/core_ext/hash.rb +18 -0
  10. data/lib/chicago/core_ext/sequel/dataset.rb +7 -0
  11. data/lib/chicago/core_ext/sequel/sql.rb +62 -0
  12. data/lib/chicago/data/month.rb +98 -0
  13. data/lib/chicago/database/constants.rb +18 -0
  14. data/lib/chicago/database/dataset_builder.rb +75 -0
  15. data/lib/chicago/database/filter.rb +109 -0
  16. data/lib/chicago/database/migration_file_writer.rb +34 -0
  17. data/lib/chicago/database/schema_generator.rb +117 -0
  18. data/lib/chicago/database/type_converters.rb +107 -0
  19. data/lib/chicago/database/value_parser.rb +23 -0
  20. data/lib/chicago/errors.rb +23 -0
  21. data/lib/chicago/query.rb +109 -0
  22. data/lib/chicago/rake_tasks.rb +50 -0
  23. data/lib/chicago/schema/builders/column_builder.rb +21 -0
  24. data/lib/chicago/schema/builders/dimension_builder.rb +69 -0
  25. data/lib/chicago/schema/builders/fact_builder.rb +74 -0
  26. data/lib/chicago/schema/builders/shrunken_dimension_builder.rb +54 -0
  27. data/lib/chicago/schema/builders/table_builder.rb +33 -0
  28. data/lib/chicago/schema/column.rb +221 -0
  29. data/lib/chicago/schema/column_parser.rb +127 -0
  30. data/lib/chicago/schema/dimension.rb +129 -0
  31. data/lib/chicago/schema/dimension_reference.rb +47 -0
  32. data/lib/chicago/schema/fact.rb +70 -0
  33. data/lib/chicago/schema/measure.rb +35 -0
  34. data/lib/chicago/schema/named_element.rb +16 -0
  35. data/lib/chicago/schema/named_element_collection.rb +64 -0
  36. data/lib/chicago/schema/query_column.rb +199 -0
  37. data/lib/chicago/schema/table.rb +41 -0
  38. data/lib/chicago/star_schema.rb +127 -0
  39. data/spec/core_ext/sequel_extensions_spec.rb +29 -0
  40. data/spec/data/month_spec.rb +67 -0
  41. data/spec/database/db_type_converter_spec.rb +125 -0
  42. data/spec/database/migration_file_writer_spec.rb +37 -0
  43. data/spec/database/schema_generator_spec.rb +199 -0
  44. data/spec/db_connections.yml.dist +4 -0
  45. data/spec/query_spec.rb +495 -0
  46. data/spec/schema/column_spec.rb +213 -0
  47. data/spec/schema/dimension_builder_spec.rb +32 -0
  48. data/spec/schema/dimension_reference_spec.rb +90 -0
  49. data/spec/schema/dimension_spec.rb +111 -0
  50. data/spec/schema/fact_spec.rb +83 -0
  51. data/spec/schema/measure_spec.rb +27 -0
  52. data/spec/schema/named_element_collection_spec.rb +67 -0
  53. data/spec/schema/pivoted_column_spec.rb +17 -0
  54. data/spec/schema/query_column_spec.rb +120 -0
  55. data/spec/spec_helper.rb +20 -0
  56. data/spec/star_schema_spec.rb +219 -0
  57. data/spec/support/matchers/be_one_of.rb +11 -0
  58. data/spec/support/matchers/column_matchers.rb +11 -0
  59. data/spec/support/shared_examples/column.rb +13 -0
  60. data/spec/support/shared_examples/schema_table.rb +17 -0
  61. data/spec/support/shared_examples/schema_visitor.rb +25 -0
  62. data/tasks/stats.rake +108 -0
  63. metadata +300 -0
@@ -0,0 +1,127 @@
1
+ require 'chicago/errors'
2
+ require 'chicago/schema/named_element_collection'
3
+ require 'chicago/schema/column'
4
+ require 'chicago/schema/measure'
5
+ require 'chicago/schema/dimension_reference'
6
+ require 'chicago/schema/dimension'
7
+ require 'chicago/schema/fact'
8
+ require 'chicago/schema/builders/fact_builder'
9
+ require 'chicago/schema/builders/dimension_builder'
10
+ require 'chicago/schema/builders/shrunken_dimension_builder'
11
+ require 'chicago/schema/builders/column_builder'
12
+
13
+ module Chicago
14
+ # A collection of facts & dimensions.
15
+ #
16
+ # @api public
17
+ class StarSchema
18
+ # A collection of all the facts defined in this schema.
19
+ # @return [Chicago::Schema::Fact]
20
+ attr_reader :facts
21
+
22
+ # a collection of all the dimensions defined in this schema.
23
+ # @return [Chicago::Schema::Dimension]
24
+ attr_reader :dimensions
25
+
26
+ # Creates a new star schema.
27
+ def initialize
28
+ @dimensions = Schema::NamedElementCollection.new
29
+ @facts = Schema::NamedElementCollection.new
30
+ end
31
+
32
+ # Returns a fact, named +name+
33
+ #
34
+ # @param [Symbol] name the name of the fact
35
+ # @return [Chicago::Schema::Fact]
36
+ def fact(name)
37
+ @facts[name]
38
+ end
39
+
40
+ # Returns a dimension, named +name+
41
+ #
42
+ # @param [Symbol] name the name of the dimension
43
+ # @return [Chicago::Schema::Dimension]
44
+ def dimension(name)
45
+ @dimensions[name]
46
+ end
47
+
48
+ # Returns all dimensions and facts in this schema.
49
+ #
50
+ # @return [Array]
51
+ def tables
52
+ @dimensions.to_a + @facts.to_a
53
+ end
54
+
55
+ # Adds a prebuilt schema table to the schema
56
+ #
57
+ # Schema tables may not be dupliates of already present tables in
58
+ # the schema.
59
+ #
60
+ # TODO: figure out how to deal with linked dimensions when adding
61
+ # facts.
62
+ def add(schema_table)
63
+ if schema_table.kind_of? Schema::Fact
64
+ collection = @facts
65
+ elsif schema_table.kind_of? Schema::Dimension
66
+ collection = @dimensions
67
+ end
68
+
69
+ add_to_collection collection, schema_table
70
+ end
71
+
72
+ # Defines a fact table named +name+ in this schema.
73
+ #
74
+ # @see Chicago::Schema::Builders::FactBuilder
75
+ # @return [Chicago::Schema::Fact] the defined fact.
76
+ # @raise Chicago::MissingDefinitionError
77
+ def define_fact(name, &block)
78
+ add Schema::Builders::FactBuilder.new(self).build(name, &block)
79
+ end
80
+
81
+ # Defines a dimension table named +name+ in this schema.
82
+ #
83
+ # For example:
84
+ #
85
+ # @schema.define_dimension(:date) do
86
+ # columns do
87
+ # date :date
88
+ # year :year
89
+ # string :month
90
+ # ...
91
+ # end
92
+ #
93
+ # natural_key :date
94
+ # null_record :id => 1, :month => "Unknown Month"
95
+ # end
96
+ #
97
+ # @see Chicago::Schema::Builders::DimensionBuilder
98
+ # @return [Chicago::Schema::Dimension] the defined dimension.
99
+ def define_dimension(name, &block)
100
+ add Schema::Builders::DimensionBuilder.new(self).build(name, &block)
101
+ end
102
+
103
+ # Defines a shrunken dimension table named +name+ in this schema.
104
+ #
105
+ # +base_name+ is the name of the base dimension that the shrunken
106
+ # dimension is derived from; this base dimention must already be
107
+ # defined.
108
+ #
109
+ # @see Chicago::Schema::Builders::ShrunkenDimensionBuilder
110
+ # @raise [Chicago::MissingDefinitionError] if the base dimension is not defined.
111
+ # @return [Chicago::Schema::Dimension] the defined dimension.
112
+ def define_shrunken_dimension(name, base_name, &block)
113
+ add Schema::Builders::ShrunkenDimensionBuilder.new(self, base_name).
114
+ build(name, &block)
115
+ end
116
+
117
+ private
118
+
119
+ def add_to_collection(collection, schema_table)
120
+ if collection.contain?(schema_table)
121
+ raise DuplicateTableError.new("#{schema_table.class} '#{schema_table.name}' has already been defined.")
122
+ end
123
+
124
+ collection << schema_table
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+
3
+ describe Symbol do
4
+ it "should have a distinct method" do
5
+ :foo.distinct.should be_kind_of(Sequel::SQL::DistinctExpression)
6
+ end
7
+ end
8
+
9
+ describe String do
10
+ it "should have a distinct method" do
11
+ "foo".distinct.should be_kind_of(Sequel::SQL::DistinctExpression)
12
+ end
13
+ end
14
+
15
+ describe Sequel::SQL::Expression do
16
+ it "should have a distinct method" do
17
+ :if.sql_function(true, 1, 0).distinct.should be_kind_of(Sequel::SQL::DistinctExpression)
18
+ end
19
+ end
20
+
21
+ describe Sequel::SQL::DistinctExpression do
22
+ it "has a reader for the wrapped expression" do
23
+ described_class.new(:foo).expression.should == :foo
24
+ end
25
+
26
+ it "is rendered as 'DISTINCT expression'" do
27
+ described_class.new(:foo).to_s(TEST_DB[:foo]).should == "DISTINCT `foo`"
28
+ end
29
+ end
@@ -0,0 +1,67 @@
1
+ require "spec_helper"
2
+
3
+ describe Chicago::Data::Month do
4
+
5
+ [:january, :february, :march, :april, :may, :june, :july, :august, :september, :october, :november, :december].each do |month|
6
+ it "should return a Month for #{month}" do
7
+ Chicago::Data::Month.send(month).should be_kind_of(Chicago::Data::Month)
8
+ end
9
+ end
10
+
11
+ it "should have a name" do
12
+ Chicago::Data::Month.january.name.should == "January"
13
+ end
14
+
15
+ it "should return the name from #to_s" do
16
+ Chicago::Data::Month.january.to_s.should == "January"
17
+ end
18
+
19
+ it "should have a short name" do
20
+ Chicago::Data::Month.january.short_name.should == "Jan"
21
+ end
22
+
23
+ it "should have a number" do
24
+ Chicago::Data::Month.january.to_i.should == 1
25
+ end
26
+
27
+ it "should return a date in a year" do
28
+ Chicago::Data::Month.january.in(2009).should == Date.new(2009,1,1)
29
+ end
30
+
31
+ it "should be the same instance on mutliple calls to a month name" do
32
+ Chicago::Data::Month.january.object_id.should == Chicago::Data::Month.january.object_id
33
+ end
34
+
35
+ it "should be comparable" do
36
+ Chicago::Data::Month.january.should < Chicago::Data::Month.february
37
+ Chicago::Data::Month.december.should > Chicago::Data::Month.january
38
+ end
39
+
40
+ it "should not be constructable" do
41
+ lambda { Chicago::Data::Month("Foo", 13) }.should raise_error
42
+ end
43
+
44
+ it "should be parsable from a short string" do
45
+ Chicago::Data::Month.parse("Jan").should == Chicago::Data::Month.january
46
+ end
47
+
48
+ it "should be parsable from a short string uncaptialized" do
49
+ Chicago::Data::Month.parse("jan").should == Chicago::Data::Month.january
50
+ end
51
+
52
+ it "should be parsable from a full name" do
53
+ Chicago::Data::Month.parse("December").should == Chicago::Data::Month.december
54
+ end
55
+
56
+ it "should be parsable from a full name uncaptialized" do
57
+ Chicago::Data::Month.parse("january").should == Chicago::Data::Month.january
58
+ end
59
+
60
+ it "should be parsable from an integer" do
61
+ Chicago::Data::Month.parse(1).should == Chicago::Data::Month.january
62
+ end
63
+
64
+ it "should return nil from a string that isn't a month name" do
65
+ Chicago::Data::Month.parse("maybe").should == nil
66
+ end
67
+ end
@@ -0,0 +1,125 @@
1
+ require "spec_helper"
2
+
3
+ shared_examples_for "All DB type converters" do
4
+ context "#db_type" do
5
+ before(:each) do
6
+ @dimension = stub(:dimension)
7
+ end
8
+
9
+ it "should return :varchar for a string column" do
10
+ column = Schema::Column.new(:id, :string)
11
+ @tc.db_type(column).should == :varchar
12
+ end
13
+
14
+ it "should return :char for a string column that has equal max and min attributes" do
15
+ column = Schema::Column.new(:id, :string, :min => 2, :max => 2)
16
+ @tc.db_type(column).should == :char
17
+ end
18
+
19
+ it "should return :integer for an column with a max but no min attribute set" do
20
+ column = Schema::Column.new(:id, :integer, :max => 127)
21
+ @tc.db_type(column).should == :integer
22
+ end
23
+
24
+ it "should return :decimal for a money column type" do
25
+ column = Schema::Column.new(:id, :money)
26
+ @tc.db_type(column).should == :decimal
27
+ end
28
+
29
+ it "should return :decimal for a percent column type" do
30
+ column = Schema::Column.new(:id, :percent)
31
+ @tc.db_type(column).should == :decimal
32
+ end
33
+
34
+ it "should assume any other type is a database type and return it" do
35
+ column = Schema::Column.new(:id, :foo)
36
+ @tc.db_type(column).should == :foo
37
+ end
38
+ end
39
+ end
40
+
41
+ describe "DbTypeConverter.for_db" do
42
+ before :each do
43
+ @mock_db = mock()
44
+ end
45
+
46
+ it "should return a type converter specific to MySQL if the database type is :mysql" do
47
+ @mock_db.should_receive(:database_type).and_return(:mysql)
48
+
49
+ converter = Database::TypeConverters::DbTypeConverter.for_db(@mock_db)
50
+ converter.should be_kind_of(Database::TypeConverters::MysqlTypeConverter)
51
+ end
52
+
53
+ it "should return a generic type converter for an unknown database type" do
54
+ @mock_db.should_receive(:database_type).and_return(:foodb)
55
+
56
+ converter = Database::TypeConverters::DbTypeConverter.for_db(@mock_db)
57
+ converter.should be_kind_of(Database::TypeConverters::DbTypeConverter)
58
+ end
59
+ end
60
+
61
+ describe "Generic DbTypeConverter" do
62
+ it_behaves_like "All DB type converters"
63
+
64
+ before :each do
65
+ @tc = Database::TypeConverters::DbTypeConverter.new
66
+ end
67
+
68
+ { :smallint => [-32768, 32767],
69
+ :smallint => [0, 65535],
70
+ }.each do |expected_db_type, range|
71
+
72
+ it "should create a #{expected_db_type} if the maximum column value < #{range.max} and min is >= #{range.min}" do
73
+ column = Schema::Column.new(:id, :integer, :max => range.max, :min => range.min)
74
+ @tc.db_type(column).should == expected_db_type
75
+ end
76
+ end
77
+ end
78
+
79
+ describe Chicago::Database::TypeConverters::MysqlTypeConverter do
80
+ it_behaves_like "All DB type converters"
81
+
82
+ before :each do
83
+ @tc = Database::TypeConverters::MysqlTypeConverter.new
84
+ end
85
+
86
+ context "#db_type" do
87
+ { :tinyint => [-127, 128],
88
+ :tinyint => [0, 255],
89
+ :smallint => [-32768, 32767],
90
+ :smallint => [0, 65535],
91
+ :mediumint => [-8388608, 8388607],
92
+ :mediumint => [0, 16777215],
93
+ :integer => [-2147483648, 2147483647],
94
+ :integer => [0, 4294967295],
95
+ :bigint => [-9223372036854775808, 9223372036854775807],
96
+ :bigint => [0, 18446744073709551615]
97
+
98
+ }.each do |expected_db_type, range|
99
+
100
+ it "should return #{expected_db_type} if the maximum column value < #{range.max} and min is >= #{range.min}" do
101
+ column = Schema::Column.new(:id, :integer, :max => range.max, :min => range.min)
102
+ @tc.db_type(column).should == expected_db_type
103
+ end
104
+ end
105
+
106
+ it "should raise an ArgumentError if either of the min/max values are out of bounds" do
107
+ column = Schema::Column.new(:id,
108
+ :integer,
109
+ :min => 0,
110
+ :max => 18_446_744_073_709_551_616)
111
+
112
+ lambda { @tc.db_type(column) }.should raise_error(ArgumentError)
113
+ end
114
+
115
+ it "should return :enum if the column definition has elements" do
116
+ column = Schema::Column.new(:id, :string, :elements => ["A", "B"])
117
+ @tc.db_type(column).should == :enum
118
+ end
119
+
120
+ it "should return :varchar if the column definition has a large number of elements" do
121
+ column = Schema::Column.new(:id, :string, :elements => stub(:size => 70_000))
122
+ @tc.db_type(column).should == :varchar
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,37 @@
1
+ require "spec_helper"
2
+
3
+ describe Chicago::Database::MigrationFileWriter do
4
+ before :each do
5
+ @mock_db = mock(:db)
6
+ @mock_db.stub(:database_type).and_return(:generic)
7
+ @builder = described_class.new(@mock_db, "schema")
8
+ end
9
+
10
+ it "should return a migration file name using the timestamp of now" do
11
+ now = Time.local(2010, 1, 1, 12, 30)
12
+ Time.should_receive(:now).and_return(now)
13
+ @builder.migration_file.should == "schema/20100101123000_auto_migration.rb"
14
+ end
15
+
16
+ it "should write out a migration file generated by Sequel::MigrationBuilder" do
17
+ schema = stub(:schema)
18
+
19
+ generator = stub(:generator)
20
+ generator.should_receive(:traverse).
21
+ with(schema).and_return(:table => :definitions)
22
+ Chicago::Database::SchemaGenerator.stub(:new).and_return(generator)
23
+
24
+ migration_builder = mock(:migration_builder)
25
+ migration_builder.should_receive(:generate_migration).
26
+ with(:table => :definitions).and_return("migration content")
27
+ Sequel::MigrationBuilder.stub(:new).and_return(migration_builder)
28
+
29
+ file = StringIO.new
30
+ File.stub(:open).and_yield(file)
31
+
32
+ @builder.write_migration_file(schema)
33
+
34
+ file.rewind
35
+ file.read.should == "migration content"
36
+ end
37
+ end
@@ -0,0 +1,199 @@
1
+ require 'spec_helper'
2
+ require 'chicago/database/schema_generator'
3
+
4
+ describe Chicago::Database::SchemaGenerator do
5
+ subject { described_class.new(Chicago::Database::TypeConverters::DbTypeConverter.new) }
6
+
7
+ it_behaves_like "a schema visitor"
8
+
9
+ describe "#visit_fact" do
10
+ before :all do
11
+ schema = Chicago::StarSchema.new
12
+ schema.define_dimension :customer
13
+ schema.define_dimension :product
14
+ @fact = schema.define_fact(:sales) do
15
+ dimensions :customer, :product
16
+ degenerate_dimensions { string :reference }
17
+ measures { integer :quantity }
18
+ natural_key :customer, :reference
19
+ end
20
+ end
21
+
22
+ it "should define a sales_facts table" do
23
+ subject.visit_fact(@fact).should have_key(:facts_sales)
24
+ end
25
+
26
+ it "should have an unsigned integer :id column" do
27
+ expected = {:name => :id, :column_type => :integer, :unsigned => true}
28
+ subject.visit_fact(@fact)[:facts_sales][:columns].
29
+ should include(expected)
30
+ end
31
+
32
+ it "should define :id as the primary key" do
33
+ subject.visit_fact(@fact)[:facts_sales][:primary_key].should == [:id]
34
+ end
35
+
36
+ it "should include a hash of table options" do
37
+ subject.visit_fact(@fact)[:facts_sales][:table_options].should == {}
38
+ end
39
+
40
+ it "should have a table type of MyISAM for mysql" do
41
+ subject.type_converter = Chicago::Database::TypeConverters::MysqlTypeConverter.new
42
+ subject.visit_fact(@fact)[:facts_sales][:table_options].should == {:engine => "myisam"}
43
+ end
44
+
45
+ it "should output the dimension foreign key columns" do
46
+ [{:name => :customer_dimension_id, :column_type => :integer, :unsigned => true, :null => false},
47
+ {:name => :product_dimension_id, :column_type => :integer, :unsigned => true, :null => false}
48
+ ].each do |column|
49
+ subject.visit_fact(@fact)[:facts_sales][:columns].should include(column)
50
+ end
51
+ end
52
+
53
+ it "should output the degenerate dimension columns" do
54
+ subject.visit_fact(@fact)[:facts_sales][:columns].should include({:name => :reference, :column_type => :varchar, :null => false})
55
+ end
56
+
57
+ it "should output the measure columns" do
58
+ subject.visit_fact(@fact)[:facts_sales][:columns].should include({:name => :quantity, :column_type => :integer, :null => true, :unsigned => false})
59
+ end
60
+
61
+ it "should define non-unique indexes for every dimension" do
62
+ subject.visit_fact(@fact)[:facts_sales][:indexes].should == {
63
+ :product_idx => { :columns => :product_dimension_id, :unique => false },
64
+ :customer_idx => { :columns => [:customer_dimension_id, :reference], :unique => true },
65
+ :reference_idx => { :columns => :reference, :unique => false }
66
+ }
67
+ end
68
+ end
69
+
70
+ describe "#visit_dimension" do
71
+ before :each do
72
+ @schema = Chicago::StarSchema.new
73
+ end
74
+
75
+ it "should define a user_dimension table" do
76
+ @dimension = @schema.define_dimension(:user)
77
+ subject.visit_dimension(@dimension).keys.should include(:dimension_user)
78
+ end
79
+
80
+ it "should have an unsigned integer :id column" do
81
+ @dimension = @schema.define_dimension(:user)
82
+ expected = {:name => :id, :column_type => :integer, :unsigned => true}
83
+ subject.visit_dimension(@dimension)[:dimension_user][:columns].should include(expected)
84
+ end
85
+
86
+ it "should define :id as the primary key" do
87
+ @dimension = @schema.define_dimension(:user)
88
+ subject.visit_dimension(@dimension)[:dimension_user][:primary_key].should == [:id]
89
+ end
90
+
91
+ it "should include a hash of table options" do
92
+ @dimension = @schema.define_dimension(:user)
93
+ subject.visit_dimension(@dimension)[:dimension_user][:table_options].should == {}
94
+ end
95
+
96
+ it "should have a table type of MyISAM for mysql" do
97
+ @dimension = @schema.define_dimension(:user)
98
+ subject.type_converter = Chicago::Database::TypeConverters::MysqlTypeConverter.new
99
+ subject.visit_dimension(@dimension)[:dimension_user][:table_options].should == {:engine => "myisam"}
100
+ end
101
+
102
+ it "should include the sequel schema for the defined columns" do
103
+ @dimension = @schema.define_dimension(:user) do
104
+ columns do
105
+ string :username, :max => 10
106
+ end
107
+ end
108
+
109
+ expected = {:name => :username, :column_type => :varchar, :size => 10, :null => false}
110
+ subject.visit_dimension(@dimension)[:dimension_user][:columns].should include(expected)
111
+ end
112
+
113
+ it "should output indexes for every column that isn't descriptive" do
114
+ @dimension = @schema.define_dimension(:user) do
115
+ columns do
116
+ string :foo, :descriptive => true
117
+ string :bar
118
+ string :baz
119
+ end
120
+ end
121
+
122
+ expected = {
123
+ :bar_idx => {:columns => :bar, :unique => false},
124
+ :baz_idx => {:columns => :baz, :unique => false}
125
+ }
126
+ subject.visit_dimension(@dimension)[:dimension_user][:indexes].should == expected
127
+ end
128
+
129
+ it "should output a natural_key unique index for the natural key" do
130
+ @dimension = @schema.define_dimension(:user) do
131
+ columns do
132
+ string :foo, :descriptive => true
133
+ string :bar
134
+ string :baz
135
+ end
136
+ natural_key :bar, :baz
137
+ end
138
+
139
+ expected = {
140
+ :bar_idx => {:columns => [:bar, :baz], :unique => true},
141
+ :baz_idx => {:columns => :baz, :unique => false}
142
+ }
143
+ subject.visit_dimension(@dimension)[:dimension_user][:indexes].should == expected
144
+ end
145
+
146
+ # This just supports internal convention at the moment
147
+ it "should create a key mapping table if an original_id column is present" do
148
+ @dimension = @schema.define_dimension(:user) do
149
+ columns do
150
+ integer :original_id, :min => 0
151
+ string :username, :max => 10
152
+ end
153
+ end
154
+
155
+ key_table = subject.visit_dimension(@dimension)[:keys_dimension_user]
156
+ key_table.should_not be_nil
157
+ key_table[:primary_key].should == [:original_id]
158
+
159
+ expected = [{:name => :original_id, :column_type => :integer, :null => false, :unsigned => true},
160
+ {:name => :dimension_id, :column_type => :integer, :null => false, :unsigned => true}]
161
+ key_table[:columns].should == expected
162
+ end
163
+
164
+ it "creates a mapping table with a binary column, for dimensions with no original_id" do
165
+ @dimension = @schema.define_dimension(:user) do
166
+ columns do
167
+ string :username, :max => 10
168
+ natural_key :username
169
+ end
170
+ end
171
+
172
+ key_table = subject.visit_dimension(@dimension)[:keys_dimension_user]
173
+ key_table.should_not be_nil
174
+ key_table[:primary_key].should == [:original_id]
175
+
176
+ expected = [{:name => :original_id, :column_type => :binary, :null => false, :size => 16},
177
+ {:name => :dimension_id, :column_type => :integer, :null => false, :unsigned => true}]
178
+ key_table[:columns].should == expected
179
+ end
180
+
181
+ it "doesn't create a key table for static dimensions" do
182
+ @dimension = @schema.define_dimension(:currency) do
183
+ has_predetermined_values
184
+
185
+ columns do
186
+ string :currency
187
+ end
188
+ end
189
+
190
+ subject.visit_dimension(@dimension)[:keys_dimension_currency].should be_nil
191
+ end
192
+
193
+ it "should have an unsigned integer :etl_batch_id column" do
194
+ @dimension = @schema.define_dimension(:user)
195
+ expected = {:name => :etl_batch_id, :column_type => :integer, :unsigned => true}
196
+ subject.visit_dimension(@dimension)[:dimension_user][:columns].should include(expected)
197
+ end
198
+ end
199
+ end