welearn-database 1.5.6.dev1__tar.gz → 1.5.7__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- welearn_database-1.5.7/PKG-INFO +150 -0
- welearn_database-1.5.7/README.md +130 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/pyproject.toml +1 -1
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/env.py +64 -19
- welearn_database-1.5.7/welearn_database/alembic/versions/1_5_7_add_feature_thread_and_guest_feature_usage_tables.py +113 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/b049924f7067_modify_corpus_name_embedding_model_lang_.py +9 -17
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/user_related.py +137 -1
- welearn_database-1.5.6.dev1/PKG-INFO +0 -59
- welearn_database-1.5.6.dev1/README.md +0 -39
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/LICENSE +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/__init__.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/README +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/script.py.mako +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/0310ff0bda4f_track_document_last_state.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/068312e7800c_add_referrer_origin_column_to_user_and_.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/0e0bc0fca384_doc_qty_per_source.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/16ff997426d3_remove_error_retrieval_unique_constraint.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/1_5_5_analytical_view_for_research.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/1_5_6_add_is_hidden_column_to_category.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/2ad4895b2674_data_collection.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/4c7161819e5a_grafana_views.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/4f5a188dd614_add_main_url_column.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/4fcbfb7f3145_added_api_key_management_table.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/5aad97149044_add_histo_table_for_materialized_views.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/5d46d7920342_support_sub_corpus.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/5d82613c9aca_context_document.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/6d4346fad6f4_view_used_feature_per_session.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/821173cf9c5d_initial_migration.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/84c42739c17b_create_table_errordataquality.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/89920abb7ff8_add_category.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/8b780aea403a_add_university_data.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/95ffe7afa64c_add_index_idx_welearn_document_keyword_.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/96bba9e4842a_merge_external_id_error_quality.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/9b4f1da0c1f2_data_collection_for_focus_group.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/a50a1db3ca2a_add_used_since_column_for_embeddings.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/b031206324b7_agent_related.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/b84462ca3800_update_grafana_process_state_view.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/ccdbd708c997_create_column_external_id.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/d22d6324c256_cleaned_document_state.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/d990f127902f_merge_keycloack_and_new_cleaned_state_.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/e354666f951d_inferred_user.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/f10903998081_keycloack_id_for_user_id.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/f1ce0ad2845b_add_doi_column_to_document.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/f5e91a5c950d_add_analytic_form.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/versions/f8602200fa99_update_session_view.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/__init__.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/details_dict.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/enumeration.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/__init__.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/agent_related.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/corpus_related.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/document_related.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/grafana.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/database_utils.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/exceptions.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/modules/__init__.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/modules/text_cleaning.py +0 -0
- {welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/regular_expression.py +0 -0
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
Metadata-Version: 2.3
|
|
2
|
+
Name: welearn-database
|
|
3
|
+
Version: 1.5.7
|
|
4
|
+
Summary: All stuff related to relationnal database from the WeLearn project
|
|
5
|
+
License: cc-by-sa-nc
|
|
6
|
+
Author: Théo
|
|
7
|
+
Author-email: theo.nardin@cri-paris.org
|
|
8
|
+
Requires-Python: >=3.10
|
|
9
|
+
Classifier: License :: Other/Proprietary License
|
|
10
|
+
Classifier: Programming Language :: Python :: 3
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
+
Requires-Dist: alembic (>=1.4.3,<2.0.0)
|
|
16
|
+
Requires-Dist: psycopg2-binary (>=2.9.11,<3.0.0)
|
|
17
|
+
Requires-Dist: python-dotenv (>=1.2.2,<2.0.0)
|
|
18
|
+
Requires-Dist: sqlalchemy (>=2.0.43,<3.0.0)
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
|
|
21
|
+
# WeLearn Database
|
|
22
|
+
|
|
23
|
+
This repository contains the database schema and sample data for the WeLearn application, an online learning platform.
|
|
24
|
+
|
|
25
|
+
## Pypi Package
|
|
26
|
+
You can install this pacakge via pypi :
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
pip install welearn-database
|
|
30
|
+
```
|
|
31
|
+
or you can read pypi page [here](https://pypi.org/project/welearn-database/)
|
|
32
|
+
|
|
33
|
+
## Environment Variables
|
|
34
|
+
Before running the application, make sure to set the following environment variables:
|
|
35
|
+
```
|
|
36
|
+
PG_USER=<pg user>
|
|
37
|
+
PG_PASSWORD=<pg password>
|
|
38
|
+
PG_HOST=<pg address>
|
|
39
|
+
PG_PORT=<pg port, 5432 by default>
|
|
40
|
+
PG_DB=<pg database name>
|
|
41
|
+
PG_DRIVER=<driver to use, pg default is : postgresql+psycopg2>
|
|
42
|
+
PG_SCHEMA=document_related,corpus_related,user_related,agent_related
|
|
43
|
+
LOG_LEVEL=INFO
|
|
44
|
+
LOG_FORMAT=[%(asctime)s][%(name)s][%(levelname)s] - %(message)s
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Release Workflow
|
|
48
|
+
A schema change goes from code to the prod database in seven steps. The commands for steps 3 and 7 are in [Database Migrations](#database-migrations).
|
|
49
|
+
|
|
50
|
+
1. **Change the code.** On a feature branch, edit the models in `welearn_database/data/models`.
|
|
51
|
+
2. **Write the Alembic migration.** Add one revision for the release in `welearn_database/alembic/versions`. Name its id after the version with underscores, and point `down_revision` at the previous one:
|
|
52
|
+
```bash
|
|
53
|
+
poetry run alembic revision -m "add my table" --rev-id 1_5_8
|
|
54
|
+
# creates welearn_database/alembic/versions/1_5_8_add_my_table.py
|
|
55
|
+
```
|
|
56
|
+
`--autogenerate` can draft it from the models, but it ignores indexes and compares against the database in `.env`, so always read and fix the result. Test `upgrade` then `downgrade` on a local database (`welearn-fullstack`, `scripts/bootstrap-db.sh`). `poetry run alembic heads` must show a single head. Open the PR and get it merged on `main`.
|
|
57
|
+
3. **Update the dev databases.** On Azure dev, preview the SQL with `--sql`, then run `alembic -x env=dev upgrade <revision>` (VPN on). On Scaleway dev, generate the SQL and run it in pgAdmin (see [Scaleway dev](#scaleway-dev-run-the-sql-in-pgadmin)). If the change only adds tables or columns, the API already deployed on dev keeps working. If it renames or drops something, deploy the matching API at the same time.
|
|
58
|
+
4. **Tag a `.dev` version and make a pre-release.**
|
|
59
|
+
- In `pyproject.toml`, set `version = "1.5.8.dev0"` and merge it on `main`.
|
|
60
|
+
- Create a GitHub release with the same tag (`1.5.8.dev0`) on `main`, with "Set as a pre-release" checked.
|
|
61
|
+
- Pushing the tag runs the workflow, which publishes the package to PyPI. Every push also publishes to TestPyPI.
|
|
62
|
+
- PyPI normalizes the version: a tag `1.5.7.dev` is published as `1.5.7.dev0`.
|
|
63
|
+
5. **Validate in dev.**
|
|
64
|
+
- In the API, pin the exact pre-release, `welearn-database = "1.5.8.dev0"`, then run `poetry lock`. A caret constraint like `^1.5.7` never picks up a `.dev` version.
|
|
65
|
+
- Deploy on dev and test the feature.
|
|
66
|
+
- If you need a fix, publish `1.5.8.dev1` the same way. If the migration itself changed, run `downgrade` then `upgrade` on dev again.
|
|
67
|
+
6. **Tag the prod version, coordinated with the API.**
|
|
68
|
+
- Set `version = "1.5.8"` (no `.dev`), merge it, and create a normal GitHub release (not a pre-release) tagged `1.5.8`.
|
|
69
|
+
- In the API, a PR moves the dependency to `^1.5.8`.
|
|
70
|
+
- Agree with the API team on when it goes out: that PR must not reach an environment whose database hasn't been migrated (step 7).
|
|
71
|
+
7. **Update the prod database.**
|
|
72
|
+
- Migrate staging first, then prod. Both are on Azure; Scaleway only has a dev environment for now.
|
|
73
|
+
- Make sure a recent backup exists, then preview with `--sql` and run `alembic -x env=<staging|prod> upgrade <revision>`. For `prod`, `env.py` asks you to type the environment name before it goes on.
|
|
74
|
+
- After that, deploy the API version that uses the new schema.
|
|
75
|
+
|
|
76
|
+
## Database Migrations
|
|
77
|
+
Migrations are Alembic revisions in `welearn_database/alembic/versions`. Run them from a clone of this repository, after `poetry install`, not from the package installed from PyPI.
|
|
78
|
+
|
|
79
|
+
### Setup
|
|
80
|
+
- Create one file per environment at the root of the repository, copied from `.env.example`: `.env.dev`, `.env.staging`, `.env.prod`. Only `PG_HOST`, `PG_DB` and the credentials change between them. `PG_DB` is the database name (the API calls it `PG_DATABASE`).
|
|
81
|
+
- These files are git-ignored. Keep them private with `chmod 600 .env.*`.
|
|
82
|
+
- Pick the database with `-x env=<name>`, which loads `.env.<name>`. The flag goes before the subcommand: `poetry run alembic -x env=staging current`. The file's values override variables exported in your shell.
|
|
83
|
+
- Without `-x env`, `env.py` loads `.env` (handy for a local database), and a variable already exported in your shell wins. Check that `env | grep PG_` prints nothing, or only the values you expect.
|
|
84
|
+
- The dev and staging databases need the VPN.
|
|
85
|
+
- The Scaleway database isn't reachable from your machine, so it has no `.env` file. See [Scaleway dev](#scaleway-dev-run-the-sql-in-pgadmin).
|
|
86
|
+
|
|
87
|
+
### Check where the database is
|
|
88
|
+
```bash
|
|
89
|
+
poetry run alembic -x env=dev current # revision the database is at
|
|
90
|
+
poetry run alembic heads # latest revision in this repository
|
|
91
|
+
poetry run alembic history # full chain
|
|
92
|
+
```
|
|
93
|
+
`env.py` prints the target first, for example `Target: staging -> <host>/welearn_datastack_staging`. For `prod`, or a host with `prod` in its name, it asks you to type the environment name before it connects.
|
|
94
|
+
|
|
95
|
+
The staging database has no `alembic_version` table yet, so `current` prints no revision there. Before its first `upgrade`, check which revision its schema matches and record it with `poetry run alembic -x env=staging stamp <revision>`. `stamp` changes nothing but `alembic_version`.
|
|
96
|
+
|
|
97
|
+
### Preview the SQL (`--sql`, nothing is applied)
|
|
98
|
+
```bash
|
|
99
|
+
poetry run alembic -x env=dev upgrade 1_5_6:1_5_7 --sql > 1_5_7.sql
|
|
100
|
+
```
|
|
101
|
+
- Offline mode only writes the SQL out. It doesn't connect to the database, doesn't change it, and doesn't update `alembic_version`. The SQL is the same for every environment, so any `-x env` works.
|
|
102
|
+
- It needs a `from:to` range, because it can't read the database's current revision.
|
|
103
|
+
- If you see `-- Running upgrade 1_5_6 -> 1_5_7` followed by SQL, nothing was applied.
|
|
104
|
+
- If you run the file by hand (psql, DBeaver…), it ends with `UPDATE alembic_version …`. That table lives in the `public` schema.
|
|
105
|
+
|
|
106
|
+
### Apply the migration
|
|
107
|
+
```bash
|
|
108
|
+
poetry run alembic -x env=staging upgrade 1_5_7 # or: upgrade head
|
|
109
|
+
poetry run alembic -x env=staging current # should print 1_5_7
|
|
110
|
+
```
|
|
111
|
+
- Upgrading to a named revision runs only what you expect.
|
|
112
|
+
- A real run logs `INFO … Running upgrade 1_5_6 -> 1_5_7, …` and prints no SQL.
|
|
113
|
+
|
|
114
|
+
### Roll back
|
|
115
|
+
```bash
|
|
116
|
+
poetry run alembic -x env=staging downgrade 1_5_6
|
|
117
|
+
```
|
|
118
|
+
This runs the revision's `downgrade()`. For `1_5_7`, that drops the `feature`, `thread` and `guest_feature_usage` tables.
|
|
119
|
+
|
|
120
|
+
### Scaleway dev: run the SQL in pgAdmin
|
|
121
|
+
The Scaleway database is only reachable from inside the cluster, so Alembic can't connect to it. Generate the SQL locally and run it in the cluster's pgAdmin:
|
|
122
|
+
1. In pgAdmin, check where the database is:
|
|
123
|
+
```sql
|
|
124
|
+
SELECT version_num FROM public.alembic_version;
|
|
125
|
+
```
|
|
126
|
+
2. Generate the SQL from that revision to the target one. Nothing connects: `-x env=dev` only fills in the URL Alembic needs.
|
|
127
|
+
```bash
|
|
128
|
+
poetry run alembic -x env=dev upgrade 1_5_6:1_5_7 --sql > 1_5_7.sql
|
|
129
|
+
```
|
|
130
|
+
3. Read the file, then paste it into pgAdmin's Query Tool on the Scaleway database and run it. It is wrapped in `BEGIN;` … `COMMIT;`, so it applies fully or not at all, and it ends with `UPDATE public.alembic_version …`.
|
|
131
|
+
4. Run the query from step 1 again: it should print the target revision.
|
|
132
|
+
|
|
133
|
+
To roll back, generate the downgrade the same way, with the range reversed: `poetry run alembic -x env=dev downgrade 1_5_7:1_5_6 --sql > 1_5_7_down.sql`.
|
|
134
|
+
|
|
135
|
+
### Order across environments
|
|
136
|
+
Migrate dev (Azure with `-x env=dev`, Scaleway through pgAdmin), then staging (`-x env=staging`), then prod (`-x env=prod`). An environment must be migrated before an API version that uses the new tables is deployed there. A migration that only adds tables, like `1_5_7`, doesn't break an older API running on the migrated database.
|
|
137
|
+
|
|
138
|
+
## Database Schema
|
|
139
|
+
The database schema is organized into four main schemas:
|
|
140
|
+
- `document_related`: Contains tables related to documents and their metadata.
|
|
141
|
+
- `corpus_related`: Contains tables related to corpora and their metadata.
|
|
142
|
+
- `user_related`: Contains tables related to users and their profiles.
|
|
143
|
+
- `agent_related`: Contains tables related to agents and their interactions.
|
|
144
|
+
|
|
145
|
+
## How to Use
|
|
146
|
+
Data models are defined using SQLAlchemy ORM. You can import the models and use them to interact with the database.
|
|
147
|
+
```python
|
|
148
|
+
from welearn_database.data.models import WeLearnDocument
|
|
149
|
+
```
|
|
150
|
+
Every model are accessible there, schema are handled under the hood.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
# WeLearn Database
|
|
2
|
+
|
|
3
|
+
This repository contains the database schema and sample data for the WeLearn application, an online learning platform.
|
|
4
|
+
|
|
5
|
+
## Pypi Package
|
|
6
|
+
You can install this pacakge via pypi :
|
|
7
|
+
|
|
8
|
+
```bash
|
|
9
|
+
pip install welearn-database
|
|
10
|
+
```
|
|
11
|
+
or you can read pypi page [here](https://pypi.org/project/welearn-database/)
|
|
12
|
+
|
|
13
|
+
## Environment Variables
|
|
14
|
+
Before running the application, make sure to set the following environment variables:
|
|
15
|
+
```
|
|
16
|
+
PG_USER=<pg user>
|
|
17
|
+
PG_PASSWORD=<pg password>
|
|
18
|
+
PG_HOST=<pg address>
|
|
19
|
+
PG_PORT=<pg port, 5432 by default>
|
|
20
|
+
PG_DB=<pg database name>
|
|
21
|
+
PG_DRIVER=<driver to use, pg default is : postgresql+psycopg2>
|
|
22
|
+
PG_SCHEMA=document_related,corpus_related,user_related,agent_related
|
|
23
|
+
LOG_LEVEL=INFO
|
|
24
|
+
LOG_FORMAT=[%(asctime)s][%(name)s][%(levelname)s] - %(message)s
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
## Release Workflow
|
|
28
|
+
A schema change goes from code to the prod database in seven steps. The commands for steps 3 and 7 are in [Database Migrations](#database-migrations).
|
|
29
|
+
|
|
30
|
+
1. **Change the code.** On a feature branch, edit the models in `welearn_database/data/models`.
|
|
31
|
+
2. **Write the Alembic migration.** Add one revision for the release in `welearn_database/alembic/versions`. Name its id after the version with underscores, and point `down_revision` at the previous one:
|
|
32
|
+
```bash
|
|
33
|
+
poetry run alembic revision -m "add my table" --rev-id 1_5_8
|
|
34
|
+
# creates welearn_database/alembic/versions/1_5_8_add_my_table.py
|
|
35
|
+
```
|
|
36
|
+
`--autogenerate` can draft it from the models, but it ignores indexes and compares against the database in `.env`, so always read and fix the result. Test `upgrade` then `downgrade` on a local database (`welearn-fullstack`, `scripts/bootstrap-db.sh`). `poetry run alembic heads` must show a single head. Open the PR and get it merged on `main`.
|
|
37
|
+
3. **Update the dev databases.** On Azure dev, preview the SQL with `--sql`, then run `alembic -x env=dev upgrade <revision>` (VPN on). On Scaleway dev, generate the SQL and run it in pgAdmin (see [Scaleway dev](#scaleway-dev-run-the-sql-in-pgadmin)). If the change only adds tables or columns, the API already deployed on dev keeps working. If it renames or drops something, deploy the matching API at the same time.
|
|
38
|
+
4. **Tag a `.dev` version and make a pre-release.**
|
|
39
|
+
- In `pyproject.toml`, set `version = "1.5.8.dev0"` and merge it on `main`.
|
|
40
|
+
- Create a GitHub release with the same tag (`1.5.8.dev0`) on `main`, with "Set as a pre-release" checked.
|
|
41
|
+
- Pushing the tag runs the workflow, which publishes the package to PyPI. Every push also publishes to TestPyPI.
|
|
42
|
+
- PyPI normalizes the version: a tag `1.5.7.dev` is published as `1.5.7.dev0`.
|
|
43
|
+
5. **Validate in dev.**
|
|
44
|
+
- In the API, pin the exact pre-release, `welearn-database = "1.5.8.dev0"`, then run `poetry lock`. A caret constraint like `^1.5.7` never picks up a `.dev` version.
|
|
45
|
+
- Deploy on dev and test the feature.
|
|
46
|
+
- If you need a fix, publish `1.5.8.dev1` the same way. If the migration itself changed, run `downgrade` then `upgrade` on dev again.
|
|
47
|
+
6. **Tag the prod version, coordinated with the API.**
|
|
48
|
+
- Set `version = "1.5.8"` (no `.dev`), merge it, and create a normal GitHub release (not a pre-release) tagged `1.5.8`.
|
|
49
|
+
- In the API, a PR moves the dependency to `^1.5.8`.
|
|
50
|
+
- Agree with the API team on when it goes out: that PR must not reach an environment whose database hasn't been migrated (step 7).
|
|
51
|
+
7. **Update the prod database.**
|
|
52
|
+
- Migrate staging first, then prod. Both are on Azure; Scaleway only has a dev environment for now.
|
|
53
|
+
- Make sure a recent backup exists, then preview with `--sql` and run `alembic -x env=<staging|prod> upgrade <revision>`. For `prod`, `env.py` asks you to type the environment name before it goes on.
|
|
54
|
+
- After that, deploy the API version that uses the new schema.
|
|
55
|
+
|
|
56
|
+
## Database Migrations
|
|
57
|
+
Migrations are Alembic revisions in `welearn_database/alembic/versions`. Run them from a clone of this repository, after `poetry install`, not from the package installed from PyPI.
|
|
58
|
+
|
|
59
|
+
### Setup
|
|
60
|
+
- Create one file per environment at the root of the repository, copied from `.env.example`: `.env.dev`, `.env.staging`, `.env.prod`. Only `PG_HOST`, `PG_DB` and the credentials change between them. `PG_DB` is the database name (the API calls it `PG_DATABASE`).
|
|
61
|
+
- These files are git-ignored. Keep them private with `chmod 600 .env.*`.
|
|
62
|
+
- Pick the database with `-x env=<name>`, which loads `.env.<name>`. The flag goes before the subcommand: `poetry run alembic -x env=staging current`. The file's values override variables exported in your shell.
|
|
63
|
+
- Without `-x env`, `env.py` loads `.env` (handy for a local database), and a variable already exported in your shell wins. Check that `env | grep PG_` prints nothing, or only the values you expect.
|
|
64
|
+
- The dev and staging databases need the VPN.
|
|
65
|
+
- The Scaleway database isn't reachable from your machine, so it has no `.env` file. See [Scaleway dev](#scaleway-dev-run-the-sql-in-pgadmin).
|
|
66
|
+
|
|
67
|
+
### Check where the database is
|
|
68
|
+
```bash
|
|
69
|
+
poetry run alembic -x env=dev current # revision the database is at
|
|
70
|
+
poetry run alembic heads # latest revision in this repository
|
|
71
|
+
poetry run alembic history # full chain
|
|
72
|
+
```
|
|
73
|
+
`env.py` prints the target first, for example `Target: staging -> <host>/welearn_datastack_staging`. For `prod`, or a host with `prod` in its name, it asks you to type the environment name before it connects.
|
|
74
|
+
|
|
75
|
+
The staging database has no `alembic_version` table yet, so `current` prints no revision there. Before its first `upgrade`, check which revision its schema matches and record it with `poetry run alembic -x env=staging stamp <revision>`. `stamp` changes nothing but `alembic_version`.
|
|
76
|
+
|
|
77
|
+
### Preview the SQL (`--sql`, nothing is applied)
|
|
78
|
+
```bash
|
|
79
|
+
poetry run alembic -x env=dev upgrade 1_5_6:1_5_7 --sql > 1_5_7.sql
|
|
80
|
+
```
|
|
81
|
+
- Offline mode only writes the SQL out. It doesn't connect to the database, doesn't change it, and doesn't update `alembic_version`. The SQL is the same for every environment, so any `-x env` works.
|
|
82
|
+
- It needs a `from:to` range, because it can't read the database's current revision.
|
|
83
|
+
- If you see `-- Running upgrade 1_5_6 -> 1_5_7` followed by SQL, nothing was applied.
|
|
84
|
+
- If you run the file by hand (psql, DBeaver…), it ends with `UPDATE alembic_version …`. That table lives in the `public` schema.
|
|
85
|
+
|
|
86
|
+
### Apply the migration
|
|
87
|
+
```bash
|
|
88
|
+
poetry run alembic -x env=staging upgrade 1_5_7 # or: upgrade head
|
|
89
|
+
poetry run alembic -x env=staging current # should print 1_5_7
|
|
90
|
+
```
|
|
91
|
+
- Upgrading to a named revision runs only what you expect.
|
|
92
|
+
- A real run logs `INFO … Running upgrade 1_5_6 -> 1_5_7, …` and prints no SQL.
|
|
93
|
+
|
|
94
|
+
### Roll back
|
|
95
|
+
```bash
|
|
96
|
+
poetry run alembic -x env=staging downgrade 1_5_6
|
|
97
|
+
```
|
|
98
|
+
This runs the revision's `downgrade()`. For `1_5_7`, that drops the `feature`, `thread` and `guest_feature_usage` tables.
|
|
99
|
+
|
|
100
|
+
### Scaleway dev: run the SQL in pgAdmin
|
|
101
|
+
The Scaleway database is only reachable from inside the cluster, so Alembic can't connect to it. Generate the SQL locally and run it in the cluster's pgAdmin:
|
|
102
|
+
1. In pgAdmin, check where the database is:
|
|
103
|
+
```sql
|
|
104
|
+
SELECT version_num FROM public.alembic_version;
|
|
105
|
+
```
|
|
106
|
+
2. Generate the SQL from that revision to the target one. Nothing connects: `-x env=dev` only fills in the URL Alembic needs.
|
|
107
|
+
```bash
|
|
108
|
+
poetry run alembic -x env=dev upgrade 1_5_6:1_5_7 --sql > 1_5_7.sql
|
|
109
|
+
```
|
|
110
|
+
3. Read the file, then paste it into pgAdmin's Query Tool on the Scaleway database and run it. It is wrapped in `BEGIN;` … `COMMIT;`, so it applies fully or not at all, and it ends with `UPDATE public.alembic_version …`.
|
|
111
|
+
4. Run the query from step 1 again: it should print the target revision.
|
|
112
|
+
|
|
113
|
+
To roll back, generate the downgrade the same way, with the range reversed: `poetry run alembic -x env=dev downgrade 1_5_7:1_5_6 --sql > 1_5_7_down.sql`.
|
|
114
|
+
|
|
115
|
+
### Order across environments
|
|
116
|
+
Migrate dev (Azure with `-x env=dev`, Scaleway through pgAdmin), then staging (`-x env=staging`), then prod (`-x env=prod`). An environment must be migrated before an API version that uses the new tables is deployed there. A migration that only adds tables, like `1_5_7`, doesn't break an older API running on the migrated database.
|
|
117
|
+
|
|
118
|
+
## Database Schema
|
|
119
|
+
The database schema is organized into four main schemas:
|
|
120
|
+
- `document_related`: Contains tables related to documents and their metadata.
|
|
121
|
+
- `corpus_related`: Contains tables related to corpora and their metadata.
|
|
122
|
+
- `user_related`: Contains tables related to users and their profiles.
|
|
123
|
+
- `agent_related`: Contains tables related to agents and their interactions.
|
|
124
|
+
|
|
125
|
+
## How to Use
|
|
126
|
+
Data models are defined using SQLAlchemy ORM. You can import the models and use them to interact with the database.
|
|
127
|
+
```python
|
|
128
|
+
from welearn_database.data.models import WeLearnDocument
|
|
129
|
+
```
|
|
130
|
+
Every model are accessible there, schema are handled under the hood.
|
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
import os
|
|
2
|
+
import sys
|
|
2
3
|
from logging.config import fileConfig
|
|
4
|
+
from pathlib import Path
|
|
3
5
|
|
|
4
6
|
from alembic import context
|
|
5
7
|
from dotenv import load_dotenv
|
|
8
|
+
from sqlalchemy import URL
|
|
6
9
|
|
|
7
10
|
from welearn_database.database_utils import create_sqlalchemy_engine
|
|
8
11
|
|
|
@@ -32,6 +35,57 @@ target_metadata = Base.metadata
|
|
|
32
35
|
# my_important_option = config.get_main_option("my_important_option")
|
|
33
36
|
# ... etc.
|
|
34
37
|
|
|
38
|
+
# Targets that ask the user to type their name before an online run.
|
|
39
|
+
PROTECTED_TARGETS = {"prod"}
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def load_target_env() -> str | None:
|
|
43
|
+
"""Load the database settings for the target given with `-x env=<name>`.
|
|
44
|
+
|
|
45
|
+
With the flag, `.env.<name>` at the repository root is loaded and its values
|
|
46
|
+
override variables already exported in the shell. Without it, `.env` is
|
|
47
|
+
loaded as before and exported variables win.
|
|
48
|
+
"""
|
|
49
|
+
target = context.get_x_argument(as_dictionary=True).get("env")
|
|
50
|
+
root = Path(config.config_file_name or "alembic.ini").resolve().parent
|
|
51
|
+
|
|
52
|
+
if not target:
|
|
53
|
+
load_dotenv(root / ".env")
|
|
54
|
+
return None
|
|
55
|
+
|
|
56
|
+
env_file = root / f".env.{target}"
|
|
57
|
+
if not env_file.is_file():
|
|
58
|
+
available = sorted(
|
|
59
|
+
p.name for p in root.glob(".env.*") if p.name != ".env.example"
|
|
60
|
+
)
|
|
61
|
+
raise SystemExit(
|
|
62
|
+
f"No {env_file.name} found in {root}. "
|
|
63
|
+
f"Available: {', '.join(available) or 'none'}"
|
|
64
|
+
)
|
|
65
|
+
load_dotenv(env_file, override=True)
|
|
66
|
+
return target
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def confirm_target(target: str | None) -> None:
|
|
70
|
+
"""Print where the migration goes, and ask before touching a protected database."""
|
|
71
|
+
host = os.getenv("PG_HOST", "")
|
|
72
|
+
db = os.getenv("PG_DB", "")
|
|
73
|
+
label = target or ".env"
|
|
74
|
+
print(f"Target: {label} -> {host}/{db}", file=sys.stderr)
|
|
75
|
+
|
|
76
|
+
if context.is_offline_mode():
|
|
77
|
+
return
|
|
78
|
+
|
|
79
|
+
if target in PROTECTED_TARGETS or "prod" in host.lower():
|
|
80
|
+
expected = target or "prod"
|
|
81
|
+
answer = input(f"Type '{expected}' to run against this database: ")
|
|
82
|
+
if answer.strip() != expected:
|
|
83
|
+
raise SystemExit("Aborted.")
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
TARGET = load_target_env()
|
|
87
|
+
confirm_target(TARGET)
|
|
88
|
+
|
|
35
89
|
|
|
36
90
|
def run_migrations_offline() -> None:
|
|
37
91
|
"""Run migrations in 'offline' mode.
|
|
@@ -45,12 +99,20 @@ def run_migrations_offline() -> None:
|
|
|
45
99
|
script output.
|
|
46
100
|
|
|
47
101
|
"""
|
|
48
|
-
|
|
102
|
+
db_url = URL.create(
|
|
103
|
+
drivername=os.getenv("PG_DRIVER", "postgresql+psycopg"),
|
|
104
|
+
username=os.environ["PG_USER"],
|
|
105
|
+
password=os.environ["PG_PASSWORD"],
|
|
106
|
+
host=os.getenv("PG_HOST", "localhost"),
|
|
107
|
+
port=int(os.getenv("PG_PORT", "5432")),
|
|
108
|
+
database=os.environ["PG_DB"],
|
|
109
|
+
)
|
|
49
110
|
context.configure(
|
|
50
|
-
url=
|
|
111
|
+
url=db_url.render_as_string(hide_password=False),
|
|
51
112
|
target_metadata=target_metadata,
|
|
52
113
|
literal_binds=True,
|
|
53
114
|
dialect_opts={"paramstyle": "named"},
|
|
115
|
+
version_table_schema="public",
|
|
54
116
|
)
|
|
55
117
|
|
|
56
118
|
with context.begin_transaction():
|
|
@@ -73,25 +135,8 @@ def run_migrations_online() -> None:
|
|
|
73
135
|
and associate a connection with the context.
|
|
74
136
|
|
|
75
137
|
"""
|
|
76
|
-
load_dotenv()
|
|
77
138
|
connectable = create_sqlalchemy_engine()
|
|
78
139
|
|
|
79
|
-
if "prod" in os.getenv("PG_HOST").lower():
|
|
80
|
-
print("Connecting to production database for migrations!")
|
|
81
|
-
input("Press Enter to continue...")
|
|
82
|
-
elif (
|
|
83
|
-
"dev" in os.getenv("PG_HOST" "").lower()
|
|
84
|
-
and "staging" in os.getenv("PG_DB" "").lower()
|
|
85
|
-
):
|
|
86
|
-
print("Connecting to staging database for migrations!")
|
|
87
|
-
elif (
|
|
88
|
-
"dev" in os.getenv("PG_HOST" "").lower()
|
|
89
|
-
and not "staging" in os.getenv("PG_DB" "").lower()
|
|
90
|
-
):
|
|
91
|
-
print("Connecting to dev database for migrations!")
|
|
92
|
-
else:
|
|
93
|
-
print("Connecting to unknown database for migrations!")
|
|
94
|
-
|
|
95
140
|
with connectable.connect() as connection:
|
|
96
141
|
context.configure(
|
|
97
142
|
connection=connection,
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"""add feature, thread and guest_feature_usage tables
|
|
2
|
+
|
|
3
|
+
Revision ID: 1_5_7
|
|
4
|
+
Revises: 1_5_6
|
|
5
|
+
Create Date: 2026-11-18 16:00:00.000000
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
from typing import Sequence, Union
|
|
9
|
+
|
|
10
|
+
from alembic import op
|
|
11
|
+
import sqlalchemy as sa
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
# revision identifiers, used by Alembic.
|
|
15
|
+
revision: str = '1_5_7'
|
|
16
|
+
down_revision: Union[str, None] = '1_5_6'
|
|
17
|
+
branch_labels: Union[str, Sequence[str], None] = None
|
|
18
|
+
depends_on: Union[str, Sequence[str], None] = None
|
|
19
|
+
|
|
20
|
+
SCHEMA = "user_related"
|
|
21
|
+
|
|
22
|
+
# created here so every environment has the same rows; code finds them by title
|
|
23
|
+
FEATURE_TITLES = ["chat", "microlearning", "syllabus"]
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def upgrade() -> None:
|
|
27
|
+
feature = op.create_table(
|
|
28
|
+
"feature",
|
|
29
|
+
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
|
|
30
|
+
sa.Column("title", sa.String(), nullable=False),
|
|
31
|
+
sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("NOW()"), nullable=False),
|
|
32
|
+
sa.PrimaryKeyConstraint("id"),
|
|
33
|
+
sa.UniqueConstraint("title", name="feature_title_key"),
|
|
34
|
+
schema=SCHEMA,
|
|
35
|
+
)
|
|
36
|
+
op.bulk_insert(feature, [{"title": title} for title in FEATURE_TITLES])
|
|
37
|
+
|
|
38
|
+
op.create_table(
|
|
39
|
+
"thread",
|
|
40
|
+
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
|
|
41
|
+
sa.Column("inferred_user_id", sa.Uuid(), nullable=False),
|
|
42
|
+
sa.Column("feature_id", sa.Uuid(), nullable=False),
|
|
43
|
+
sa.Column("title", sa.String(), nullable=True),
|
|
44
|
+
sa.Column("current_step", sa.Integer(), nullable=True),
|
|
45
|
+
sa.Column("max_step", sa.Integer(), nullable=True),
|
|
46
|
+
sa.Column(
|
|
47
|
+
"is_finished",
|
|
48
|
+
sa.Boolean(),
|
|
49
|
+
sa.Computed("COALESCE(current_step >= max_step, false)", persisted=True),
|
|
50
|
+
nullable=False,
|
|
51
|
+
),
|
|
52
|
+
sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("NOW()"), nullable=False),
|
|
53
|
+
sa.Column("updated_at", sa.TIMESTAMP(), server_default=sa.text("NOW()"), nullable=False),
|
|
54
|
+
sa.PrimaryKeyConstraint("id"),
|
|
55
|
+
sa.ForeignKeyConstraint(
|
|
56
|
+
["inferred_user_id"],
|
|
57
|
+
[f"{SCHEMA}.inferred_user.id"],
|
|
58
|
+
name="thread_inferred_user_id_fkey",
|
|
59
|
+
ondelete="CASCADE",
|
|
60
|
+
),
|
|
61
|
+
sa.ForeignKeyConstraint(
|
|
62
|
+
["feature_id"],
|
|
63
|
+
[f"{SCHEMA}.feature.id"],
|
|
64
|
+
name="thread_feature_id_fkey",
|
|
65
|
+
),
|
|
66
|
+
schema=SCHEMA,
|
|
67
|
+
)
|
|
68
|
+
op.create_index(
|
|
69
|
+
"thread_user_feature_updated_idx",
|
|
70
|
+
"thread",
|
|
71
|
+
["inferred_user_id", "feature_id", sa.text("updated_at DESC")],
|
|
72
|
+
schema=SCHEMA,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
op.create_table(
|
|
76
|
+
"guest_feature_usage",
|
|
77
|
+
sa.Column("id", sa.Uuid(), server_default=sa.text("gen_random_uuid()"), nullable=False),
|
|
78
|
+
sa.Column("inferred_user_id", sa.Uuid(), nullable=False),
|
|
79
|
+
sa.Column("feature_id", sa.Uuid(), nullable=False),
|
|
80
|
+
sa.Column("action", sa.String(), nullable=False),
|
|
81
|
+
sa.Column("created_at", sa.TIMESTAMP(), server_default=sa.text("NOW()"), nullable=False),
|
|
82
|
+
sa.PrimaryKeyConstraint("id"),
|
|
83
|
+
sa.ForeignKeyConstraint(
|
|
84
|
+
["inferred_user_id"],
|
|
85
|
+
[f"{SCHEMA}.inferred_user.id"],
|
|
86
|
+
name="guest_feature_usage_inferred_user_id_fkey",
|
|
87
|
+
ondelete="CASCADE",
|
|
88
|
+
),
|
|
89
|
+
sa.ForeignKeyConstraint(
|
|
90
|
+
["feature_id"],
|
|
91
|
+
[f"{SCHEMA}.feature.id"],
|
|
92
|
+
name="guest_feature_usage_feature_id_fkey",
|
|
93
|
+
),
|
|
94
|
+
schema=SCHEMA,
|
|
95
|
+
)
|
|
96
|
+
op.create_index(
|
|
97
|
+
"guest_feature_usage_user_feature_action_created_idx",
|
|
98
|
+
"guest_feature_usage",
|
|
99
|
+
["inferred_user_id", "feature_id", "action", "created_at"],
|
|
100
|
+
schema=SCHEMA,
|
|
101
|
+
)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def downgrade() -> None:
|
|
105
|
+
op.drop_index(
|
|
106
|
+
"guest_feature_usage_user_feature_action_created_idx",
|
|
107
|
+
table_name="guest_feature_usage",
|
|
108
|
+
schema=SCHEMA,
|
|
109
|
+
)
|
|
110
|
+
op.drop_table("guest_feature_usage", schema=SCHEMA)
|
|
111
|
+
op.drop_index("thread_user_feature_updated_idx", table_name="thread", schema=SCHEMA)
|
|
112
|
+
op.drop_table("thread", schema=SCHEMA)
|
|
113
|
+
op.drop_table("feature", schema=SCHEMA)
|
|
@@ -19,13 +19,10 @@ depends_on: Union[str, Sequence[str], None] = None
|
|
|
19
19
|
|
|
20
20
|
|
|
21
21
|
def upgrade() -> None:
|
|
22
|
-
op.execute(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
)
|
|
27
|
-
op.execute(
|
|
28
|
-
"""
|
|
22
|
+
op.execute("""
|
|
23
|
+
DROP MATERIALIZED VIEW IF EXISTS corpus_related.corpus_name_embedding_model_lang;
|
|
24
|
+
""")
|
|
25
|
+
op.execute("""
|
|
29
26
|
|
|
30
27
|
CREATE MATERIALIZED VIEW corpus_related.corpus_name_embedding_model_lang
|
|
31
28
|
TABLESPACE pg_default
|
|
@@ -67,18 +64,14 @@ WHERE
|
|
|
67
64
|
rn = 1
|
|
68
65
|
|
|
69
66
|
WITH DATA;
|
|
70
|
-
"""
|
|
71
|
-
)
|
|
67
|
+
""")
|
|
72
68
|
|
|
73
69
|
|
|
74
70
|
def downgrade() -> None:
|
|
75
|
-
op.execute(
|
|
76
|
-
"""
|
|
71
|
+
op.execute("""
|
|
77
72
|
DROP MATERIALIZED VIEW corpus_related.corpus_name_embedding_model_lang;
|
|
78
|
-
"""
|
|
79
|
-
|
|
80
|
-
op.execute(
|
|
81
|
-
"""
|
|
73
|
+
""")
|
|
74
|
+
op.execute("""
|
|
82
75
|
CREATE MATERIALIZED VIEW corpus_related.corpus_name_embedding_model_lang
|
|
83
76
|
TABLESPACE pg_default
|
|
84
77
|
AS SELECT corpus.source_name,
|
|
@@ -89,5 +82,4 @@ def downgrade() -> None:
|
|
|
89
82
|
JOIN corpus_related.embedding_model ON embedding_model.id = corpus_embedding_model.embedding_model_id
|
|
90
83
|
WHERE corpus.is_active
|
|
91
84
|
WITH DATA;
|
|
92
|
-
"""
|
|
93
|
-
)
|
|
85
|
+
""")
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/user_related.py
RENAMED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
from datetime import datetime
|
|
2
2
|
from uuid import UUID
|
|
3
3
|
|
|
4
|
-
from sqlalchemy import ForeignKey, func, types
|
|
4
|
+
from sqlalchemy import Computed, ForeignKey, Index, func, text, types
|
|
5
5
|
from sqlalchemy.dialects.postgresql import ENUM, TIMESTAMP
|
|
6
6
|
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
|
7
7
|
|
|
@@ -304,3 +304,139 @@ class AnalyticForm(Base):
|
|
|
304
304
|
server_default="NOW()",
|
|
305
305
|
)
|
|
306
306
|
session = relationship("Session", foreign_keys=[session_id])
|
|
307
|
+
|
|
308
|
+
|
|
309
|
+
class Feature(Base):
|
|
310
|
+
"""
|
|
311
|
+
A WeLearn feature a user can start runs of (chat, microlearning...). Rows are
|
|
312
|
+
created by the migrations, and code finds a feature by its unique title.
|
|
313
|
+
"""
|
|
314
|
+
|
|
315
|
+
__tablename__ = "feature"
|
|
316
|
+
__table_args__ = {"schema": DbSchemaEnum.USER_RELATED.value}
|
|
317
|
+
|
|
318
|
+
id: Mapped[UUID] = mapped_column(
|
|
319
|
+
types.Uuid, primary_key=True, nullable=False, server_default="gen_random_uuid()"
|
|
320
|
+
)
|
|
321
|
+
title: Mapped[str] = mapped_column(nullable=False, unique=True)
|
|
322
|
+
created_at: Mapped[datetime] = mapped_column(
|
|
323
|
+
TIMESTAMP(timezone=False),
|
|
324
|
+
nullable=False,
|
|
325
|
+
default=func.localtimestamp(),
|
|
326
|
+
server_default="NOW()",
|
|
327
|
+
)
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
class Thread(Base):
|
|
331
|
+
"""
|
|
332
|
+
One run of a feature by a user: a chat conversation, a microlearning... For a
|
|
333
|
+
chat, the id is the agent's thread id. is_finished is computed by Postgres
|
|
334
|
+
from current_step and max_step, so it can't be written; a thread without
|
|
335
|
+
steps is never finished.
|
|
336
|
+
"""
|
|
337
|
+
|
|
338
|
+
__tablename__ = "thread"
|
|
339
|
+
__table_args__ = (
|
|
340
|
+
Index(
|
|
341
|
+
"thread_user_feature_updated_idx",
|
|
342
|
+
"inferred_user_id",
|
|
343
|
+
"feature_id",
|
|
344
|
+
text("updated_at DESC"),
|
|
345
|
+
),
|
|
346
|
+
{"schema": DbSchemaEnum.USER_RELATED.value},
|
|
347
|
+
)
|
|
348
|
+
|
|
349
|
+
id: Mapped[UUID] = mapped_column(
|
|
350
|
+
types.Uuid, primary_key=True, nullable=False, server_default="gen_random_uuid()"
|
|
351
|
+
)
|
|
352
|
+
inferred_user_id: Mapped[UUID] = mapped_column(
|
|
353
|
+
types.Uuid,
|
|
354
|
+
ForeignKey(
|
|
355
|
+
f"{DbSchemaEnum.USER_RELATED.value}.inferred_user.id",
|
|
356
|
+
name="thread_inferred_user_id_fkey",
|
|
357
|
+
ondelete="CASCADE",
|
|
358
|
+
),
|
|
359
|
+
nullable=False,
|
|
360
|
+
)
|
|
361
|
+
feature_id: Mapped[UUID] = mapped_column(
|
|
362
|
+
types.Uuid,
|
|
363
|
+
ForeignKey(
|
|
364
|
+
f"{DbSchemaEnum.USER_RELATED.value}.feature.id",
|
|
365
|
+
name="thread_feature_id_fkey",
|
|
366
|
+
),
|
|
367
|
+
nullable=False,
|
|
368
|
+
)
|
|
369
|
+
title: Mapped[str | None]
|
|
370
|
+
current_step: Mapped[int | None]
|
|
371
|
+
max_step: Mapped[int | None]
|
|
372
|
+
is_finished: Mapped[bool] = mapped_column(
|
|
373
|
+
Computed("COALESCE(current_step >= max_step, false)", persisted=True)
|
|
374
|
+
)
|
|
375
|
+
created_at: Mapped[datetime] = mapped_column(
|
|
376
|
+
TIMESTAMP(timezone=False),
|
|
377
|
+
nullable=False,
|
|
378
|
+
default=func.localtimestamp(),
|
|
379
|
+
server_default="NOW()",
|
|
380
|
+
)
|
|
381
|
+
|
|
382
|
+
updated_at: Mapped[datetime] = mapped_column(
|
|
383
|
+
TIMESTAMP(timezone=False),
|
|
384
|
+
nullable=False,
|
|
385
|
+
default=func.localtimestamp(),
|
|
386
|
+
server_default="NOW()",
|
|
387
|
+
onupdate=func.localtimestamp(),
|
|
388
|
+
)
|
|
389
|
+
inferred_user: Mapped["InferredUser"] = relationship()
|
|
390
|
+
feature: Mapped["Feature"] = relationship()
|
|
391
|
+
|
|
392
|
+
|
|
393
|
+
class GuestFeatureUsage(Base):
|
|
394
|
+
"""
|
|
395
|
+
One limited action of a guest within a feature: a chat question, a syllabus
|
|
396
|
+
generated, feedback sent on it, course files read... The API counts a
|
|
397
|
+
guest's rows of the last 24h for one (feature, action) against its daily
|
|
398
|
+
quota, deletes the latest one to refund a failed use, and can purge older
|
|
399
|
+
rows.
|
|
400
|
+
"""
|
|
401
|
+
|
|
402
|
+
__tablename__ = "guest_feature_usage"
|
|
403
|
+
__table_args__ = (
|
|
404
|
+
Index(
|
|
405
|
+
"guest_feature_usage_user_feature_action_created_idx",
|
|
406
|
+
"inferred_user_id",
|
|
407
|
+
"feature_id",
|
|
408
|
+
"action",
|
|
409
|
+
"created_at",
|
|
410
|
+
),
|
|
411
|
+
{"schema": DbSchemaEnum.USER_RELATED.value},
|
|
412
|
+
)
|
|
413
|
+
|
|
414
|
+
id: Mapped[UUID] = mapped_column(
|
|
415
|
+
types.Uuid, primary_key=True, nullable=False, server_default="gen_random_uuid()"
|
|
416
|
+
)
|
|
417
|
+
inferred_user_id: Mapped[UUID] = mapped_column(
|
|
418
|
+
types.Uuid,
|
|
419
|
+
ForeignKey(
|
|
420
|
+
f"{DbSchemaEnum.USER_RELATED.value}.inferred_user.id",
|
|
421
|
+
name="guest_feature_usage_inferred_user_id_fkey",
|
|
422
|
+
ondelete="CASCADE",
|
|
423
|
+
),
|
|
424
|
+
nullable=False,
|
|
425
|
+
)
|
|
426
|
+
feature_id: Mapped[UUID] = mapped_column(
|
|
427
|
+
types.Uuid,
|
|
428
|
+
ForeignKey(
|
|
429
|
+
f"{DbSchemaEnum.USER_RELATED.value}.feature.id",
|
|
430
|
+
name="guest_feature_usage_feature_id_fkey",
|
|
431
|
+
),
|
|
432
|
+
nullable=False,
|
|
433
|
+
)
|
|
434
|
+
action: Mapped[str] = mapped_column(nullable=False)
|
|
435
|
+
created_at: Mapped[datetime] = mapped_column(
|
|
436
|
+
TIMESTAMP(timezone=False),
|
|
437
|
+
nullable=False,
|
|
438
|
+
default=func.localtimestamp(),
|
|
439
|
+
server_default="NOW()",
|
|
440
|
+
)
|
|
441
|
+
inferred_user: Mapped["InferredUser"] = relationship()
|
|
442
|
+
feature: Mapped["Feature"] = relationship()
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.3
|
|
2
|
-
Name: welearn-database
|
|
3
|
-
Version: 1.5.6.dev1
|
|
4
|
-
Summary: All stuff related to relationnal database from the WeLearn project
|
|
5
|
-
License: cc-by-sa-nc
|
|
6
|
-
Author: Théo
|
|
7
|
-
Author-email: theo.nardin@cri-paris.org
|
|
8
|
-
Requires-Python: >=3.10
|
|
9
|
-
Classifier: License :: Other/Proprietary License
|
|
10
|
-
Classifier: Programming Language :: Python :: 3
|
|
11
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
12
|
-
Classifier: Programming Language :: Python :: 3.11
|
|
13
|
-
Classifier: Programming Language :: Python :: 3.12
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
-
Requires-Dist: alembic (>=1.4.3,<2.0.0)
|
|
16
|
-
Requires-Dist: psycopg2-binary (>=2.9.11,<3.0.0)
|
|
17
|
-
Requires-Dist: python-dotenv (>=1.2.2,<2.0.0)
|
|
18
|
-
Requires-Dist: sqlalchemy (>=2.0.43,<3.0.0)
|
|
19
|
-
Description-Content-Type: text/markdown
|
|
20
|
-
|
|
21
|
-
# WeLearn Database
|
|
22
|
-
|
|
23
|
-
This repository contains the database schema and sample data for the WeLearn application, an online learning platform.
|
|
24
|
-
|
|
25
|
-
## Pypi Package
|
|
26
|
-
You can install this pacakge via pypi :
|
|
27
|
-
|
|
28
|
-
```bash
|
|
29
|
-
pip install welearn-database
|
|
30
|
-
```
|
|
31
|
-
or you can read pypi page [here](https://pypi.org/project/welearn-database/)
|
|
32
|
-
|
|
33
|
-
## Environment Variables
|
|
34
|
-
Before running the application, make sure to set the following environment variables:
|
|
35
|
-
```
|
|
36
|
-
PG_USER=<pg user>
|
|
37
|
-
PG_PASSWORD=<pg password>
|
|
38
|
-
PG_HOST=<pg address>
|
|
39
|
-
PG_PORT=<pg port, 5432 by default>
|
|
40
|
-
PG_DB=<pg database name>
|
|
41
|
-
PG_DRIVER=<driver to use, pg default is : postgresql+psycopg2>
|
|
42
|
-
PG_SCHEMA=document_related,corpus_related,user_related,agent_related
|
|
43
|
-
LOG_LEVEL=INFO
|
|
44
|
-
LOG_FORMAT=[%(asctime)s][%(name)s][%(levelname)s] - %(message)s
|
|
45
|
-
```
|
|
46
|
-
|
|
47
|
-
## Database Schema
|
|
48
|
-
The database schema is organized into four main schemas:
|
|
49
|
-
- `document_related`: Contains tables related to documents and their metadata.
|
|
50
|
-
- `corpus_related`: Contains tables related to corpora and their metadata.
|
|
51
|
-
- `user_related`: Contains tables related to users and their profiles.
|
|
52
|
-
- `agent_related`: Contains tables related to agents and their interactions.
|
|
53
|
-
|
|
54
|
-
## How to Use
|
|
55
|
-
Data models are defined using SQLAlchemy ORM. You can import the models and use them to interact with the database.
|
|
56
|
-
```python
|
|
57
|
-
from welearn_database.data.models import WeLearnDocument
|
|
58
|
-
```
|
|
59
|
-
Every model are accessible there, schema are handled under the hood.
|
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
# WeLearn Database
|
|
2
|
-
|
|
3
|
-
This repository contains the database schema and sample data for the WeLearn application, an online learning platform.
|
|
4
|
-
|
|
5
|
-
## Pypi Package
|
|
6
|
-
You can install this pacakge via pypi :
|
|
7
|
-
|
|
8
|
-
```bash
|
|
9
|
-
pip install welearn-database
|
|
10
|
-
```
|
|
11
|
-
or you can read pypi page [here](https://pypi.org/project/welearn-database/)
|
|
12
|
-
|
|
13
|
-
## Environment Variables
|
|
14
|
-
Before running the application, make sure to set the following environment variables:
|
|
15
|
-
```
|
|
16
|
-
PG_USER=<pg user>
|
|
17
|
-
PG_PASSWORD=<pg password>
|
|
18
|
-
PG_HOST=<pg address>
|
|
19
|
-
PG_PORT=<pg port, 5432 by default>
|
|
20
|
-
PG_DB=<pg database name>
|
|
21
|
-
PG_DRIVER=<driver to use, pg default is : postgresql+psycopg2>
|
|
22
|
-
PG_SCHEMA=document_related,corpus_related,user_related,agent_related
|
|
23
|
-
LOG_LEVEL=INFO
|
|
24
|
-
LOG_FORMAT=[%(asctime)s][%(name)s][%(levelname)s] - %(message)s
|
|
25
|
-
```
|
|
26
|
-
|
|
27
|
-
## Database Schema
|
|
28
|
-
The database schema is organized into four main schemas:
|
|
29
|
-
- `document_related`: Contains tables related to documents and their metadata.
|
|
30
|
-
- `corpus_related`: Contains tables related to corpora and their metadata.
|
|
31
|
-
- `user_related`: Contains tables related to users and their profiles.
|
|
32
|
-
- `agent_related`: Contains tables related to agents and their interactions.
|
|
33
|
-
|
|
34
|
-
## How to Use
|
|
35
|
-
Data models are defined using SQLAlchemy ORM. You can import the models and use them to interact with the database.
|
|
36
|
-
```python
|
|
37
|
-
from welearn_database.data.models import WeLearnDocument
|
|
38
|
-
```
|
|
39
|
-
Every model are accessible there, schema are handled under the hood.
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/alembic/script.py.mako
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/details_dict.py
RENAMED
|
File without changes
|
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/__init__.py
RENAMED
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/agent_related.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/data/models/grafana.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/modules/text_cleaning.py
RENAMED
|
File without changes
|
{welearn_database-1.5.6.dev1 → welearn_database-1.5.7}/welearn_database/regular_expression.py
RENAMED
|
File without changes
|