sequel_migration_builder 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.2
@@ -111,8 +111,13 @@ module Sequel
111
111
  def create_table_statement(table_name, table)
112
112
  add_line "create_table #{table_name.inspect}#{pretty_hash(table[:table_options])} do"
113
113
  indent do
114
+ table[:primary_key] = [table[:primary_key]] if table[:primary_key].kind_of?(Symbol)
114
115
  table[:columns].each do |c|
115
- add_line Schema::DbColumn.build_from_hash(c).define_statement
116
+ column = Schema::DbColumn.build_from_hash(c)
117
+ if table[:primary_key] && table[:primary_key].size == 1 && table[:primary_key].first == column.name
118
+ column.single_primary_key = true
119
+ end
120
+ add_line column.define_statement
116
121
  end
117
122
  if table[:indexes]
118
123
  add_blank_line
@@ -122,7 +127,7 @@ module Sequel
122
127
  add_line "index #{columns.inspect}, :name => #{name.to_sym.inspect}#{pretty_hash(opts)}"
123
128
  end
124
129
  end
125
- if table[:primary_key]
130
+ if table[:primary_key] && table[:primary_key].size > 1
126
131
  add_blank_line
127
132
  add_line "primary_key #{table[:primary_key].inspect}"
128
133
  end
@@ -44,7 +44,7 @@ module Sequel
44
44
  result = []
45
45
 
46
46
  diffs = db_column.diff(new_column)
47
- result << :change_type_statement if [:column_type, :size, :unsigned].any? {|sym| diffs.include?(sym) }
47
+ result << :change_type_statement if [:elements, :column_type, :size, :unsigned].any? {|sym| diffs.include?(sym) }
48
48
  # only need to explicitly set the default if we're not changing the column type.
49
49
  result << :change_default_statement if diffs.include?(:default) && result.empty?
50
50
  result << :change_null_statement if diffs.include?(:null)
@@ -2,7 +2,7 @@ require 'set'
2
2
 
3
3
  module Sequel
4
4
  module Schema
5
- DbColumn = Struct.new(:name, :column_type, :null, :default, :unsigned, :size, :elements)
5
+ DbColumn = Struct.new(:name, :column_type, :null, :default, :unsigned, :size, :elements, :single_primary_key)
6
6
 
7
7
  # A column in a database table.
8
8
  #
@@ -30,7 +30,11 @@ module Sequel
30
30
  # create_table block.
31
31
  #
32
32
  def define_statement
33
- ["#{column_type} #{name.inspect}", options].compact.join(", ")
33
+ if single_primary_key
34
+ ["primary_key #{name.inspect}, :type => #{column_type.inspect}", options].compact.join(", ")
35
+ else
36
+ ["#{column_type} #{name.inspect}", options].compact.join(", ")
37
+ end
34
38
  end
35
39
 
36
40
  # Returns a Sequel migration statement to remove the column.
@@ -97,7 +97,7 @@ module Sequel
97
97
  def extract_enum_elements(db_type_string, type)
98
98
  return unless type == :enum
99
99
 
100
- match = db_type_string.match(/\(([^)]+)\)/)
100
+ match = db_type_string.match(/\((.+)\)/)
101
101
  eval('[' + match[1] + ']') if match[1]
102
102
  end
103
103
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sequel_migration_builder}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.2"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Roland Swingler"]
12
- s.date = %q{2010-07-02}
12
+ s.date = %q{2010-07-13}
13
13
  s.description = %q{Build Sequel Migrations based on the differences between two schemas}
14
14
  s.email = %q{roland.swingler@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -60,6 +60,14 @@ describe "Sequel::Schema::AlterTableOperations.build_column_operations" do
60
60
  ops.size.should == 1
61
61
  ops.first.up.should == "set_column_type :foo, :smallint, :default => 2"
62
62
  end
63
+
64
+ it "should return a ChangeColumn operation if the elements are different" do
65
+ a = build_column(:name => :foo, :column_type => :enum, :elements => ["A"])
66
+ b = build_column(:name => :foo, :column_type => :enum, :elements => ["A", "B"])
67
+ ops = Sequel::Schema::AlterTableOperations.build_column_operations(a,b)
68
+
69
+ ops.first.up.should == "set_column_type :foo, :enum, :default => nil, :elements => [\"A\", \"B\"]"
70
+ end
63
71
  end
64
72
 
65
73
  describe "Sequel::Schema::AlterTableOperations.build" do
@@ -9,6 +9,11 @@ describe Sequel::Schema::DbColumn do
9
9
  it "should return a #define_statement" do
10
10
  @column.define_statement.should == "integer :foo, :null => false, :default => 10, :unsigned => true, :size => 10"
11
11
  end
12
+
13
+ it "should return a primary_key invocation if single_primary_key is set" do
14
+ @column.single_primary_key = true
15
+ @column.define_statement.should == "primary_key :foo, :type => :integer, :null => false, :default => 10, :unsigned => true, :size => 10"
16
+ end
12
17
 
13
18
  it "should return a #drop_statement" do
14
19
  @column.drop_statement.should == "drop_column :foo"
@@ -87,6 +92,13 @@ describe Sequel::Schema::DbColumn do
87
92
  b.diff(a).should == [:default].to_set
88
93
  end
89
94
 
95
+ it "should consider columns with different elements to be different" do
96
+ a = Sequel::Schema::DbColumn.new(:foo, :enum, true, nil, true, nil, ["A"])
97
+ b = Sequel::Schema::DbColumn.new(:foo, :enum, true, nil, true, nil, ["A", "B"])
98
+ a.diff(b).should == [:elements].to_set
99
+ b.diff(a).should == [:elements].to_set
100
+ end
101
+
90
102
  it "should be buildable from a Hash" do
91
103
  Sequel::Schema::DbColumn.build_from_hash(:name => "foo",
92
104
  :column_type => "integer").column_type.should == "integer"
@@ -137,3 +137,18 @@ describe "Parsing a text column" do
137
137
  lambda { parser.parse_table_schema(schema) }.should_not raise_error
138
138
  end
139
139
  end
140
+
141
+ describe "Parsing an enum column" do
142
+ it "should not raise an error when enum values contains brackets" do
143
+ parser = Sequel::Schema::DbSchemaParser.for_db(stub(:database))
144
+ schema = [[:example_column,
145
+ { :type => :enum,
146
+ :default => nil,
147
+ :ruby_default => nil,
148
+ :primary_key => false,
149
+ :db_type => "enum('foo (bar)', 'baz')",
150
+ :allow_null => true }]]
151
+
152
+ lambda { parser.parse_table_schema(schema) }.should_not raise_error(SyntaxError)
153
+ end
154
+ end
@@ -78,10 +78,8 @@ END
78
78
 
79
79
  expected = <<-END
80
80
  create_table :example_table do
81
- integer :foo, :null => false
81
+ primary_key :foo, :type => :integer, :null => false
82
82
  varchar :bar, :null => false
83
-
84
- primary_key :foo
85
83
  end
86
84
  END
87
85
 
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 1
8
- - 0
9
- version: 0.1.0
8
+ - 2
9
+ version: 0.1.2
10
10
  platform: ruby
11
11
  authors:
12
12
  - Roland Swingler
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-02 00:00:00 +01:00
17
+ date: 2010-07-13 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency