Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

better support for sqlserver #39

Open
wants to merge 6 commits into from

1 participant

Commits on Feb 19, 2012
  1. @patcheng

    better support for sqlserver

    patcheng authored
Commits on Feb 23, 2012
  1. @patcheng
  2. @patcheng

    when setting up primary key, don't have to ignore them if serial is d…

    patcheng authored
    …efined. IDENTITY does not create primary key automatically.
Commits on Mar 1, 2012
  1. @patcheng
Commits on Mar 2, 2012
  1. @patcheng
Commits on Mar 13, 2012
  1. @patcheng
This page is out of date. Refresh to see the latest.
View
2  lib/dm-migrations/adapters/dm-do-adapter.rb
@@ -202,7 +202,7 @@ def property_schema_hash(property)
schema_primitive = schema[:primitive]
- if primitive == String && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR'
+ if primitive == String && schema_primitive != 'TEXT' && schema_primitive != 'CLOB' && schema_primitive != 'NVARCHAR' && schema_primitive != 'VARBINARY'
schema[:length] = property.length
elsif primitive == BigDecimal || primitive == Float
schema[:precision] = property.precision
View
25 lib/dm-migrations/adapters/dm-oracle-adapter.rb
@@ -166,6 +166,25 @@ def schema_name
end
# @api private
+ def property_schema_hash(property)
+ schema = super
+
+ if (property.kind_of?(Property::Binary))
+ # BLOB does not support length
+ schema.delete(:length)
+ end
+
+ if (property.primitive == String && property.length > 4000)
+ # Oracle doesn't support string over 4000 bytes
+ schema[:primitive] = 'NCLOB'
+ # CLOB and NCLOB do not support length
+ schema.delete(:length)
+ end
+
+ schema
+ end
+
+ # @api private
def create_sequence_statements(model)
name = self.name
table_name = model.storage_name(name)
@@ -282,7 +301,7 @@ def type_map
precision = Property::Numeric.precision
scale = Property::Decimal.scale
- {
+ super.merge(
Integer => { :primitive => 'NUMBER', :precision => precision, :scale => 0 },
String => { :primitive => 'VARCHAR2', :length => length },
Class => { :primitive => 'VARCHAR2', :length => length },
@@ -292,8 +311,8 @@ def type_map
Date => { :primitive => 'DATE' },
Time => { :primitive => 'DATE' },
TrueClass => { :primitive => 'NUMBER', :precision => 1, :scale => 0 },
- Property::Text => { :primitive => 'CLOB' },
- }.freeze
+ Property::Text => { :primitive => 'CLOB' }
+ ).freeze
end
# Use table truncate or delete for auto_migrate! to speed up test execution
View
31 lib/dm-migrations/adapters/dm-sqlserver-adapter.rb
@@ -40,9 +40,8 @@ def supports_drop_table_if_exists?
end
# @api private
- def schema_name
- # TODO: is there a cleaner way to find out the current DB we are connected to?
- @options[:path].split('/').last
+ def schema_name
+ select('SELECT DB_NAME()')
end
# TODO: update dkubb/dm-more/dm-migrations to use schema_name and remove this
@@ -56,9 +55,9 @@ def create_table_statement(connection, model, properties)
(#{properties.map { |property| property_schema_statement(connection, property_schema_hash(property)) }.join(', ')}
SQL
- unless properties.any? { |property| property.serial? }
+ # specific the primary keys.
+ # don't have to filter out Serial (aka IDENTITY) type
statement << ", PRIMARY KEY(#{properties.key.map { |property| quote_name(property.field) }.join(', ')})"
- end
statement << ')'
statement
@@ -75,6 +74,16 @@ def property_schema_hash(property)
schema[:primitive] = integer_column_statement(min..max) if min && max
end
+ schema_primitive = schema[:primitive]
+
+ if schema_primitive == 'NVARCHAR'
+ if property.length <= 4000
+ schema[:length] = property.length
+ else
+ schema[:length] = 'max'
+ end
+ end
+
if schema[:primitive] == 'TEXT'
schema.delete(:default)
end
@@ -163,11 +172,13 @@ def type_map
scale = Property::Decimal.scale
super.merge(
- DateTime => { :primitive => 'DATETIME' },
- Date => { :primitive => 'SMALLDATETIME' },
- Time => { :primitive => 'SMALLDATETIME' },
- TrueClass => { :primitive => 'BIT', },
- Property::Text => { :primitive => 'NVARCHAR', :length => 'max' }
+ DateTime => { :primitive => 'DATETIME' },
+ Date => { :primitive => 'SMALLDATETIME' },
+ Time => { :primitive => 'SMALLDATETIME' },
+ TrueClass => { :primitive => 'BIT', },
+ String => { :primitive => 'NVARCHAR', :length => length },
+ Property::Text => { :primitive => 'NVARCHAR', :length => 'max' },
+ Property::Binary => { :primitive => 'VARBINARY', :length => 'max' }
).freeze
end
end
View
8 lib/dm-migrations/migration.rb
@@ -289,9 +289,11 @@ def setup!
@adapter = DataMapper.repository(@repository).adapter
case @adapter.class.name
- when /Sqlite/ then @adapter.extend(SQL::Sqlite)
- when /Mysql/ then @adapter.extend(SQL::Mysql)
- when /Postgres/ then @adapter.extend(SQL::Postgres)
+ when /Sqlite/ then @adapter.extend(SQL::Sqlite)
+ when /Mysql/ then @adapter.extend(SQL::Mysql)
+ when /Postgres/ then @adapter.extend(SQL::Postgres)
+ when /Sqlserver/ then @adapter.extend(SQL::Sqlserver)
+ when /Oracle/ then @adapter.extend(SQL::Oracle)
else
raise(RuntimeError,"Unsupported Migration Adapter #{@adapter.class}",caller)
end
View
2  lib/dm-migrations/sql.rb
@@ -3,3 +3,5 @@
require 'dm-migrations/sql/sqlite'
require 'dm-migrations/sql/mysql'
require 'dm-migrations/sql/postgres'
+require 'dm-migrations/sql/sqlserver'
+require 'dm-migrations/sql/oracle'
View
11 lib/dm-migrations/sql/oracle.rb
@@ -0,0 +1,11 @@
+require 'dm-migrations/sql/table'
+
+module SQL
+ module Oracle
+
+ def change_column_type_statement(name, column)
+ "ALTER TABLE #{quote_name(name)} MODIFY ( #{column.to_sql} )"
+ end
+
+ end
+end
View
11 lib/dm-migrations/sql/sqlserver.rb
@@ -0,0 +1,11 @@
+require 'dm-migrations/sql/table'
+
+module SQL
+ module Sqlserver
+
+ def change_column_type_statement(name, column)
+ "ALTER TABLE #{quote_name(name)} ALTER COLUMN #{column.to_sql}"
+ end
+
+ end
+end
Something went wrong with that request. Please try again.