class ActiveRecord::Tasks::SQLServerDatabaseTasks

Constants

DEFAULT_COLLATION

Attributes

configuration[R]
configuration_hash[R]

Public Class Methods

new(configuration) click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 19
def initialize(configuration)
  @configuration = configuration
  @configuration_hash = @configuration.configuration_hash
end
using_database_configurations?() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 15
def self.using_database_configurations?
  true
end

Public Instance Methods

charset() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 41
def charset
  connection.charset
end
clear_active_connections!() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 55
def clear_active_connections!
  ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
end
collation() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 45
def collation
  connection.collation
end
create(master_established = false) click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 24
def create(master_established = false)
  establish_master_connection unless master_established
  connection.create_database configuration.database, configuration_hash.merge(collation: default_collation)
  establish_connection configuration
rescue ActiveRecord::StatementInvalid => e
  if /database .* already exists/i === e.message
    raise DatabaseAlreadyExists
  else
    raise
  end
end
drop() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 36
def drop
  establish_master_connection
  connection.drop_database configuration.database
end
purge() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 49
def purge
  clear_active_connections!
  drop
  create true
end
structure_dump(filename, extra_flags) click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 59
def structure_dump(filename, extra_flags)
  server_arg = "-S #{Shellwords.escape(configuration_hash[:host])}"
  server_arg += ":#{Shellwords.escape(configuration_hash[:port])}" if configuration_hash[:port]
  command = [
    "defncopy-ttds",
    server_arg,
    "-D #{Shellwords.escape(configuration_hash[:database])}",
    "-U #{Shellwords.escape(configuration_hash[:username])}",
    "-P #{Shellwords.escape(configuration_hash[:password])}",
    "-o #{Shellwords.escape(filename)}",
  ]
  table_args = connection.tables.map { |t| Shellwords.escape(t) }
  command.concat(table_args)
  view_args = connection.views.map { |v| Shellwords.escape(v) }
  command.concat(view_args)
  raise "Error dumping database" unless Kernel.system(command.join(" "))

  dump = File.read(filename)
  dump.gsub!(/^USE .*$\nGO\n/, "")                      # Strip db USE statements
  dump.gsub!(/^GO\n/, "")                               # Strip db GO statements
  dump.gsub!(/nvarchar\(8000\)/, "nvarchar(4000)")      # Fix nvarchar(8000) column defs
  dump.gsub!(/nvarchar\(-1\)/, "nvarchar(max)")         # Fix nvarchar(-1) column defs
  dump.gsub!(/text\(\d+\)/, "text")                     # Fix text(16) column defs
  File.open(filename, "w") { |file| file.puts dump }
end
structure_load(filename, extra_flags) click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 85
def structure_load(filename, extra_flags)
  connection.execute File.read(filename)
end

Private Instance Methods

default_collation() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 93
def default_collation
  configuration_hash[:collation] || DEFAULT_COLLATION
end
establish_master_connection() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 97
def establish_master_connection
  establish_connection configuration_hash.merge(database: "master")
end