How do you best manage/execute database migrations if the same database is being used within multiple projects?
Some background:
I have a project with a monorepo structure build with Turborepo on top of npm workspaces. In the repo I have an internal library that exports a client to perform operations on a postgres database. I then have two apps that use this library to perform operations on the database.
The shared library uses Drizzle ORM to manage the database schema and migrations. It basically exports a client that
export class SomeClient {
private db: PostgresJsDatabase;
constructor(connection: string) {
const queryClient = postgres(connection);
this.db = drizzle(queryClient);
}
// business logic functions...
}
I create and keep all migrations in a folder within the library directory using Drizzles CLI, Drizzle Kit. But now I am wondering how to execute the migrations. My initial idea was to implement a migrate()
function on the client:
migrate() {
return migrate(this.db, { migrationsFolder: 'migrations' });
}
However, I can't execute this function within the apps that use the client, because the folder with the migrations is in the library.
One idea to workaround this, was to simply copy all migrations into the apps after creating them or to create a symlink to the migrations folder of the library. My goal is to execute the migrations automatically in the apps on startup. Do you have any ideas or alternative approaches?