mirror of
https://github.com/simple-login/app.git
synced 2025-02-23 07:13:18 +08:00
Add how to generate migration script
This commit is contained in:
parent
042a421c2c
commit
7c31d39919
1 changed files with 19 additions and 11 deletions
30
README.md
30
README.md
|
@ -1155,23 +1155,31 @@ Output:
|
||||||
|
|
||||||
The database migration is handled by `alembic`
|
The database migration is handled by `alembic`
|
||||||
|
|
||||||
Whenever the model changes, a new migration has to be created
|
Whenever the model changes, a new migration has to be created.
|
||||||
|
|
||||||
Set the database connection to use a current database (i.e. the one without the model changes you just made), for example, if you have a staging config at `~/config/simplelogin/staging.env`, you can do:
|
If you have Docker installed, you can create the migration by the following script:
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
ln -sf ~/config/simplelogin/staging.env .env
|
# create a postgres database for SimpleLogin
|
||||||
|
docker rm -f sl-db
|
||||||
|
docker run -p 5432:5432 --name sl-db -e POSTGRES_PASSWORD=postgres -e POSTGRES_DB=sl -d postgres
|
||||||
|
|
||||||
|
# run run `flask db upgrade` to upgrade the DB to the latest stage and
|
||||||
|
env DB_URI=postgresql://postgres:postgres@127.0.0.1:5432/sl flask db upgrade
|
||||||
|
|
||||||
|
# finally `flask db migrate` to generate the migration script.
|
||||||
|
env DB_URI=postgresql://postgres:postgres@127.0.0.1:5432/sl flask db migrate
|
||||||
|
|
||||||
|
# remove the db
|
||||||
|
docker rm -f sl-db
|
||||||
```
|
```
|
||||||
|
|
||||||
Generate the migration script and make sure to review it before committing it. Sometimes (very rarely though), the migration generation can go wrong.
|
Make sure to review the migration script before committing it.
|
||||||
|
Sometimes (very rarely though), the automatically generated script can be incorrect.
|
||||||
|
|
||||||
```bash
|
We cannot use the local database to generate migration script as the local database doesn't use migration.
|
||||||
flask db migrate
|
It is created via `db.create_all()` (cf `fake_data()` method). This is convenient for development and
|
||||||
```
|
unit tests as we don't have to wait for the migration.
|
||||||
|
|
||||||
In local the database creation in Sqlite doesn't use migration and uses directly `db.create_all()` (cf `fake_data()` method).
|
|
||||||
This is because Sqlite doesn't handle well the migration. As sqlite is only used during development, the database is deleted
|
|
||||||
and re-populated at each run.
|
|
||||||
|
|
||||||
### Code structure
|
### Code structure
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue