From c47c429afb52fb7a1c750a6be594b9aeeaf2052a Mon Sep 17 00:00:00 2001 From: Rachel Lambda Samuelsson Date: Wed, 12 Jun 2024 21:39:37 +0200 Subject: [PATCH 1/5] migrations first taste --- dev.sh | 47 ++++++++++++++++++++++++++++ src/data.py | 13 ++++---- src/migrate.py | 49 ++++++++++++++++++++++++++++++ src/migrations/01-string-length.py | 25 +++++++++++++++ src/tv.py | 3 ++ 5 files changed, 130 insertions(+), 7 deletions(-) create mode 100755 dev.sh create mode 100644 src/migrate.py create mode 100644 src/migrations/01-string-length.py diff --git a/dev.sh b/dev.sh new file mode 100755 index 0000000..d7a6556 --- /dev/null +++ b/dev.sh @@ -0,0 +1,47 @@ +#!/bin/sh +set -e +printf 'Changing Working Directory to %s\n' "${0%/*}" +cd "${0%/*}" + +name="${0##*/}" + +setup() { + virtualenv venv + ./venv/bin/pip -r requirments.txt +} + +run() { + cd src + SECRET_KEY="${SECRET_KEY:-default}" ../venv/bin/uwsgi --enable-threads --http-socket :"${PORT:-8080}" --module tv:app +} + +shell() { + export PATH="$PWD/venv/bin:$PATH" + bash +} + +usage() { + cat < Date: Wed, 12 Jun 2024 21:55:10 +0200 Subject: [PATCH 2/5] document migrations and dev script --- README.md | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 0dc1efa..f252e0c 100644 --- a/README.md +++ b/README.md @@ -11,9 +11,21 @@ A day starts at 5:00 and ends at 5:00 the next day. This means that an end date ### Priority The default priority is 0. If a PR has its priority set to 1, it will be the only PR shown until its end date (useful for pubs etc.). +### Migrations +In order to enable changes to the database schema there is the `src/migrations` folder, files in this folder named like `07-myname.py` (-.py) are migrations which are used to migrate to new database schemas. + +Files in the migrations folder should define a function `upgrade()` which updates from the previous schema to the new one. Please thoroughly test that your migration works properly locally before pushing any remote changes. + +Concretely, when you make changes to `src/data.py`, you must also create a new migration, with number following the last existing one, which runs database operations migrating from the old format to the new one. + +The database tracks its current migration version and runs all new migrations on application startup. + +## Running locally +In order to aid running locally there is a shell script `dev.sh`, running `./dev.sh setup` sets up a python virtualenv with all the dependencies, running `./dev.sh run` will run the application on port `8080`, running `./dev.sh shell` will run bash with the virtual env tools in path. + ## Running in Docker The provided sample compose file should work out of the box provided a -`SECRET_KEY` env-variable. Aditional variables can be found in `src/config.py`. +`SECRET_KEY` env-variable. Additional variables can be found in `src/config.py`. -At first launc the database is populated with a user _admin_ with the password +At first launch the database is populated with a user _admin_ with the password _pass_. It is suggested you change this immediately. From 6817d226f70b3e1f3bdcd4cdeb052ee2296a1101 Mon Sep 17 00:00:00 2001 From: Rachel Lambda Samuelsson Date: Wed, 12 Jun 2024 22:23:09 +0200 Subject: [PATCH 3/5] allow only one entry in migration table --- src/migrate.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/migrate.py b/src/migrate.py index d09fbbd..1ac8202 100644 --- a/src/migrate.py +++ b/src/migrate.py @@ -8,8 +8,11 @@ def ensure_migration_table(): table = db.session.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='migration'") if len(table.fetchall()) != 1: print("Creating migration table") - db.session.execute("CREATE TABLE migration (version INTEGER)") - db.session.execute("INSERT INTO migration (version) VALUES (0)") + db.session.execute("CREATE TABLE migration (" + "id INTEGER PRIMARY KEY CHECK (id = 0)," + "version INTEGER" + ")") + db.session.execute("INSERT INTO migration (id, version) VALUES (0, 0)") db.session.commit() def get_migration_version(): From 2f367380a2b4e8e6822df19812584f0445a47280 Mon Sep 17 00:00:00 2001 From: Rachel Lambda Samuelsson Date: Thu, 13 Jun 2024 00:38:58 +0200 Subject: [PATCH 4/5] Update dev.sh Co-authored-by: Simon Renhult <49367447+Multipacker@users.noreply.github.com> --- dev.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev.sh b/dev.sh index d7a6556..c869543 100755 --- a/dev.sh +++ b/dev.sh @@ -7,7 +7,7 @@ name="${0##*/}" setup() { virtualenv venv - ./venv/bin/pip -r requirments.txt + ./venv/bin/pip install -r requirements.txt } run() { From 1e4f19176032495fa392b718d41384f938b51da0 Mon Sep 17 00:00:00 2001 From: Rachel Lambda Samuelsson Date: Tue, 25 Jun 2024 12:05:52 +0200 Subject: [PATCH 5/5] =?UTF-8?q?fixa=20feedback=20fr=C3=A5n=20cral?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/migrate.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/migrate.py b/src/migrate.py index 1ac8202..cfaa733 100644 --- a/src/migrate.py +++ b/src/migrate.py @@ -3,26 +3,28 @@ import re import importlib -def ensure_migration_table(): +def ensure_migration_table(commit=True): with app.app_context(): table = db.session.execute("SELECT name FROM sqlite_master WHERE type='table' AND name='migration'") if len(table.fetchall()) != 1: print("Creating migration table") - db.session.execute("CREATE TABLE migration (" + db.session.execute("CREATE TABLE IF NOT EXISTS migration (" "id INTEGER PRIMARY KEY CHECK (id = 0)," "version INTEGER" ")") db.session.execute("INSERT INTO migration (id, version) VALUES (0, 0)") - db.session.commit() + if commit: + db.session.commit() def get_migration_version(): - ensure_migration_table() + ensure_migration_table(commit=False) with app.app_context(): version = db.session.execute("SELECT version FROM migration").fetchall()[0][0]; db.session.commit() return version def set_migration_version(version): + ensure_migration_table(commit=False) with app.app_context(): db.session.execute(f"UPDATE MIGRATION SET version = :version", { 'version': version }); db.session.commit()