Migration for quotas added

This commit is contained in:
Profitroll 2023-11-25 18:05:12 +01:00
parent 0f423166f1
commit 0043abdbad
Signed by: profitroll
GPG Key ID: FA35CAB49DACD3B2
4 changed files with 47 additions and 0 deletions

View File

@ -0,0 +1,9 @@
from mongodb_migrations.base import BaseMigration
class Migration(BaseMigration):
def upgrade(self):
self.db.users.update_many({}, {"$set": {"quota": None}})
def downgrade(self):
self.db.test_collection.update_many({}, {"$unset": "quota"})

23
modules/migrator.py Normal file
View File

@ -0,0 +1,23 @@
from typing import Any, Mapping
from mongodb_migrations.cli import MigrationManager
from mongodb_migrations.config import Configuration
from modules.utils import configGet
def migrate_database() -> None:
"""Apply migrations from folder `migrations/` to the database"""
db_config: Mapping[str, Any] = configGet("database")
manager_config = Configuration(
{
"mongo_host": db_config["host"],
"mongo_port": db_config["port"],
"mongo_database": db_config["name"],
"mongo_username": db_config["user"],
"mongo_password": db_config["password"],
}
)
manager = MigrationManager(manager_config)
manager.run()

View File

@ -1,4 +1,5 @@
import logging
from argparse import ArgumentParser
from os import makedirs
from pathlib import Path
@ -6,6 +7,7 @@ from fastapi.responses import FileResponse
from modules.app import app
from modules.extensions_loader import dynamic_import_from_src
from modules.migrator import migrate_database
from modules.scheduler import scheduler
makedirs(Path("data/users"), exist_ok=True)
@ -27,3 +29,15 @@ dynamic_import_from_src("extensions", star_import=True)
# =================================================================================
scheduler.start()
parser = ArgumentParser(
prog="PhotosAPI",
description="Small and simple API server for saving photos and videos.",
)
parser.add_argument("--migrate", action="store_true")
args = parser.parse_args()
if args.migrate:
migrate_database()

View File

@ -2,6 +2,7 @@ aiofiles==23.2.1
apscheduler~=3.10.1
exif==1.6.0
fastapi[all]==0.104.1
mongodb-migrations==1.3.0
opencv-python~=4.8.1.78
passlib~=1.7.4
pymongo>=4.3.3