Skip to content

Commit

Permalink
Do not define any method on TOPLEVEL
Browse files Browse the repository at this point in the history
  • Loading branch information
amatsuda committed Jan 14, 2025
1 parent fec92b9 commit 4aec189
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions test/fake_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@
require 'active_record/railtie'

module DatabaseRewinderTestApp
class << self
def borrow_connection(name)
connection_handler = ActiveRecord::Base.establish_connection(name)

if ActiveRecord.gem_version >= '7.2'
connection_handler.lease_connection
else
connection_handler.connection
end
end
end

Application = Class.new(Rails::Application) do
# Rais.root
config.root = __dir__
Expand All @@ -19,18 +31,8 @@ module DatabaseRewinderTestApp

require 'active_record/base'

def borrow_connection(name)
connection_handler = ActiveRecord::Base.establish_connection(name)

if ActiveRecord.gem_version >= '7.2'
connection_handler.lease_connection
else
connection_handler.connection
end
end

if ENV['DB'] == 'postgresql'
borrow_connection(:superuser_connection).execute(<<-CREATE_ROLE_SQL)
DatabaseRewinderTestApp.borrow_connection(:superuser_connection).execute(<<-CREATE_ROLE_SQL)
DO
$do$
BEGIN
Expand Down Expand Up @@ -66,7 +68,7 @@ def self.up
create_table(:foos) {|t| t.string :name; t.references :bar, foreign_key: true }
create_table(:bazs) {|t| t.string :name }

test2_connection = borrow_connection(:test2)
test2_connection = DatabaseRewinderTestApp.borrow_connection(:test2)
test2_connection.create_table(:quus) {|t| t.string :name }
ActiveRecord::Base.establish_connection :test
end
Expand All @@ -76,7 +78,7 @@ def self.down
drop_table(:bars) {|t| t.string :name }
drop_table(:bazs) {|t| t.string :name }

test2_connection = borrow_connection(:test2)
test2_connection = DatabaseRewinderTestApp.borrow_connection(:test2)
test2_connection.drop_table :quus
ActiveRecord::Base.establish_connection :test
end
Expand Down

0 comments on commit 4aec189

Please sign in to comment.