Skip to content

Commit

Permalink
Add support for multi db setups with a different adapter
Browse files Browse the repository at this point in the history
I was implementing solid_cable using SQLite as an adapter and came across this issue, where the schema dumper was trying to dump the functions and triggers for the SQLite solid cable schema. Obviously, this isn't possible.

I've modified the code to hook into the `ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper` instead of the generic `ActiveRecord::SchemaDumper` and added a test

This should also fix teoljungberg#155
  • Loading branch information
manuelvanrijn committed Nov 26, 2024
1 parent 6dbcc0f commit 812f5ef
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 1 deletion.
2 changes: 2 additions & 0 deletions fx.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,7 @@ Gem::Specification.new do |spec|
spec.add_dependency "activerecord", ">= 7.0"
spec.add_dependency "railties", ">= 7.0"

spec.add_development_dependency "sqlite3"

spec.required_ruby_version = ">= 3.0"
end
2 changes: 1 addition & 1 deletion lib/fx.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module Fx
def self.load
ActiveRecord::Migration::CommandRecorder.include(Fx::CommandRecorder)
ActiveRecord::ConnectionAdapters::AbstractAdapter.include(Fx::Statements)
ActiveRecord::SchemaDumper.prepend(Fx::SchemaDumper)
ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend(Fx::SchemaDumper)

true
end
Expand Down
34 changes: 34 additions & 0 deletions spec/fx/schema_dumper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,40 @@
expect(output).to include("EXECUTE FUNCTION uppercase_users_name()")
end

context "with a sqlite adapter" do
let(:sqlite_connection) do
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.connection
end
after :each do
sqlite_connection.disconnect!
end

it "does not dump functions or triggers for SQLite connections" do
sql_definition = <<~EOS
CREATE OR REPLACE FUNCTION my_function()
RETURNS text AS $$
BEGIN
RETURN 'test';
END;
$$ LANGUAGE plpgsql;
EOS
connection.create_function :my_function, sql_definition: sql_definition
connection.create_table :my_table
postgres_stream = StringIO.new
sqlite_stream = StringIO.new

dump(connection: connection, stream: postgres_stream)
dump(connection: sqlite_connection, stream: sqlite_stream)

output = postgres_stream.string

expect(output).to include("create_function :my_function")
expect(output).to include("RETURN 'test';")
expect(output).to include('create_table "my_table"')
end
end

def dump(connection:, stream:)
if Rails.version >= "7.2"
ActiveRecord::SchemaDumper.dump(connection.pool, stream)
Expand Down

0 comments on commit 812f5ef

Please sign in to comment.