Skip to content

Commit

Permalink
Photos migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Zaczero committed Apr 3, 2024
1 parent cadc6e0 commit ffe4bd5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -201,3 +201,5 @@ pyrightconfig.json

data/*
cert/*

.migrate.json
4 changes: 4 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from middlewares.version_middleware import VersionMiddleware
from services.aed_service import AEDService
from services.country_service import CountryService
from services.photo_service import PhotoService
from services.worker_service import WorkerService


Expand All @@ -28,6 +29,9 @@ async def lifespan(_):
await tg.start(CountryService.update_db_task)
await tg.start(AEDService.update_db_task)

# TODO: remove after migration
await PhotoService.migrate()

await worker_state.set_state('running')
yield

Expand Down
22 changes: 22 additions & 0 deletions services/photo_service.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
import logging
import pathlib
from io import BytesIO

from fastapi import UploadFile
from PIL import Image, ImageOps
from sentry_sdk import trace
from sqlalchemy import select

from config import IMAGE_LIMIT_PIXELS, IMAGE_MAX_FILE_SIZE
from db import db_read, db_write
from models.db.photo import Photo
from utils import JSON_DECODE


class PhotoService:
@staticmethod
async def migrate() -> None:
async with db_write() as session:
stmt = select(Photo.id).limit(1)
scalar = await session.scalar(stmt)
if scalar is not None:
return

logging.info('Migrating photos')
file = pathlib.Path('.migrate.json').read_bytes()
data: list[dict] = JSON_DECODE(file)
for item in data:
photo = Photo(
node_id=int(item['node_id']),
user_id=int(item['user_id']),
)
photo.id = item['id']
session.add(photo)

@staticmethod
@trace
async def get_by_id(id: str, *, check_file: bool = True) -> Photo | None:
Expand Down

0 comments on commit ffe4bd5

Please sign in to comment.