Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created December 29, 2020 12:13
Show Gist options
  • Save alpaca-tc/f4335960b312e026808712a7f43a7947 to your computer and use it in GitHub Desktop.
Save alpaca-tc/f4335960b312e026808712a7f43a7947 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~> 6.1.0'
gem "sqlite3"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
db = Tempfile.new(['db', '.db'])
ActiveRecord::Base.configurations = {
primary: { adapter: "sqlite3", database: db },
primary_replica: { adapter: "sqlite3", database: db }
}
ActiveRecord::Base.legacy_connection_handling = false
ActiveRecord::Base.establish_connection(:primary)
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
end
end
ActiveRecord::Base.connects_to(
database: {
writing: :primary,
reading: :primary_replica
}
)
class BaseRecord < ActiveRecord::Base
self.abstract_class = true
end
class User < BaseRecord
end
class BugTest < Minitest::Test
include ActiveRecord::TestFixtures
def test_readonly
BaseRecord.connected_to(role: :reading) do
# expected true, got false
assert User.connection.preventing_writes?
# ActiveRecord::ReadOnlyError expected but nothing was raised.
assert_raises(ActiveRecord::ReadOnlyError) do
User.create!
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment