rakko-orm 1.0.0a0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- rakko/__init__.py +613 -0
- rakko/__main__.py +10 -0
- rakko/apps.py +356 -0
- rakko/backends/__init__.py +4 -0
- rakko/backends/asyncpg/__init__.py +7 -0
- rakko/backends/asyncpg/client.py +223 -0
- rakko/backends/asyncpg/executor.py +20 -0
- rakko/backends/asyncpg/schema_generator.py +17 -0
- rakko/backends/base/__init__.py +4 -0
- rakko/backends/base/client.py +438 -0
- rakko/backends/base/config_generator.py +249 -0
- rakko/backends/base/executor.py +764 -0
- rakko/backends/base/schema_generator.py +580 -0
- rakko/backends/base_postgres/__init__.py +4 -0
- rakko/backends/base_postgres/client.py +170 -0
- rakko/backends/base_postgres/executor.py +149 -0
- rakko/backends/base_postgres/schema_generator.py +84 -0
- rakko/backends/mssql/__init__.py +7 -0
- rakko/backends/mssql/client.py +127 -0
- rakko/backends/mssql/executor.py +17 -0
- rakko/backends/mssql/schema_generator.py +125 -0
- rakko/backends/mysql/__init__.py +7 -0
- rakko/backends/mysql/client.py +312 -0
- rakko/backends/mysql/executor.py +154 -0
- rakko/backends/mysql/schema_generator.py +101 -0
- rakko/backends/odbc/__init__.py +4 -0
- rakko/backends/odbc/client.py +221 -0
- rakko/backends/odbc/executor.py +20 -0
- rakko/backends/oracle/__init__.py +7 -0
- rakko/backends/oracle/client.py +136 -0
- rakko/backends/oracle/executor.py +27 -0
- rakko/backends/oracle/schema_generator.py +120 -0
- rakko/backends/psycopg/__init__.py +7 -0
- rakko/backends/psycopg/client.py +288 -0
- rakko/backends/psycopg/executor.py +14 -0
- rakko/backends/psycopg/schema_generator.py +17 -0
- rakko/backends/sqlite/__init__.py +14 -0
- rakko/backends/sqlite/client.py +299 -0
- rakko/backends/sqlite/executor.py +88 -0
- rakko/backends/sqlite/schema_generator.py +32 -0
- rakko/cli/__init__.py +13 -0
- rakko/cli/cli.py +878 -0
- rakko/cli/utils.py +181 -0
- rakko/config.py +315 -0
- rakko/connection.py +345 -0
- rakko/context.py +571 -0
- rakko/contrib/__init__.py +4 -0
- rakko/contrib/aiohttp/__init__.py +99 -0
- rakko/contrib/blacksheep/__init__.py +121 -0
- rakko/contrib/fastapi/__init__.py +301 -0
- rakko/contrib/mysql/__init__.py +4 -0
- rakko/contrib/mysql/fields.py +63 -0
- rakko/contrib/mysql/functions.py +19 -0
- rakko/contrib/mysql/indexes.py +28 -0
- rakko/contrib/mysql/json_functions.py +80 -0
- rakko/contrib/mysql/search.py +49 -0
- rakko/contrib/postgres/__init__.py +4 -0
- rakko/contrib/postgres/array_functions.py +35 -0
- rakko/contrib/postgres/fields.py +105 -0
- rakko/contrib/postgres/functions.py +43 -0
- rakko/contrib/postgres/indexes.py +33 -0
- rakko/contrib/postgres/json_functions.py +113 -0
- rakko/contrib/postgres/regex.py +31 -0
- rakko/contrib/postgres/search.py +483 -0
- rakko/contrib/pydantic/__init__.py +16 -0
- rakko/contrib/pydantic/base.py +147 -0
- rakko/contrib/pydantic/creator.py +681 -0
- rakko/contrib/pydantic/descriptions.py +207 -0
- rakko/contrib/pydantic/utils.py +38 -0
- rakko/contrib/pylint/__init__.py +158 -0
- rakko/contrib/quart/__init__.py +117 -0
- rakko/contrib/sanic/__init__.py +116 -0
- rakko/contrib/sqlite/__init__.py +4 -0
- rakko/contrib/sqlite/functions.py +16 -0
- rakko/contrib/sqlite/regex.py +46 -0
- rakko/contrib/starlette/__init__.py +180 -0
- rakko/contrib/test/__init__.py +336 -0
- rakko/contrib/test/condition.py +42 -0
- rakko/converters.py +160 -0
- rakko/exceptions.py +130 -0
- rakko/expressions.py +962 -0
- rakko/fields/__init__.py +111 -0
- rakko/fields/base.py +685 -0
- rakko/fields/data.py +1308 -0
- rakko/fields/db_defaults.py +105 -0
- rakko/fields/relational.py +703 -0
- rakko/filters.py +639 -0
- rakko/functions.py +142 -0
- rakko/indexes.py +165 -0
- rakko/log.py +8 -0
- rakko/manager.py +29 -0
- rakko/migrations/__init__.py +41 -0
- rakko/migrations/api/__init__.py +16 -0
- rakko/migrations/api/migrate.py +77 -0
- rakko/migrations/api/plan.py +76 -0
- rakko/migrations/api/sqlmigrate.py +69 -0
- rakko/migrations/autodetector.py +203 -0
- rakko/migrations/constraints.py +31 -0
- rakko/migrations/exceptions.py +10 -0
- rakko/migrations/executor.py +368 -0
- rakko/migrations/expressions.py +13 -0
- rakko/migrations/graph.py +158 -0
- rakko/migrations/loader.py +127 -0
- rakko/migrations/migration.py +189 -0
- rakko/migrations/operations.py +1288 -0
- rakko/migrations/recorder.py +104 -0
- rakko/migrations/schema_editor/__init__.py +25 -0
- rakko/migrations/schema_editor/asyncpg.py +11 -0
- rakko/migrations/schema_editor/base.py +945 -0
- rakko/migrations/schema_editor/base_postgres.py +171 -0
- rakko/migrations/schema_editor/data.py +18 -0
- rakko/migrations/schema_editor/mssql.py +339 -0
- rakko/migrations/schema_editor/mysql.py +408 -0
- rakko/migrations/schema_editor/oracle.py +259 -0
- rakko/migrations/schema_editor/psycopg.py +11 -0
- rakko/migrations/schema_editor/sqlite.py +424 -0
- rakko/migrations/schema_generator/__init__.py +5 -0
- rakko/migrations/schema_generator/operation_generator.py +163 -0
- rakko/migrations/schema_generator/state.py +224 -0
- rakko/migrations/schema_generator/state_apps.py +165 -0
- rakko/migrations/schema_generator/state_diff.py +419 -0
- rakko/migrations/writer.py +515 -0
- rakko/models.py +1854 -0
- rakko/py.typed +0 -0
- rakko/query_api.py +134 -0
- rakko/query_utils.py +238 -0
- rakko/queryset.py +2743 -0
- rakko/relations.py +85 -0
- rakko/router.py +55 -0
- rakko/schema_quoting.py +62 -0
- rakko/signals.py +73 -0
- rakko/timezone.py +173 -0
- rakko/transactions.py +69 -0
- rakko/utils.py +50 -0
- rakko/validators.py +412 -0
- rakko/warnings.py +22 -0
- rakko_orm-1.0.0a0.dist-info/METADATA +114 -0
- rakko_orm-1.0.0a0.dist-info/RECORD +143 -0
- rakko_orm-1.0.0a0.dist-info/WHEEL +4 -0
- rakko_orm-1.0.0a0.dist-info/entry_points.txt +5 -0
- rakko_orm-1.0.0a0.dist-info/licenses/CONTRIBUTORS.rst +86 -0
- rakko_orm-1.0.0a0.dist-info/licenses/LICENSE.txt +201 -0
- rakko_orm-1.0.0a0.dist-info/licenses/NOTICE.txt +22 -0
|
@@ -0,0 +1,1288 @@
|
|
|
1
|
+
# Copyright (c) 2026 ropmyung (Rakko ORM modifications and additions).
|
|
2
|
+
# Derived from Tortoise ORM; maintained and modified for Rakko ORM.
|
|
3
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
from __future__ import annotations
|
|
6
|
+
|
|
7
|
+
import inspect
|
|
8
|
+
from copy import deepcopy
|
|
9
|
+
from typing import TYPE_CHECKING, Any, TypeAlias, cast
|
|
10
|
+
|
|
11
|
+
from rakko import BaseDBAsyncClient, Model
|
|
12
|
+
from rakko.fields import Field
|
|
13
|
+
from rakko.fields.relational import (
|
|
14
|
+
ForeignKeyFieldInstance,
|
|
15
|
+
ManyToManyFieldInstance,
|
|
16
|
+
OneToOneFieldInstance,
|
|
17
|
+
)
|
|
18
|
+
from rakko.indexes import Index
|
|
19
|
+
from rakko.migrations.constraints import CheckConstraint, UniqueConstraint
|
|
20
|
+
from rakko.migrations.exceptions import IncompatibleStateError
|
|
21
|
+
from rakko.migrations.schema_editor.base import BaseSchemaEditor
|
|
22
|
+
from rakko.migrations.schema_generator.state import ModelState, State
|
|
23
|
+
from rakko.migrations.schema_generator.state_apps import StateApps
|
|
24
|
+
from rakko.models import table_name_generator
|
|
25
|
+
|
|
26
|
+
if TYPE_CHECKING:
|
|
27
|
+
from rakko.fields.base import Field as BaseField
|
|
28
|
+
from rakko.fields.relational import ManyToManyRelation
|
|
29
|
+
|
|
30
|
+
FieldLike: TypeAlias = BaseField[Any] | ManyToManyRelation[Any] | None
|
|
31
|
+
else:
|
|
32
|
+
FieldLike = Field
|
|
33
|
+
|
|
34
|
+
DIRECT_RELATION_FIELDS = (
|
|
35
|
+
ForeignKeyFieldInstance,
|
|
36
|
+
ManyToManyFieldInstance,
|
|
37
|
+
OneToOneFieldInstance,
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Operation:
|
|
42
|
+
reversible = True
|
|
43
|
+
reduces_to_sql = True
|
|
44
|
+
atomic: bool | None = False
|
|
45
|
+
|
|
46
|
+
def describe(self) -> str:
|
|
47
|
+
return self.__class__.__name__
|
|
48
|
+
|
|
49
|
+
async def run(
|
|
50
|
+
self,
|
|
51
|
+
app_label: str,
|
|
52
|
+
state: State,
|
|
53
|
+
dry_run: bool,
|
|
54
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
55
|
+
) -> None:
|
|
56
|
+
raise NotImplementedError()
|
|
57
|
+
|
|
58
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
59
|
+
raise NotImplementedError()
|
|
60
|
+
|
|
61
|
+
async def database_forward(
|
|
62
|
+
self,
|
|
63
|
+
app_label: str,
|
|
64
|
+
old_state: State,
|
|
65
|
+
new_state: State,
|
|
66
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
67
|
+
) -> None:
|
|
68
|
+
raise NotImplementedError()
|
|
69
|
+
|
|
70
|
+
async def database_backward(
|
|
71
|
+
self,
|
|
72
|
+
app_label: str,
|
|
73
|
+
old_state: State,
|
|
74
|
+
new_state: State,
|
|
75
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
76
|
+
) -> None:
|
|
77
|
+
raise NotImplementedError()
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
class SQLOperation(Operation):
|
|
81
|
+
def __init__(self, query: str, values: list[Any]):
|
|
82
|
+
self.query = query
|
|
83
|
+
self.values = values
|
|
84
|
+
|
|
85
|
+
async def run(
|
|
86
|
+
self,
|
|
87
|
+
app_label: str,
|
|
88
|
+
state: State,
|
|
89
|
+
dry_run: bool,
|
|
90
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
91
|
+
) -> None:
|
|
92
|
+
if not dry_run and state_editor:
|
|
93
|
+
if self.values:
|
|
94
|
+
if state_editor.collect_sql:
|
|
95
|
+
state_editor.collected_sql.append(f"{self.query} -- params: {self.values!r}")
|
|
96
|
+
else:
|
|
97
|
+
await state_editor.client.execute_query_dict(self.query, self.values)
|
|
98
|
+
else:
|
|
99
|
+
await state_editor._run_sql(self.query)
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
class TortoiseOperation(Operation):
|
|
103
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
104
|
+
return None
|
|
105
|
+
|
|
106
|
+
@staticmethod
|
|
107
|
+
def get_model_state(state: State, app_label: str, model_name: str) -> ModelState:
|
|
108
|
+
model = state.models.get((app_label, model_name))
|
|
109
|
+
if not model:
|
|
110
|
+
raise IncompatibleStateError()
|
|
111
|
+
|
|
112
|
+
return model
|
|
113
|
+
|
|
114
|
+
async def database_forward(
|
|
115
|
+
self,
|
|
116
|
+
app_label: str,
|
|
117
|
+
old_state: State,
|
|
118
|
+
new_state: State,
|
|
119
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
120
|
+
) -> None:
|
|
121
|
+
return None
|
|
122
|
+
|
|
123
|
+
async def database_backward(
|
|
124
|
+
self,
|
|
125
|
+
app_label: str,
|
|
126
|
+
old_state: State,
|
|
127
|
+
new_state: State,
|
|
128
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
129
|
+
) -> None:
|
|
130
|
+
return None
|
|
131
|
+
|
|
132
|
+
async def run(
|
|
133
|
+
self,
|
|
134
|
+
app_label: str,
|
|
135
|
+
state: State,
|
|
136
|
+
dry_run: bool,
|
|
137
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
138
|
+
) -> None:
|
|
139
|
+
old_state = state.clone() if (not dry_run and state_editor) else None
|
|
140
|
+
self.state_forward(app_label, state)
|
|
141
|
+
if dry_run or not state_editor:
|
|
142
|
+
return
|
|
143
|
+
await self.database_forward(app_label, old_state, state, state_editor) # type: ignore[arg-type]
|
|
144
|
+
|
|
145
|
+
|
|
146
|
+
class CreateModel(TortoiseOperation):
|
|
147
|
+
def __init__(
|
|
148
|
+
self,
|
|
149
|
+
name: str,
|
|
150
|
+
fields: list[tuple[str, FieldLike]],
|
|
151
|
+
options: dict[str, Any] | None = None,
|
|
152
|
+
bases: list[str] | None = None,
|
|
153
|
+
) -> None:
|
|
154
|
+
self.options = options
|
|
155
|
+
self.fields = fields
|
|
156
|
+
self.name = name
|
|
157
|
+
self.bases = bases
|
|
158
|
+
self._model: type[Model] | None = None
|
|
159
|
+
|
|
160
|
+
def describe(self) -> str:
|
|
161
|
+
return f"Create model {self.name}"
|
|
162
|
+
|
|
163
|
+
@property
|
|
164
|
+
def model(self) -> type[Model]:
|
|
165
|
+
if not self._model:
|
|
166
|
+
meta_class = type("Meta", (), self.options or {})
|
|
167
|
+
|
|
168
|
+
attributes: dict[str, Any] = dict(self.fields)
|
|
169
|
+
attributes["Meta"] = meta_class
|
|
170
|
+
attributes["_no_comments"] = True
|
|
171
|
+
self._model = cast(type[Model], type(self.name, (Model,), attributes))
|
|
172
|
+
|
|
173
|
+
return self._model
|
|
174
|
+
|
|
175
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
176
|
+
model_state = ModelState.make_from_model(app_label, self.model)
|
|
177
|
+
state.models[(app_label, self.name)] = model_state
|
|
178
|
+
|
|
179
|
+
models_to_reload = {(app_label, self.name)}
|
|
180
|
+
|
|
181
|
+
for field in model_state.fields.values():
|
|
182
|
+
if not isinstance(field, DIRECT_RELATION_FIELDS):
|
|
183
|
+
continue
|
|
184
|
+
|
|
185
|
+
related_key = state.apps.split_reference(field.model_name)
|
|
186
|
+
if related_key in state.models:
|
|
187
|
+
models_to_reload.add(related_key)
|
|
188
|
+
|
|
189
|
+
# Also find existing models that reference the newly created model.
|
|
190
|
+
# This handles the case where a model with a FK was created before its
|
|
191
|
+
# target (e.g. alphabetical ordering: Alert before Warehouse).
|
|
192
|
+
new_model_ref = f"{app_label}.{self.name}"
|
|
193
|
+
for key, existing_state in state.models.items():
|
|
194
|
+
if key == (app_label, self.name):
|
|
195
|
+
continue
|
|
196
|
+
for field in existing_state.fields.values():
|
|
197
|
+
if isinstance(field, DIRECT_RELATION_FIELDS) and field.model_name == new_model_ref:
|
|
198
|
+
models_to_reload.add(key)
|
|
199
|
+
break
|
|
200
|
+
|
|
201
|
+
state.reload_models(models_to_reload)
|
|
202
|
+
|
|
203
|
+
async def database_forward(
|
|
204
|
+
self,
|
|
205
|
+
app_label: str,
|
|
206
|
+
old_state: State,
|
|
207
|
+
new_state: State,
|
|
208
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
209
|
+
) -> None:
|
|
210
|
+
if not state_editor:
|
|
211
|
+
return
|
|
212
|
+
model = new_state.apps.get_model(f"{app_label}.{self.name}")
|
|
213
|
+
await state_editor.create_model(model)
|
|
214
|
+
|
|
215
|
+
async def database_backward(
|
|
216
|
+
self,
|
|
217
|
+
app_label: str,
|
|
218
|
+
old_state: State,
|
|
219
|
+
new_state: State,
|
|
220
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
221
|
+
) -> None:
|
|
222
|
+
if not state_editor:
|
|
223
|
+
return
|
|
224
|
+
model = old_state.apps.get_model(f"{app_label}.{self.name}")
|
|
225
|
+
await state_editor.delete_model(model)
|
|
226
|
+
|
|
227
|
+
async def run_sql(self, db_connection: BaseDBAsyncClient | None = None) -> None:
|
|
228
|
+
return None
|
|
229
|
+
|
|
230
|
+
|
|
231
|
+
class RenameModel(TortoiseOperation):
|
|
232
|
+
def __init__(self, old_name: str, new_name: str) -> None:
|
|
233
|
+
self.old_name = old_name
|
|
234
|
+
self.new_name = new_name
|
|
235
|
+
|
|
236
|
+
def describe(self) -> str:
|
|
237
|
+
return f"Rename model {self.old_name} to {self.new_name}"
|
|
238
|
+
|
|
239
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
240
|
+
model_state_to_change = state.models.pop((app_label, self.old_name), None)
|
|
241
|
+
if not model_state_to_change:
|
|
242
|
+
raise IncompatibleStateError()
|
|
243
|
+
|
|
244
|
+
state.apps.unregister_model(app_label, self.old_name)
|
|
245
|
+
|
|
246
|
+
old_table = model_state_to_change.table
|
|
247
|
+
model_state_to_change.name = self.new_name
|
|
248
|
+
if old_table == table_name_generator(self.old_name):
|
|
249
|
+
model_state_to_change.table = table_name_generator(self.new_name)
|
|
250
|
+
if model_state_to_change.options.get("table") == old_table:
|
|
251
|
+
model_state_to_change.options["table"] = model_state_to_change.table
|
|
252
|
+
state.models[(app_label, self.new_name)] = model_state_to_change
|
|
253
|
+
old_model_reference = f"{app_label}.{self.old_name}"
|
|
254
|
+
new_model_reference = f"{app_label}.{self.new_name}"
|
|
255
|
+
|
|
256
|
+
for model_state in state.models.values():
|
|
257
|
+
for field_name, field in model_state.fields.items():
|
|
258
|
+
if not isinstance(
|
|
259
|
+
field,
|
|
260
|
+
(
|
|
261
|
+
ForeignKeyFieldInstance,
|
|
262
|
+
OneToOneFieldInstance,
|
|
263
|
+
ManyToManyFieldInstance,
|
|
264
|
+
),
|
|
265
|
+
):
|
|
266
|
+
continue
|
|
267
|
+
|
|
268
|
+
if field.model_name == old_model_reference:
|
|
269
|
+
new_field = deepcopy(field)
|
|
270
|
+
new_field.model_name = new_model_reference
|
|
271
|
+
model_state.fields[field_name] = new_field
|
|
272
|
+
|
|
273
|
+
state.reload_model(app_label, self.new_name)
|
|
274
|
+
|
|
275
|
+
async def database_forward(
|
|
276
|
+
self,
|
|
277
|
+
app_label: str,
|
|
278
|
+
old_state: State,
|
|
279
|
+
new_state: State,
|
|
280
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
281
|
+
) -> None:
|
|
282
|
+
if not state_editor:
|
|
283
|
+
return
|
|
284
|
+
old_model = old_state.apps.get_model(f"{app_label}.{self.old_name}")
|
|
285
|
+
new_model = new_state.apps.get_model(f"{app_label}.{self.new_name}")
|
|
286
|
+
old_table = old_model._meta.db_table
|
|
287
|
+
new_table = new_model._meta.db_table
|
|
288
|
+
if old_table == new_table:
|
|
289
|
+
return
|
|
290
|
+
await state_editor.rename_table(new_model, old_table, new_table)
|
|
291
|
+
|
|
292
|
+
async def database_backward(
|
|
293
|
+
self,
|
|
294
|
+
app_label: str,
|
|
295
|
+
old_state: State,
|
|
296
|
+
new_state: State,
|
|
297
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
298
|
+
) -> None:
|
|
299
|
+
if not state_editor:
|
|
300
|
+
return
|
|
301
|
+
old_model = old_state.apps.get_model(f"{app_label}.{self.new_name}")
|
|
302
|
+
new_model = new_state.apps.get_model(f"{app_label}.{self.old_name}")
|
|
303
|
+
old_table = old_model._meta.db_table
|
|
304
|
+
new_table = new_model._meta.db_table
|
|
305
|
+
if old_table == new_table:
|
|
306
|
+
return
|
|
307
|
+
await state_editor.rename_table(new_model, old_table, new_table)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
class DeleteModel(TortoiseOperation):
|
|
311
|
+
def __init__(self, name: str) -> None:
|
|
312
|
+
self.name = name
|
|
313
|
+
|
|
314
|
+
def describe(self) -> str:
|
|
315
|
+
return f"Delete model {self.name}"
|
|
316
|
+
|
|
317
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
318
|
+
model_ref = f"{app_label}.{self.name}"
|
|
319
|
+
|
|
320
|
+
for model_state in state.models.values():
|
|
321
|
+
for field_name, field in model_state.fields.items():
|
|
322
|
+
if not isinstance(field, DIRECT_RELATION_FIELDS):
|
|
323
|
+
continue
|
|
324
|
+
|
|
325
|
+
if field.model_name == model_ref:
|
|
326
|
+
raise IncompatibleStateError(
|
|
327
|
+
f"{model_ref} is still referenced from {model_state.app}.{model_state.name}"
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
model_state_to_delete = state.models.pop((app_label, self.name), None)
|
|
331
|
+
if not model_state_to_delete:
|
|
332
|
+
raise IncompatibleStateError()
|
|
333
|
+
|
|
334
|
+
models_to_reload = set()
|
|
335
|
+
|
|
336
|
+
for field in model_state_to_delete.fields.values():
|
|
337
|
+
if not isinstance(field, DIRECT_RELATION_FIELDS):
|
|
338
|
+
continue
|
|
339
|
+
|
|
340
|
+
models_to_reload.add(state.apps.split_reference(field.model_name))
|
|
341
|
+
|
|
342
|
+
state.apps.unregister_model(app_label, self.name)
|
|
343
|
+
state.reload_models(models_to_reload)
|
|
344
|
+
|
|
345
|
+
async def database_forward(
|
|
346
|
+
self,
|
|
347
|
+
app_label: str,
|
|
348
|
+
old_state: State,
|
|
349
|
+
new_state: State,
|
|
350
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
351
|
+
) -> None:
|
|
352
|
+
if not state_editor:
|
|
353
|
+
return
|
|
354
|
+
model = old_state.apps.get_model(f"{app_label}.{self.name}")
|
|
355
|
+
await state_editor.delete_model(model)
|
|
356
|
+
|
|
357
|
+
async def database_backward(
|
|
358
|
+
self,
|
|
359
|
+
app_label: str,
|
|
360
|
+
old_state: State,
|
|
361
|
+
new_state: State,
|
|
362
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
363
|
+
) -> None:
|
|
364
|
+
if not state_editor:
|
|
365
|
+
return
|
|
366
|
+
model = new_state.apps.get_model(f"{app_label}.{self.name}")
|
|
367
|
+
await state_editor.create_model(model)
|
|
368
|
+
|
|
369
|
+
|
|
370
|
+
class AlterModelOptions(TortoiseOperation):
|
|
371
|
+
# Options carried by other operations; the rest belong to this one.
|
|
372
|
+
UNMANAGED_OPTION_KEYS = frozenset({"table", "app", "indexes", "unique_together", "constraints"})
|
|
373
|
+
|
|
374
|
+
def __init__(self, name: str, options: dict[str, Any]):
|
|
375
|
+
self.name = name
|
|
376
|
+
self.options = options
|
|
377
|
+
|
|
378
|
+
def describe(self) -> str:
|
|
379
|
+
return f"Alter options for {self.name}"
|
|
380
|
+
|
|
381
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
382
|
+
model_state = self.get_model_state(state, app_label, self.name)
|
|
383
|
+
|
|
384
|
+
model_state.options.update(self.options)
|
|
385
|
+
# update() only adds or changes keys, so an option removed from the model would
|
|
386
|
+
# otherwise survive here and be re-detected on every later makemigrations run.
|
|
387
|
+
for key in model_state.options.keys() - self.UNMANAGED_OPTION_KEYS - self.options.keys():
|
|
388
|
+
del model_state.options[key]
|
|
389
|
+
state.reload_model(app_label, self.name)
|
|
390
|
+
|
|
391
|
+
async def database_forward(
|
|
392
|
+
self,
|
|
393
|
+
app_label: str,
|
|
394
|
+
old_state: State,
|
|
395
|
+
new_state: State,
|
|
396
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
397
|
+
) -> None:
|
|
398
|
+
return None
|
|
399
|
+
|
|
400
|
+
async def database_backward(
|
|
401
|
+
self,
|
|
402
|
+
app_label: str,
|
|
403
|
+
old_state: State,
|
|
404
|
+
new_state: State,
|
|
405
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
406
|
+
) -> None:
|
|
407
|
+
return None
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
class AddField(TortoiseOperation):
|
|
411
|
+
def __init__(self, model_name: str, name: str, field: FieldLike) -> None:
|
|
412
|
+
self.model_name = model_name
|
|
413
|
+
self.name = name
|
|
414
|
+
self.field = field
|
|
415
|
+
|
|
416
|
+
def describe(self) -> str:
|
|
417
|
+
return f"Add field {self.name} to {self.model_name}"
|
|
418
|
+
|
|
419
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
420
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
421
|
+
|
|
422
|
+
if self.name in model_state.fields:
|
|
423
|
+
raise IncompatibleStateError(
|
|
424
|
+
f"Field {self.name} already present on model {app_label}.{self.model_name}"
|
|
425
|
+
)
|
|
426
|
+
|
|
427
|
+
model_state.fields[self.name] = cast(Field, deepcopy(self.field))
|
|
428
|
+
models_to_reload = {(app_label, self.model_name)}
|
|
429
|
+
|
|
430
|
+
if isinstance(self.field, DIRECT_RELATION_FIELDS):
|
|
431
|
+
models_to_reload.add(state.apps.split_reference(self.field.model_name))
|
|
432
|
+
|
|
433
|
+
state.reload_models(models_to_reload)
|
|
434
|
+
|
|
435
|
+
async def database_forward(
|
|
436
|
+
self,
|
|
437
|
+
app_label: str,
|
|
438
|
+
old_state: State,
|
|
439
|
+
new_state: State,
|
|
440
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
441
|
+
) -> None:
|
|
442
|
+
if not state_editor:
|
|
443
|
+
return
|
|
444
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
445
|
+
await state_editor.add_field(model, self.name)
|
|
446
|
+
|
|
447
|
+
async def database_backward(
|
|
448
|
+
self,
|
|
449
|
+
app_label: str,
|
|
450
|
+
old_state: State,
|
|
451
|
+
new_state: State,
|
|
452
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
453
|
+
) -> None:
|
|
454
|
+
if not state_editor:
|
|
455
|
+
return
|
|
456
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
457
|
+
field = model._meta.fields_map[self.name]
|
|
458
|
+
await state_editor.remove_field(model, field)
|
|
459
|
+
|
|
460
|
+
|
|
461
|
+
class RemoveField(TortoiseOperation):
|
|
462
|
+
def __init__(self, model_name: str, name: str) -> None:
|
|
463
|
+
self.model_name = model_name
|
|
464
|
+
self.name = name
|
|
465
|
+
|
|
466
|
+
def describe(self) -> str:
|
|
467
|
+
return f"Remove field {self.name} from {self.model_name}"
|
|
468
|
+
|
|
469
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
470
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
471
|
+
|
|
472
|
+
field = model_state.fields.pop(self.name, None)
|
|
473
|
+
if not field:
|
|
474
|
+
raise IncompatibleStateError(
|
|
475
|
+
f"Field {self.name} is not present on model {app_label}.{self.model_name}"
|
|
476
|
+
)
|
|
477
|
+
|
|
478
|
+
models_to_reload = {(app_label, self.model_name)}
|
|
479
|
+
if isinstance(field, DIRECT_RELATION_FIELDS):
|
|
480
|
+
models_to_reload.add(state.apps.split_reference(field.model_name))
|
|
481
|
+
|
|
482
|
+
state.reload_models(models_to_reload)
|
|
483
|
+
|
|
484
|
+
async def database_forward(
|
|
485
|
+
self,
|
|
486
|
+
app_label: str,
|
|
487
|
+
old_state: State,
|
|
488
|
+
new_state: State,
|
|
489
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
490
|
+
) -> None:
|
|
491
|
+
if not state_editor:
|
|
492
|
+
return
|
|
493
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
494
|
+
field = model._meta.fields_map[self.name]
|
|
495
|
+
await state_editor.remove_field(model, field)
|
|
496
|
+
|
|
497
|
+
async def database_backward(
|
|
498
|
+
self,
|
|
499
|
+
app_label: str,
|
|
500
|
+
old_state: State,
|
|
501
|
+
new_state: State,
|
|
502
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
503
|
+
) -> None:
|
|
504
|
+
if not state_editor:
|
|
505
|
+
return
|
|
506
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
507
|
+
await state_editor.add_field(model, self.name)
|
|
508
|
+
|
|
509
|
+
|
|
510
|
+
class AlterField(TortoiseOperation):
|
|
511
|
+
def __init__(self, model_name: str, name: str, field: FieldLike) -> None:
|
|
512
|
+
self.model_name = model_name
|
|
513
|
+
self.name = name
|
|
514
|
+
self.field = field
|
|
515
|
+
|
|
516
|
+
def describe(self) -> str:
|
|
517
|
+
return f"Alter field {self.name} on {self.model_name}"
|
|
518
|
+
|
|
519
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
520
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
521
|
+
|
|
522
|
+
if self.name not in model_state.fields:
|
|
523
|
+
raise IncompatibleStateError(
|
|
524
|
+
f"Field {self.name} is not present on model {app_label}.{self.model_name}"
|
|
525
|
+
)
|
|
526
|
+
|
|
527
|
+
model_state.fields[self.name] = cast(Field, deepcopy(self.field))
|
|
528
|
+
models_to_reload = {(app_label, self.model_name)}
|
|
529
|
+
if isinstance(self.field, DIRECT_RELATION_FIELDS):
|
|
530
|
+
models_to_reload.add(state.apps.split_reference(self.field.model_name))
|
|
531
|
+
|
|
532
|
+
state.reload_models(models_to_reload)
|
|
533
|
+
|
|
534
|
+
async def database_forward(
|
|
535
|
+
self,
|
|
536
|
+
app_label: str,
|
|
537
|
+
old_state: State,
|
|
538
|
+
new_state: State,
|
|
539
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
540
|
+
) -> None:
|
|
541
|
+
if not state_editor:
|
|
542
|
+
return
|
|
543
|
+
old_model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
544
|
+
new_model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
545
|
+
await state_editor.alter_field(old_model, new_model, self.name)
|
|
546
|
+
|
|
547
|
+
async def database_backward(
|
|
548
|
+
self,
|
|
549
|
+
app_label: str,
|
|
550
|
+
old_state: State,
|
|
551
|
+
new_state: State,
|
|
552
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
553
|
+
) -> None:
|
|
554
|
+
if not state_editor:
|
|
555
|
+
return
|
|
556
|
+
old_model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
557
|
+
new_model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
558
|
+
await state_editor.alter_field(old_model, new_model, self.name)
|
|
559
|
+
|
|
560
|
+
|
|
561
|
+
class RenameField(TortoiseOperation):
|
|
562
|
+
def __init__(self, model_name: str, old_name: str, new_name: str) -> None:
|
|
563
|
+
self.model_name = model_name
|
|
564
|
+
self.old_name = old_name
|
|
565
|
+
self.new_name = new_name
|
|
566
|
+
|
|
567
|
+
def describe(self) -> str:
|
|
568
|
+
return f"Rename field {self.old_name} to {self.new_name} on {self.model_name}"
|
|
569
|
+
|
|
570
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
571
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
572
|
+
|
|
573
|
+
if self.new_name in model_state.fields:
|
|
574
|
+
raise IncompatibleStateError(
|
|
575
|
+
f"Field {self.new_name} already present on model {app_label}.{self.model_name}"
|
|
576
|
+
)
|
|
577
|
+
|
|
578
|
+
field = model_state.fields.pop(self.old_name, None)
|
|
579
|
+
if not field:
|
|
580
|
+
raise IncompatibleStateError(
|
|
581
|
+
f"Field {self.old_name} is not present on model {app_label}.{self.model_name}"
|
|
582
|
+
)
|
|
583
|
+
|
|
584
|
+
model_state.fields[self.new_name] = deepcopy(field)
|
|
585
|
+
models_to_reload = {(app_label, self.model_name)}
|
|
586
|
+
if isinstance(field, DIRECT_RELATION_FIELDS):
|
|
587
|
+
models_to_reload.add(state.apps.split_reference(field.model_name))
|
|
588
|
+
|
|
589
|
+
state.reload_models(models_to_reload)
|
|
590
|
+
|
|
591
|
+
async def database_forward(
|
|
592
|
+
self,
|
|
593
|
+
app_label: str,
|
|
594
|
+
old_state: State,
|
|
595
|
+
new_state: State,
|
|
596
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
597
|
+
) -> None:
|
|
598
|
+
if not state_editor:
|
|
599
|
+
return
|
|
600
|
+
old_model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
601
|
+
new_model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
602
|
+
old_field = old_model._meta.fields_map[self.old_name]
|
|
603
|
+
new_field = new_model._meta.fields_map[self.new_name]
|
|
604
|
+
old_db_field = old_field.source_field or old_field.model_field_name
|
|
605
|
+
new_db_field = new_field.source_field or new_field.model_field_name
|
|
606
|
+
if old_db_field == new_db_field:
|
|
607
|
+
return
|
|
608
|
+
await state_editor._run_sql(
|
|
609
|
+
state_editor.RENAME_FIELD_TEMPLATE.format(
|
|
610
|
+
table=state_editor._qualify_table_name(
|
|
611
|
+
new_model._meta.db_table, new_model._meta.schema
|
|
612
|
+
),
|
|
613
|
+
old_column=old_db_field,
|
|
614
|
+
new_column=new_db_field,
|
|
615
|
+
)
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
async def database_backward(
|
|
619
|
+
self,
|
|
620
|
+
app_label: str,
|
|
621
|
+
old_state: State,
|
|
622
|
+
new_state: State,
|
|
623
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
624
|
+
) -> None:
|
|
625
|
+
if not state_editor:
|
|
626
|
+
return
|
|
627
|
+
old_model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
628
|
+
new_model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
629
|
+
old_field = old_model._meta.fields_map[self.new_name]
|
|
630
|
+
new_field = new_model._meta.fields_map[self.old_name]
|
|
631
|
+
old_db_field = old_field.source_field or old_field.model_field_name
|
|
632
|
+
new_db_field = new_field.source_field or new_field.model_field_name
|
|
633
|
+
if old_db_field == new_db_field:
|
|
634
|
+
return
|
|
635
|
+
await state_editor._run_sql(
|
|
636
|
+
state_editor.RENAME_FIELD_TEMPLATE.format(
|
|
637
|
+
table=state_editor._qualify_table_name(
|
|
638
|
+
new_model._meta.db_table, new_model._meta.schema
|
|
639
|
+
),
|
|
640
|
+
old_column=old_db_field,
|
|
641
|
+
new_column=new_db_field,
|
|
642
|
+
)
|
|
643
|
+
)
|
|
644
|
+
|
|
645
|
+
|
|
646
|
+
def _get_option_list(model_state: ModelState, key: str) -> list:
|
|
647
|
+
value = model_state.options.get(key)
|
|
648
|
+
if not value:
|
|
649
|
+
return []
|
|
650
|
+
if isinstance(value, tuple):
|
|
651
|
+
return list(value)
|
|
652
|
+
return list(value)
|
|
653
|
+
|
|
654
|
+
|
|
655
|
+
def _set_option_list(model_state: ModelState, key: str, values: list) -> None:
|
|
656
|
+
if values:
|
|
657
|
+
model_state.options[key] = tuple(values)
|
|
658
|
+
else:
|
|
659
|
+
model_state.options.pop(key, None)
|
|
660
|
+
|
|
661
|
+
|
|
662
|
+
class AddIndex(TortoiseOperation):
|
|
663
|
+
def __init__(self, model_name: str, index: Index) -> None:
|
|
664
|
+
self.model_name = model_name
|
|
665
|
+
self.index = index
|
|
666
|
+
|
|
667
|
+
def describe(self) -> str:
|
|
668
|
+
return f"Add index to {self.model_name}"
|
|
669
|
+
|
|
670
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
671
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
672
|
+
indexes = _get_option_list(model_state, "indexes")
|
|
673
|
+
indexes.append(self.index)
|
|
674
|
+
_set_option_list(model_state, "indexes", indexes)
|
|
675
|
+
state.reload_model(app_label, self.model_name)
|
|
676
|
+
|
|
677
|
+
async def database_forward(
|
|
678
|
+
self,
|
|
679
|
+
app_label: str,
|
|
680
|
+
old_state: State,
|
|
681
|
+
new_state: State,
|
|
682
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
683
|
+
) -> None:
|
|
684
|
+
if not state_editor:
|
|
685
|
+
return
|
|
686
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
687
|
+
await state_editor.add_index(model, self.index)
|
|
688
|
+
|
|
689
|
+
async def database_backward(
|
|
690
|
+
self,
|
|
691
|
+
app_label: str,
|
|
692
|
+
old_state: State,
|
|
693
|
+
new_state: State,
|
|
694
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
695
|
+
) -> None:
|
|
696
|
+
if not state_editor:
|
|
697
|
+
return
|
|
698
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
699
|
+
await state_editor.remove_index(model, self.index)
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
class RemoveIndex(TortoiseOperation):
|
|
703
|
+
def __init__(
|
|
704
|
+
self, model_name: str, name: str | None = None, fields: list[str] | None = None
|
|
705
|
+
) -> None:
|
|
706
|
+
if not name and not fields:
|
|
707
|
+
raise ValueError("RemoveIndex requires name or fields.")
|
|
708
|
+
self.model_name = model_name
|
|
709
|
+
self.name = name
|
|
710
|
+
self.fields = fields
|
|
711
|
+
|
|
712
|
+
def describe(self) -> str:
|
|
713
|
+
return f"Remove index from {self.model_name}"
|
|
714
|
+
|
|
715
|
+
def _find_index(self, model_state: ModelState) -> Index:
|
|
716
|
+
indexes = _get_option_list(model_state, "indexes")
|
|
717
|
+
if self.name:
|
|
718
|
+
for index in indexes:
|
|
719
|
+
if isinstance(index, Index) and index.name == self.name:
|
|
720
|
+
return index
|
|
721
|
+
if self.fields:
|
|
722
|
+
for index in indexes:
|
|
723
|
+
if isinstance(index, Index) and list(index.field_names) == list(self.fields):
|
|
724
|
+
return index
|
|
725
|
+
if not isinstance(index, Index) and list(index) == list(self.fields):
|
|
726
|
+
return Index(fields=tuple(self.fields))
|
|
727
|
+
raise IncompatibleStateError(
|
|
728
|
+
f"Index {self.name or self.fields} is not present on {self.model_name}"
|
|
729
|
+
)
|
|
730
|
+
|
|
731
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
732
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
733
|
+
indexes = _get_option_list(model_state, "indexes")
|
|
734
|
+
index = self._find_index(model_state)
|
|
735
|
+
indexes.remove(index)
|
|
736
|
+
_set_option_list(model_state, "indexes", indexes)
|
|
737
|
+
state.reload_model(app_label, self.model_name)
|
|
738
|
+
|
|
739
|
+
async def database_forward(
|
|
740
|
+
self,
|
|
741
|
+
app_label: str,
|
|
742
|
+
old_state: State,
|
|
743
|
+
new_state: State,
|
|
744
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
745
|
+
) -> None:
|
|
746
|
+
if not state_editor:
|
|
747
|
+
return
|
|
748
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
749
|
+
model_state = old_state.models[(app_label, self.model_name)]
|
|
750
|
+
index = self._find_index(model_state)
|
|
751
|
+
await state_editor.remove_index(model, index)
|
|
752
|
+
|
|
753
|
+
async def database_backward(
|
|
754
|
+
self,
|
|
755
|
+
app_label: str,
|
|
756
|
+
old_state: State,
|
|
757
|
+
new_state: State,
|
|
758
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
759
|
+
) -> None:
|
|
760
|
+
if not state_editor:
|
|
761
|
+
return
|
|
762
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
763
|
+
model_state = new_state.models[(app_label, self.model_name)]
|
|
764
|
+
index = self._find_index(model_state)
|
|
765
|
+
await state_editor.add_index(model, index)
|
|
766
|
+
|
|
767
|
+
|
|
768
|
+
class RenameIndex(TortoiseOperation):
|
|
769
|
+
def __init__(
|
|
770
|
+
self,
|
|
771
|
+
model_name: str,
|
|
772
|
+
new_name: str,
|
|
773
|
+
*,
|
|
774
|
+
old_name: str | None = None,
|
|
775
|
+
old_fields: list[str] | None = None,
|
|
776
|
+
) -> None:
|
|
777
|
+
if not old_name and not old_fields:
|
|
778
|
+
raise ValueError("RenameIndex requires old_name or old_fields.")
|
|
779
|
+
if old_name and old_fields:
|
|
780
|
+
raise ValueError("RenameIndex.old_name and old_fields are mutually exclusive.")
|
|
781
|
+
self.model_name = model_name
|
|
782
|
+
self.new_name = new_name
|
|
783
|
+
self.old_name = old_name
|
|
784
|
+
self.old_fields = old_fields
|
|
785
|
+
|
|
786
|
+
def describe(self) -> str:
|
|
787
|
+
return f"Rename index on {self.model_name}"
|
|
788
|
+
|
|
789
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
790
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
791
|
+
indexes = _get_option_list(model_state, "indexes")
|
|
792
|
+
if self.old_fields:
|
|
793
|
+
for index in list(indexes):
|
|
794
|
+
if isinstance(index, Index) and list(index.field_names) == list(self.old_fields):
|
|
795
|
+
indexes.remove(index)
|
|
796
|
+
elif not isinstance(index, Index) and list(index) == list(self.old_fields):
|
|
797
|
+
indexes.remove(index)
|
|
798
|
+
indexes.append(Index(fields=tuple(self.old_fields), name=self.new_name))
|
|
799
|
+
_set_option_list(model_state, "indexes", indexes)
|
|
800
|
+
state.reload_model(app_label, self.model_name)
|
|
801
|
+
return
|
|
802
|
+
for index in indexes:
|
|
803
|
+
if isinstance(index, Index) and index.name == self.old_name:
|
|
804
|
+
indexes.remove(index)
|
|
805
|
+
indexes.append(Index(fields=tuple(index.field_names), name=self.new_name))
|
|
806
|
+
break
|
|
807
|
+
else:
|
|
808
|
+
raise IncompatibleStateError(
|
|
809
|
+
f"Index {self.old_name} is not present on {self.model_name}"
|
|
810
|
+
)
|
|
811
|
+
_set_option_list(model_state, "indexes", indexes)
|
|
812
|
+
state.reload_model(app_label, self.model_name)
|
|
813
|
+
|
|
814
|
+
def _resolve_old_index(self, model_state: ModelState) -> Index:
|
|
815
|
+
if self.old_name:
|
|
816
|
+
for index in _get_option_list(model_state, "indexes"):
|
|
817
|
+
if isinstance(index, Index) and index.name == self.old_name:
|
|
818
|
+
return index
|
|
819
|
+
if self.old_fields:
|
|
820
|
+
return Index(fields=tuple(self.old_fields), name=self.old_name)
|
|
821
|
+
raise IncompatibleStateError()
|
|
822
|
+
|
|
823
|
+
def _resolve_new_index(self, model_state: ModelState) -> Index:
|
|
824
|
+
for index in _get_option_list(model_state, "indexes"):
|
|
825
|
+
if isinstance(index, Index) and index.name == self.new_name:
|
|
826
|
+
return index
|
|
827
|
+
raise IncompatibleStateError()
|
|
828
|
+
|
|
829
|
+
async def database_forward(
|
|
830
|
+
self,
|
|
831
|
+
app_label: str,
|
|
832
|
+
old_state: State,
|
|
833
|
+
new_state: State,
|
|
834
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
835
|
+
) -> None:
|
|
836
|
+
if not state_editor:
|
|
837
|
+
return
|
|
838
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
839
|
+
old_index = self._resolve_old_index(old_state.models[(app_label, self.model_name)])
|
|
840
|
+
new_index = self._resolve_new_index(new_state.models[(app_label, self.model_name)])
|
|
841
|
+
await state_editor.rename_index(model, old_index, new_index)
|
|
842
|
+
|
|
843
|
+
async def database_backward(
|
|
844
|
+
self,
|
|
845
|
+
app_label: str,
|
|
846
|
+
old_state: State,
|
|
847
|
+
new_state: State,
|
|
848
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
849
|
+
) -> None:
|
|
850
|
+
if not state_editor:
|
|
851
|
+
return
|
|
852
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
853
|
+
old_index = self._resolve_new_index(old_state.models[(app_label, self.model_name)])
|
|
854
|
+
new_index = self._resolve_old_index(new_state.models[(app_label, self.model_name)])
|
|
855
|
+
await state_editor.rename_index(model, old_index, new_index)
|
|
856
|
+
|
|
857
|
+
|
|
858
|
+
class AddConstraint(TortoiseOperation):
|
|
859
|
+
def __init__(self, model_name: str, constraint: UniqueConstraint | CheckConstraint) -> None:
|
|
860
|
+
self.model_name = model_name
|
|
861
|
+
self.constraint = constraint
|
|
862
|
+
|
|
863
|
+
def describe(self) -> str:
|
|
864
|
+
return f"Add constraint to {self.model_name}"
|
|
865
|
+
|
|
866
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
867
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
868
|
+
if isinstance(self.constraint, CheckConstraint):
|
|
869
|
+
constraints = _get_option_list(model_state, "constraints")
|
|
870
|
+
constraints.append(self.constraint)
|
|
871
|
+
_set_option_list(model_state, "constraints", constraints)
|
|
872
|
+
elif self.constraint.name:
|
|
873
|
+
constraints = _get_option_list(model_state, "constraints")
|
|
874
|
+
constraints.append(self.constraint)
|
|
875
|
+
_set_option_list(model_state, "constraints", constraints)
|
|
876
|
+
else:
|
|
877
|
+
unique_together = _get_option_list(model_state, "unique_together")
|
|
878
|
+
unique_together.append(self.constraint.fields)
|
|
879
|
+
_set_option_list(model_state, "unique_together", unique_together)
|
|
880
|
+
state.reload_model(app_label, self.model_name)
|
|
881
|
+
|
|
882
|
+
async def database_forward(
|
|
883
|
+
self,
|
|
884
|
+
app_label: str,
|
|
885
|
+
old_state: State,
|
|
886
|
+
new_state: State,
|
|
887
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
888
|
+
) -> None:
|
|
889
|
+
if not state_editor:
|
|
890
|
+
return
|
|
891
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
892
|
+
await state_editor.add_constraint(model, self.constraint)
|
|
893
|
+
|
|
894
|
+
async def database_backward(
|
|
895
|
+
self,
|
|
896
|
+
app_label: str,
|
|
897
|
+
old_state: State,
|
|
898
|
+
new_state: State,
|
|
899
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
900
|
+
) -> None:
|
|
901
|
+
if not state_editor:
|
|
902
|
+
return
|
|
903
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
904
|
+
await state_editor.remove_constraint(model, self.constraint)
|
|
905
|
+
|
|
906
|
+
|
|
907
|
+
class RemoveConstraint(TortoiseOperation):
|
|
908
|
+
def __init__(self, model_name: str, name: str | None = None, fields: list[str] | None = None):
|
|
909
|
+
if not name and not fields:
|
|
910
|
+
raise ValueError("RemoveConstraint requires name or fields.")
|
|
911
|
+
self.model_name = model_name
|
|
912
|
+
self.name = name
|
|
913
|
+
self.fields = fields
|
|
914
|
+
|
|
915
|
+
def describe(self) -> str:
|
|
916
|
+
return f"Remove constraint from {self.model_name}"
|
|
917
|
+
|
|
918
|
+
def _resolve_constraint(self, model_state: ModelState) -> UniqueConstraint | CheckConstraint:
|
|
919
|
+
if self.name:
|
|
920
|
+
for constraint in _get_option_list(model_state, "constraints"):
|
|
921
|
+
if isinstance(constraint, UniqueConstraint) and constraint.name == self.name:
|
|
922
|
+
return constraint
|
|
923
|
+
if isinstance(constraint, CheckConstraint) and constraint.name == self.name:
|
|
924
|
+
return constraint
|
|
925
|
+
if self.fields:
|
|
926
|
+
for fields in _get_option_list(model_state, "unique_together"):
|
|
927
|
+
if tuple(fields) == tuple(self.fields):
|
|
928
|
+
return UniqueConstraint(tuple(self.fields))
|
|
929
|
+
raise IncompatibleStateError(
|
|
930
|
+
f"Constraint {self.name or self.fields} is not present on {self.model_name}"
|
|
931
|
+
)
|
|
932
|
+
|
|
933
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
934
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
935
|
+
if self.name:
|
|
936
|
+
constraints = _get_option_list(model_state, "constraints")
|
|
937
|
+
constraints = [
|
|
938
|
+
constraint
|
|
939
|
+
for constraint in constraints
|
|
940
|
+
if not (
|
|
941
|
+
(isinstance(constraint, UniqueConstraint) and constraint.name == self.name)
|
|
942
|
+
or (isinstance(constraint, CheckConstraint) and constraint.name == self.name)
|
|
943
|
+
)
|
|
944
|
+
]
|
|
945
|
+
_set_option_list(model_state, "constraints", constraints)
|
|
946
|
+
if self.fields:
|
|
947
|
+
unique_together = _get_option_list(model_state, "unique_together")
|
|
948
|
+
unique_together = [
|
|
949
|
+
fields for fields in unique_together if tuple(fields) != tuple(self.fields)
|
|
950
|
+
]
|
|
951
|
+
_set_option_list(model_state, "unique_together", unique_together)
|
|
952
|
+
state.reload_model(app_label, self.model_name)
|
|
953
|
+
|
|
954
|
+
async def database_forward(
|
|
955
|
+
self,
|
|
956
|
+
app_label: str,
|
|
957
|
+
old_state: State,
|
|
958
|
+
new_state: State,
|
|
959
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
960
|
+
) -> None:
|
|
961
|
+
if not state_editor:
|
|
962
|
+
return
|
|
963
|
+
model = old_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
964
|
+
model_state = old_state.models[(app_label, self.model_name)]
|
|
965
|
+
constraint = self._resolve_constraint(model_state)
|
|
966
|
+
await state_editor.remove_constraint(model, constraint)
|
|
967
|
+
|
|
968
|
+
async def database_backward(
|
|
969
|
+
self,
|
|
970
|
+
app_label: str,
|
|
971
|
+
old_state: State,
|
|
972
|
+
new_state: State,
|
|
973
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
974
|
+
) -> None:
|
|
975
|
+
if not state_editor:
|
|
976
|
+
return
|
|
977
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
978
|
+
model_state = new_state.models[(app_label, self.model_name)]
|
|
979
|
+
constraint = self._resolve_constraint(model_state)
|
|
980
|
+
await state_editor.add_constraint(model, constraint)
|
|
981
|
+
|
|
982
|
+
|
|
983
|
+
class RenameConstraint(TortoiseOperation):
|
|
984
|
+
def __init__(self, model_name: str, old_name: str, new_name: str) -> None:
|
|
985
|
+
self.model_name = model_name
|
|
986
|
+
self.old_name = old_name
|
|
987
|
+
self.new_name = new_name
|
|
988
|
+
|
|
989
|
+
def describe(self) -> str:
|
|
990
|
+
return f"Rename constraint on {self.model_name}"
|
|
991
|
+
|
|
992
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
993
|
+
model_state = self.get_model_state(state, app_label, self.model_name)
|
|
994
|
+
constraints = _get_option_list(model_state, "constraints")
|
|
995
|
+
for constraint in constraints:
|
|
996
|
+
if isinstance(constraint, UniqueConstraint) and constraint.name == self.old_name:
|
|
997
|
+
constraints.remove(constraint)
|
|
998
|
+
constraints.append(
|
|
999
|
+
UniqueConstraint(
|
|
1000
|
+
fields=constraint.fields,
|
|
1001
|
+
name=self.new_name,
|
|
1002
|
+
condition=constraint.condition,
|
|
1003
|
+
)
|
|
1004
|
+
)
|
|
1005
|
+
_set_option_list(model_state, "constraints", constraints)
|
|
1006
|
+
state.reload_model(app_label, self.model_name)
|
|
1007
|
+
return
|
|
1008
|
+
if isinstance(constraint, CheckConstraint) and constraint.name == self.old_name:
|
|
1009
|
+
constraints.remove(constraint)
|
|
1010
|
+
constraints.append(
|
|
1011
|
+
CheckConstraint(
|
|
1012
|
+
check=constraint.check,
|
|
1013
|
+
name=self.new_name,
|
|
1014
|
+
)
|
|
1015
|
+
)
|
|
1016
|
+
_set_option_list(model_state, "constraints", constraints)
|
|
1017
|
+
state.reload_model(app_label, self.model_name)
|
|
1018
|
+
return
|
|
1019
|
+
raise IncompatibleStateError(
|
|
1020
|
+
f"Constraint {self.old_name} is not present on {self.model_name}"
|
|
1021
|
+
)
|
|
1022
|
+
|
|
1023
|
+
def _find_constraint_by_name(
|
|
1024
|
+
self, model_state: ModelState, name: str
|
|
1025
|
+
) -> UniqueConstraint | CheckConstraint:
|
|
1026
|
+
"""Look up a constraint by name from the model state's constraints list."""
|
|
1027
|
+
for constraint in _get_option_list(model_state, "constraints"):
|
|
1028
|
+
if (
|
|
1029
|
+
isinstance(constraint, (UniqueConstraint, CheckConstraint))
|
|
1030
|
+
and constraint.name == name
|
|
1031
|
+
):
|
|
1032
|
+
return constraint
|
|
1033
|
+
# Fallback for unnamed unique_together constraints that were given a name
|
|
1034
|
+
return UniqueConstraint(fields=(), name=name)
|
|
1035
|
+
|
|
1036
|
+
async def database_forward(
|
|
1037
|
+
self,
|
|
1038
|
+
app_label: str,
|
|
1039
|
+
old_state: State,
|
|
1040
|
+
new_state: State,
|
|
1041
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1042
|
+
) -> None:
|
|
1043
|
+
if not state_editor:
|
|
1044
|
+
return
|
|
1045
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
1046
|
+
# Look up actual constraint from old state to get the real type and fields
|
|
1047
|
+
old_model_state = old_state.models.get((app_label, self.model_name))
|
|
1048
|
+
if old_model_state:
|
|
1049
|
+
old_constraint = self._find_constraint_by_name(old_model_state, self.old_name)
|
|
1050
|
+
else:
|
|
1051
|
+
old_constraint = UniqueConstraint(fields=(), name=self.old_name)
|
|
1052
|
+
# Build new constraint with same type but new name
|
|
1053
|
+
if isinstance(old_constraint, CheckConstraint):
|
|
1054
|
+
new_constraint: UniqueConstraint | CheckConstraint = CheckConstraint(
|
|
1055
|
+
check=old_constraint.check, name=self.new_name
|
|
1056
|
+
)
|
|
1057
|
+
else:
|
|
1058
|
+
new_constraint = UniqueConstraint(
|
|
1059
|
+
fields=old_constraint.fields, name=self.new_name, condition=old_constraint.condition
|
|
1060
|
+
)
|
|
1061
|
+
await state_editor.rename_constraint(model, old_constraint, new_constraint)
|
|
1062
|
+
|
|
1063
|
+
async def database_backward(
|
|
1064
|
+
self,
|
|
1065
|
+
app_label: str,
|
|
1066
|
+
old_state: State,
|
|
1067
|
+
new_state: State,
|
|
1068
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1069
|
+
) -> None:
|
|
1070
|
+
if not state_editor:
|
|
1071
|
+
return
|
|
1072
|
+
model = new_state.apps.get_model(f"{app_label}.{self.model_name}")
|
|
1073
|
+
# Look up actual constraint from new state (which has the new_name)
|
|
1074
|
+
new_model_state = new_state.models.get((app_label, self.model_name))
|
|
1075
|
+
if new_model_state:
|
|
1076
|
+
new_constraint = self._find_constraint_by_name(new_model_state, self.new_name)
|
|
1077
|
+
else:
|
|
1078
|
+
new_constraint = UniqueConstraint(fields=(), name=self.new_name)
|
|
1079
|
+
# Build old constraint with same type but old name
|
|
1080
|
+
if isinstance(new_constraint, CheckConstraint):
|
|
1081
|
+
old_constraint: UniqueConstraint | CheckConstraint = CheckConstraint(
|
|
1082
|
+
check=new_constraint.check, name=self.old_name
|
|
1083
|
+
)
|
|
1084
|
+
else:
|
|
1085
|
+
old_constraint = UniqueConstraint(
|
|
1086
|
+
fields=new_constraint.fields, name=self.old_name, condition=new_constraint.condition
|
|
1087
|
+
)
|
|
1088
|
+
await state_editor.rename_constraint(model, new_constraint, old_constraint)
|
|
1089
|
+
|
|
1090
|
+
|
|
1091
|
+
class RunPython(TortoiseOperation):
|
|
1092
|
+
reduces_to_sql = False
|
|
1093
|
+
|
|
1094
|
+
def __init__(
|
|
1095
|
+
self,
|
|
1096
|
+
code,
|
|
1097
|
+
reverse_code=None,
|
|
1098
|
+
*,
|
|
1099
|
+
atomic: bool | None = None,
|
|
1100
|
+
) -> None:
|
|
1101
|
+
if not callable(code):
|
|
1102
|
+
raise ValueError("RunPython must be supplied with a callable")
|
|
1103
|
+
if reverse_code is not None and not callable(reverse_code):
|
|
1104
|
+
raise ValueError("RunPython must be supplied with callable arguments")
|
|
1105
|
+
self.code = code
|
|
1106
|
+
self.reverse_code = reverse_code
|
|
1107
|
+
self.atomic = atomic
|
|
1108
|
+
self.reversible = reverse_code is not None
|
|
1109
|
+
|
|
1110
|
+
def describe(self) -> str:
|
|
1111
|
+
return "Run Python code"
|
|
1112
|
+
|
|
1113
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
1114
|
+
return None
|
|
1115
|
+
|
|
1116
|
+
async def database_forward(
|
|
1117
|
+
self,
|
|
1118
|
+
app_label: str,
|
|
1119
|
+
old_state: State,
|
|
1120
|
+
new_state: State,
|
|
1121
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1122
|
+
) -> None:
|
|
1123
|
+
if not state_editor:
|
|
1124
|
+
return
|
|
1125
|
+
result = self.code(old_state.apps, state_editor)
|
|
1126
|
+
if inspect.isawaitable(result):
|
|
1127
|
+
await result
|
|
1128
|
+
|
|
1129
|
+
async def database_backward(
|
|
1130
|
+
self,
|
|
1131
|
+
app_label: str,
|
|
1132
|
+
old_state: State,
|
|
1133
|
+
new_state: State,
|
|
1134
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1135
|
+
) -> None:
|
|
1136
|
+
if not state_editor:
|
|
1137
|
+
return
|
|
1138
|
+
if self.reverse_code is None:
|
|
1139
|
+
raise NotImplementedError("RunPython reverse_code is not set")
|
|
1140
|
+
result = self.reverse_code(old_state.apps, state_editor)
|
|
1141
|
+
if inspect.isawaitable(result):
|
|
1142
|
+
await result
|
|
1143
|
+
|
|
1144
|
+
@staticmethod
|
|
1145
|
+
def noop(apps: StateApps, schema_editor: BaseSchemaEditor) -> None:
|
|
1146
|
+
return None
|
|
1147
|
+
|
|
1148
|
+
|
|
1149
|
+
class CreateSchema(TortoiseOperation):
|
|
1150
|
+
"""Create a database schema before tables that use it."""
|
|
1151
|
+
|
|
1152
|
+
def __init__(self, schema_name: str) -> None:
|
|
1153
|
+
self.schema_name = schema_name
|
|
1154
|
+
|
|
1155
|
+
def describe(self) -> str:
|
|
1156
|
+
return f"Create schema {self.schema_name}"
|
|
1157
|
+
|
|
1158
|
+
async def database_forward(
|
|
1159
|
+
self,
|
|
1160
|
+
app_label: str,
|
|
1161
|
+
old_state: State,
|
|
1162
|
+
new_state: State,
|
|
1163
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1164
|
+
) -> None:
|
|
1165
|
+
if not state_editor:
|
|
1166
|
+
return
|
|
1167
|
+
await state_editor.create_schema(self.schema_name)
|
|
1168
|
+
|
|
1169
|
+
async def database_backward(
|
|
1170
|
+
self,
|
|
1171
|
+
app_label: str,
|
|
1172
|
+
old_state: State,
|
|
1173
|
+
new_state: State,
|
|
1174
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1175
|
+
) -> None:
|
|
1176
|
+
if not state_editor:
|
|
1177
|
+
return
|
|
1178
|
+
await state_editor.drop_schema(self.schema_name)
|
|
1179
|
+
|
|
1180
|
+
|
|
1181
|
+
class DropSchema(TortoiseOperation):
|
|
1182
|
+
"""Drop a database schema."""
|
|
1183
|
+
|
|
1184
|
+
def __init__(self, schema_name: str) -> None:
|
|
1185
|
+
self.schema_name = schema_name
|
|
1186
|
+
|
|
1187
|
+
def describe(self) -> str:
|
|
1188
|
+
return f"Drop schema {self.schema_name}"
|
|
1189
|
+
|
|
1190
|
+
async def database_forward(
|
|
1191
|
+
self,
|
|
1192
|
+
app_label: str,
|
|
1193
|
+
old_state: State,
|
|
1194
|
+
new_state: State,
|
|
1195
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1196
|
+
) -> None:
|
|
1197
|
+
if not state_editor:
|
|
1198
|
+
return
|
|
1199
|
+
await state_editor.drop_schema(self.schema_name)
|
|
1200
|
+
|
|
1201
|
+
async def database_backward(
|
|
1202
|
+
self,
|
|
1203
|
+
app_label: str,
|
|
1204
|
+
old_state: State,
|
|
1205
|
+
new_state: State,
|
|
1206
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1207
|
+
) -> None:
|
|
1208
|
+
if not state_editor:
|
|
1209
|
+
return
|
|
1210
|
+
await state_editor.create_schema(self.schema_name)
|
|
1211
|
+
|
|
1212
|
+
|
|
1213
|
+
class RunSQL(TortoiseOperation):
|
|
1214
|
+
"""
|
|
1215
|
+
Run raw SQL statements. Optionally provide reverse SQL for rollback.
|
|
1216
|
+
|
|
1217
|
+
Supports:
|
|
1218
|
+
- Single SQL string
|
|
1219
|
+
- List/tuple of SQL strings
|
|
1220
|
+
- List/tuple of (sql, params) tuples for parameterized queries
|
|
1221
|
+
"""
|
|
1222
|
+
|
|
1223
|
+
reduces_to_sql = True
|
|
1224
|
+
noop = ""
|
|
1225
|
+
|
|
1226
|
+
def __init__(
|
|
1227
|
+
self,
|
|
1228
|
+
sql,
|
|
1229
|
+
reverse_sql=None,
|
|
1230
|
+
*,
|
|
1231
|
+
atomic: bool | None = None,
|
|
1232
|
+
) -> None:
|
|
1233
|
+
self.sql = sql
|
|
1234
|
+
self.reverse_sql = reverse_sql
|
|
1235
|
+
self.atomic = atomic
|
|
1236
|
+
self.reversible = reverse_sql is not None
|
|
1237
|
+
|
|
1238
|
+
def describe(self) -> str:
|
|
1239
|
+
return "Run SQL"
|
|
1240
|
+
|
|
1241
|
+
def state_forward(self, app_label: str, state: State) -> None:
|
|
1242
|
+
return None
|
|
1243
|
+
|
|
1244
|
+
async def database_forward(
|
|
1245
|
+
self,
|
|
1246
|
+
app_label: str,
|
|
1247
|
+
old_state: State,
|
|
1248
|
+
new_state: State,
|
|
1249
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1250
|
+
) -> None:
|
|
1251
|
+
if not state_editor:
|
|
1252
|
+
return
|
|
1253
|
+
await self._run_sql(state_editor, self.sql)
|
|
1254
|
+
|
|
1255
|
+
async def database_backward(
|
|
1256
|
+
self,
|
|
1257
|
+
app_label: str,
|
|
1258
|
+
old_state: State,
|
|
1259
|
+
new_state: State,
|
|
1260
|
+
state_editor: BaseSchemaEditor | None = None,
|
|
1261
|
+
) -> None:
|
|
1262
|
+
if not state_editor:
|
|
1263
|
+
return
|
|
1264
|
+
if self.reverse_sql is None:
|
|
1265
|
+
raise NotImplementedError("RunSQL reverse_sql is not set")
|
|
1266
|
+
await self._run_sql(state_editor, self.reverse_sql)
|
|
1267
|
+
|
|
1268
|
+
async def _run_sql(self, state_editor: BaseSchemaEditor, sqls) -> None:
|
|
1269
|
+
"""Execute SQL statements using the schema editor."""
|
|
1270
|
+
if isinstance(sqls, (list, tuple)):
|
|
1271
|
+
for sql in sqls:
|
|
1272
|
+
params = None
|
|
1273
|
+
if isinstance(sql, (list, tuple)):
|
|
1274
|
+
elements = len(sql)
|
|
1275
|
+
if elements == 2:
|
|
1276
|
+
sql, params = sql
|
|
1277
|
+
else:
|
|
1278
|
+
raise ValueError(f"Expected a 2-tuple but got {elements}")
|
|
1279
|
+
|
|
1280
|
+
if params:
|
|
1281
|
+
if state_editor.collect_sql:
|
|
1282
|
+
state_editor.collected_sql.append(f"{sql} -- params: {params!r}")
|
|
1283
|
+
else:
|
|
1284
|
+
await state_editor.client.execute_query(sql, params)
|
|
1285
|
+
else:
|
|
1286
|
+
await state_editor._run_sql(sql)
|
|
1287
|
+
elif sqls != RunSQL.noop:
|
|
1288
|
+
await state_editor._run_sql(sqls)
|