-
-
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
This file contains 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
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