Permalink
Browse files

Improve change_column

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...
1 parent 4dab545 commit 417949bc90c82ce0ddc33cb9a49258ada6e7027e @grobie committed Feb 22, 2012
Showing with 23 additions and 5 deletions.
  1. +1 −2 lib/lhm/migrator.rb
  2. +4 −0 spec/fixtures/small_table.ddl
  3. +16 −0 spec/integration/lhm_spec.rb
  4. +2 −3 spec/unit/migrator_spec.rb
View
@@ -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
@@ -0,0 +1,4 @@
+CREATE TABLE `small_table` (
+ `id` INT(11),
+ PRIMARY KEY (`id`)
+) ENGINE=InnoDB DEFAULT CHARSET=utf8
@@ -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 }'") }
@@ -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

0 comments on commit 417949b

Please sign in to comment.