Skip to content
Browse files

[dm-migrations] Fixes CREATE TABLE for MySQL.

The CREATE TABLE statement was being output with the table options
before the column list; this was incorrect and caused a MySQL error.  As
mentioned in the Lighthouse ticket, it would be a good idea to add true
integration tests (which actually run migrations against a database) for
all supported adapters.

Credits go to Greg Campbell for the meat of this patch

[#827 state:resolved]
  • Loading branch information...
1 parent 771372c commit ef6ffb561c98afc54fb340d07add45e6c7a3f22e @snusnu snusnu committed
View
4 lib/sql/mysql.rb
@@ -22,8 +22,8 @@ def supports_serial?
true
end
- def create_table_statement(quoted_table_name)
- "CREATE TABLE #{quoted_table_name} ENGINE = InnoDB CHARACTER SET #{character_set} COLLATE #{collation}"
+ def table_options
+ " ENGINE = InnoDB CHARACTER SET #{character_set} COLLATE #{collation}"
end
# TODO: move to dm-more/dm-migrations
View
4 lib/sql/postgresql.rb
@@ -32,8 +32,8 @@ def property_schema_statement(connection, schema)
statement
end
- def create_table_statement(quoted_table_name)
- "CREATE TABLE #{quoted_table_name}"
+ def table_options
+ ''
end
class Table < SQL::Table
View
4 lib/sql/sqlite3.rb
@@ -18,8 +18,8 @@ def recreate_database
# do nothing, sqlite will automatically create the database file
end
- def create_table_statement(quoted_table_name)
- "CREATE TABLE #{quoted_table_name}"
+ def table_options
+ ''
end
def supports_serial?
View
2 lib/sql/table_creator.rb
@@ -21,7 +21,7 @@ def column(name, type, opts = {})
end
def to_sql
- "#{@adapter.create_table_statement(quoted_table_name)} (#{@columns.map{ |c| c.to_sql }.join(', ')})"
+ "CREATE TABLE #{quoted_table_name} (#{@columns.map{ |c| c.to_sql }.join(', ')})#{@adapter.table_options}"
end
# A helper for using the native NOW() SQL function in a default
View
2 spec/integration/sql_spec.rb
@@ -67,7 +67,7 @@ def repository(*args)
when :mysql
it "should create an InnoDB database for MySQL" do
#can't get an exact == comparison here because character set and collation may differ per connection
- @creator.to_sql.should match(/^CREATE TABLE "people" ENGINE = InnoDB CHARACTER SET \w+ COLLATE \w+ \("id" serial PRIMARY KEY, "name" varchar\(50\) NOT NULL, "long_string" VARCHAR\(200\)\)\z/)
+ @creator.to_sql.should match(/^CREATE TABLE "people" \("id" serial PRIMARY KEY, "name" varchar\(50\) NOT NULL, "long_string" VARCHAR\(200\)\) ENGINE = InnoDB CHARACTER SET \w+ COLLATE \w+\z/)
end
when :postgres
it "should output a CREATE TABLE statement when sent #to_sql" do
View
2 spec/unit/sql/table_creator_spec.rb
@@ -53,7 +53,7 @@
end
it 'should output an SQL CREATE statement to build itself' do
- @adapter.should_receive(:create_table_statement).with("'users'").and_return(%{CREATE TABLE 'users'})
+ @adapter.stub!(:table_options).and_return("")
@tc.to_sql.should ==
%{CREATE TABLE 'users' ()}
end

0 comments on commit ef6ffb5

Please sign in to comment.
Something went wrong with that request. Please try again.