Merge pull request #6026 from FinnStutzenstein/variableMediafileTablename

Setting for the mediafile database tablename
This commit is contained in:
Finn Stutzenstein 2021-07-08 11:10:40 +02:00 committed by GitHub
commit 19a9eedf17
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 45 additions and 10 deletions

View File

@ -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=

View File

@ -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

View File

@ -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:

View File

@ -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")

View File

@ -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")

View File

@ -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)],
)