Skip to content

Commit

Permalink
fix(ena-submission): Use db url instead of host for connections, do n…
Browse files Browse the repository at this point in the history
…ot hardcode db name (#2890)

* Use url instead of host, do not hardcode db name

* Rename to schema from ena-submission to schema_ena_deposition
  • Loading branch information
anna-parker authored Sep 27, 2024
1 parent fcbe3a7 commit 43bdbf0
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 29 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-arm-images.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ jobs:
with:
build_arm: true
trigger-ena-submission:
uses: ./.github/workflows/ena-submission-image.yml
uses: ./.github/workflows/ena-submission-image.yaml
with:
build_arm: true
trigger-ena-submission-flyway:
uses: ./.github/workflows/ena-submission-flyway-image.yml
uses: ./.github/workflows/ena-submission-flyway-image.yaml
with:
build_arm: true
trigger-keycloakify:
Expand Down
2 changes: 1 addition & 1 deletion ena-submission/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ In our kubernetes pod we run flyway in a docker container, however when running
You can then create the schema using the following command:

```sh
flyway -user=postgres -password=unsecure -url=jdbc:postgresql://127.0.0.1:5432/loculus -schemas=ena-submission -locations=filesystem:./flyway/sql migrate
flyway -user=postgres -password=unsecure -url=jdbc:postgresql://127.0.0.1:5432/loculus -schemas=ena_deposition_schema -locations=filesystem:./flyway/sql migrate
```

If you want to test the docker image locally. It can be built and run using the commands:
Expand Down
2 changes: 1 addition & 1 deletion ena-submission/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ backend_url: http://localhost:8079/
keycloak_token_url: http://localhost:8083/realms/loculus/protocol/openid-connect/token
db_username: postgres
db_password: unsecure
db_host: "127.0.0.1"
db_url: "jdbc:postgresql://127.0.0.1:5432/loculus?options=-c%20search_path%3Dena_deposition_schema"
organisms:
cchf:
ingest:
Expand Down
2 changes: 1 addition & 1 deletion ena-submission/flyway/conf/flyway.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
flyway.locations=filesystem:/flyway/sql
flyway.schemas=ena-submission
flyway.schemas=ena_deposition_schema
flyway.baselineOnMigrate=true
4 changes: 2 additions & 2 deletions ena-submission/scripts/create_assembly.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class Config:
password: str
db_username: str
db_password: str
db_host: str
db_url: str
db_name: str
unique_project_suffix: str
ena_submission_url: str
Expand Down Expand Up @@ -565,7 +565,7 @@ def create_assembly(
config = Config(**relevant_config)
logger.info(f"Config: {config}")

db_config = db_init(config.db_password, config.db_username, config.db_host)
db_config = db_init(config.db_password, config.db_username, config.db_url)
slack_config = slack_conn_init(
slack_hook_default=config.slack_hook,
slack_token_default=config.slack_token,
Expand Down
4 changes: 2 additions & 2 deletions ena-submission/scripts/create_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ class Config:
password: str
db_username: str
db_password: str
db_host: str
db_url: str
db_name: str
unique_project_suffix: str
ena_submission_url: str
Expand Down Expand Up @@ -389,7 +389,7 @@ def create_project(log_level, config_file, test=False, time_between_iterations=1
config = Config(**relevant_config)
logger.info(f"Config: {config}")

db_config = db_init(config.db_password, config.db_username, config.db_host)
db_config = db_init(config.db_password, config.db_username, config.db_url)
slack_config = slack_conn_init(
slack_hook_default=config.slack_hook,
slack_token_default=config.slack_token,
Expand Down
4 changes: 2 additions & 2 deletions ena-submission/scripts/create_sample.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class Config:
password: str
db_username: str
db_password: str
db_host: str
db_url: str
db_name: str
unique_project_suffix: str
ena_submission_url: str
Expand Down Expand Up @@ -431,7 +431,7 @@ def create_sample(log_level, config_file, test=False, time_between_iterations=10
config = Config(**relevant_config)
logger.info(f"Config: {config}")

db_config = db_init(config.db_password, config.db_username, config.db_host)
db_config = db_init(config.db_password, config.db_username, config.db_url)
slack_config = slack_conn_init(
slack_hook_default=config.slack_hook,
slack_token_default=config.slack_token,
Expand Down
4 changes: 2 additions & 2 deletions ena-submission/scripts/get_ena_submission_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Config:
ingest_pipeline_submitter: str
db_username: str
db_password: str
db_host: str
db_url: str
slack_hook: str
slack_token: str
slack_channel_id: str
Expand Down Expand Up @@ -125,7 +125,7 @@ def get_ena_submission_list(log_level, config_file, output_file):
db_config = db_init(
db_password_default=config.db_password,
db_username_default=config.db_username,
db_host_default=config.db_host,
db_url_default=config.db_url,
)

entries_to_submit = {}
Expand Down
30 changes: 23 additions & 7 deletions ena-submission/scripts/submission_db_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import re
from dataclasses import dataclass
from datetime import datetime, timedelta
from enum import Enum
Expand All @@ -9,8 +10,24 @@
from psycopg2.pool import SimpleConnectionPool


def convert_jdbc_to_psycopg2(jdbc_url):
jdbc_pattern = r"jdbc:postgresql://(?P<host>[^:/]+)(?::(?P<port>\d+))?/(?P<dbname>[^?]+)"

match = re.match(jdbc_pattern, jdbc_url)

if not match:
msg = "Invalid JDBC URL format."
raise ValueError(msg)

host = match.group("host")
port = match.group("port") or "5432" # Default to 5432 if no port is provided
dbname = match.group("dbname")

return f"postgresql://{host}:{port}/{dbname}"


def db_init(
db_password_default: str, db_username_default: str, db_host_default: str
db_password_default: str, db_username_default: str, db_url_default: str
) -> SimpleConnectionPool:
db_password = os.getenv("DB_PASSWORD")
if not db_password:
Expand All @@ -20,18 +37,17 @@ def db_init(
if not db_username:
db_username = db_username_default

db_host = os.getenv("DB_HOST")
if not db_host:
db_host = db_host_default
db_url = os.getenv("DB_URL")
if not db_url:
db_url = db_url_default

db_dsn = convert_jdbc_to_psycopg2(db_url) + "?options=-c%20search_path%3Dena_deposition_schema"
return SimpleConnectionPool(
minconn=1,
maxconn=2, # max 7*2 connections to db allowed
dbname="loculus",
user=db_username,
host=db_host,
password=db_password,
options="-c search_path=ena-submission",
dsn=db_dsn,
)


Expand Down
5 changes: 2 additions & 3 deletions ena-submission/scripts/trigger_submission_to_ena.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This script adds all approved sequences to the submission_table
# - this should trigger the submission process.

import base64
import json
import logging
import time
Expand Down Expand Up @@ -34,7 +33,7 @@ class Config:
organism: str
db_username: str
db_password: str
db_host: str
db_url: str
github_url: str


Expand Down Expand Up @@ -94,7 +93,7 @@ def trigger_submission_to_ena(
config = Config(**relevant_config)
logger.info(f"Config: {config}")

db_config = db_init(config.db_password, config.db_username, config.db_host)
db_config = db_init(config.db_password, config.db_username, config.db_url)

if input_file:
# Get sequences to upload from a file
Expand Down
4 changes: 2 additions & 2 deletions ena-submission/scripts/upload_external_metadata_to_loculus.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class Config:
ena_specific_metadata: list[str]
db_username: str
db_password: str
db_host: str
db_url: str
slack_hook: str
slack_token: str
slack_channel_id: str
Expand Down Expand Up @@ -222,7 +222,7 @@ def upload_external_metadata(log_level, config_file, time_between_iterations=10)
relevant_config = {key: full_config.get(key, []) for key in Config.__annotations__}
config = Config(**relevant_config)
logger.info(f"Config: {config}")
db_config = db_init(config.db_password, config.db_username, config.db_host)
db_config = db_init(config.db_password, config.db_username, config.db_url)
slack_config = slack_conn_init(
slack_hook_default=config.slack_hook,
slack_token_default=config.slack_token,
Expand Down
8 changes: 4 additions & 4 deletions kubernetes/loculus/templates/ena-submission-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,11 @@ spec:
secretKeyRef:
name: service-accounts
key: externalMetadataUpdaterPassword
- name: DB_HOST
- name: DB_URL
valueFrom:
secretKeyRef:
name: database
key: host
key: url
- name: DB_USERNAME
valueFrom:
secretKeyRef:
Expand Down Expand Up @@ -152,11 +152,11 @@ spec:
secretKeyRef:
name: service-accounts
key: externalMetadataUpdaterPassword
- name: DB_HOST
- name: DB_URL
valueFrom:
secretKeyRef:
name: database
key: host
key: url
- name: DB_USERNAME
valueFrom:
secretKeyRef:
Expand Down

0 comments on commit 43bdbf0

Please sign in to comment.