mirror of
https://github.com/Monadical-SAS/reflector.git
synced 2025-12-20 20:29:06 +00:00
* Sync authentik users * Migrate user_id from uid to id * Fix auth user id * Fix ci migration test * Fix meeting token creation * Move user id migration to a script * Add user on first login * Fix migration chain * Rename uid column to authentik_uid * Fix broken ws test
52 lines
1.4 KiB
Python
52 lines
1.4 KiB
Python
import contextvars
|
|
from typing import Optional
|
|
|
|
import databases
|
|
import sqlalchemy
|
|
|
|
from reflector.events import subscribers_shutdown, subscribers_startup
|
|
from reflector.settings import settings
|
|
|
|
metadata = sqlalchemy.MetaData()
|
|
|
|
_database_context: contextvars.ContextVar[Optional[databases.Database]] = (
|
|
contextvars.ContextVar("database", default=None)
|
|
)
|
|
|
|
|
|
def get_database() -> databases.Database:
|
|
"""Get database instance for current asyncio context"""
|
|
db = _database_context.get()
|
|
if db is None:
|
|
db = databases.Database(settings.DATABASE_URL)
|
|
_database_context.set(db)
|
|
return db
|
|
|
|
|
|
# import models
|
|
import reflector.db.calendar_events # noqa
|
|
import reflector.db.daily_participant_sessions # noqa
|
|
import reflector.db.meetings # noqa
|
|
import reflector.db.recordings # noqa
|
|
import reflector.db.rooms # noqa
|
|
import reflector.db.transcripts # noqa
|
|
import reflector.db.user_api_keys # noqa
|
|
import reflector.db.users # noqa
|
|
|
|
kwargs = {}
|
|
if "postgres" not in settings.DATABASE_URL:
|
|
raise Exception("Only postgres database is supported in reflector")
|
|
engine = sqlalchemy.create_engine(settings.DATABASE_URL, **kwargs)
|
|
|
|
|
|
@subscribers_startup.append
|
|
async def database_connect(_):
|
|
database = get_database()
|
|
await database.connect()
|
|
|
|
|
|
@subscribers_shutdown.append
|
|
async def database_disconnect(_):
|
|
database = get_database()
|
|
await database.disconnect()
|