Class: ActiveRecord::Tasks::SQLServerDatabaseTasks

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record/tasks/sqlserver_database_tasks.rb

Constant Summary collapse

DEFAULT_COLLATION =
"SQL_Latin1_General_CP1_CI_AS"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(configuration) ⇒ SQLServerDatabaseTasks

Returns a new instance of SQLServerDatabaseTasks.



19
20
21
22
# File 'lib/active_record/tasks/sqlserver_database_tasks.rb', line 19

def initialize(configuration)
  @configuration = configuration
  @configuration_hash = @configuration.configuration_hash
end

Class Method Details

.using_database_configurations?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/active_record/tasks/sqlserver_database_tasks.rb', line 15

def self.using_database_configurations?
  true
end

Instance Method Details

#charsetObject



43
44
45
# File 'lib/active_record/tasks/sqlserver_database_tasks.rb', line 43

def charset
  with_connection { |connection| connection.charset }
end

#clear_active_connections!Object



57
58
59
# File 'lib/active_record/tasks/sqlserver_database_tasks.rb', line 57

def clear_active_connections!
  ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
end

#collationObject



47
48
49
# File 'lib/active_record/tasks/sqlserver_database_tasks.rb', line 47

def collation
  with_connection { |connection| connection.collation }
end

#create(master_established = false) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
# 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

#dropObject



38
39
40
41
# 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

#purgeObject



51
52
53
54
55
# 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) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



89
90
91
92
93
# 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