Connect to database that does not exist in database.yml


I am building a project that supports multi-tenant with database per tenant. I wanna create/connect to the database in runtime.

I tried to use

db_config = ActiveRecord::Base.connection_db_config.configuration_hash.deep_dup
ActiveRecord::Base.establish_connection(db_config.merge(database: 'tenant-database'))

But it will check in the database.yml the database name and raise error because this name is not existed.

I wonder how I can connect/create new database without editing the database.yml