Skip to content

Created ActiveRecord::Tasks::SQLServerDatabaseTasks class #366

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,6 @@ test/profile/output/*
.idea
coverage/*
.flooignore
.floo
.floo
.ruby-gemset
.ruby-version
2 changes: 1 addition & 1 deletion RUNNING_UNIT_TESTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ The tests of this adapter depend on the existence of the Rails which under the 3
$ git clone git://github.com/rails-sqlserver/activerecord-sqlserver-adapter.git
```

Optionally, you an just let bundler do all the work and assuming there is a git tag for the Rails version, you can set `RAILS_VERSION` before bundling.
Optionally, you can just let bundler do all the work and assuming there is a git tag for the Rails version, you can set `RAILS_VERSION` before bundling.

```
$ export RAILS_VERSION='3.2.13'
Expand Down
62 changes: 62 additions & 0 deletions lib/active_record/tasks/sqlserver_database_tasks.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
module ActiveRecord
module Tasks # :nodoc:
class SQLServerDatabaseTasks # :nodoc:
delegate :connection, :establish_connection, to: ActiveRecord::Base

def initialize(configuration)
@configuration = configuration
end

def create(master_established = false)
establish_master_connection unless master_established
connection.create_database configuration['database']
establish_connection configuration

rescue ActiveRecord::StatementInvalid => error
if /Database .* already exists/ === error.message
raise DatabaseAlreadyExists
else
raise
end
end

def drop(master_established = false)
establish_master_connection unless master_established
connection.drop_database configuration['database']
establish_connection configuration
end

def purge
establish_connection configuration
connection.recreate_database
end

private

def configuration
@configuration
end

def creation_options
Hash.new.tap do |options|
options[:charset] = configuration['encoding'] if configuration.include? 'encoding'
options[:collation] = configuration['collation'] if configuration.include? 'collation'

# Set default charset only when collation isn't set.
options[:charset] ||= DEFAULT_CHARSET unless options[:collation]

# Set default collation only when charset is also default.
options[:collation] ||= DEFAULT_COLLATION if options[:charset] == DEFAULT_CHARSET
end
end

def establish_master_connection
establish_connection configuration.merge(
'database' => 'master'
)
end

ActiveRecord::Tasks::DatabaseTasks.register_task(/sqlserver/, ActiveRecord::Tasks::SQLServerDatabaseTasks)
end
end
end
1 change: 1 addition & 0 deletions lib/activerecord-sqlserver-adapter.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
require 'active_record/connection_adapters/sqlserver_adapter'
require 'active_record/tasks/sqlserver_database_tasks'
5 changes: 5 additions & 0 deletions test/cases/associations_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
require 'cases/sqlserver_helper'
require 'models/owner'
require 'models/pet'
require 'models/toy'

class HasManyThroughAssociationsTest < ActiveRecord::TestCase
COERCED_TESTS = [:test_has_many_through_obeys_order_on_through_association]
# Rails does not do a case-insensive comparison
# Until that patch is made to rails we are preventing this test from running in this gem.

include SqlserverCoercedTest

fixtures :owners, :pets, :toys

def test_coerced_has_many_through_obeys_order_on_through_association
owner = owners(:blackbeard)
# assert owner.toys.to_sql.include?("pets.name desc") # What's currently in rails
Expand Down
4 changes: 4 additions & 0 deletions test/cases/resolver_test_sqlserver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ class ResolverTest < ActiveRecord::TestCase

include SqlserverCoercedTest

def resolve(spec, config={})
Resolver.new(config).resolve(spec)
end

COERCED_TESTS = [
:test_url_host_no_db,
:test_url_host_db,
Expand Down
58 changes: 58 additions & 0 deletions test/cases/tasks/sqlserver_rake_test_sqlserver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
require 'cases/helper'
require(Dir.pwd + '/lib/active_record/tasks/sqlserver_database_tasks.rb')

module ActiveRecord
class SQLServerDropTest < ActiveRecord::TestCase
def setup
@connection = stub(:drop_database => true)
@configuration = {
'adapter' => 'sqlserver',
'database' => 'activerecord_unittest'
}

ActiveRecord::Base.stubs(:connection).returns(@connection)
ActiveRecord::Base.stubs(:establish_connection).returns(true)
ActiveRecord::Tasks::DatabaseTasks.register_task(/sqlserver/, ActiveRecord::Tasks::SQLServerDatabaseTasks)
end

def test_establishes_connection_to_sqlserver_database
ActiveRecord::Base.expects(:establish_connection).with @configuration

ActiveRecord::Tasks::DatabaseTasks.drop @configuration
end

def test_drops_database
@connection.expects(:drop_database).with('activerecord_unittest')

ActiveRecord::Tasks::DatabaseTasks.drop @configuration
end
end

class SQLServerPurgeTest < ActiveRecord::TestCase
def setup
@connection = stub(:recreate_database => true)
@configuration = {
'adapter' => 'sqlserver',
'database' => 'activerecord_unittest'
}

ActiveRecord::Base.stubs(:connection).returns(@connection)
ActiveRecord::Base.stubs(:establish_connection).returns(true)
ActiveRecord::Tasks::DatabaseTasks.register_task(/sqlserver/, ActiveRecord::Tasks::SQLServerDatabaseTasks)
end

def test_establishes_connection_to_test_database
ActiveRecord::Base.expects(:establish_connection).with(@configuration)

ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end

def test_recreates_database
@connection.expects(:recreate_database)

ActiveRecord::Tasks::DatabaseTasks.purge @configuration
end

end

end
Empty file modified test/profile/query_plan_complex.rb
100755 → 100644
Empty file.
Empty file modified test/profile/query_plan_simple.rb
100755 → 100644
Empty file.