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 43
def charset
  with_connection { |connection| connection.charset }
end
clear_active_connections!() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 57
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 47
def collation
  with_connection { |connection| 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
  with_connection do |connection|
    connection.create_database(configuration.database, configuration_hash.merge(collation: default_collation))
  end
  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 38
def drop
  establish_master_connection
  with_connection { |connection| connection.drop_database(configuration.database) }
end
purge() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 51
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 61
def structure_dump(filename, _extra_flags)
  with_connection do |connection|
    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
end
structure_load(filename, _extra_flags) click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 89
def structure_load(filename, _extra_flags)
  with_connection do |connection|
    connection.execute File.read(filename)
  end
end

Private Instance Methods

default_collation() click to toggle source
# File lib/active_record/tasks/sqlserver_database_tasks.rb, line 99
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 103
def establish_master_connection
  establish_connection configuration_hash.merge(database: "master")
end