From 27831154fa5214782d5cc9ead0debf32464605eb Mon Sep 17 00:00:00 2001 From: Finn Stutzenstein Date: Thu, 22 Apr 2021 12:41:08 +0200 Subject: [PATCH] Setting for the mediafile database tablename --- docker/.env | 6 ++++++ docker/docker-compose.yml.m4 | 8 ++++++++ docker/docker-stack.yml.m4 | 8 ++++++++ server/docker/settings.py | 13 +++++++------ .../management/commands/export_mediafiles.py | 9 ++++++++- server/openslides/mediafiles/views.py | 11 ++++++++--- 6 files changed, 45 insertions(+), 10 deletions(-) diff --git a/docker/.env b/docker/.env index 540991f74..d4547fee7 100644 --- a/docker/.env +++ b/docker/.env @@ -103,6 +103,12 @@ DATABASE_HOST= DATABASE_PASSWORD= DATABASE_PORT= DATABASE_USER= +MEDIAFILE_DATABASE_NAME= +MEDIAFILE_DATABASE_USER= +MEDIAFILE_DATABASE_PASSWORD= +MEDIAFILE_DATABASE_HOST= +MEDIAFILE_DATABASE_PORT= +MEDIAFILE_DATABASE_TABLENAME= EMAIL_HOST= EMAIL_HOST_PASSWORD= EMAIL_HOST_USER= diff --git a/docker/docker-compose.yml.m4 b/docker/docker-compose.yml.m4 index 6ba6a3cbc..accadca46 100644 --- a/docker/docker-compose.yml.m4 +++ b/docker/docker-compose.yml.m4 @@ -72,6 +72,12 @@ x-osserver-env: &default-osserver-env DATABASE_PASSWORD: "ifenvelse(`DATABASE_PASSWORD', openslides)" DATABASE_PORT: ifenvelse(`DATABASE_PORT', 5432) DATABASE_USER: "ifenvelse(`DATABASE_USER', openslides)" + MEDIAFILE_DATABASE_NAME: "read_env(`MEDIAFILE_DATABASE_NAME')" + MEDIAFILE_DATABASE_USER: "read_env(`MEDIAFILE_DATABASE_USER')" + MEDIAFILE_DATABASE_PASSWORD: "read_env(`MEDIAFILE_DATABASE_PASSWORD')" + MEDIAFILE_DATABASE_HOST: "read_env(`MEDIAFILE_DATABASE_HOST')" + MEDIAFILE_DATABASE_PORT: "read_env(`MEDIAFILE_DATABASE_PORT')" + MEDIAFILE_DATABASE_TABLENAME: "read_env(`MEDIAFILE_DATABASE_TABLENAME')" DEFAULT_FROM_EMAIL: "ifenvelse(`DEFAULT_FROM_EMAIL', noreply@example.com)" EMAIL_HOST: "ifenvelse(`EMAIL_HOST', postfix)" EMAIL_HOST_PASSWORD: "ifenvelse(`EMAIL_HOST_PASSWORD',)" @@ -248,6 +254,8 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' - CACHE_SIZE=ifenvelse(`CACHE_SIZE', 10) - CACHE_DATA_MIN_SIZE_KB=ifenvelse(`CACHE_DATA_MIN_SIZE_KB', 0) - CACHE_DATA_MAX_SIZE_KB=ifenvelse(`CACHE_DATA_MAX_SIZE_KB', 10240) + - DATABASE_NAME=read_env(`MEDIAFILE_DATABASE_NAME') + - DATABASE_TABLE=read_env(`MEDIAFILE_DATABASE_TABLENAME') restart: always networks: - back diff --git a/docker/docker-stack.yml.m4 b/docker/docker-stack.yml.m4 index b6b8f11d8..c20164ecb 100644 --- a/docker/docker-stack.yml.m4 +++ b/docker/docker-stack.yml.m4 @@ -71,6 +71,12 @@ x-osserver-env: &default-osserver-env DATABASE_PASSWORD: "ifenvelse(`DATABASE_PASSWORD', openslides)" DATABASE_PORT: ifenvelse(`DATABASE_PORT', 5432) DATABASE_USER: "ifenvelse(`DATABASE_USER', openslides)" + MEDIAFILE_DATABASE_NAME: "read_env(`MEDIAFILE_DATABASE_NAME')" + MEDIAFILE_DATABASE_USER: "read_env(`MEDIAFILE_DATABASE_USER')" + MEDIAFILE_DATABASE_PASSWORD: "read_env(`MEDIAFILE_DATABASE_PASSWORD')" + MEDIAFILE_DATABASE_HOST: "read_env(`MEDIAFILE_DATABASE_HOST')" + MEDIAFILE_DATABASE_PORT: "read_env(`MEDIAFILE_DATABASE_PORT')" + MEDIAFILE_DATABASE_TABLENAME: "read_env(`MEDIAFILE_DATABASE_TABLENAME')" DEFAULT_FROM_EMAIL: "ifenvelse(`DEFAULT_FROM_EMAIL', noreply@example.com)" EMAIL_HOST: "ifenvelse(`EMAIL_HOST', postfix)" EMAIL_HOST_PASSWORD: "ifenvelse(`EMAIL_HOST_PASSWORD',)" @@ -271,6 +277,8 @@ ifelse(read_env(`PGNODE_3_ENABLED'), 1, `' - CACHE_SIZE=ifenvelse(`CACHE_SIZE', 10) - CACHE_DATA_MIN_SIZE_KB=ifenvelse(`CACHE_DATA_MIN_SIZE_KB', 0) - CACHE_DATA_MAX_SIZE_KB=ifenvelse(`CACHE_DATA_MAX_SIZE_KB', 10240) + - DATABASE_NAME=read_env(`MEDIAFILE_DATABASE_NAME') + - DATABASE_TABLE=read_env(`MEDIAFILE_DATABASE_TABLENAME') deploy: replicas: ifenvelse(`MEDIA_SERVICE_REPLICAS', 2) restart_policy: diff --git a/server/docker/settings.py b/server/docker/settings.py index 633749c26..897e06976 100644 --- a/server/docker/settings.py +++ b/server/docker/settings.py @@ -78,7 +78,7 @@ DATA_UPLOAD_MAX_MEMORY_SIZE = 104857600 DATABASES = { "default": { "ENGINE": "django.db.backends.postgresql", - "NAME": "openslides", + "NAME": get_env("DATABASE_NAME", "openslides"), "USER": get_env("DATABASE_USER", "openslides"), "PASSWORD": get_env("DATABASE_PASSWORD", "openslides"), "HOST": get_env("DATABASE_HOST", "db"), @@ -88,13 +88,14 @@ DATABASES = { }, "mediafiles": { "ENGINE": "django.db.backends.postgresql", - "NAME": "mediafiledata", - "USER": get_env("DATABASE_USER", "openslides"), - "PASSWORD": get_env("DATABASE_PASSWORD", "openslides"), - "HOST": get_env("DATABASE_HOST", "db"), - "PORT": get_env("DATABASE_PORT", "5432"), + "NAME": get_env("MEDIAFILE_DATABASE_NAME", "mediafiledata"), + "USER": get_env("MEDIAFILE_DATABASE_USER", "openslides"), + "PASSWORD": get_env("MEDIAFILE_DATABASE_PASSWORD", "openslides"), + "HOST": get_env("MEDIAFILE_DATABASE_HOST", "db"), + "PORT": get_env("MEDIAFILE_DATABASE_PORT", "5432"), }, } +MEDIAFILE_DATABASE_TABLENAME = get_env("MEDIAFILE_DATABASE_TABLENAME", "mediafile_data") # Redis REDIS_HOST = get_env("REDIS_HOST", "redis") diff --git a/server/openslides/mediafiles/management/commands/export_mediafiles.py b/server/openslides/mediafiles/management/commands/export_mediafiles.py index bd9629071..414afc138 100644 --- a/server/openslides/mediafiles/management/commands/export_mediafiles.py +++ b/server/openslides/mediafiles/management/commands/export_mediafiles.py @@ -1,6 +1,7 @@ import mimetypes from typing import cast +from django.conf import settings from django.core.management.base import BaseCommand from openslides.mediafiles.models import Mediafile @@ -19,6 +20,10 @@ class Command(BaseCommand): def handle(self, *args, **options): path = cast(str, options.get("path")) + mediafile_database_tablename = ( + settings.MEDIAFILE_DATABASE_TABLENAME or "mediafile_data" + ) + mediafile_count = 0 with open(path, "w") as f: f.write("-- Generated file to import into the media service db\n") @@ -27,7 +32,9 @@ class Command(BaseCommand): mediafile_count += 1 id = mediafile.id mimetype = mimetypes.guess_type(mediafile.mediafile.name)[0] - f.write("\nINSERT INTO mediafile_data (id, mimetype, data) VALUES ") + f.write( + f"\nINSERT INTO {mediafile_database_tablename} (id, mimetype, data) VALUES " + ) f.write(f"({id}, '{mimetype}', decode('") file_handle = open(mediafile.mediafile.path, "rb") diff --git a/server/openslides/mediafiles/views.py b/server/openslides/mediafiles/views.py index d74db036f..dc8f55220 100644 --- a/server/openslides/mediafiles/views.py +++ b/server/openslides/mediafiles/views.py @@ -28,7 +28,12 @@ logger = logging.getLogger(__name__) use_mediafile_database = "mediafiles" in connections if use_mediafile_database: - logger.info("Using a standalone mediafile database") + mediafile_database_tablename = ( + settings.MEDIAFILE_DATABASE_TABLENAME or "mediafile_data" + ) + logger.info( + f"Using a standalone mediafile database with the table '{mediafile_database_tablename}'" + ) max_upload_size = getattr( settings, "MEDIAFILE_MAX_SIZE", 100 * 1024 * 1024 @@ -146,7 +151,7 @@ class MediafileViewSet(ModelViewSet): if use_mediafile_database: with connections["mediafiles"].cursor() as cursor: cursor.execute( - "INSERT INTO mediafile_data (id, data, mimetype) VALUES (%s, %s, %s)", + f"INSERT INTO {mediafile_database_tablename} (id, data, mimetype) VALUES (%s, %s, %s)", [ db_mediafile.id, mediafile.open().read(), @@ -166,7 +171,7 @@ class MediafileViewSet(ModelViewSet): if use_mediafile_database: with connections["mediafiles"].cursor() as cursor: cursor.execute( - "DELETE FROM mediafile_data WHERE id IN %s", + f"DELETE FROM {mediafile_database_tablename} WHERE id IN %s", [tuple(id for id in deleted_ids)], )