2021-05-20 01:57:40 +02:00
|
|
|
import logging
|
2022-10-14 22:11:27 +02:00
|
|
|
from logging.config import dictConfig
|
2021-05-02 16:51:54 +02:00
|
|
|
from pathlib import Path
|
2022-01-26 10:11:16 +01:00
|
|
|
|
|
|
|
import pkg_resources
|
2022-10-14 22:11:27 +02:00
|
|
|
import urllib3.util
|
2021-12-05 18:37:35 +01:00
|
|
|
from aerich import Command
|
2022-10-14 22:11:27 +02:00
|
|
|
from tortoise import Tortoise
|
|
|
|
|
|
|
|
from mobilizon_reshare.config.config import (
|
|
|
|
get_settings,
|
|
|
|
get_settings_without_validation,
|
|
|
|
)
|
2021-08-16 10:49:52 +02:00
|
|
|
from mobilizon_reshare.config.publishers import publisher_names
|
2021-11-24 23:58:06 +01:00
|
|
|
from mobilizon_reshare.storage.query.write import update_publishers
|
2021-12-05 18:37:35 +01:00
|
|
|
|
2021-05-20 01:57:40 +02:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2021-05-02 16:51:54 +02:00
|
|
|
|
2022-10-14 22:11:27 +02:00
|
|
|
def get_db_url() -> urllib3.util.Url:
|
|
|
|
return urllib3.util.parse_url(get_settings_without_validation().db_url)
|
2021-12-05 18:37:35 +01:00
|
|
|
|
|
|
|
|
2021-12-14 22:37:49 +01:00
|
|
|
def get_tortoise_orm():
|
2022-10-14 22:11:27 +02:00
|
|
|
|
2021-12-14 22:37:49 +01:00
|
|
|
return {
|
2022-10-14 22:11:27 +02:00
|
|
|
"connections": {"default": get_db_url().url},
|
2021-12-14 22:37:49 +01:00
|
|
|
"apps": {
|
|
|
|
"models": {
|
|
|
|
"models": [
|
|
|
|
"mobilizon_reshare.models.event",
|
|
|
|
"mobilizon_reshare.models.notification",
|
|
|
|
"mobilizon_reshare.models.publication",
|
|
|
|
"mobilizon_reshare.models.publisher",
|
|
|
|
"aerich.models",
|
|
|
|
],
|
|
|
|
"default_connection": "default",
|
|
|
|
},
|
2021-12-05 18:37:35 +01:00
|
|
|
},
|
2022-01-26 10:11:16 +01:00
|
|
|
# always store UTC time in database
|
|
|
|
"use_tz": True,
|
2021-12-14 22:37:49 +01:00
|
|
|
}
|
2021-12-07 16:58:12 +01:00
|
|
|
|
|
|
|
|
2021-12-18 12:54:09 +01:00
|
|
|
TORTOISE_ORM = get_tortoise_orm()
|
|
|
|
|
|
|
|
|
2021-08-16 10:49:52 +02:00
|
|
|
class MoReDB:
|
2022-10-14 22:11:27 +02:00
|
|
|
def get_migration_location(self):
|
|
|
|
scheme = get_db_url().scheme
|
|
|
|
return pkg_resources.resource_filename(
|
|
|
|
"mobilizon_reshare", f"migrations/{scheme}"
|
|
|
|
)
|
2021-05-02 16:51:54 +02:00
|
|
|
|
2021-12-05 17:11:24 +01:00
|
|
|
async def _implement_db_changes(self):
|
2022-10-14 22:11:27 +02:00
|
|
|
logging.info("Performing aerich migrations.")
|
2022-01-26 10:11:16 +01:00
|
|
|
command = Command(
|
2022-10-14 22:11:27 +02:00
|
|
|
tortoise_config=get_tortoise_orm(),
|
2022-01-26 10:11:16 +01:00
|
|
|
app="models",
|
2022-10-14 22:11:27 +02:00
|
|
|
location=self.get_migration_location(),
|
2022-01-26 10:11:16 +01:00
|
|
|
)
|
|
|
|
await command.init()
|
2022-02-09 00:54:56 +01:00
|
|
|
migrations = await command.upgrade()
|
|
|
|
if migrations:
|
2022-10-14 22:11:27 +02:00
|
|
|
logging.info("Updated database to latest version")
|
2021-12-04 17:55:43 +01:00
|
|
|
|
2021-05-02 16:51:54 +02:00
|
|
|
async def setup(self):
|
2022-12-07 21:46:57 +01:00
|
|
|
tortoise_config = get_tortoise_orm()
|
|
|
|
Tortoise.init_models(
|
|
|
|
tortoise_config["apps"]["models"]["models"], "models", _init_relations=True
|
|
|
|
)
|
2021-12-14 22:37:49 +01:00
|
|
|
await self._implement_db_changes()
|
2022-12-07 21:46:57 +01:00
|
|
|
await Tortoise.init(config=tortoise_config)
|
2022-10-14 22:11:27 +02:00
|
|
|
await Tortoise.generate_schemas()
|
2021-08-05 00:29:50 +02:00
|
|
|
await update_publishers(publisher_names)
|
|
|
|
|
2021-05-31 01:19:24 +02:00
|
|
|
|
2022-10-14 22:11:27 +02:00
|
|
|
class MoReSQLiteDB(MoReDB):
|
|
|
|
def __init__(self):
|
|
|
|
|
|
|
|
if (
|
|
|
|
get_db_url().path
|
|
|
|
): # if in-memory sqlite there's no path so nothing has to be initialized
|
|
|
|
self.path = Path(get_db_url().path)
|
|
|
|
# TODO: Check if DB is openable/"queriable"
|
|
|
|
self.is_init = self.path.exists() and (not self.path.is_dir())
|
|
|
|
if not self.is_init:
|
|
|
|
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
|
|
|
|
|
|
|
2021-07-21 09:08:19 +02:00
|
|
|
async def tear_down():
|
|
|
|
return await Tortoise.close_connections()
|
2022-10-14 22:11:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
async def init(init_logging=True):
|
|
|
|
|
|
|
|
if init_logging:
|
|
|
|
dictConfig(get_settings()["logging"])
|
|
|
|
|
|
|
|
# init storage
|
|
|
|
url = get_db_url()
|
|
|
|
if url.scheme == "sqlite":
|
|
|
|
db = MoReSQLiteDB()
|
|
|
|
else:
|
|
|
|
db = MoReDB()
|
|
|
|
await db.setup()
|