Class: ActiveRecord::Tasks::SQLServerDatabaseTasks
- Inherits:
-
Object
- Object
- ActiveRecord::Tasks::SQLServerDatabaseTasks
- 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
- #charset ⇒ Object
- #clear_active_connections! ⇒ Object
- #collation ⇒ Object
- #create(master_established = false) ⇒ Object
- #drop ⇒ Object
-
#initialize(configuration) ⇒ SQLServerDatabaseTasks
constructor
A new instance of SQLServerDatabaseTasks.
- #purge ⇒ Object
- #structure_dump(filename, _extra_flags) ⇒ Object
- #structure_load(filename, _extra_flags) ⇒ Object
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
15 16 17 |
# File 'lib/active_record/tasks/sqlserver_database_tasks.rb', line 15 def self.using_database_configurations? true end |
Instance Method Details
#charset ⇒ Object
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 |
#collation ⇒ Object
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. raise DatabaseAlreadyExists else raise end end |
#drop ⇒ Object
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 |
#purge ⇒ Object
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 |