Skip to content

Commit

Permalink
Migrate to Postgres 17
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Nov 6, 2024
1 parent 48f8f29 commit 0fd1fbc
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 8 deletions.
4 changes: 2 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyproj import Transformer

NAME = 'openaedmap-backend'
VERSION = '2.13.0'
VERSION = '2.14.0'
CREATED_BY = f'{NAME} {VERSION}'
WEBSITE = 'https://openaedmap.org'

Expand All @@ -16,7 +16,7 @@
LOG_LEVEL = 'DEBUG'

POSTGRES_LOG = os.getenv('POSTGRES_LOG', '0').strip().lower() in ('1', 'true', 'yes')
POSTGRES_URL = 'postgresql+asyncpg://postgres:postgres@/postgres?host=/tmp/openaedmap-postgres'
POSTGRES_URL = 'postgresql+asyncpg://postgres@/postgres?host=/tmp/openaedmap-postgres'
VALKEY_URL = os.getenv('VALKEY_URL', 'unix:///tmp/openaedmap-valkey.sock?protocol=3')

DEFAULT_CACHE_MAX_AGE = timedelta(minutes=1)
Expand Down
2 changes: 1 addition & 1 deletion config/alembic.ini
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ sqlalchemy.url = driver://user:pass@localhost/dbname
hooks = ruff
ruff.type = exec
ruff.executable = ruff
ruff.options = --fix REVISION_SCRIPT_FILENAME
ruff.options = check --fix REVISION_SCRIPT_FILENAME

# Logging configuration
[loggers]
Expand Down
3 changes: 2 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,6 @@ services:
- ${LISTEN:-80}:8000

volumes:
- ./data/postgres:/app/data/postgres
- ./data/postgres:/app/data/postgres16
- ./data/postgres17:/app/data/postgres
- /mnt/data/${TAG}/photos:/app/data/photos
6 changes: 6 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
from anyio import create_task_group
from fastapi import APIRouter, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from sqlalchemy import text
from starlette_compress import CompressMiddleware

from db import db_write
from json_response import JSONResponseUTF8
from middlewares.cache_control_middleware import CacheControlMiddleware
from middlewares.cache_response_middleware import CacheResponseMiddleware
Expand All @@ -24,6 +26,10 @@ async def lifespan(_):
worker_state = await WorkerService.init()

if worker_state.is_primary:
async with db_write() as session:
await session.connection(execution_options={'isolation_level': 'AUTOCOMMIT'})
await session.execute(text('VACUUM ANALYZE'))

async with create_task_group() as tg:
await tg.start(CountryService.update_db_task)
await tg.start(AEDService.update_db_task)
Expand Down
22 changes: 18 additions & 4 deletions shell.nix
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,15 @@ let
'';
});

postgres16 = with pkgs; postgresql_16_jit.withPackages (ps: [ ps.postgis ]);
postgres17 = with pkgs; postgresql_17_jit.withPackages (ps: [ ps.postgis ]);

packages' = with pkgs; [
python'
uv
ruff
coreutils
(postgresql_16_jit.withPackages (ps: [ ps.postgis ]))
postgres17
valkey

# Scripts
Expand Down Expand Up @@ -58,9 +61,20 @@ let
--icu-locale=und \
--no-locale \
--text-search-config=pg_catalog.simple \
--auth=password \
--username=postgres \
--pwfile=<(echo postgres)
--auth=trust \
--username=postgres
if [ $(cat data/postgres16/PG_VERSION) = "16" ]; then
mkdir -p /tmp/openaedmap-postgres
cp data/postgres/pg_hba.conf data/postgres16/pg_hba.conf
${postgres16}/bin/pg_ctl start -o "-c config_file=config/postgres.conf" -D data/postgres16
${postgres16}/bin/pg_dump -h localhost -U postgres -d postgres -F c -Z 3 -f backup.pgdump
${postgres16}/bin/pg_ctl stop -D data/postgres16
${postgres17}/bin/pg_ctl start -o "-c config_file=config/postgres.conf" -D data/postgres
${postgres17}/bin/pg_restore -h localhost -U postgres -d postgres backup.pgdump
${postgres17}/bin/pg_ctl stop -D data/postgres
rm backup.pgdump
fi
fi
mkdir -p /tmp/openaedmap-postgres data/supervisor
Expand Down

0 comments on commit 0fd1fbc

Please sign in to comment.