cartesi / rollups-node

Reference implementation of the Cartesi Rollups Node
Apache License 2.0
20 stars 63 forks source link

Create PostgreSQL database if it doesn't exists #403

Open endersonmaia opened 4 months ago

endersonmaia commented 4 months ago

📚 Context

Currently, rollups-node expects CARTESI_POSTGRES_ENDPOINT to provide the right credentials and a database name.

It would be nice if rollups-node could check if the /<database> part of the endpoint could be created in case it doesn't exists and the credentials provided have the right to do it.

✔️ Solution

So the logic would be something like this:

if database_exists?
    success
else if can_create_databas?
    create_database
    success
else
    error "No permissions to create database."

In a scenario where a single database is provided for many rollups-node instances, database should be created with unique names to avoid collision.

It could be something like chain-$chainId-dapp-$dappCointractAddres, for ex. chain-11155111-dapp-b3b3efb3ca185b63faf4ed125d7cad3daf4c5dbf

endersonmaia commented 4 months ago

Considering some PostgreSQL cloud providers, with cheaper shared instances, where you only get credentials for a single database, we could provide a flag that would make the node isolate each application inside schemas instead of a database.

This is a common multi-tenant database pattern instead of relying on a database table column to identify the tenants.