sequel_migration_builder 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/VERSION +1 -1
 - data/lib/sequel/migration_builder.rb +7 -2
 - data/lib/sequel/schema/alter_table_operations.rb +1 -1
 - data/lib/sequel/schema/db_column.rb +6 -2
 - data/lib/sequel/schema/db_schema_parser.rb +1 -1
 - data/sequel_migration_builder.gemspec +2 -2
 - data/spec/alter_table_operations_spec.rb +8 -0
 - data/spec/db_column_spec.rb +12 -0
 - data/spec/db_schema_parser_spec.rb +15 -0
 - data/spec/migration_builder_spec.rb +1 -3
 - metadata +3 -3
 
    
        data/VERSION
    CHANGED
    
    | 
         @@ -1 +1 @@ 
     | 
|
| 
       1 
     | 
    
         
            -
            0.1. 
     | 
| 
      
 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 
     | 
    
         
            -
                       
     | 
| 
      
 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 
     | 
    
         
            -
                     
     | 
| 
      
 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.
         
     | 
| 
         @@ -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. 
     | 
| 
      
 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- 
     | 
| 
      
 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
         
     | 
    
        data/spec/db_column_spec.rb
    CHANGED
    
    | 
         @@ -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
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version 
     | 
|
| 
       5 
5 
     | 
    
         
             
              segments: 
         
     | 
| 
       6 
6 
     | 
    
         
             
              - 0
         
     | 
| 
       7 
7 
     | 
    
         
             
              - 1
         
     | 
| 
       8 
     | 
    
         
            -
              -  
     | 
| 
       9 
     | 
    
         
            -
              version: 0.1. 
     | 
| 
      
 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- 
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2010-07-13 00:00:00 +01:00
         
     | 
| 
       18 
18 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       19 
19 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     |