encoredev / encore

Encore is the Backend Development Platform purpose-built to help you create event-driven and distributed systems.
https://encore.dev
Mozilla Public License 2.0
5.13k stars 232 forks source link

Proposal: Manual database management #413

Open Minivera opened 1 year ago

Minivera commented 1 year ago

Background

Database are currently handled by adding a migrations file in the migrations folder, once that's done, a database is created and you can access it in that system or other systems. This can cause some confusion, for example:

This behavior can be confusing, one might expect the database to always exist if I create a system, or at least if I try to connect to a database, even without a migrations file. In addition, this might not work long term if Encore start support NoSQL database since those don't necessarily have migrations.

Proposal

@eandre had a proposal to give us a pattern similar to PubSub where we could create a new database object, which then returns a sql driver object, like this:

package users

import "encore.dev/storage"

var usersDB = storage.NewDatabase("users", storage.WithMigrations("path/to/migrations"), ...) // Maybe a "storage.WithDriver" to allow MySQL databases?

Which can then be used across the package, Named could potentially be replaced by this using a function like ConnectToDatabase.

This is a rough idea for this proposal, it very much could be improved.

Backwards compatibility

One big issue I can see with this approach is how to support existing applications and use cases that have gotten used to the current way of managing databases? I could see two path forward for supporting this feature for both old and new codebases.

gedw99 commented 1 year ago

Atlas can do auto migration I think if that helps. It’s written by the Ent Team