Skip to content

Instantly share code, notes, and snippets.

@daniel-rikowski
Last active July 13, 2016 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save daniel-rikowski/fea2a87dbab975bd5d72556f0a426e34 to your computer and use it in GitHub Desktop.
Save daniel-rikowski/fea2a87dbab975bd5d72556f0a426e34 to your computer and use it in GitHub Desktop.
Example script to demonstrate the delayed touch problem with schema search path switching
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'activerecord', '5.0.0'
gem 'pg'
end
require 'active_record'
require 'minitest/autorun'
require 'logger'
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test)
ActiveRecord::Base.establish_connection(adapter: 'postgresql',
database: 'bugreport', username: 'bugreport', password: 'bugreport') # UPDATE FOR YOUR DATABASE
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
connection.execute <<-SQL
DROP SCHEMA IF EXISTS tenant1 CASCADE;
CREATE SCHEMA tenant1;
SQL
connection.schema_search_path = 'public'
create_table :tenants, force: true do |t|
t.timestamps
end
connection.schema_search_path = 'tenant1'
create_table :posts, force: true do |t|
t.timestamps
end
create_table :comments, force: true do |t|
t.integer :post_id
t.timestamps
end
connection.schema_search_path = 'public'
end
class Tenant < ActiveRecord::Base
end
class Post < ActiveRecord::Base
has_many :comments
end
class Comment < ActiveRecord::Base
belongs_to :post, touch: true
end
class BugTest < Minitest::Test
def test_touch_with_schemas
Tenant.connection.schema_search_path = 'public'
Tenant.transaction do
Tenant.connection.schema_search_path = 'tenant1, public'
post = Post.create!
post.comments << Comment.create!
Tenant.connection.schema_search_path = 'public'
end # ActiveRecord::StatementInvalid: PG::UndefinedTable:
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment