Skip to content

Comments

Refactor how credentials works to be a bit more agnostic#1135

Merged
jwoertink merged 2 commits intomainfrom
issues/415
Jan 19, 2026
Merged

Refactor how credentials works to be a bit more agnostic#1135
jwoertink merged 2 commits intomainfrom
issues/415

Conversation

@jwoertink
Copy link
Member

Ref #415

This PR starts the work to make Avram a little more agnostic. Supporting alternate DBs like MySQL, or SQLite would still present a very hefty challenge since there's not a lot of people that contribute to this library, and I only use postgres. However, we've already had a few issues where CockroachDB differs from postgres even though it's uses the postgres protocol. With this change, we can now at least do something like

if AppDatabase.settings.engine.cockroachdb?
  exec("cockroach query")
else
  exec("postgres query")
end

A ton of things would need massive refactoring and thought before trying to support any non-postgres related DB like the PG.connect_listen or using PQ::Notification , etc.. I'll worry about those later.

For now, the big benefit is Avram::Credentials is now just a thing wrapper around URI so we no longer need to handle complex building logic.

… new placeholder idea Engine to support alternate engines. We can now start changing code for Cockroach specific cases. Ref #415
@akadusei
Copy link
Contributor

akadusei commented Jan 19, 2026

Looks good. Can't the engine be detected automatically?:

def cockroachdb?
  with_connection do |connection|
    version = connection.scalar("SELECT version();").as(String)
    version.starts_with?("CockroachDB") # <= or .starts_with?("PostgreSQL") etc
  end
end

@jwoertink
Copy link
Member Author

It could, yeah, but wouldn't that need to happen a lot? If we had 3 spots that checked for the engine type, then those 3 spots are running SQL queries and using a connection every time. I'd imagine that if there's any case where the DB gets backed up, then this would compound the issue. Or, I guess if someone along the way wrote some case statement checking which engine it's using, that could be a lot of queries that could lead to a false positive.

case database
when .cockroachdb? # runs a query
  ...
when .postgres? # runs a query
  # gets here because it starts_with?("postgres")
when .redshift?
  # never gets here because it does start with postgres, but ends with "redshift"
end
image

@akadusei
Copy link
Contributor

I was thinking of such method being called once somewhere higher up the chain (or memoized) and used everywhere.

You make a very good point on redshift, though. I guess there may be more cases like that that make the version string unreliable. Your current solution's better, I think.

I guess once this is merged we can start to replace all the previous cockroachdb hacks?

@jwoertink
Copy link
Member Author

once this is merged we can start to replace all the previous cockroachdb hacks?

Yup!

Now, I am open to alternate implementations, but I think it still needs to be at the DB instance level so in case you have your main DB, but maybe separate models that connect to say ClickHouse, or Redshift for pulling stats. This was just the first method that came to mind that seemed the most straight forward and easy. So if anyone has an idea, we can always mess with this a bit more.

@jwoertink jwoertink merged commit 25cf0ac into main Jan 19, 2026
15 checks passed
@jwoertink jwoertink deleted the issues/415 branch January 19, 2026 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants