feat(database): manage the schema with typeorm migrations
- replace synchronize with migrations, starting with a generated initial migration that adopts databases created by synchronize - apply pending migrations in the docker container before the server, and refuse to start the server while migrations are pending - share one entity list and data source between app, migration runner, typeorm cli and tests; tests run the real migrations - add migration:run, migration:show and migration:generate scripts and a test that fails when entities and migrations differ - exit with code 1 when startup fails - bump version to 1.20.0
This commit is contained in:
1 parent
0ae807d68d
commit
95c3bd7bcb
17 files changed
+526
-80
No files matched your search
@@ -86,6 +86,36 @@ The built files are emitted to `client/dist/`.
|
||||
|
||||
---
|
||||
|
||||
## Database migrations
|
||||
|
||||
The SQLite schema is managed by TypeORM migrations in `server/src/database/migrations/`; `synchronize` is off.
|
||||
|
||||
- **Docker:** the container applies pending migrations before starting the server.
|
||||
- **Locally:** build, then apply them before starting the server (it refuses to start while migrations are pending):
|
||||
|
||||
```bash
|
||||
npm -w server run build
|
||||
npm -w server run migration:run # apply pending migrations to DB_PATH
|
||||
npm -w server run migration:show # list migrations and whether they're applied
|
||||
```
|
||||
|
||||
The scripts run from `server/`, so `DB_PATH` (default `data/liqa.db`) is relative to it, and they read `server/.env` if present, like the server does.
|
||||
|
||||
**Changing the schema** (adding or changing an entity):
|
||||
|
||||
1. Change the entity and build (`npm -w server run build`).
|
||||
2. Make sure your local database is on the latest migration (`migration:run`).
|
||||
3. Generate a migration from the difference between the entities and that database:
|
||||
|
||||
```bash
|
||||
npm -w server run migration:generate -- src/database/migrations/AddSomething
|
||||
```
|
||||
|
||||
4. Review the generated file, then add its class to `MIGRATIONS` in `src/database/migrations/index.ts` (they're listed explicitly, oldest first).
|
||||
5. Run the tests: `test/migrations.spec.ts` checks that the migrations produce exactly the schema the entities describe, and fails if an entity change has no migration.
|
||||
|
||||
The initial migration (`Initial…`) adopts databases created before migrations existed (by `synchronize`): if the tables already exist, it's only recorded as applied.
|
||||
|
||||
## Running tests
|
||||
|
||||
### Server (Jest)
|
||||
|
||||
Reference in new issue
Block a user