Created
February 18, 2022 16:32
-
-
Save KDGundermann/13f0123ca7a0562cc69a3c7ca14c4a86 to your computer and use it in GitHub Desktop.
SQL Server Adapter: Using non-dbo schema prefix on table or view
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "tiny_tds" | |
gem "activerecord-sqlserver-adapter", "6.1" | |
gem 'byebug' | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
require 'byebug' | |
ActiveRecord::Base.establish_connection( | |
adapter: "sqlserver", | |
timeout: 5000, | |
pool: 100, | |
encoding: "utf8", | |
database: "activerecord_unittest", | |
username: "rails", | |
password: "", | |
host: "localhost", | |
port: 1433, | |
) | |
ActiveRecord::Base.logger = Logger.new(STDOUT) | |
ActiveRecord::Schema.define do | |
connection.execute "CREATE SCHEMA dbo1;" rescue nil | |
connection.execute "DROP TABLE dbo1.accounts;" rescue nil | |
connection.execute "CREATE TABLE dbo1.[accounts] ([id] bigint NOT NULL IDENTITY(1,1) PRIMARY KEY, [name] nvarchar(4000), [active] bit)" rescue nil | |
connection.execute "CREATE SCHEMA dbo2;" rescue nil | |
connection.execute "DROP VIEW dbo2.[active_accounts];" rescue nil | |
connection.execute "CREATE VIEW dbo2.[active_accounts] AS SELECT id AS account_id, name AS account_name, active FROM dbo1.accounts WHERE active = 1;" rescue nil | |
connection.execute "IF NOT EXISTS (SELECT name FROM master.sys.server_principals WHERE name = 'activerecordtestuser') CREATE LOGIN [activerecordtestuser] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [activerecord_unittest];" | |
connection.execute "USE [activerecord_unittest]; DROP USER IF EXISTS [activerecordtestuser]; CREATE USER [activerecordtestuser] FOR LOGIN [activerecordtestuser] WITH DEFAULT_SCHEMA = dbo2; GRANT SELECT, INSERT, UPDATE, DELETE ON SCHEMA :: dbo2 TO [activerecordtestuser] GRANT VIEW DEFINITION TO [activerecordtestuser];" | |
end | |
class Account < ActiveRecord::Base | |
self.table_name = "active_accounts" | |
self.table_name_prefix = 'dbo2.' | |
end | |
ActiveRecord::Base.establish_connection( | |
adapter: "sqlserver", | |
timeout: 5000, | |
pool: 100, | |
encoding: "utf8", | |
database: "activerecord_unittest", | |
username: "activerecordtestuser", | |
password: "", | |
host: "localhost", | |
port: 1433, | |
) | |
class AccountTest < Minitest::Test | |
def setup | |
@bug_test = Account.create!(account_name: 'Small Corp', active: true) | |
@bug_test = Account.create!(account_name: 'Middle Corp', active: false) | |
@bug_test = Account.create!(account_name: 'Big Corp', active: true) | |
end | |
def test_count | |
default_schema = Account.connection.select_one('SELECT SCHEMA_NAME() AS default_schema')['default_schema'] | |
assert_equal 'dbo2', default_schema | |
# Model based on view in 'dbo2' schema. | |
assert_equal 2, Account.count | |
assert_equal Account.all.map(&:account_name).sort, ["Big Corp", "Small Corp"] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment