added migration generation script

This commit is contained in:
Simone Robutti 2022-10-07 20:04:14 +02:00
parent b6a0c792ae
commit 20c9f5e433
6 changed files with 94 additions and 4 deletions

View File

@ -0,0 +1,12 @@
version: "3.7"
services:
db:
image: postgres:13
env_file:
- ./.env
healthcheck:
test: ["CMD", "pg_isready", "-U", "mobilizon_reshare"]
interval: 5s
retries: 5
ports:
- 5432:5432

View File

@ -130,3 +130,7 @@ class CustomConfig:
def get_settings():
return CustomConfig.get_instance().settings
def get_settings_without_validation():
return build_settings()

View File

@ -7,7 +7,10 @@ import urllib3.util
from aerich import Command
from tortoise import Tortoise
from mobilizon_reshare.config.config import get_settings
from mobilizon_reshare.config.config import (
get_settings,
get_settings_without_validation,
)
from mobilizon_reshare.config.publishers import publisher_names
from mobilizon_reshare.storage.query.write import update_publishers
@ -15,10 +18,11 @@ logger = logging.getLogger(__name__)
def get_db_url() -> urllib3.util.Url:
return urllib3.util.parse_url(get_settings().db_url)
return urllib3.util.parse_url(get_settings_without_validation().db_url)
def get_tortoise_orm():
return {
"connections": {"default": get_db_url().url},
"apps": {
@ -49,7 +53,6 @@ class MoReDB:
)
async def _implement_db_changes(self):
logging.info("Performing aerich migrations.")
command = Command(
tortoise_config=get_tortoise_orm(),

2
poetry.lock generated
View File

@ -181,7 +181,7 @@ python-versions = ">=2.7, !=3.0.*, !=3.1.*, !=3.2.*, !=3.3.*, !=3.4.*"
[[package]]
name = "coverage"
version = "6.4.4"
version = "6.5.0"
description = "Code coverage measurement for Python"
category = "dev"
optional = false

View File

@ -0,0 +1,54 @@
#!/bin/sh
set -eu
get_abs_filename() {
# $1 : relative filename
echo "$(cd "$(dirname "$1")" && pwd)/"
}
PROJECT_DIR="$(get_abs_filename $0)/.."
cleanup() {
# cleaning sqlite db
echo "Removing /tmp/foo"
rm -rf /tmp/tmp.db
# shutting down postgres container
cd $PROJECT_DIR
docker-compose -f docker-compose-migration.yml down
}
# making sure we leave the system clean
trap cleanup EXIT
poetry install
# I activate the env instead of using poetry run because the pyproject in the migration folder gives it problems
. "$(poetry env info -p)/bin/activate"
# I create a new SQLite db to run the migrations and generate a new one
echo("Generating SQLite migrations")
export DYNACONF_DB_URL="sqlite:///tmp/tmp.db"
cd "$PROJECT_DIR/mobilizon_reshare/migrations/sqlite/"
aerich upgrade
aerich migrate
# I use a dedicated docker-compose file to spin up a postgres instance, connect to it, run the migrations and generate a
# new one
echo("Generating postgres migrations")
export DYNACONF_DB_URL="postgres://mobilizon_reshare:mobilizon_reshare@localhost:5432/mobilizon_reshare"
cd $PROJECT_DIR
docker-compose -f docker-compose-migration.yml up -d
cd "$PROJECT_DIR/mobilizon_reshare/migrations/postgres/"
aerich upgrade
aerich migrate
cleanup

17
scripts/test_cd.sh Executable file
View File

@ -0,0 +1,17 @@
#!/bin/sh
get_abs_filename() {
# $1 : relative filename
echo "$(cd "$(dirname "$1")" && pwd)/"
}
set -eu
PROJECT_DIR="$(get_abs_filename $0)/.."
echo $PROJECT_DIR
echo "$(pwd)"
cd "$PROJECT_DIR/mobilizon_reshare/migrations/sqlite/"
echo $(pwd)
cd $PROJECT_DIR
echo $(pwd)
cd "$PROJECT_DIR/mobilizon_reshare/migrations/postgres/"
echo $(pwd)