From 816f37a2adde4449a17712cb0891a15f2c2ea7c0 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Fri, 28 Oct 2005 07:40:28 +0000 Subject: Added migration support to SQL Server adapter (please someone do the same for Oracle and DB2) (closes #2625) [Tom Ward] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2778 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- activerecord/lib/active_record/base.rb | 16 ++++++ .../abstract/schema_statements.rb | 14 +++-- .../connection_adapters/sqlserver_adapter.rb | 66 ++++++++++++++++++++++ 3 files changed, 90 insertions(+), 6 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index fa7ed9482a..d408173c01 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -707,6 +707,22 @@ module ActiveRecord #:nodoc: class_name end + # Indicates whether the table associated with this class exists + def table_exists? + if connection.respond_to?(:tables) + connection.tables.include? table_name + else + # if the connection adapter hasn't implemented tables, there are two crude tests that can be + # used - see if getting column info raises an error, or if the number of columns returned is zero + begin + reset_column_information + columns.size > 0 + rescue ActiveRecord::StatementInvalid + false + end + end + end + # Returns an array of column objects for the table associated with this class. def columns unless @columns diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb index 366192909e..6a6f368210 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb @@ -184,23 +184,25 @@ module ActiveRecord # remove_index :accounts, :column => :branch_id # Remove the index named by_branch_party in the accounts table. # remove_index :accounts, :name => :by_branch_party + def remove_index(table_name, options = {}) + execute "DROP INDEX #{index_name(table_name, options)} ON #{table_name}" + end + + def index_name(table_name, options) #:nodoc: if Hash === options # legacy support if options[:column] - index_name = "#{table_name}_#{options[:column]}_index" + "#{table_name}_#{options[:column]}_index" elsif options[:name] - index_name = options[:name] + options[:name] else raise ArgumentError, "You must specify the index name" end else - index_name = "#{table_name}_#{options}_index" + "#{table_name}_#{options}_index" end - - execute "DROP INDEX #{index_name} ON #{table_name}" end - # Returns a string of CREATE TABLE SQL statement(s) for recreating the # entire structure of the database. def structure_dump diff --git a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb index c6e82605ee..7f8933fbb4 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -13,6 +13,10 @@ require 'active_record/connection_adapters/abstract_adapter' # # Current maintainer: Ryan Tomayko # +# Modifications (Migrations): Tom Ward +# Date: 27/10/2005 +# + module ActiveRecord class Base def self.sqlserver_connection(config) #:nodoc: @@ -73,6 +77,7 @@ module ActiveRecord when :datetime then cast_to_datetime(value) when :timestamp then cast_to_time(value) when :time then cast_to_time(value) + when :boolean then value == true or (value =~ /^t(rue)?$/i) == 0 or value.to_s == '1' else value end end @@ -183,6 +188,10 @@ module ActiveRecord def adapter_name 'SQLServer' end + + def supports_migrations? #:nodoc: + true + end def select_all(sql, name = nil) select(sql, name) @@ -332,6 +341,63 @@ module ActiveRecord def create_database(name) execute "CREATE DATABASE #{name}" end + + def tables(name = nil) + tables = [] + execute("SELECT table_name from information_schema.tables WHERE table_type = 'BASE TABLE'", name).each {|field| tables << field[0]} + tables + end + + def indexes(table_name, name = nil) + indexes = [] + execute("EXEC sp_helpindex #{table_name}", name).each do |index| + unique = index[1] =~ /unique/ + primary = index[1] =~ /primary key/ + if !primary + indexes << IndexDefinition.new(table_name, index[0], unique, index[2].split(", ")) + end + end + indexes + end + + def rename_table(name, new_name) + execute "EXEC sp_rename '#{name}', '#{new_name}'" + end + + def remove_column(table_name, column_name) + execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name}" + end + + def rename_column(table, column, new_column_name) + execute "EXEC sp_rename '#{table}.#{column}', '#{new_column_name}'" + end + + def change_column(table_name, column_name, type, options = {}) #:nodoc: + sql_commands = ["ALTER TABLE #{table_name} ALTER COLUMN #{column_name} #{type_to_sql(type, options[:limit])}"] + if options[:default] + remove_default_constraint(table_name, column_name) + sql_commands << "ALTER TABLE #{table_name} ADD CONSTRAINT DF_#{table_name}_#{column_name} DEFAULT #{options[:default]} FOR #{column_name}" + end + sql_commands.each {|c| + execute(c) + } + end + + def remove_column(table_name, column_name) + remove_default_constraint(table_name, column_name) + execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name}" + end + + def remove_default_constraint(table_name, column_name) + defaults = select "select def.name from sysobjects def, syscolumns col, sysobjects tab where col.cdefault = def.id and col.name = '#{column_name}' and tab.name = '#{table_name}' and col.id = tab.id" + defaults.each {|constraint| + execute "ALTER TABLE #{table_name} DROP CONSTRAINT #{constraint["name"]}" + } + end + + def remove_index(table_name, options = {}) + execute "DROP INDEX #{table_name}.#{index_name(table_name, options)}" + end private def select(sql, name = nil) -- cgit v1.2.3