Skip to content

Commit

Permalink
Improve change_column
Browse files Browse the repository at this point in the history
This change allows to change the definition of the last column in a
table (it's not possible to remove all columns from a table) and
speedups the migration as only one alter statement is necessary for each
change_column.
  • Loading branch information
grobie committed Feb 22, 2012
1 parent 4dab545 commit 417949b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
3 changes: 1 addition & 2 deletions lib/lhm/migrator.rb
Expand Up @@ -66,8 +66,7 @@ def add_column(name, definition)
# @param [String] name Name of the column to change
# @param [String] definition Valid SQL column definition
def change_column(name, definition)
remove_column(name)
add_column(name, definition)
ddl("alter table `%s` modify column `%s` %s" % [@name, name, definition])
end

# Remove a column from a table
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/small_table.ddl
@@ -0,0 +1,4 @@
CREATE TABLE `small_table` (
`id` INT(11),
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
16 changes: 16 additions & 0 deletions spec/integration/lhm_spec.rb
Expand Up @@ -119,6 +119,22 @@
end
end

it "should change the last column in a table" do
table_create(:small_table)

Lhm.change_table(:small_table) do |t|
t.change_column(:id, "int(5)")
end

slave do
table_read(:small_table).columns["id"].must_equal({
:type => "int(5)",
:is_nullable => "NO",
:column_default => "0"
})
end
end

describe "parallel" do
it "should perserve inserts during migration" do
50.times { |n| execute("insert into users set reference = '#{ n }'") }
Expand Down
5 changes: 2 additions & 3 deletions spec/unit/migrator_spec.rb
Expand Up @@ -66,11 +66,10 @@
end

it "should change a column" do
@creator.change_column("logins", "INT(255)")
@creator.change_column("logins", "INT(11)")

@creator.statements.must_equal([
"alter table `lhmn_alt` drop `logins`",
"alter table `lhmn_alt` add column `logins` INT(255)"
"alter table `lhmn_alt` modify column `logins` INT(11)"
])
end
end
Expand Down

0 comments on commit 417949b

Please sign in to comment.