Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.1.0
6.2.0
9 changes: 1 addition & 8 deletions example/multi-database-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ databases:
database: app_main
allow_migration_for_empty_database: true
additional_parameters: ""
is_patroni_postgresql: true


# Analytics database
analytics:
Expand All @@ -32,8 +30,6 @@ databases:
database: analytics
allow_migration_for_empty_database: false
additional_parameters: ""
is_patroni_postgresql: true


# Audit database with custom parameters
audit:
Expand All @@ -47,8 +43,6 @@ databases:
database: audit_logs
allow_migration_for_empty_database: true
additional_parameters: "-x audit_mode=yes"
is_patroni_postgresql: true


# Example with environment variable substitution (if supported by your deployment)
reports:
Expand All @@ -61,5 +55,4 @@ databases:
port: 5432
database: reports
allow_migration_for_empty_database: false
additional_parameters: ""
is_patroni_postgresql: true
additional_parameters: ""
4 changes: 1 addition & 3 deletions example/simple-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ databases:
port: 5432
database: myapp
allow_migration_for_empty_database: true
is_patroni_postgresql: false

# Cache database
cache:
Expand All @@ -23,5 +22,4 @@ databases:
host: redis-db
port: 5432
database: cache_db
allow_migration_for_empty_database: false
is_patroni_postgresql: true
allow_migration_for_empty_database: false
1 change: 0 additions & 1 deletion src/chartreuse/chartreuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ def __init__(
allow_migration_for_empty_database=db_config.allow_migration_for_empty_database,
additional_parameters=additional_params,
alembic_section_name=db_name,
is_patroni_postgresql=db_config.is_patroni_postgresql,
)
self.migration_helpers[db_name] = helper

Expand Down
5 changes: 1 addition & 4 deletions src/chartreuse/config_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ class DatabaseConfig(BaseModel):
# Optional migration settings
allow_migration_for_empty_database: bool = Field(default=True, description="Allow migrations on empty database")
additional_parameters: str = Field(default="", description="Additional Alembic parameters")
is_patroni_postgresql: bool = Field(default=False, description="Whether this is a Patroni PostgreSQL database")

@computed_field
@property
Expand All @@ -40,7 +39,7 @@ def url(self) -> str:
@classmethod
def validate_dialect(cls, v: str) -> str:
"""Validate database dialect."""
supported_dialects = ["postgresql", "clickhouse"]
supported_dialects = ["postgresql", "mysql", "sqlite", "oracle", "mssql", "clickhouse"]
if v.lower() not in supported_dialects:
logger.warning("Dialect '%s' might not be supported. Supported dialects: %s", v, supported_dialects)
return v
Expand Down Expand Up @@ -97,7 +96,6 @@ def load_multi_database_config(config_path: str) -> dict[str, DatabaseConfig]:
alembic_config_file_path: alembic.ini
allow_migration_for_empty_database: true
additional_parameters: ""
is_patroni_postgresql: false

# For single database setups, just include one database:
# analytics:
Expand All @@ -111,7 +109,6 @@ def load_multi_database_config(config_path: str) -> dict[str, DatabaseConfig]:
# alembic_config_file_path: alembic.ini
# allow_migration_for_empty_database: false
# additional_parameters: ""
# is_patroni_postgresql: true
```
"""
try:
Expand Down
2 changes: 0 additions & 2 deletions src/chartreuse/tests/unit_tests/test_chartreuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ def test_chartreuse_init_with_kubernetes_helper(self, mocker: MockerFixture) ->
allow_migration_for_empty_database=True,
additional_parameters="--verbose -n test-db",
alembic_section_name="test-db",
is_patroni_postgresql=False,
)

# Verify kubernetes helper is set
Expand Down Expand Up @@ -517,5 +516,4 @@ def test_multi_chartreuse_default_values(self, mocker: MockerFixture) -> None:
allow_migration_for_empty_database=True, # Default value from DatabaseConfig
additional_parameters="-n test", # Section name parameter added (no leading space when original is empty)
alembic_section_name="test", # New parameter for multi-database support
is_patroni_postgresql=False, # Default value from DatabaseConfig
)
116 changes: 0 additions & 116 deletions src/chartreuse/tests/unit_tests/test_patroni_postgresql.py

This file was deleted.

59 changes: 1 addition & 58 deletions src/chartreuse/utils/alembic_migration_helper.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
import logging
import os
import re
from configparser import ConfigParser
from subprocess import SubprocessError
from time import sleep, time

import sqlalchemy
from sqlalchemy import inspect, text
from sqlalchemy.pool import NullPool
from sqlalchemy import inspect
from wiremind_kubernetes.utils import run_command

logger = logging.getLogger(__name__)
Expand All @@ -26,7 +23,6 @@ def __init__(
configure: bool = True,
# skip_db_checks is used for testing purposes only
skip_db_checks: bool = False,
is_patroni_postgresql: bool = False,
):
if not database_url:
raise ValueError("database_url not set, not upgrading database.")
Expand All @@ -38,12 +34,6 @@ def __init__(
self.alembic_config_file_path = alembic_config_file_path
self.alembic_section_name = alembic_section_name
self.skip_db_checks = skip_db_checks
self.is_patroni_postgresql = is_patroni_postgresql

# Chartreuse will upgrade a PG managed/configured by postgres-operator
if self.is_patroni_postgresql and not skip_db_checks:
self.additional_parameters += " -x patroni_postgresql=yes"
self._wait_postgres_is_configured()

if configure:
self._configure()
Expand Down Expand Up @@ -73,53 +63,6 @@ def _configure(self) -> None:

logger.info("alembic.ini was configured for section %s.", self.alembic_section_name)

def _wait_postgres_is_configured(self) -> None:
"""
Make sure the user `wiremind_owner_user` was created by the postgres-operator
and that default privileges were configured.
# TODO: Maybe make this a readinessProbe on Patroni PG Pods
# We need to remove this, it's an open source tool...
"""
wait_timeout = int(os.getenv("CHARTREUSE_ALEMBIC_POSTGRES_WAIT_CONFIGURED_TIMEOUT", 60))
engine = sqlalchemy.create_engine(self.database_url, poolclass=NullPool, connect_args={"connect_timeout": 1})

default_privileges_checks: list[str] = [
"SET ROLE wiremind_owner", # The real owner, alembic will switch to it before running migrations.
"CREATE TABLE _chartreuse_test_default_privileges(id serial)",
"SET ROLE wiremind_writer_user",
"INSERT INTO _chartreuse_test_default_privileges VALUES(1)", # id = 1
"SET ROLE wiremind_reader_user",
"SELECT id from _chartreuse_test_default_privileges",
]
start_time = time()

while time() - start_time < wait_timeout:
try:
# Yes, we may create a connection each time.
with engine.connect() as connection:
transac = connection.begin()
# TODO: Use scalar_one() once sqlachemly >= 1.4
_id = connection.execute(text(";".join(default_privileges_checks))).scalar()
assert _id == 1
transac.rollback()
logger.info(
"The role wiremind_owner_user was created and the default privileges"
" were set by the postgres-operator."
)
return
except Exception as e:
# TODO: Learn about exceptions that should be caught here, otherwise we'll wait for nothing
logger.info("Caught: %s", e)
logger.info(
"Waiting for the postgres-operator to create the user wiremind_owner_user"
" (that alembic and I use) and to set default privileges..."
)
sleep(2)
raise Exception(
f"I'm fed up! Waited {wait_timeout}s for postgres-operator to configure the"
f" Postgres database. Check the Postgres logs and then postgres-operator for anything fishy."
)

def _get_table_list(self) -> list[str]:
return inspect(sqlalchemy.create_engine(self.database_url)).get_table_names()

Expand Down
Loading