Permalink
Browse files

Add support to specify table options when creating a table via migrat…

…ions. (For things like specifying MySQL storage engines, etc)
  • Loading branch information...
nevir authored and xaviershay committed Mar 22, 2011
1 parent 76d3606 commit 9b3fb2ace4af8dccb7aae9aec39637c2a0fd2798
@@ -49,7 +49,7 @@ def schema_name
# @api private
def create_table_statement(connection, model, properties)
- "#{super} ENGINE = #{DEFAULT_ENGINE} CHARACTER SET #{character_set} COLLATE #{collation}"
+ "#{super} ENGINE = #{storage_engine} CHARACTER SET #{character_set} COLLATE #{collation}"
end
# @api private
@@ -82,6 +82,13 @@ def property_schema_statement(connection, schema)
statement
end
+ # @api private
+ def storage_engine
+ # Don't pull the default engine via show_variable for backwards compat where it was hard
+ # coded to InnoDB
+ @storage_engine ||= DEFAULT_ENGINE
+ end
+
# @api private
def character_set
@character_set ||= show_variable('character_set_connection') || DEFAULT_CHARACTER_SET
@@ -21,8 +21,12 @@ def supports_serial?
true
end
- def table_options
- " ENGINE = InnoDB CHARACTER SET #{character_set} COLLATE #{collation}"
+ def table_options(opts)
+ opt_engine = opts[:storage_engine] || storage_engine
+ opt_char_set = opts[:character_set] || character_set
+ opt_collation = opts[:collation] || collation
+
+ " ENGINE = #{opt_engine} CHARACTER SET #{opt_char_set} COLLATE #{opt_collation}"
end
def property_schema_statement(connection, schema)
@@ -32,7 +32,7 @@ def property_schema_statement(connection, schema)
statement
end
- def table_options
+ def table_options(opts)
''
end
@@ -19,7 +19,7 @@ def recreate_database
# do nothing, sqlite will automatically create the database file
end
- def table_options
+ def table_options(opts)
''
end
@@ -26,7 +26,7 @@ def column(name, type, opts = {})
end
def to_sql
- "CREATE TABLE #{quoted_table_name} (#{@columns.map{ |c| c.to_sql }.join(', ')})#{@adapter.table_options}"
+ "CREATE TABLE #{quoted_table_name} (#{@columns.map{ |c| c.to_sql }.join(', ')})#{@adapter.table_options(@opts)}"
end
# A helper for using the native NOW() SQL function in a default
@@ -70,6 +70,23 @@
#can't get an exact == comparison here because character set and collation may differ per connection
@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
+
+ it "should allow for custom table creation options for MySQL" do
+ opts = {
+ :storage_engine => 'MyISAM',
+ :character_set => 'big5',
+ :collation => 'big5_chinese_ci',
+ }
+
+ creator = DataMapper::Migration::TableCreator.new(@adapter, :people, opts) do
+ column :id, DataMapper::Property::Serial
+ column :name, 'VARCHAR(50)', :allow_nil => false
+ column :long_string, String, :size => 200
+ end
+
+ creator.to_sql.should match(/^CREATE TABLE `people` \(`id` SERIAL PRIMARY KEY, `name` VARCHAR\(50\) NOT NULL, `long_string` VARCHAR\(200\)\) ENGINE = MyISAM CHARACTER SET big5 COLLATE big5_chinese_ci\z/)
+ end
+
when :postgres
it "should output a CREATE TABLE statement when sent #to_sql" do
@creator.to_sql.should == %q{CREATE TABLE "people" ("id" SERIAL PRIMARY KEY, "name" VARCHAR(50) NOT NULL, "long_string" VARCHAR(200))}

0 comments on commit 9b3fb2a

Please sign in to comment.