activerecord-sqlserver-adapter-schemas 1.0.1 → 1.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/active_record/schemas/connection_adapters/sqlserver_adapter.rb +0 -24
- data/lib/activerecord-sqlserver-adapter-schemas.rb +24 -6
- data/test/schemas/connection.rb +28 -0
- data/test/schemas/definitions.rb +49 -0
- data/test/schemas/quoting.rb +24 -0
- data/test/schemas/table_checks.rb +59 -0
- data/test/schemas/unqualify.rb +43 -0
- metadata +7 -2
@@ -3,15 +3,6 @@ module Schemas
|
|
3
3
|
module ConnectionAdapters
|
4
4
|
|
5
5
|
module SqlserverAdapter
|
6
|
-
|
7
|
-
# When this module is included, the following methods are chained in order
|
8
|
-
# to strip off the schema names if the schema is the default schema
|
9
|
-
def self.included(base)
|
10
|
-
base.alias_method_chain :columns, :default_schema_check
|
11
|
-
base.alias_method_chain :table_exists?, :default_schema_check
|
12
|
-
base.alias_method_chain :table_name_or_views_table_name, :default_schema_check
|
13
|
-
end
|
14
|
-
|
15
6
|
def default_schema
|
16
7
|
unless sqlserver_2000?
|
17
8
|
@default_schema ||= select_values("SELECT default_schema_name FROM sys.database_principals WHERE type = 'S' and name = '#{self.quote_string(@connection_options[:username])}'").first
|
@@ -20,21 +11,6 @@ module SqlserverAdapter
|
|
20
11
|
end
|
21
12
|
attr_writer :default_schema
|
22
13
|
|
23
|
-
def table_name_or_views_table_name_with_default_schema_check(table_name)
|
24
|
-
table_name = unqualify_table_name_if_default_schema(table_name)
|
25
|
-
table_name_or_views_table_name_without_default_schema_check(table_name)
|
26
|
-
end
|
27
|
-
|
28
|
-
def table_exists_with_default_schema_check?(table_name)
|
29
|
-
table_name = unqualify_table_name_if_default_schema(table_name)
|
30
|
-
table_exists_without_default_schema_check?(table_name)
|
31
|
-
end
|
32
|
-
|
33
|
-
def columns_with_default_schema_check(table_name, column_name = nil)
|
34
|
-
table_name = unqualify_table_name_if_default_schema(table_name) if table_name
|
35
|
-
columns_without_default_schema_check(table_name, column_name)
|
36
|
-
end
|
37
|
-
|
38
14
|
def unqualify_schema_name(table_name)
|
39
15
|
parts = table_name.to_s.split('.')
|
40
16
|
parts.length == 1 || parts[parts.length - 2].blank? ? default_schema : parts[parts.length - 2].gsub(/[\[\]]/,'')
|
@@ -12,6 +12,16 @@ end
|
|
12
12
|
class ActiveRecord::ConnectionAdapters::SQLServerAdapter
|
13
13
|
include ActiveRecord::Schemas::ConnectionAdapters::SqlserverAdapter
|
14
14
|
|
15
|
+
def table_exists?(table_name)
|
16
|
+
table_name = unqualify_table_name_if_default_schema(table_name)
|
17
|
+
super(table_name) || tables.include?(table_name) || views.include?(table_name)
|
18
|
+
end
|
19
|
+
|
20
|
+
def table_name_or_views_table_name(table_name)
|
21
|
+
unquoted_table_name = unqualify_table_name_if_default_schema(table_name)
|
22
|
+
views.include?(unquoted_table_name) ? view_table_name(unquoted_table_name) : unquoted_table_name
|
23
|
+
end
|
24
|
+
|
15
25
|
# This method is overridden to support linked servers
|
16
26
|
def unqualify_db_name(table_name)
|
17
27
|
table_names = table_name.to_s.split('.')
|
@@ -31,12 +41,20 @@ class ActiveRecord::ConnectionAdapters::SQLServerAdapter
|
|
31
41
|
# This method is overridden to support schema names
|
32
42
|
def views(name = nil)
|
33
43
|
# return schema.view unless the schema is the default schema, in which case just return view
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
44
|
+
info_schema_query do
|
45
|
+
select_values("SELECT TABLE_SCHEMA + '.' + TABLE_NAME FROM INFORMATION_SCHEMA.VIEWS WHERE TABLE_NAME NOT IN ('sysconstraints','syssegments')").collect do |view|
|
46
|
+
default_schema && view.index("#{default_schema}.") == 0 ? view[default_schema.length + 1..view.length] : view
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def columns(table_name, name = nil)
|
52
|
+
return [] if table_name.blank?
|
53
|
+
cache_key = unqualify_table_name_if_default_schema(table_name)
|
54
|
+
@sqlserver_columns_cache[cache_key] ||= column_definitions(table_name).collect do |ci|
|
55
|
+
sqlserver_options = ci.except(:name,:default_value,:type,:null).merge(:database_year=>database_year)
|
56
|
+
::ActiveRecord::ConnectionAdapters::SQLServerColumn.new ci[:name], ci[:default_value], ci[:type], ci[:null], sqlserver_options
|
57
|
+
end
|
40
58
|
end
|
41
59
|
|
42
60
|
# This method is overridden to support references such as database..table
|
@@ -0,0 +1,28 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'active_record'
|
3
|
+
require 'activerecord-sqlserver-adapter'
|
4
|
+
require File.join(File.dirname(__FILE__), '..', '..', 'lib', 'activerecord-sqlserver-adapter-schemas')
|
5
|
+
|
6
|
+
ActiveRecord::Base.configurations = {
|
7
|
+
'arunit' => {
|
8
|
+
:adapter => 'sqlserver',
|
9
|
+
:mode => 'ODBC',
|
10
|
+
:host => 'localhost',
|
11
|
+
:username => 'rails',
|
12
|
+
:dsn => ENV['ACTIVERECORD_UNITTEST_DSN'] || 'activerecord_unittest',
|
13
|
+
:database => 'activerecord_unittest'
|
14
|
+
}
|
15
|
+
}
|
16
|
+
ActiveRecord::Base.establish_connection 'arunit'
|
17
|
+
ActiveRecord::Base.connection.default_schema = 'dbo'
|
18
|
+
|
19
|
+
unless ActiveRecord::Base.connection.table_exists?('schema_checks')
|
20
|
+
ActiveRecord::Base.connection.create_table 'schema_checks' do |table|
|
21
|
+
table.string :name, :limit => 32
|
22
|
+
table.timestamps
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
unless ActiveRecord::Base.connection.table_exists?('schema_checks_view')
|
27
|
+
ActiveRecord::Base.connection.execute "create view schema_checks_view as select * from schema_checks"
|
28
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'test/unit'
|
3
|
+
require File.join(File.dirname(__FILE__), 'connection')
|
4
|
+
|
5
|
+
module Schemas
|
6
|
+
|
7
|
+
class Definitions < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
con.default_schema = 'dbo'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_column_definitions
|
13
|
+
assert con.column_definitions('schema_checks').length.nonzero?
|
14
|
+
assert con.column_definitions('dbo.schema_checks').length.nonzero?
|
15
|
+
assert con.column_definitions('activerecord_unittest..schema_checks').length.nonzero?
|
16
|
+
assert con.column_definitions('activerecord_unittest.dbo.schema_checks').length.nonzero?
|
17
|
+
assert con.column_definitions('foo.schema_checks').length.zero?
|
18
|
+
|
19
|
+
con.default_schema = 'foo'
|
20
|
+
assert con.column_definitions('schema_checks').length.zero?
|
21
|
+
assert con.column_definitions('dbo.schema_checks').length.nonzero?
|
22
|
+
assert con.column_definitions('activerecord_unittest..schema_checks').length.zero?
|
23
|
+
assert con.column_definitions('activerecord_unittest.dbo.schema_checks').length.nonzero?
|
24
|
+
assert con.column_definitions('activerecord_unittest.foo.schema_checks').length.zero?
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_view_information
|
28
|
+
assert_nil con.view_information 'foo.schema_checks_view'
|
29
|
+
assert_not_nil con.view_information 'dbo.schema_checks_view'
|
30
|
+
assert_not_nil con.view_information 'schema_checks_view'
|
31
|
+
assert_not_nil con.view_information 'activerecord_unittest.dbo.schema_checks_view'
|
32
|
+
assert_not_nil con.view_information 'activerecord_unittest..schema_checks_view'
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_columns
|
36
|
+
assert con.columns('schema_checks').length.nonzero?
|
37
|
+
assert con.columns('dbo.schema_checks').length.nonzero?
|
38
|
+
assert con.columns('foo.schema_checks').length.zero?
|
39
|
+
assert con.columns('activerecord_unittest..schema_checks').length.nonzero?
|
40
|
+
assert con.columns('activerecord_unittest.dbo.schema_checks').length.nonzero?
|
41
|
+
assert con.columns('activerecord_unittest.foo.schema_checks').length.zero?
|
42
|
+
end
|
43
|
+
|
44
|
+
def con
|
45
|
+
con = ActiveRecord::Base.connection
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'test/unit'
|
3
|
+
require File.join(File.dirname(__FILE__), 'connection')
|
4
|
+
|
5
|
+
module Schemas
|
6
|
+
|
7
|
+
class Quoting < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
con.default_schema = 'dbo'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_quote_column_name
|
13
|
+
assert_equal con.quote_column_name('schema_checks'), '[schema_checks]'
|
14
|
+
assert_equal con.quote_column_name('dbo.schema_checks'), '[dbo].[schema_checks]'
|
15
|
+
assert_equal con.quote_column_name('activerecord_unittest..schema_checks'), '[activerecord_unittest]..[schema_checks]'
|
16
|
+
assert_equal con.quote_column_name('activerecord_unittest.dbo.schema_checks'), '[activerecord_unittest].[dbo].[schema_checks]'
|
17
|
+
end
|
18
|
+
|
19
|
+
def con
|
20
|
+
con = ActiveRecord::Base.connection
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'test/unit'
|
3
|
+
require File.join(File.dirname(__FILE__), 'connection')
|
4
|
+
|
5
|
+
module Schemas
|
6
|
+
|
7
|
+
class TableChecks < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
con.default_schema = 'dbo'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_tables
|
13
|
+
# test that tables() prefixes the table name with the schema if the schema is not the default schema
|
14
|
+
assert_nil con.tables.find { |table| table.start_with?('dbo.') }
|
15
|
+
con.default_schema = 'foo'
|
16
|
+
assert_not_nil con.tables.find { |table| table.start_with?('dbo.') }
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_views
|
20
|
+
# test that views() prefixes the table name with the schema if the schema is not the default schema
|
21
|
+
assert_nil con.views.find { |table| table.start_with?('dbo.') }
|
22
|
+
con.default_schema = 'foo'
|
23
|
+
assert_not_nil con.views.find { |table| table.start_with?('dbo.') }
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_table_exists
|
27
|
+
assert con.table_exists?('dbo.schema_checks')
|
28
|
+
assert con.table_exists?('schema_checks')
|
29
|
+
assert !con.table_exists?('foo.schema_checks')
|
30
|
+
|
31
|
+
con.default_schema = 'foo'
|
32
|
+
assert con.table_exists?('dbo.schema_checks')
|
33
|
+
assert !con.table_exists?('schema_checks')
|
34
|
+
assert !con.table_exists?('foo.schema_checks')
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_table_name_or_views_table_name
|
38
|
+
assert_equal con.table_name_or_views_table_name('schema_checks'), 'schema_checks'
|
39
|
+
assert_equal con.table_name_or_views_table_name('dbo.schema_checks'), 'schema_checks'
|
40
|
+
assert_equal con.table_name_or_views_table_name('activerecord_unittest.dbo.schema_checks'), 'schema_checks'
|
41
|
+
assert_equal con.table_name_or_views_table_name('activerecord_unittest..schema_checks'), 'schema_checks'
|
42
|
+
assert_equal con.table_name_or_views_table_name('activerecord_unittest.foo.schema_checks'), 'activerecord_unittest.foo.schema_checks'
|
43
|
+
assert_equal con.table_name_or_views_table_name('foo.schema_checks'), 'foo.schema_checks'
|
44
|
+
|
45
|
+
con.default_schema = 'foo'
|
46
|
+
assert_equal con.table_name_or_views_table_name('schema_checks'), 'schema_checks'
|
47
|
+
assert_equal con.table_name_or_views_table_name('dbo.schema_checks'), 'dbo.schema_checks'
|
48
|
+
assert_equal con.table_name_or_views_table_name('activerecord_unittest.dbo.schema_checks'), 'activerecord_unittest.dbo.schema_checks'
|
49
|
+
assert_equal con.table_name_or_views_table_name('activerecord_unittest..schema_checks'), 'schema_checks'
|
50
|
+
assert_equal con.table_name_or_views_table_name('activerecord_unittest.foo.schema_checks'), 'schema_checks'
|
51
|
+
assert_equal con.table_name_or_views_table_name('foo.schema_checks'), 'schema_checks'
|
52
|
+
end
|
53
|
+
|
54
|
+
def con
|
55
|
+
con = ActiveRecord::Base.connection
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require 'test/unit'
|
3
|
+
require File.join(File.dirname(__FILE__), 'connection')
|
4
|
+
|
5
|
+
module Schemas
|
6
|
+
|
7
|
+
class Unqualify < Test::Unit::TestCase
|
8
|
+
def setup
|
9
|
+
con.default_schema = 'dbo'
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_unqualify_schema_name
|
13
|
+
assert_equal con.unqualify_schema_name('MyDatabase.foo.my_table'), 'foo'
|
14
|
+
assert_equal con.unqualify_schema_name('MyDatabase..my_table'), 'dbo'
|
15
|
+
assert_equal con.unqualify_schema_name('foo.MyTable'), 'foo'
|
16
|
+
assert_equal con.unqualify_schema_name('MyTable'), 'dbo'
|
17
|
+
assert_equal con.unqualify_schema_name('LinkedServer.MyDatabase.foo.my_table'), 'foo'
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_unqualify_table_name_if_default_schema
|
21
|
+
assert_equal con.unqualify_table_name_if_default_schema('my_table'), 'my_table'
|
22
|
+
|
23
|
+
assert_equal con.unqualify_table_name_if_default_schema('dbo.my_table'), 'my_table'
|
24
|
+
assert_equal con.unqualify_table_name_if_default_schema('foo.my_table'), 'foo.my_table'
|
25
|
+
|
26
|
+
# Are these next 3 tests really the correct behavior?
|
27
|
+
assert_equal con.unqualify_table_name_if_default_schema('MyDatabase..my_table'), 'my_table'
|
28
|
+
assert_equal con.unqualify_table_name_if_default_schema('LinkedServer.MyDatabase.dbo.my_table'), 'my_table'
|
29
|
+
assert_equal con.unqualify_table_name_if_default_schema('LinkedServer.MyDatabase.foo.my_table'), 'LinkedServer.MyDatabase.foo.my_table'
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_unqualify_db_name
|
33
|
+
assert_equal con.unqualify_db_name('LinkedServer.MyDatabase.dbo.my_table'), 'LinkedServer.MyDatabase'
|
34
|
+
assert_equal con.unqualify_db_name('MyDatabase.dbo.my_table'), 'MyDatabase'
|
35
|
+
assert_equal con.unqualify_db_name('MyDatabase..my_table'), 'MyDatabase'
|
36
|
+
end
|
37
|
+
|
38
|
+
def con
|
39
|
+
con = ActiveRecord::Base.connection
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-sqlserver-adapter-schemas
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Airlite Plastics
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2010-07-
|
12
|
+
date: 2010-07-09 00:00:00 -05:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -36,6 +36,11 @@ files:
|
|
36
36
|
- lib/activerecord-sqlserver-adapter-schemas.rb
|
37
37
|
- lib/active_record/schemas/base.rb
|
38
38
|
- lib/active_record/schemas/connection_adapters/sqlserver_adapter.rb
|
39
|
+
- test/schemas/connection.rb
|
40
|
+
- test/schemas/definitions.rb
|
41
|
+
- test/schemas/quoting.rb
|
42
|
+
- test/schemas/table_checks.rb
|
43
|
+
- test/schemas/unqualify.rb
|
39
44
|
has_rdoc: true
|
40
45
|
homepage:
|
41
46
|
licenses: []
|