Permalink
Browse files

[dm-migrations] Updated custom types to mix-in default options for mi…

…gration

* Update column types to be all uppercase to match convention in dm-core
* Minor whitespace formatting change

[#938 state:resolved]
  • Loading branch information...
dkubb committed Aug 14, 2009
1 parent f90372b commit 7427e6197cb6db9f771cf50d68b80350eb4fb2aa
View
@@ -1,9 +1,5 @@
module SQL
-
class Column
-
attr_accessor :name, :type, :not_null, :default_value, :primary_key, :unique
-
end
-
end
View
@@ -26,10 +26,9 @@ def table_options
" ENGINE = InnoDB CHARACTER SET #{character_set} COLLATE #{collation}"
end
- # TODO: move to dm-more/dm-migrations
def property_schema_statement(connection, schema)
if supports_serial? && schema[:serial]
- statement = "#{schema[:quote_column_name]} serial PRIMARY KEY"
+ statement = "#{schema[:quote_column_name]} SERIAL PRIMARY KEY"
else
super
end
@@ -51,7 +50,5 @@ def initialize(col_struct)
@not_null = col_struct.notnull == 0
end
end
-
-
end
end
View
@@ -10,9 +10,9 @@ def table(table_name)
end
def recreate_database
- execute "DROP SCHEMA IF EXISTS test CASCADE"
- execute "CREATE SCHEMA test"
- execute "SET search_path TO test"
+ execute 'DROP SCHEMA IF EXISTS test CASCADE'
+ execute 'CREATE SCHEMA test'
+ execute 'SET search_path TO test'
end
def supports_serial?
@@ -21,7 +21,7 @@ def supports_serial?
def property_schema_statement(connection, schema)
if supports_serial? && schema[:serial]
- statement = "#{schema[:quote_column_name]} serial PRIMARY KEY"
+ statement = "#{schema[:quote_column_name]} SERIAL PRIMARY KEY"
else
statement = super
if schema.has_key?(:sequence_name)
@@ -64,9 +64,7 @@ def query_column_constraints
end
end
end
-
end
-
end
class Column < SQL::Column
@@ -75,8 +73,6 @@ def initialize(col_struct)
@not_null = col_struct.is_nullable != "YES"
end
-
end
-
end
end
View
@@ -42,7 +42,5 @@ def initialize(col_struct)
@not_null = col_struct.notnull == 0
end
end
-
-
end
end
View
@@ -2,9 +2,7 @@
require Pathname(__FILE__).dirname.expand_path + 'column'
module SQL
-
class Table
-
attr_accessor :name, :columns
def to_s
@@ -14,7 +12,5 @@ def to_s
def column(column_name)
@columns.select { |c| c.name == column_name.to_s }.first
end
-
end
-
end
View
@@ -55,32 +55,42 @@ def initialize(adapter, name, type, opts = {})
@type = build_type(type)
end
+ def to_sql
+ type
+ end
+
+ private
+
def build_type(type_class)
- schema = {:name => @name, :quote_column_name => quoted_name}.merge(@opts)
- schema[:serial?] ||= schema[:serial]
- unless schema.has_key?(:nullable?)
- schema[:nullable?] = schema.has_key?(:nullable) ? schema[:nullable] : !schema[:not_null]
+ schema = { :name => @name, :quote_column_name => quoted_name }.merge(@opts)
+
+ unless schema.key?(:nullable)
+ schema[:nullable] = !schema[:not_null]
end
- if type_class.is_a?(String)
+
+ schema[:length] ||= schema.delete(:size) if schema.key?(:size)
+
+ if type_class.kind_of?(String)
schema[:primitive] = type_class
else
primitive = type_class.respond_to?(:primitive) ? type_class.primitive : type_class
- schema = @adapter.class.type_map[primitive].merge(schema)
+ options = @adapter.class.type_map[primitive].dup
+
+ if type_class.respond_to?(:options)
+ options.update(type_class.options)
+ end
+
+ schema = options.update(schema)
end
+
@adapter.send(:with_connection) do |connection|
@adapter.property_schema_statement(connection, schema)
end
end
- def to_sql
- type
- end
-
def quoted_name
@adapter.send(:quote_name, name)
end
end
-
end
-
end
@@ -47,7 +47,5 @@ def quote_column_name(name)
def quoted_table_name
@adapter.send(:quote_name, table_name)
end
-
end
-
end
@@ -19,8 +19,8 @@ def repository(*args)
before do
@creator = DataMapper::Migration::TableCreator.new(repository(adapter).adapter, :people) do
- column :id, Integer, :serial => true
- column :name, 'varchar(50)', :nullable => false
+ column :id, DataMapper::Types::Serial
+ column :name, 'VARCHAR(50)', :nullable => false
column :long_string, String, :size => 200
end
end
@@ -67,15 +67,15 @@ 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" \("id" serial PRIMARY KEY, "name" varchar\(50\) NOT NULL, "long_string" VARCHAR\(200\)\) ENGINE = InnoDB CHARACTER SET \w+ COLLATE \w+\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
- @creator.to_sql.should == %q{CREATE TABLE "people" ("id" serial PRIMARY KEY, "name" varchar(50) NOT NULL, "long_string" VARCHAR(200))}
+ @creator.to_sql.should == %q{CREATE TABLE "people" ("id" SERIAL PRIMARY KEY, "name" VARCHAR(50) NOT NULL, "long_string" VARCHAR(200))}
end
when :sqlite3
it "should output a CREATE TABLE statement when sent #to_sql" do
- @creator.to_sql.should == %q{CREATE TABLE "people" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" varchar(50) NOT NULL, "long_string" VARCHAR(200))}
+ @creator.to_sql.should == %q{CREATE TABLE "people" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "name" VARCHAR(50) NOT NULL, "long_string" VARCHAR(200))}
end
end
end

0 comments on commit 7427e61

Please sign in to comment.