-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrations.py
More file actions
224 lines (196 loc) · 6.14 KB
/
migrations.py
File metadata and controls
224 lines (196 loc) · 6.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from __future__ import annotations
from collections.abc import Callable, Mapping
from typing import TYPE_CHECKING
from sqlalchemy import (
Column,
Index,
Integer,
MetaData,
String,
Table,
insert,
inspect,
select,
text,
update,
)
from sqlalchemy.exc import IntegrityError
if TYPE_CHECKING:
from sqlalchemy.engine import Connection
STATE_STORE_SCHEMA_NAME = "state_store"
CURRENT_STATE_STORE_SCHEMA_VERSION = 3
_SCHEMA_VERSION_METADATA = MetaData()
_SCHEMA_VERSIONS = Table(
"a2a_schema_version",
_SCHEMA_VERSION_METADATA,
Column("name", String, primary_key=True),
Column("version", Integer, nullable=False),
)
def _add_missing_nullable_column(
connection: Connection,
*,
table: Table,
column_name: str,
) -> None:
existing_columns = {column["name"] for column in inspect(connection).get_columns(table.name)}
if column_name in existing_columns:
return
column = table.c[column_name]
if column.primary_key or not column.nullable:
raise RuntimeError(f"Unsupported state-store migration for {table.name}.{column_name}")
preparer = connection.dialect.identifier_preparer
table_name_sql = preparer.quote(table.name)
column_name_sql = preparer.quote(column_name)
column_type_sql = column.type.compile(dialect=connection.dialect)
connection.execute(
text(f"ALTER TABLE {table_name_sql} ADD COLUMN {column_name_sql} {column_type_sql}")
)
def _migration_1_add_interrupt_details_json(
connection: Connection,
*,
interrupt_requests_table: Table,
) -> None:
_add_missing_nullable_column(
connection,
table=interrupt_requests_table,
column_name="details_json",
)
def _migration_2_add_pending_claim_expires_at(
connection: Connection,
*,
pending_session_claims_table: Table,
) -> None:
_add_missing_nullable_column(
connection,
table=pending_session_claims_table,
column_name="expires_at",
)
def _create_missing_index(
connection: Connection,
*,
index: Index,
) -> None:
table = index.table
if table is None:
raise RuntimeError("State-store index is missing table metadata")
existing_indexes = {
existing_index["name"] for existing_index in inspect(connection).get_indexes(table.name)
}
if index.name in existing_indexes:
return
index.create(connection)
def _migration_3_add_lightweight_state_indexes(
connection: Connection,
*,
pending_session_claims_table: Table,
interrupt_requests_table: Table,
) -> None:
indexes = sorted(
[
*pending_session_claims_table.indexes,
*interrupt_requests_table.indexes,
],
key=lambda index: index.name or "",
)
for index in indexes:
_create_missing_index(connection, index=index)
def _read_schema_version(
connection: Connection,
*,
version_table: Table,
scope: str,
) -> int | None:
result = connection.execute(
select(version_table.c.version).where(version_table.c.name == scope)
)
version = result.scalar_one_or_none()
return int(version) if version is not None else None
def _write_schema_version(
connection: Connection,
*,
version_table: Table,
scope: str,
version: int,
) -> None:
existing_version = _read_schema_version(
connection,
version_table=version_table,
scope=scope,
)
if existing_version is not None:
connection.execute(
update(version_table).where(version_table.c.name == scope).values(version=version)
)
return
try:
connection.execute(insert(version_table).values(name=scope, version=version))
except IntegrityError:
connection.execute(
update(version_table).where(version_table.c.name == scope).values(version=version)
)
def _apply_schema_migrations(
connection: Connection,
*,
version_table: Table,
scope: str,
current_version: int,
migrations: Mapping[int, Callable[[Connection], None]],
) -> int:
if current_version < 0:
raise ValueError("current_version must be non-negative")
stored_version = _read_schema_version(
connection,
version_table=version_table,
scope=scope,
)
if stored_version is not None and stored_version > current_version:
raise RuntimeError(
f"Database schema scope {scope!r} is newer than this application supports"
)
starting_version = stored_version or 0
for next_version in range(starting_version + 1, current_version + 1):
migration = migrations.get(next_version)
if migration is None:
raise RuntimeError(
f"Missing migration for schema scope {scope!r} version {next_version}"
)
migration(connection)
_write_schema_version(
connection,
version_table=version_table,
scope=scope,
version=next_version,
)
return current_version
def migrate_state_store_schema(
connection: Connection,
*,
state_metadata: MetaData,
pending_session_claims_table: Table,
interrupt_requests_table: Table,
current_version: int = CURRENT_STATE_STORE_SCHEMA_VERSION,
) -> int:
_SCHEMA_VERSION_METADATA.create_all(connection)
state_metadata.create_all(connection)
migrations: dict[int, Callable[[Connection], None]] = {
1: lambda conn: _migration_1_add_interrupt_details_json(
conn,
interrupt_requests_table=interrupt_requests_table,
),
2: lambda conn: _migration_2_add_pending_claim_expires_at(
conn,
pending_session_claims_table=pending_session_claims_table,
),
3: lambda conn: _migration_3_add_lightweight_state_indexes(
conn,
pending_session_claims_table=pending_session_claims_table,
interrupt_requests_table=interrupt_requests_table,
),
}
return _apply_schema_migrations(
connection,
version_table=_SCHEMA_VERSIONS,
scope=STATE_STORE_SCHEMA_NAME,
current_version=current_version,
migrations=migrations,
)