Skip to content

Allow overriding current DB escape hatch #1093

@jwoertink

Description

@jwoertink

In #1089 we now have a way to specify different Read and Write databases. One case I wasn't able to figure out a good solution for is when you might need to override this value on the fly for specific edge cases.

Imagine something along these lines:

# temporarily switch to a different DB only for these read/write
# actions.
Avram.use_database(CustomDB) do
  user = UserQuery.find(1)
  SaveUser.update!(user, whatever: true)
  SoftDeleteUser.delete(user)
end

# uses the normal ReadDatabase
UserQuery.find(2)

I'm thinking that in order to do this, we may need to refactor how the databases are setup. At the moment we use a class as the definition:

def self.read_database : Avram::Database.class
  ReadDatabase
end

But this may need to become an instance....

getter read_database : Avram::Database do
  ReadDatabase.new
end

If we went with instances everywhere, then we could easily do something like

def self.use_database(db) do
  old_db = current_db
  current_db = db
  yield
  current_db = old_db
end

This will need a bit more planning though to make sure it will be thread safe so when multithreadding comes we're ready.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions