2025-04-16 18:54:05 +05:30
|
|
|
import docker
|
|
|
|
|
import pytest
|
|
|
|
|
from testcontainers.core.container import Network
|
|
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
from fixtures import dev, types
|
|
|
|
|
from fixtures.logger import setup_logger
|
|
|
|
|
|
|
|
|
|
logger = setup_logger(__name__)
|
2025-04-16 18:54:05 +05:30
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture(name="migrator", scope="package")
|
|
|
|
|
def migrator(
|
|
|
|
|
network: Network,
|
|
|
|
|
clickhouse: types.TestContainerClickhouse,
|
|
|
|
|
request: pytest.FixtureRequest,
|
2025-04-26 15:50:02 +05:30
|
|
|
pytestconfig: pytest.Config,
|
2025-07-29 14:44:16 +05:30
|
|
|
) -> types.Operation:
|
2025-04-16 18:54:05 +05:30
|
|
|
"""
|
|
|
|
|
Package-scoped fixture for running schema migrations.
|
|
|
|
|
"""
|
|
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
def create() -> None:
|
|
|
|
|
version = request.config.getoption("--schema-migrator-version")
|
|
|
|
|
client = docker.from_env()
|
|
|
|
|
|
|
|
|
|
container = client.containers.run(
|
|
|
|
|
image=f"signoz/signoz-schema-migrator:{version}",
|
|
|
|
|
command=f"sync --replication=true --cluster-name=cluster --up= --dsn={clickhouse.env["SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_DSN"]}",
|
|
|
|
|
detach=True,
|
|
|
|
|
auto_remove=False,
|
|
|
|
|
network=network.id,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = container.wait()
|
|
|
|
|
|
|
|
|
|
if result["StatusCode"] != 0:
|
|
|
|
|
logs = container.logs().decode(encoding="utf-8")
|
|
|
|
|
container.remove()
|
|
|
|
|
print(logs)
|
|
|
|
|
raise RuntimeError("failed to run migrations on clickhouse")
|
2025-04-16 18:54:05 +05:30
|
|
|
|
|
|
|
|
container.remove()
|
|
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
container = client.containers.run(
|
|
|
|
|
image=f"signoz/signoz-schema-migrator:{version}",
|
|
|
|
|
command=f"async --replication=true --cluster-name=cluster --up= --dsn={clickhouse.env["SIGNOZ_TELEMETRYSTORE_CLICKHOUSE_DSN"]}",
|
|
|
|
|
detach=True,
|
|
|
|
|
auto_remove=False,
|
|
|
|
|
network=network.id,
|
|
|
|
|
)
|
2025-04-16 18:54:05 +05:30
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
result = container.wait()
|
2025-04-16 18:54:05 +05:30
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
if result["StatusCode"] != 0:
|
|
|
|
|
logs = container.logs().decode(encoding="utf-8")
|
|
|
|
|
container.remove()
|
|
|
|
|
print(logs)
|
|
|
|
|
raise RuntimeError("failed to run migrations on clickhouse")
|
2025-04-16 18:54:05 +05:30
|
|
|
|
|
|
|
|
container.remove()
|
|
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
return types.Operation(name="migrator")
|
|
|
|
|
|
|
|
|
|
def delete(_: types.Operation) -> None:
|
|
|
|
|
pass
|
2025-04-26 15:50:02 +05:30
|
|
|
|
2025-07-29 14:44:16 +05:30
|
|
|
def restore(cache: dict) -> types.Operation:
|
|
|
|
|
return types.Operation(name=cache["name"])
|
|
|
|
|
|
|
|
|
|
return dev.wrap(
|
|
|
|
|
request,
|
|
|
|
|
pytestconfig,
|
|
|
|
|
"migrator",
|
|
|
|
|
lambda: types.Operation(name=""),
|
|
|
|
|
create,
|
|
|
|
|
delete,
|
|
|
|
|
restore,
|
|
|
|
|
)
|