-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(launcher): add information about which user launched a study (#1761
) Co-authored-by: Laurent LAPORTE <[email protected]> (cherry picked from commit ccd849e)
- Loading branch information
1 parent
1f23998
commit 2be18ed
Showing
16 changed files
with
536 additions
and
200 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
alembic/versions/d495746853cc_add_owner_id_to_jobresult.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
"""Add owner_id to JobResult | ||
Revision ID: d495746853cc | ||
Revises: e65e0c04606b | ||
Create Date: 2023-10-19 13:16:29.969047 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = "d495746853cc" | ||
down_revision = "e65e0c04606b" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade() -> None: | ||
with op.batch_alter_table("job_result", schema=None) as batch_op: | ||
batch_op.add_column(sa.Column('owner_id', sa.Integer(), default=None)) | ||
batch_op.create_foreign_key("fk_job_result_owner_id", "identities", ["owner_id"], ["id"], ondelete="SET NULL") | ||
|
||
|
||
def downgrade() -> None: | ||
with op.batch_alter_table("job_result", schema=None) as batch_op: | ||
batch_op.drop_column("owner_id") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,67 @@ | ||
# Database management | ||
# Database Management | ||
|
||
We support two database types : | ||
- postgresql (for production deployment) | ||
- sqlite (for the local desktop application) | ||
We support two types of databases: | ||
- PostgreSQL (for production deployment) | ||
- SQLite (for the local desktop application) | ||
|
||
## SQLAlchemy & Alembic | ||
|
||
We use [sqlalchemy](https://www.sqlalchemy.org/) and [alembic](https://alembic.sqlalchemy.org/en/latest/) | ||
to manage database and database entities. | ||
We utilize [SQLAlchemy](https://www.sqlalchemy.org/) and [Alembic](https://alembic.sqlalchemy.org/en/latest/) for managing databases and their entities. | ||
|
||
Schema is described by sqlalchemy models that are grouped and imported within | ||
the file [dbmodel.py](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/antarest/dbmodel.py). | ||
This file is then used by alembic [env file](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/alembic/env.py) | ||
to create the [database migration scripts](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/alembic/versions). | ||
The schema is described by SQLAlchemy models that are organized and imported within the file [dbmodel.py](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/antarest/dbmodel.py). | ||
This file is then used by the Alembic [env file](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/alembic/env.py) to create the [database migration scripts](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/alembic/versions). | ||
|
||
These migration scripts are used by alembic to update a target database defined in the env file which | ||
uses the database url defined in an [application config]('../install/2-CONFIG.md'), whether on command line | ||
(this is the method used on production deployment): | ||
``` | ||
export ANTAREST_CONF=<some path to config.yaml> | ||
These migration scripts are used by Alembic to update a target database defined in the env file, which uses the database URL defined in an [application config]('../install/2-CONFIG.md'). This can be done either on the command line (the method used in production deployment): | ||
|
||
```shell | ||
export ANTAREST_CONF=/path/to/your/application.yaml | ||
alembic upgrade head | ||
``` | ||
or within the application launch (see [this file](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/antarest/core/persistence.py)) : | ||
``` | ||
python antarest/main.py --auto-upgrade-db | ||
# or with the gui (default auto upgrade) | ||
python antarest/gui.py | ||
|
||
or within the application launch (refer to [this file](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/antarest/core/persistence.py)): | ||
|
||
```shell | ||
python3 antarest/main.py --auto-upgrade-db | ||
# or with the GUI (default auto-upgrade) | ||
python3 antarest/gui.py | ||
``` | ||
|
||
### How to update the schema | ||
### How to Update the Schema | ||
|
||
When developing for antarest we use a development configuration file that target | ||
a development database (usually sqlite but could be postgresql). After a first successful launch the database | ||
schema is migrated to the latest version. | ||
The schema version is stored in a table named `alembic_version` that contains the revision id of the last migration file. | ||
This information should match with the result of the command `alembic show head` that display the last revision id of the migration file tree. | ||
When developing for AntaREST, we use a development configuration file that targets a development database (usually SQLite but could be PostgreSQL). | ||
After a successful initial launch, the database schema is migrated to the latest version. | ||
The schema version is stored in a table named `alembic_version`, which contains the revision ID of the last migration file. | ||
This information should match the result of the command `alembic show head`, which displays the last revision ID of the migration file tree. | ||
|
||
To update the schema, there is two step. | ||
To update the schema, there is two steps: | ||
|
||
First we make the modification we want in the existing models (for instance in `study/model.py`, `login/model.py`, etc.) | ||
or create **new models in a separate file that will need to be added to the [dbmodel.py](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/antarest/dbmodel.py) file**. | ||
Most of the unit test that create the database from scratch using `sqlalchemy.sql.schema.MetaData.create_all` will do just fine but the integration tests (`tests/integration`) will probably | ||
fail since they use the alembic migration files process. | ||
First, we make the modifications we want in the existing models (e.g., in `study/model.py`, `login/model.py`, etc.) or create **new models in a separate file that will need to be added to the [dbmodel.py](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/antarest/dbmodel.py) file**. | ||
Most of the unit tests that create the database from scratch using `sqlalchemy.sql.schema.MetaData.create_all` will work fine, but the integration tests (`tests/integration`) will probably fail since they use the alembic migration files process. | ||
|
||
So second step is to create the migration file corresponding to the model change. We could create one from scratch, but most of the time, | ||
the script [create_db_migration.sh](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/scripts/create_db_migration.sh) (that just wraps the `alembic revision` command) will do: | ||
``` | ||
export ANTAREST_CONF=<dev conf> | ||
So the second step is to create the migration file corresponding to the model change. | ||
We could create one from scratch, but most of the time, the script [create_db_migration.sh](https://github.com/AntaresSimulatorTeam/AntaREST/blob/master/scripts/create_db_migration.sh) (that just wraps the `alembic revision` command) will do: | ||
|
||
```shell | ||
export ANTAREST_CONF=/path/to/your/application.yaml | ||
./script/create_db_migration.sh <migration_message> | ||
``` | ||
This will create a new migration file in `alembic/versions` that contains two prefilled methods `upgrade` and `downgrade`. | ||
Though for a newly created model the edition of this file should be minimal or nul, edition is sometimes required, especially in these cases: | ||
- handling compatibility/specificity of the databases (eg. adding a sequence `alembic/versions/2ed6bf9f1690_add_tasks.py`) | ||
- migrating data (eg. renaming/moving a field `alembic/versions/0146b79f723c_update_study.py`) | ||
|
||
The `create_db_migration.sh` script will also update the `scripts/rollback.sh` which (as the name indicated) is used to rollback the database to a previous schema. | ||
This will create a new migration file in `alembic/versions` that contains two prefilled methods `upgrade` and `downgrade`. | ||
However, for a newly created model, the editing of this file should be minimal or null. | ||
Editing is sometimes required, especially in these cases: | ||
- handling compatibility/specificity of the databases (e.g., adding a sequence `alembic/versions/2ed6bf9f1690_add_tasks.py`) | ||
- migrating data (e.g., renaming/moving a field `alembic/versions/0146b79f723c_update_study.py`) | ||
|
||
The `create_db_migration.sh` script will also update the `scripts/rollback.sh` which (as the name indicates) is used to roll back the database to a previous schema. | ||
|
||
At this point the development database is not yet migrated. It is only after launching the app (or calling `alembic upgrade head`) that our | ||
development database will be upgraded. | ||
At this point, the development database is not yet migrated. | ||
It is only after launching the app (or calling `alembic upgrade head`) that our development database will be upgraded. | ||
|
||
Now if we want to: | ||
- modify the model | ||
- checkout an other branch to test the application prior to this schema update | ||
- checkout another branch to test the application prior to this schema update | ||
|
||
we need to apply the `rollback.sh` script that will revert our local dev database to its previous schema. | ||
Then we will be able to either launch the app at a previous database schema or continue modifying the model and reapply | ||
the migration file creation process (in that case we should delete the now obsolete migration file lastly created). | ||
we need to apply the `rollback.sh` script that will revert our local dev database to its previous schema. | ||
Then we will be able to either launch the app at a previous database schema or continue modifying the model and reapply the migration file creation process (in that case, we should delete the now obsolete migration file lastly created). | ||
|
||
/!\ Note that when deploying in production a new version with multiple database migration file, the revision id in `rollback.sh` file | ||
should be the last revision id of the deployed application schema. | ||
|
||
⚠️ Note that when deploying a new version in production with multiple database migration files, the revision ID in the `rollback.sh` file should be the last revision ID of the deployed application schema. |
Oops, something went wrong.