feat(cloud-store): add Alembic baseline migration

This commit is contained in:
2026-07-12 16:55:46 +08:00
parent 64aa9b39bc
commit 24d9dbf38b
10 changed files with 328 additions and 2 deletions
+28 -1
View File
@@ -1,6 +1,6 @@
from __future__ import annotations
from sqlalchemy import Integer, String, Text
from sqlalchemy import Integer, String, Text, text
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
@@ -37,9 +37,36 @@ class ScheduledTaskRow(Base):
status: Mapped[str] = mapped_column(String, nullable=False)
assigned_device_id: Mapped[str | None] = mapped_column(String, nullable=True)
assigned_host_id: Mapped[str | None] = mapped_column(String, nullable=True)
attempt_count: Mapped[int] = mapped_column(
Integer,
nullable=False,
default=0,
server_default=text("0"),
)
lease_id: Mapped[str | None] = mapped_column(String, nullable=True)
lease_expires_at: Mapped[str | None] = mapped_column(String, nullable=True)
failure_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
result_json: Mapped[str | None] = mapped_column(Text, nullable=True)
updated_at: Mapped[str | None] = mapped_column(String, nullable=True)
created_at: Mapped[str] = mapped_column(String, nullable=False)
class TaskAttemptRow(Base):
__tablename__ = "task_attempts"
task_id: Mapped[str] = mapped_column(String, primary_key=True)
attempt: Mapped[int] = mapped_column(Integer, primary_key=True)
lease_id: Mapped[str] = mapped_column(String, nullable=False)
host_id: Mapped[str] = mapped_column(String, nullable=False)
device_id: Mapped[str] = mapped_column(String, nullable=False)
status: Mapped[str] = mapped_column(String, nullable=False)
lease_expires_at: Mapped[str] = mapped_column(String, nullable=False)
created_at: Mapped[str] = mapped_column(String, nullable=False)
completed_at: Mapped[str | None] = mapped_column(String, nullable=True)
failure_reason: Mapped[str | None] = mapped_column(Text, nullable=True)
result_json: Mapped[str | None] = mapped_column(Text, nullable=True)
class PluginRow(Base):
__tablename__ = "plugins"
@@ -0,0 +1 @@
"""Alembic migration environment bundled with device-cloud-platform."""
@@ -0,0 +1,38 @@
[alembic]
script_location = %(here)s
prepend_sys_path = .
sqlalchemy.url = sqlite:///cloud/cloud.sqlite3
[loggers]
keys = root,sqlalchemy,alembic
[handlers]
keys = console
[formatters]
keys = generic
[logger_root]
level = WARN
handlers = console
qualname =
[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine
[logger_alembic]
level = INFO
handlers =
qualname = alembic
[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic
[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
@@ -0,0 +1,63 @@
from __future__ import annotations
import os
from logging.config import fileConfig
from alembic import context
from sqlalchemy import engine_from_config, pool
from cloud.database import normalize_database_url
from cloud.db_models import Base
config = context.config
if config.config_file_name is not None:
fileConfig(config.config_file_name)
target_metadata = Base.metadata
def _database_url() -> str:
return normalize_database_url(
os.environ.get("CLOUD_DATABASE_URL")
or config.get_main_option("sqlalchemy.url")
)
def run_migrations_offline() -> None:
context.configure(
url=_database_url(),
target_metadata=target_metadata,
literal_binds=True,
dialect_opts={"paramstyle": "named"},
)
with context.begin_transaction():
context.run_migrations()
def run_migrations_online() -> None:
supplied_connection = config.attributes.get("connection")
if supplied_connection is not None:
context.configure(connection=supplied_connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
return
configuration = config.get_section(config.config_ini_section) or {}
configuration["sqlalchemy.url"] = _database_url()
connectable = engine_from_config(
configuration,
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
with context.begin_transaction():
context.run_migrations()
connectable.dispose()
if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
@@ -0,0 +1,22 @@
"""${message}
Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}
"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}
def upgrade() -> None:
${upgrades if upgrades else "pass"}
def downgrade() -> None:
${downgrades if downgrades else "pass"}
@@ -0,0 +1,112 @@
"""Create the cloud repository schema and lease fields."""
from __future__ import annotations
from alembic import op
import sqlalchemy as sa
revision = "0001_cloud_repository"
down_revision = None
branch_labels = None
depends_on = None
LEASE_COLUMNS = (
sa.Column("attempt_count", sa.Integer(), nullable=False, server_default="0"),
sa.Column("lease_id", sa.String(), nullable=True),
sa.Column("lease_expires_at", sa.String(), nullable=True),
sa.Column("failure_reason", sa.Text(), nullable=True),
sa.Column("result_json", sa.Text(), nullable=True),
sa.Column("updated_at", sa.String(), nullable=True),
)
def upgrade() -> None:
inspector = sa.inspect(op.get_bind())
tables = set(inspector.get_table_names())
if "host_registrations" not in tables:
_create_base_tables()
else:
existing_columns = {
column["name"] for column in inspector.get_columns("scheduled_tasks")
}
with op.batch_alter_table("scheduled_tasks") as batch:
for column in LEASE_COLUMNS:
if column.name not in existing_columns:
batch.add_column(column.copy())
inspector = sa.inspect(op.get_bind())
if "task_attempts" not in inspector.get_table_names():
_create_attempts_table()
def downgrade() -> None:
inspector = sa.inspect(op.get_bind())
tables = set(inspector.get_table_names())
if "task_attempts" in tables:
op.drop_table("task_attempts")
if "scheduled_tasks" not in tables:
return
existing_columns = {
column["name"] for column in inspector.get_columns("scheduled_tasks")
}
with op.batch_alter_table("scheduled_tasks") as batch:
for column in reversed(LEASE_COLUMNS):
if column.name in existing_columns:
batch.drop_column(column.name)
def _create_base_tables() -> None:
op.create_table(
"host_registrations",
sa.Column("host_id", sa.String(), primary_key=True),
sa.Column("address", sa.String(), nullable=True),
sa.Column("last_seen_at", sa.String(), nullable=False),
)
op.create_table(
"pooled_devices",
sa.Column("host_id", sa.String(), primary_key=True),
sa.Column("device_id", sa.String(), primary_key=True),
sa.Column("driver_type", sa.String(), nullable=False),
sa.Column("status", sa.String(), nullable=False),
sa.Column("capability_tags_json", sa.Text(), nullable=False),
sa.Column("synced_at", sa.String(), nullable=True),
)
op.create_table(
"scheduled_tasks",
sa.Column("id", sa.String(), primary_key=True),
sa.Column("goal", sa.Text(), nullable=True),
sa.Column("workflow_definition_id", sa.String(), nullable=True),
sa.Column("constraints_json", sa.Text(), nullable=False),
sa.Column("status", sa.String(), nullable=False),
sa.Column("assigned_device_id", sa.String(), nullable=True),
sa.Column("assigned_host_id", sa.String(), nullable=True),
*[column.copy() for column in LEASE_COLUMNS],
sa.Column("created_at", sa.String(), nullable=False),
)
op.create_table(
"plugins",
sa.Column("name", sa.String(), primary_key=True),
sa.Column("version", sa.String(), nullable=False),
sa.Column("entry_point_kind", sa.String(), nullable=False),
sa.Column("target", sa.String(), nullable=False),
sa.Column("wired", sa.Integer(), nullable=False),
)
def _create_attempts_table() -> None:
op.create_table(
"task_attempts",
sa.Column("task_id", sa.String(), primary_key=True),
sa.Column("attempt", sa.Integer(), primary_key=True),
sa.Column("lease_id", sa.String(), nullable=False),
sa.Column("host_id", sa.String(), nullable=False),
sa.Column("device_id", sa.String(), nullable=False),
sa.Column("status", sa.String(), nullable=False),
sa.Column("lease_expires_at", sa.String(), nullable=False),
sa.Column("created_at", sa.String(), nullable=False),
sa.Column("completed_at", sa.String(), nullable=True),
sa.Column("failure_reason", sa.Text(), nullable=True),
sa.Column("result_json", sa.Text(), nullable=True),
)
@@ -0,0 +1 @@
"""Cloud database schema revisions."""
+4
View File
@@ -5,6 +5,7 @@ description = "Cloud scheduling, device pooling, plugins, and SDK for Device Age
readme = "README.md"
requires-python = ">=3.14"
dependencies = [
"alembic>=1.14.0",
"device-agent-runtime==0.1.0",
"psycopg[binary]>=3.2.0",
"sqlalchemy>=2.0.0",
@@ -18,5 +19,8 @@ build-backend = "setuptools.build_meta"
where = ["."]
include = ["cloud*"]
[tool.setuptools.package-data]
cloud = ["migrations/*.ini", "migrations/*.mako"]
[tool.uv.sources]
device-agent-runtime = { workspace = true }