base.py 72 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893
  1. # mypy: allow-untyped-calls
  2. from __future__ import annotations
  3. from contextlib import contextmanager
  4. import re
  5. import textwrap
  6. from typing import Any
  7. from typing import Awaitable
  8. from typing import Callable
  9. from typing import Dict
  10. from typing import Iterator
  11. from typing import List # noqa
  12. from typing import Mapping
  13. from typing import NoReturn
  14. from typing import Optional
  15. from typing import overload
  16. from typing import Sequence # noqa
  17. from typing import Tuple
  18. from typing import Type # noqa
  19. from typing import TYPE_CHECKING
  20. from typing import TypeVar
  21. from typing import Union
  22. from sqlalchemy.sql.elements import conv
  23. from . import batch
  24. from . import schemaobj
  25. from .. import util
  26. from ..util import sqla_compat
  27. from ..util.compat import formatannotation_fwdref
  28. from ..util.compat import inspect_formatargspec
  29. from ..util.compat import inspect_getfullargspec
  30. from ..util.sqla_compat import _literal_bindparam
  31. if TYPE_CHECKING:
  32. from typing import Literal
  33. from sqlalchemy import Table
  34. from sqlalchemy.engine import Connection
  35. from sqlalchemy.sql import Executable
  36. from sqlalchemy.sql.expression import ColumnElement
  37. from sqlalchemy.sql.expression import TableClause
  38. from sqlalchemy.sql.expression import TextClause
  39. from sqlalchemy.sql.functions import Function
  40. from sqlalchemy.sql.schema import Column
  41. from sqlalchemy.sql.schema import Computed
  42. from sqlalchemy.sql.schema import Identity
  43. from sqlalchemy.sql.schema import SchemaItem
  44. from sqlalchemy.types import TypeEngine
  45. from .batch import BatchOperationsImpl
  46. from .ops import AddColumnOp
  47. from .ops import AddConstraintOp
  48. from .ops import AlterColumnOp
  49. from .ops import AlterTableOp
  50. from .ops import BulkInsertOp
  51. from .ops import CreateIndexOp
  52. from .ops import CreateTableCommentOp
  53. from .ops import CreateTableOp
  54. from .ops import DropColumnOp
  55. from .ops import DropConstraintOp
  56. from .ops import DropIndexOp
  57. from .ops import DropTableCommentOp
  58. from .ops import DropTableOp
  59. from .ops import ExecuteSQLOp
  60. from .ops import MigrateOperation
  61. from ..ddl import DefaultImpl
  62. from ..runtime.migration import MigrationContext
  63. __all__ = ("Operations", "BatchOperations")
  64. _T = TypeVar("_T")
  65. _C = TypeVar("_C", bound=Callable[..., Any])
  66. class AbstractOperations(util.ModuleClsProxy):
  67. """Base class for Operations and BatchOperations.
  68. .. versionadded:: 1.11.0
  69. """
  70. impl: Union[DefaultImpl, BatchOperationsImpl]
  71. _to_impl = util.Dispatcher()
  72. def __init__(
  73. self,
  74. migration_context: MigrationContext,
  75. impl: Optional[BatchOperationsImpl] = None,
  76. ) -> None:
  77. """Construct a new :class:`.Operations`
  78. :param migration_context: a :class:`.MigrationContext`
  79. instance.
  80. """
  81. self.migration_context = migration_context
  82. if impl is None:
  83. self.impl = migration_context.impl
  84. else:
  85. self.impl = impl
  86. self.schema_obj = schemaobj.SchemaObjects(migration_context)
  87. @classmethod
  88. def register_operation(
  89. cls, name: str, sourcename: Optional[str] = None
  90. ) -> Callable[[Type[_T]], Type[_T]]:
  91. """Register a new operation for this class.
  92. This method is normally used to add new operations
  93. to the :class:`.Operations` class, and possibly the
  94. :class:`.BatchOperations` class as well. All Alembic migration
  95. operations are implemented via this system, however the system
  96. is also available as a public API to facilitate adding custom
  97. operations.
  98. .. seealso::
  99. :ref:`operation_plugins`
  100. """
  101. def register(op_cls: Type[_T]) -> Type[_T]:
  102. if sourcename is None:
  103. fn = getattr(op_cls, name)
  104. source_name = fn.__name__
  105. else:
  106. fn = getattr(op_cls, sourcename)
  107. source_name = fn.__name__
  108. spec = inspect_getfullargspec(fn)
  109. name_args = spec[0]
  110. assert name_args[0:2] == ["cls", "operations"]
  111. name_args[0:2] = ["self"]
  112. args = inspect_formatargspec(
  113. *spec, formatannotation=formatannotation_fwdref
  114. )
  115. num_defaults = len(spec[3]) if spec[3] else 0
  116. defaulted_vals: Tuple[Any, ...]
  117. if num_defaults:
  118. defaulted_vals = tuple(name_args[0 - num_defaults :])
  119. else:
  120. defaulted_vals = ()
  121. defaulted_vals += tuple(spec[4])
  122. # here, we are using formatargspec in a different way in order
  123. # to get a string that will re-apply incoming arguments to a new
  124. # function call
  125. apply_kw = inspect_formatargspec(
  126. name_args + spec[4],
  127. spec[1],
  128. spec[2],
  129. defaulted_vals,
  130. formatvalue=lambda x: "=" + x,
  131. formatannotation=formatannotation_fwdref,
  132. )
  133. args = re.sub(
  134. r'[_]?ForwardRef\(([\'"].+?[\'"])\)',
  135. lambda m: m.group(1),
  136. args,
  137. )
  138. func_text = textwrap.dedent(
  139. """\
  140. def %(name)s%(args)s:
  141. %(doc)r
  142. return op_cls.%(source_name)s%(apply_kw)s
  143. """
  144. % {
  145. "name": name,
  146. "source_name": source_name,
  147. "args": args,
  148. "apply_kw": apply_kw,
  149. "doc": fn.__doc__,
  150. }
  151. )
  152. globals_ = dict(globals())
  153. globals_.update({"op_cls": op_cls})
  154. lcl: Dict[str, Any] = {}
  155. exec(func_text, globals_, lcl)
  156. setattr(cls, name, lcl[name])
  157. fn.__func__.__doc__ = (
  158. "This method is proxied on "
  159. "the :class:`.%s` class, via the :meth:`.%s.%s` method."
  160. % (cls.__name__, cls.__name__, name)
  161. )
  162. if hasattr(fn, "_legacy_translations"):
  163. lcl[name]._legacy_translations = fn._legacy_translations
  164. return op_cls
  165. return register
  166. @classmethod
  167. def implementation_for(cls, op_cls: Any) -> Callable[[_C], _C]:
  168. """Register an implementation for a given :class:`.MigrateOperation`.
  169. This is part of the operation extensibility API.
  170. .. seealso::
  171. :ref:`operation_plugins` - example of use
  172. """
  173. def decorate(fn: _C) -> _C:
  174. cls._to_impl.dispatch_for(op_cls)(fn)
  175. return fn
  176. return decorate
  177. @classmethod
  178. @contextmanager
  179. def context(
  180. cls, migration_context: MigrationContext
  181. ) -> Iterator[Operations]:
  182. op = Operations(migration_context)
  183. op._install_proxy()
  184. yield op
  185. op._remove_proxy()
  186. @contextmanager
  187. def batch_alter_table(
  188. self,
  189. table_name: str,
  190. schema: Optional[str] = None,
  191. recreate: Literal["auto", "always", "never"] = "auto",
  192. partial_reordering: Optional[Tuple[Any, ...]] = None,
  193. copy_from: Optional[Table] = None,
  194. table_args: Tuple[Any, ...] = (),
  195. table_kwargs: Mapping[str, Any] = util.immutabledict(),
  196. reflect_args: Tuple[Any, ...] = (),
  197. reflect_kwargs: Mapping[str, Any] = util.immutabledict(),
  198. naming_convention: Optional[Dict[str, str]] = None,
  199. ) -> Iterator[BatchOperations]:
  200. """Invoke a series of per-table migrations in batch.
  201. Batch mode allows a series of operations specific to a table
  202. to be syntactically grouped together, and allows for alternate
  203. modes of table migration, in particular the "recreate" style of
  204. migration required by SQLite.
  205. "recreate" style is as follows:
  206. 1. A new table is created with the new specification, based on the
  207. migration directives within the batch, using a temporary name.
  208. 2. the data copied from the existing table to the new table.
  209. 3. the existing table is dropped.
  210. 4. the new table is renamed to the existing table name.
  211. The directive by default will only use "recreate" style on the
  212. SQLite backend, and only if directives are present which require
  213. this form, e.g. anything other than ``add_column()``. The batch
  214. operation on other backends will proceed using standard ALTER TABLE
  215. operations.
  216. The method is used as a context manager, which returns an instance
  217. of :class:`.BatchOperations`; this object is the same as
  218. :class:`.Operations` except that table names and schema names
  219. are omitted. E.g.::
  220. with op.batch_alter_table("some_table") as batch_op:
  221. batch_op.add_column(Column("foo", Integer))
  222. batch_op.drop_column("bar")
  223. The operations within the context manager are invoked at once
  224. when the context is ended. When run against SQLite, if the
  225. migrations include operations not supported by SQLite's ALTER TABLE,
  226. the entire table will be copied to a new one with the new
  227. specification, moving all data across as well.
  228. The copy operation by default uses reflection to retrieve the current
  229. structure of the table, and therefore :meth:`.batch_alter_table`
  230. in this mode requires that the migration is run in "online" mode.
  231. The ``copy_from`` parameter may be passed which refers to an existing
  232. :class:`.Table` object, which will bypass this reflection step.
  233. .. note:: The table copy operation will currently not copy
  234. CHECK constraints, and may not copy UNIQUE constraints that are
  235. unnamed, as is possible on SQLite. See the section
  236. :ref:`sqlite_batch_constraints` for workarounds.
  237. :param table_name: name of table
  238. :param schema: optional schema name.
  239. :param recreate: under what circumstances the table should be
  240. recreated. At its default of ``"auto"``, the SQLite dialect will
  241. recreate the table if any operations other than ``add_column()``,
  242. ``create_index()``, or ``drop_index()`` are
  243. present. Other options include ``"always"`` and ``"never"``.
  244. :param copy_from: optional :class:`~sqlalchemy.schema.Table` object
  245. that will act as the structure of the table being copied. If omitted,
  246. table reflection is used to retrieve the structure of the table.
  247. .. seealso::
  248. :ref:`batch_offline_mode`
  249. :paramref:`~.Operations.batch_alter_table.reflect_args`
  250. :paramref:`~.Operations.batch_alter_table.reflect_kwargs`
  251. :param reflect_args: a sequence of additional positional arguments that
  252. will be applied to the table structure being reflected / copied;
  253. this may be used to pass column and constraint overrides to the
  254. table that will be reflected, in lieu of passing the whole
  255. :class:`~sqlalchemy.schema.Table` using
  256. :paramref:`~.Operations.batch_alter_table.copy_from`.
  257. :param reflect_kwargs: a dictionary of additional keyword arguments
  258. that will be applied to the table structure being copied; this may be
  259. used to pass additional table and reflection options to the table that
  260. will be reflected, in lieu of passing the whole
  261. :class:`~sqlalchemy.schema.Table` using
  262. :paramref:`~.Operations.batch_alter_table.copy_from`.
  263. :param table_args: a sequence of additional positional arguments that
  264. will be applied to the new :class:`~sqlalchemy.schema.Table` when
  265. created, in addition to those copied from the source table.
  266. This may be used to provide additional constraints such as CHECK
  267. constraints that may not be reflected.
  268. :param table_kwargs: a dictionary of additional keyword arguments
  269. that will be applied to the new :class:`~sqlalchemy.schema.Table`
  270. when created, in addition to those copied from the source table.
  271. This may be used to provide for additional table options that may
  272. not be reflected.
  273. :param naming_convention: a naming convention dictionary of the form
  274. described at :ref:`autogen_naming_conventions` which will be applied
  275. to the :class:`~sqlalchemy.schema.MetaData` during the reflection
  276. process. This is typically required if one wants to drop SQLite
  277. constraints, as these constraints will not have names when
  278. reflected on this backend. Requires SQLAlchemy **0.9.4** or greater.
  279. .. seealso::
  280. :ref:`dropping_sqlite_foreign_keys`
  281. :param partial_reordering: a list of tuples, each suggesting a desired
  282. ordering of two or more columns in the newly created table. Requires
  283. that :paramref:`.batch_alter_table.recreate` is set to ``"always"``.
  284. Examples, given a table with columns "a", "b", "c", and "d":
  285. Specify the order of all columns::
  286. with op.batch_alter_table(
  287. "some_table",
  288. recreate="always",
  289. partial_reordering=[("c", "d", "a", "b")],
  290. ) as batch_op:
  291. pass
  292. Ensure "d" appears before "c", and "b", appears before "a"::
  293. with op.batch_alter_table(
  294. "some_table",
  295. recreate="always",
  296. partial_reordering=[("d", "c"), ("b", "a")],
  297. ) as batch_op:
  298. pass
  299. The ordering of columns not included in the partial_reordering
  300. set is undefined. Therefore it is best to specify the complete
  301. ordering of all columns for best results.
  302. .. note:: batch mode requires SQLAlchemy 0.8 or above.
  303. .. seealso::
  304. :ref:`batch_migrations`
  305. """
  306. impl = batch.BatchOperationsImpl(
  307. self,
  308. table_name,
  309. schema,
  310. recreate,
  311. copy_from,
  312. table_args,
  313. table_kwargs,
  314. reflect_args,
  315. reflect_kwargs,
  316. naming_convention,
  317. partial_reordering,
  318. )
  319. batch_op = BatchOperations(self.migration_context, impl=impl)
  320. yield batch_op
  321. impl.flush()
  322. def get_context(self) -> MigrationContext:
  323. """Return the :class:`.MigrationContext` object that's
  324. currently in use.
  325. """
  326. return self.migration_context
  327. @overload
  328. def invoke(self, operation: CreateTableOp) -> Table:
  329. ...
  330. @overload
  331. def invoke(
  332. self,
  333. operation: Union[
  334. AddConstraintOp,
  335. DropConstraintOp,
  336. CreateIndexOp,
  337. DropIndexOp,
  338. AddColumnOp,
  339. AlterColumnOp,
  340. AlterTableOp,
  341. CreateTableCommentOp,
  342. DropTableCommentOp,
  343. DropColumnOp,
  344. BulkInsertOp,
  345. DropTableOp,
  346. ExecuteSQLOp,
  347. ],
  348. ) -> None:
  349. ...
  350. @overload
  351. def invoke(self, operation: MigrateOperation) -> Any:
  352. ...
  353. def invoke(self, operation: MigrateOperation) -> Any:
  354. """Given a :class:`.MigrateOperation`, invoke it in terms of
  355. this :class:`.Operations` instance.
  356. """
  357. fn = self._to_impl.dispatch(
  358. operation, self.migration_context.impl.__dialect__
  359. )
  360. return fn(self, operation)
  361. def f(self, name: str) -> conv:
  362. """Indicate a string name that has already had a naming convention
  363. applied to it.
  364. This feature combines with the SQLAlchemy ``naming_convention`` feature
  365. to disambiguate constraint names that have already had naming
  366. conventions applied to them, versus those that have not. This is
  367. necessary in the case that the ``"%(constraint_name)s"`` token
  368. is used within a naming convention, so that it can be identified
  369. that this particular name should remain fixed.
  370. If the :meth:`.Operations.f` is used on a constraint, the naming
  371. convention will not take effect::
  372. op.add_column("t", "x", Boolean(name=op.f("ck_bool_t_x")))
  373. Above, the CHECK constraint generated will have the name
  374. ``ck_bool_t_x`` regardless of whether or not a naming convention is
  375. in use.
  376. Alternatively, if a naming convention is in use, and 'f' is not used,
  377. names will be converted along conventions. If the ``target_metadata``
  378. contains the naming convention
  379. ``{"ck": "ck_bool_%(table_name)s_%(constraint_name)s"}``, then the
  380. output of the following:
  381. op.add_column("t", "x", Boolean(name="x"))
  382. will be::
  383. CONSTRAINT ck_bool_t_x CHECK (x in (1, 0)))
  384. The function is rendered in the output of autogenerate when
  385. a particular constraint name is already converted.
  386. """
  387. return conv(name)
  388. def inline_literal(
  389. self, value: Union[str, int], type_: Optional[TypeEngine[Any]] = None
  390. ) -> _literal_bindparam:
  391. r"""Produce an 'inline literal' expression, suitable for
  392. using in an INSERT, UPDATE, or DELETE statement.
  393. When using Alembic in "offline" mode, CRUD operations
  394. aren't compatible with SQLAlchemy's default behavior surrounding
  395. literal values,
  396. which is that they are converted into bound values and passed
  397. separately into the ``execute()`` method of the DBAPI cursor.
  398. An offline SQL
  399. script needs to have these rendered inline. While it should
  400. always be noted that inline literal values are an **enormous**
  401. security hole in an application that handles untrusted input,
  402. a schema migration is not run in this context, so
  403. literals are safe to render inline, with the caveat that
  404. advanced types like dates may not be supported directly
  405. by SQLAlchemy.
  406. See :meth:`.Operations.execute` for an example usage of
  407. :meth:`.Operations.inline_literal`.
  408. The environment can also be configured to attempt to render
  409. "literal" values inline automatically, for those simple types
  410. that are supported by the dialect; see
  411. :paramref:`.EnvironmentContext.configure.literal_binds` for this
  412. more recently added feature.
  413. :param value: The value to render. Strings, integers, and simple
  414. numerics should be supported. Other types like boolean,
  415. dates, etc. may or may not be supported yet by various
  416. backends.
  417. :param type\_: optional - a :class:`sqlalchemy.types.TypeEngine`
  418. subclass stating the type of this value. In SQLAlchemy
  419. expressions, this is usually derived automatically
  420. from the Python type of the value itself, as well as
  421. based on the context in which the value is used.
  422. .. seealso::
  423. :paramref:`.EnvironmentContext.configure.literal_binds`
  424. """
  425. return sqla_compat._literal_bindparam(None, value, type_=type_)
  426. def get_bind(self) -> Connection:
  427. """Return the current 'bind'.
  428. Under normal circumstances, this is the
  429. :class:`~sqlalchemy.engine.Connection` currently being used
  430. to emit SQL to the database.
  431. In a SQL script context, this value is ``None``. [TODO: verify this]
  432. """
  433. return self.migration_context.impl.bind # type: ignore[return-value]
  434. def run_async(
  435. self,
  436. async_function: Callable[..., Awaitable[_T]],
  437. *args: Any,
  438. **kw_args: Any,
  439. ) -> _T:
  440. """Invoke the given asynchronous callable, passing an asynchronous
  441. :class:`~sqlalchemy.ext.asyncio.AsyncConnection` as the first
  442. argument.
  443. This method allows calling async functions from within the
  444. synchronous ``upgrade()`` or ``downgrade()`` alembic migration
  445. method.
  446. The async connection passed to the callable shares the same
  447. transaction as the connection running in the migration context.
  448. Any additional arg or kw_arg passed to this function are passed
  449. to the provided async function.
  450. .. versionadded: 1.11
  451. .. note::
  452. This method can be called only when alembic is called using
  453. an async dialect.
  454. """
  455. if not sqla_compat.sqla_14_18:
  456. raise NotImplementedError("SQLAlchemy 1.4.18+ required")
  457. sync_conn = self.get_bind()
  458. if sync_conn is None:
  459. raise NotImplementedError("Cannot call run_async in SQL mode")
  460. if not sync_conn.dialect.is_async:
  461. raise ValueError("Cannot call run_async with a sync engine")
  462. from sqlalchemy.ext.asyncio import AsyncConnection
  463. from sqlalchemy.util import await_only
  464. async_conn = AsyncConnection._retrieve_proxy_for_target(sync_conn)
  465. return await_only(async_function(async_conn, *args, **kw_args))
  466. class Operations(AbstractOperations):
  467. """Define high level migration operations.
  468. Each operation corresponds to some schema migration operation,
  469. executed against a particular :class:`.MigrationContext`
  470. which in turn represents connectivity to a database,
  471. or a file output stream.
  472. While :class:`.Operations` is normally configured as
  473. part of the :meth:`.EnvironmentContext.run_migrations`
  474. method called from an ``env.py`` script, a standalone
  475. :class:`.Operations` instance can be
  476. made for use cases external to regular Alembic
  477. migrations by passing in a :class:`.MigrationContext`::
  478. from alembic.migration import MigrationContext
  479. from alembic.operations import Operations
  480. conn = myengine.connect()
  481. ctx = MigrationContext.configure(conn)
  482. op = Operations(ctx)
  483. op.alter_column("t", "c", nullable=True)
  484. Note that as of 0.8, most of the methods on this class are produced
  485. dynamically using the :meth:`.Operations.register_operation`
  486. method.
  487. """
  488. if TYPE_CHECKING:
  489. # START STUB FUNCTIONS: op_cls
  490. # ### the following stubs are generated by tools/write_pyi.py ###
  491. # ### do not edit ###
  492. def add_column(
  493. self,
  494. table_name: str,
  495. column: Column[Any],
  496. *,
  497. schema: Optional[str] = None,
  498. ) -> None:
  499. """Issue an "add column" instruction using the current
  500. migration context.
  501. e.g.::
  502. from alembic import op
  503. from sqlalchemy import Column, String
  504. op.add_column("organization", Column("name", String()))
  505. The :meth:`.Operations.add_column` method typically corresponds
  506. to the SQL command "ALTER TABLE... ADD COLUMN". Within the scope
  507. of this command, the column's name, datatype, nullability,
  508. and optional server-generated defaults may be indicated.
  509. .. note::
  510. With the exception of NOT NULL constraints or single-column FOREIGN
  511. KEY constraints, other kinds of constraints such as PRIMARY KEY,
  512. UNIQUE or CHECK constraints **cannot** be generated using this
  513. method; for these constraints, refer to operations such as
  514. :meth:`.Operations.create_primary_key` and
  515. :meth:`.Operations.create_check_constraint`. In particular, the
  516. following :class:`~sqlalchemy.schema.Column` parameters are
  517. **ignored**:
  518. * :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
  519. typically do not support an ALTER operation that can add
  520. individual columns one at a time to an existing primary key
  521. constraint, therefore it's less ambiguous to use the
  522. :meth:`.Operations.create_primary_key` method, which assumes no
  523. existing primary key constraint is present.
  524. * :paramref:`~sqlalchemy.schema.Column.unique` - use the
  525. :meth:`.Operations.create_unique_constraint` method
  526. * :paramref:`~sqlalchemy.schema.Column.index` - use the
  527. :meth:`.Operations.create_index` method
  528. The provided :class:`~sqlalchemy.schema.Column` object may include a
  529. :class:`~sqlalchemy.schema.ForeignKey` constraint directive,
  530. referencing a remote table name. For this specific type of constraint,
  531. Alembic will automatically emit a second ALTER statement in order to
  532. add the single-column FOREIGN KEY constraint separately::
  533. from alembic import op
  534. from sqlalchemy import Column, INTEGER, ForeignKey
  535. op.add_column(
  536. "organization",
  537. Column("account_id", INTEGER, ForeignKey("accounts.id")),
  538. )
  539. The column argument passed to :meth:`.Operations.add_column` is a
  540. :class:`~sqlalchemy.schema.Column` construct, used in the same way it's
  541. used in SQLAlchemy. In particular, values or functions to be indicated
  542. as producing the column's default value on the database side are
  543. specified using the ``server_default`` parameter, and not ``default``
  544. which only specifies Python-side defaults::
  545. from alembic import op
  546. from sqlalchemy import Column, TIMESTAMP, func
  547. # specify "DEFAULT NOW" along with the column add
  548. op.add_column(
  549. "account",
  550. Column("timestamp", TIMESTAMP, server_default=func.now()),
  551. )
  552. :param table_name: String name of the parent table.
  553. :param column: a :class:`sqlalchemy.schema.Column` object
  554. representing the new column.
  555. :param schema: Optional schema name to operate within. To control
  556. quoting of the schema outside of the default behavior, use
  557. the SQLAlchemy construct
  558. :class:`~sqlalchemy.sql.elements.quoted_name`.
  559. """ # noqa: E501
  560. ...
  561. def alter_column(
  562. self,
  563. table_name: str,
  564. column_name: str,
  565. *,
  566. nullable: Optional[bool] = None,
  567. comment: Union[str, Literal[False], None] = False,
  568. server_default: Any = False,
  569. new_column_name: Optional[str] = None,
  570. type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None,
  571. existing_type: Union[
  572. TypeEngine[Any], Type[TypeEngine[Any]], None
  573. ] = None,
  574. existing_server_default: Union[
  575. str, bool, Identity, Computed, None
  576. ] = False,
  577. existing_nullable: Optional[bool] = None,
  578. existing_comment: Optional[str] = None,
  579. schema: Optional[str] = None,
  580. **kw: Any,
  581. ) -> None:
  582. r"""Issue an "alter column" instruction using the
  583. current migration context.
  584. Generally, only that aspect of the column which
  585. is being changed, i.e. name, type, nullability,
  586. default, needs to be specified. Multiple changes
  587. can also be specified at once and the backend should
  588. "do the right thing", emitting each change either
  589. separately or together as the backend allows.
  590. MySQL has special requirements here, since MySQL
  591. cannot ALTER a column without a full specification.
  592. When producing MySQL-compatible migration files,
  593. it is recommended that the ``existing_type``,
  594. ``existing_server_default``, and ``existing_nullable``
  595. parameters be present, if not being altered.
  596. Type changes which are against the SQLAlchemy
  597. "schema" types :class:`~sqlalchemy.types.Boolean`
  598. and :class:`~sqlalchemy.types.Enum` may also
  599. add or drop constraints which accompany those
  600. types on backends that don't support them natively.
  601. The ``existing_type`` argument is
  602. used in this case to identify and remove a previous
  603. constraint that was bound to the type object.
  604. :param table_name: string name of the target table.
  605. :param column_name: string name of the target column,
  606. as it exists before the operation begins.
  607. :param nullable: Optional; specify ``True`` or ``False``
  608. to alter the column's nullability.
  609. :param server_default: Optional; specify a string
  610. SQL expression, :func:`~sqlalchemy.sql.expression.text`,
  611. or :class:`~sqlalchemy.schema.DefaultClause` to indicate
  612. an alteration to the column's default value.
  613. Set to ``None`` to have the default removed.
  614. :param comment: optional string text of a new comment to add to the
  615. column.
  616. :param new_column_name: Optional; specify a string name here to
  617. indicate the new name within a column rename operation.
  618. :param type\_: Optional; a :class:`~sqlalchemy.types.TypeEngine`
  619. type object to specify a change to the column's type.
  620. For SQLAlchemy types that also indicate a constraint (i.e.
  621. :class:`~sqlalchemy.types.Boolean`, :class:`~sqlalchemy.types.Enum`),
  622. the constraint is also generated.
  623. :param autoincrement: set the ``AUTO_INCREMENT`` flag of the column;
  624. currently understood by the MySQL dialect.
  625. :param existing_type: Optional; a
  626. :class:`~sqlalchemy.types.TypeEngine`
  627. type object to specify the previous type. This
  628. is required for all MySQL column alter operations that
  629. don't otherwise specify a new type, as well as for
  630. when nullability is being changed on a SQL Server
  631. column. It is also used if the type is a so-called
  632. SQLAlchemy "schema" type which may define a constraint (i.e.
  633. :class:`~sqlalchemy.types.Boolean`,
  634. :class:`~sqlalchemy.types.Enum`),
  635. so that the constraint can be dropped.
  636. :param existing_server_default: Optional; The existing
  637. default value of the column. Required on MySQL if
  638. an existing default is not being changed; else MySQL
  639. removes the default.
  640. :param existing_nullable: Optional; the existing nullability
  641. of the column. Required on MySQL if the existing nullability
  642. is not being changed; else MySQL sets this to NULL.
  643. :param existing_autoincrement: Optional; the existing autoincrement
  644. of the column. Used for MySQL's system of altering a column
  645. that specifies ``AUTO_INCREMENT``.
  646. :param existing_comment: string text of the existing comment on the
  647. column to be maintained. Required on MySQL if the existing comment
  648. on the column is not being changed.
  649. :param schema: Optional schema name to operate within. To control
  650. quoting of the schema outside of the default behavior, use
  651. the SQLAlchemy construct
  652. :class:`~sqlalchemy.sql.elements.quoted_name`.
  653. :param postgresql_using: String argument which will indicate a
  654. SQL expression to render within the Postgresql-specific USING clause
  655. within ALTER COLUMN. This string is taken directly as raw SQL which
  656. must explicitly include any necessary quoting or escaping of tokens
  657. within the expression.
  658. """ # noqa: E501
  659. ...
  660. def bulk_insert(
  661. self,
  662. table: Union[Table, TableClause],
  663. rows: List[Dict[str, Any]],
  664. *,
  665. multiinsert: bool = True,
  666. ) -> None:
  667. """Issue a "bulk insert" operation using the current
  668. migration context.
  669. This provides a means of representing an INSERT of multiple rows
  670. which works equally well in the context of executing on a live
  671. connection as well as that of generating a SQL script. In the
  672. case of a SQL script, the values are rendered inline into the
  673. statement.
  674. e.g.::
  675. from alembic import op
  676. from datetime import date
  677. from sqlalchemy.sql import table, column
  678. from sqlalchemy import String, Integer, Date
  679. # Create an ad-hoc table to use for the insert statement.
  680. accounts_table = table(
  681. "account",
  682. column("id", Integer),
  683. column("name", String),
  684. column("create_date", Date),
  685. )
  686. op.bulk_insert(
  687. accounts_table,
  688. [
  689. {
  690. "id": 1,
  691. "name": "John Smith",
  692. "create_date": date(2010, 10, 5),
  693. },
  694. {
  695. "id": 2,
  696. "name": "Ed Williams",
  697. "create_date": date(2007, 5, 27),
  698. },
  699. {
  700. "id": 3,
  701. "name": "Wendy Jones",
  702. "create_date": date(2008, 8, 15),
  703. },
  704. ],
  705. )
  706. When using --sql mode, some datatypes may not render inline
  707. automatically, such as dates and other special types. When this
  708. issue is present, :meth:`.Operations.inline_literal` may be used::
  709. op.bulk_insert(
  710. accounts_table,
  711. [
  712. {
  713. "id": 1,
  714. "name": "John Smith",
  715. "create_date": op.inline_literal("2010-10-05"),
  716. },
  717. {
  718. "id": 2,
  719. "name": "Ed Williams",
  720. "create_date": op.inline_literal("2007-05-27"),
  721. },
  722. {
  723. "id": 3,
  724. "name": "Wendy Jones",
  725. "create_date": op.inline_literal("2008-08-15"),
  726. },
  727. ],
  728. multiinsert=False,
  729. )
  730. When using :meth:`.Operations.inline_literal` in conjunction with
  731. :meth:`.Operations.bulk_insert`, in order for the statement to work
  732. in "online" (e.g. non --sql) mode, the
  733. :paramref:`~.Operations.bulk_insert.multiinsert`
  734. flag should be set to ``False``, which will have the effect of
  735. individual INSERT statements being emitted to the database, each
  736. with a distinct VALUES clause, so that the "inline" values can
  737. still be rendered, rather than attempting to pass the values
  738. as bound parameters.
  739. :param table: a table object which represents the target of the INSERT.
  740. :param rows: a list of dictionaries indicating rows.
  741. :param multiinsert: when at its default of True and --sql mode is not
  742. enabled, the INSERT statement will be executed using
  743. "executemany()" style, where all elements in the list of
  744. dictionaries are passed as bound parameters in a single
  745. list. Setting this to False results in individual INSERT
  746. statements being emitted per parameter set, and is needed
  747. in those cases where non-literal values are present in the
  748. parameter sets.
  749. """ # noqa: E501
  750. ...
  751. def create_check_constraint(
  752. self,
  753. constraint_name: Optional[str],
  754. table_name: str,
  755. condition: Union[str, ColumnElement[bool], TextClause],
  756. *,
  757. schema: Optional[str] = None,
  758. **kw: Any,
  759. ) -> None:
  760. """Issue a "create check constraint" instruction using the
  761. current migration context.
  762. e.g.::
  763. from alembic import op
  764. from sqlalchemy.sql import column, func
  765. op.create_check_constraint(
  766. "ck_user_name_len",
  767. "user",
  768. func.len(column("name")) > 5,
  769. )
  770. CHECK constraints are usually against a SQL expression, so ad-hoc
  771. table metadata is usually needed. The function will convert the given
  772. arguments into a :class:`sqlalchemy.schema.CheckConstraint` bound
  773. to an anonymous table in order to emit the CREATE statement.
  774. :param name: Name of the check constraint. The name is necessary
  775. so that an ALTER statement can be emitted. For setups that
  776. use an automated naming scheme such as that described at
  777. :ref:`sqla:constraint_naming_conventions`,
  778. ``name`` here can be ``None``, as the event listener will
  779. apply the name to the constraint object when it is associated
  780. with the table.
  781. :param table_name: String name of the source table.
  782. :param condition: SQL expression that's the condition of the
  783. constraint. Can be a string or SQLAlchemy expression language
  784. structure.
  785. :param deferrable: optional bool. If set, emit DEFERRABLE or
  786. NOT DEFERRABLE when issuing DDL for this constraint.
  787. :param initially: optional string. If set, emit INITIALLY <value>
  788. when issuing DDL for this constraint.
  789. :param schema: Optional schema name to operate within. To control
  790. quoting of the schema outside of the default behavior, use
  791. the SQLAlchemy construct
  792. :class:`~sqlalchemy.sql.elements.quoted_name`.
  793. """ # noqa: E501
  794. ...
  795. def create_exclude_constraint(
  796. self,
  797. constraint_name: str,
  798. table_name: str,
  799. *elements: Any,
  800. **kw: Any,
  801. ) -> Optional[Table]:
  802. """Issue an alter to create an EXCLUDE constraint using the
  803. current migration context.
  804. .. note:: This method is Postgresql specific, and additionally
  805. requires at least SQLAlchemy 1.0.
  806. e.g.::
  807. from alembic import op
  808. op.create_exclude_constraint(
  809. "user_excl",
  810. "user",
  811. ("period", "&&"),
  812. ("group", "="),
  813. where=("group != 'some group'"),
  814. )
  815. Note that the expressions work the same way as that of
  816. the ``ExcludeConstraint`` object itself; if plain strings are
  817. passed, quoting rules must be applied manually.
  818. :param name: Name of the constraint.
  819. :param table_name: String name of the source table.
  820. :param elements: exclude conditions.
  821. :param where: SQL expression or SQL string with optional WHERE
  822. clause.
  823. :param deferrable: optional bool. If set, emit DEFERRABLE or
  824. NOT DEFERRABLE when issuing DDL for this constraint.
  825. :param initially: optional string. If set, emit INITIALLY <value>
  826. when issuing DDL for this constraint.
  827. :param schema: Optional schema name to operate within.
  828. """ # noqa: E501
  829. ...
  830. def create_foreign_key(
  831. self,
  832. constraint_name: Optional[str],
  833. source_table: str,
  834. referent_table: str,
  835. local_cols: List[str],
  836. remote_cols: List[str],
  837. *,
  838. onupdate: Optional[str] = None,
  839. ondelete: Optional[str] = None,
  840. deferrable: Optional[bool] = None,
  841. initially: Optional[str] = None,
  842. match: Optional[str] = None,
  843. source_schema: Optional[str] = None,
  844. referent_schema: Optional[str] = None,
  845. **dialect_kw: Any,
  846. ) -> None:
  847. """Issue a "create foreign key" instruction using the
  848. current migration context.
  849. e.g.::
  850. from alembic import op
  851. op.create_foreign_key(
  852. "fk_user_address",
  853. "address",
  854. "user",
  855. ["user_id"],
  856. ["id"],
  857. )
  858. This internally generates a :class:`~sqlalchemy.schema.Table` object
  859. containing the necessary columns, then generates a new
  860. :class:`~sqlalchemy.schema.ForeignKeyConstraint`
  861. object which it then associates with the
  862. :class:`~sqlalchemy.schema.Table`.
  863. Any event listeners associated with this action will be fired
  864. off normally. The :class:`~sqlalchemy.schema.AddConstraint`
  865. construct is ultimately used to generate the ALTER statement.
  866. :param constraint_name: Name of the foreign key constraint. The name
  867. is necessary so that an ALTER statement can be emitted. For setups
  868. that use an automated naming scheme such as that described at
  869. :ref:`sqla:constraint_naming_conventions`,
  870. ``name`` here can be ``None``, as the event listener will
  871. apply the name to the constraint object when it is associated
  872. with the table.
  873. :param source_table: String name of the source table.
  874. :param referent_table: String name of the destination table.
  875. :param local_cols: a list of string column names in the
  876. source table.
  877. :param remote_cols: a list of string column names in the
  878. remote table.
  879. :param onupdate: Optional string. If set, emit ON UPDATE <value> when
  880. issuing DDL for this constraint. Typical values include CASCADE,
  881. DELETE and RESTRICT.
  882. :param ondelete: Optional string. If set, emit ON DELETE <value> when
  883. issuing DDL for this constraint. Typical values include CASCADE,
  884. DELETE and RESTRICT.
  885. :param deferrable: optional bool. If set, emit DEFERRABLE or NOT
  886. DEFERRABLE when issuing DDL for this constraint.
  887. :param source_schema: Optional schema name of the source table.
  888. :param referent_schema: Optional schema name of the destination table.
  889. """ # noqa: E501
  890. ...
  891. def create_index(
  892. self,
  893. index_name: Optional[str],
  894. table_name: str,
  895. columns: Sequence[Union[str, TextClause, Function[Any]]],
  896. *,
  897. schema: Optional[str] = None,
  898. unique: bool = False,
  899. if_not_exists: Optional[bool] = None,
  900. **kw: Any,
  901. ) -> None:
  902. r"""Issue a "create index" instruction using the current
  903. migration context.
  904. e.g.::
  905. from alembic import op
  906. op.create_index("ik_test", "t1", ["foo", "bar"])
  907. Functional indexes can be produced by using the
  908. :func:`sqlalchemy.sql.expression.text` construct::
  909. from alembic import op
  910. from sqlalchemy import text
  911. op.create_index("ik_test", "t1", [text("lower(foo)")])
  912. :param index_name: name of the index.
  913. :param table_name: name of the owning table.
  914. :param columns: a list consisting of string column names and/or
  915. :func:`~sqlalchemy.sql.expression.text` constructs.
  916. :param schema: Optional schema name to operate within. To control
  917. quoting of the schema outside of the default behavior, use
  918. the SQLAlchemy construct
  919. :class:`~sqlalchemy.sql.elements.quoted_name`.
  920. :param unique: If True, create a unique index.
  921. :param quote: Force quoting of this column's name on or off,
  922. corresponding to ``True`` or ``False``. When left at its default
  923. of ``None``, the column identifier will be quoted according to
  924. whether the name is case sensitive (identifiers with at least one
  925. upper case character are treated as case sensitive), or if it's a
  926. reserved word. This flag is only needed to force quoting of a
  927. reserved word which is not known by the SQLAlchemy dialect.
  928. :param if_not_exists: If True, adds IF NOT EXISTS operator when
  929. creating the new index.
  930. .. versionadded:: 1.12.0
  931. :param \**kw: Additional keyword arguments not mentioned above are
  932. dialect specific, and passed in the form
  933. ``<dialectname>_<argname>``.
  934. See the documentation regarding an individual dialect at
  935. :ref:`dialect_toplevel` for detail on documented arguments.
  936. """ # noqa: E501
  937. ...
  938. def create_primary_key(
  939. self,
  940. constraint_name: Optional[str],
  941. table_name: str,
  942. columns: List[str],
  943. *,
  944. schema: Optional[str] = None,
  945. ) -> None:
  946. """Issue a "create primary key" instruction using the current
  947. migration context.
  948. e.g.::
  949. from alembic import op
  950. op.create_primary_key("pk_my_table", "my_table", ["id", "version"])
  951. This internally generates a :class:`~sqlalchemy.schema.Table` object
  952. containing the necessary columns, then generates a new
  953. :class:`~sqlalchemy.schema.PrimaryKeyConstraint`
  954. object which it then associates with the
  955. :class:`~sqlalchemy.schema.Table`.
  956. Any event listeners associated with this action will be fired
  957. off normally. The :class:`~sqlalchemy.schema.AddConstraint`
  958. construct is ultimately used to generate the ALTER statement.
  959. :param constraint_name: Name of the primary key constraint. The name
  960. is necessary so that an ALTER statement can be emitted. For setups
  961. that use an automated naming scheme such as that described at
  962. :ref:`sqla:constraint_naming_conventions`
  963. ``name`` here can be ``None``, as the event listener will
  964. apply the name to the constraint object when it is associated
  965. with the table.
  966. :param table_name: String name of the target table.
  967. :param columns: a list of string column names to be applied to the
  968. primary key constraint.
  969. :param schema: Optional schema name to operate within. To control
  970. quoting of the schema outside of the default behavior, use
  971. the SQLAlchemy construct
  972. :class:`~sqlalchemy.sql.elements.quoted_name`.
  973. """ # noqa: E501
  974. ...
  975. def create_table(
  976. self, table_name: str, *columns: SchemaItem, **kw: Any
  977. ) -> Table:
  978. r"""Issue a "create table" instruction using the current migration
  979. context.
  980. This directive receives an argument list similar to that of the
  981. traditional :class:`sqlalchemy.schema.Table` construct, but without the
  982. metadata::
  983. from sqlalchemy import INTEGER, VARCHAR, NVARCHAR, Column
  984. from alembic import op
  985. op.create_table(
  986. "account",
  987. Column("id", INTEGER, primary_key=True),
  988. Column("name", VARCHAR(50), nullable=False),
  989. Column("description", NVARCHAR(200)),
  990. Column("timestamp", TIMESTAMP, server_default=func.now()),
  991. )
  992. Note that :meth:`.create_table` accepts
  993. :class:`~sqlalchemy.schema.Column`
  994. constructs directly from the SQLAlchemy library. In particular,
  995. default values to be created on the database side are
  996. specified using the ``server_default`` parameter, and not
  997. ``default`` which only specifies Python-side defaults::
  998. from alembic import op
  999. from sqlalchemy import Column, TIMESTAMP, func
  1000. # specify "DEFAULT NOW" along with the "timestamp" column
  1001. op.create_table(
  1002. "account",
  1003. Column("id", INTEGER, primary_key=True),
  1004. Column("timestamp", TIMESTAMP, server_default=func.now()),
  1005. )
  1006. The function also returns a newly created
  1007. :class:`~sqlalchemy.schema.Table` object, corresponding to the table
  1008. specification given, which is suitable for
  1009. immediate SQL operations, in particular
  1010. :meth:`.Operations.bulk_insert`::
  1011. from sqlalchemy import INTEGER, VARCHAR, NVARCHAR, Column
  1012. from alembic import op
  1013. account_table = op.create_table(
  1014. "account",
  1015. Column("id", INTEGER, primary_key=True),
  1016. Column("name", VARCHAR(50), nullable=False),
  1017. Column("description", NVARCHAR(200)),
  1018. Column("timestamp", TIMESTAMP, server_default=func.now()),
  1019. )
  1020. op.bulk_insert(
  1021. account_table,
  1022. [
  1023. {"name": "A1", "description": "account 1"},
  1024. {"name": "A2", "description": "account 2"},
  1025. ],
  1026. )
  1027. :param table_name: Name of the table
  1028. :param \*columns: collection of :class:`~sqlalchemy.schema.Column`
  1029. objects within
  1030. the table, as well as optional :class:`~sqlalchemy.schema.Constraint`
  1031. objects
  1032. and :class:`~.sqlalchemy.schema.Index` objects.
  1033. :param schema: Optional schema name to operate within. To control
  1034. quoting of the schema outside of the default behavior, use
  1035. the SQLAlchemy construct
  1036. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1037. :param \**kw: Other keyword arguments are passed to the underlying
  1038. :class:`sqlalchemy.schema.Table` object created for the command.
  1039. :return: the :class:`~sqlalchemy.schema.Table` object corresponding
  1040. to the parameters given.
  1041. """ # noqa: E501
  1042. ...
  1043. def create_table_comment(
  1044. self,
  1045. table_name: str,
  1046. comment: Optional[str],
  1047. *,
  1048. existing_comment: Optional[str] = None,
  1049. schema: Optional[str] = None,
  1050. ) -> None:
  1051. """Emit a COMMENT ON operation to set the comment for a table.
  1052. :param table_name: string name of the target table.
  1053. :param comment: string value of the comment being registered against
  1054. the specified table.
  1055. :param existing_comment: String value of a comment
  1056. already registered on the specified table, used within autogenerate
  1057. so that the operation is reversible, but not required for direct
  1058. use.
  1059. .. seealso::
  1060. :meth:`.Operations.drop_table_comment`
  1061. :paramref:`.Operations.alter_column.comment`
  1062. """ # noqa: E501
  1063. ...
  1064. def create_unique_constraint(
  1065. self,
  1066. constraint_name: Optional[str],
  1067. table_name: str,
  1068. columns: Sequence[str],
  1069. *,
  1070. schema: Optional[str] = None,
  1071. **kw: Any,
  1072. ) -> Any:
  1073. """Issue a "create unique constraint" instruction using the
  1074. current migration context.
  1075. e.g.::
  1076. from alembic import op
  1077. op.create_unique_constraint("uq_user_name", "user", ["name"])
  1078. This internally generates a :class:`~sqlalchemy.schema.Table` object
  1079. containing the necessary columns, then generates a new
  1080. :class:`~sqlalchemy.schema.UniqueConstraint`
  1081. object which it then associates with the
  1082. :class:`~sqlalchemy.schema.Table`.
  1083. Any event listeners associated with this action will be fired
  1084. off normally. The :class:`~sqlalchemy.schema.AddConstraint`
  1085. construct is ultimately used to generate the ALTER statement.
  1086. :param name: Name of the unique constraint. The name is necessary
  1087. so that an ALTER statement can be emitted. For setups that
  1088. use an automated naming scheme such as that described at
  1089. :ref:`sqla:constraint_naming_conventions`,
  1090. ``name`` here can be ``None``, as the event listener will
  1091. apply the name to the constraint object when it is associated
  1092. with the table.
  1093. :param table_name: String name of the source table.
  1094. :param columns: a list of string column names in the
  1095. source table.
  1096. :param deferrable: optional bool. If set, emit DEFERRABLE or
  1097. NOT DEFERRABLE when issuing DDL for this constraint.
  1098. :param initially: optional string. If set, emit INITIALLY <value>
  1099. when issuing DDL for this constraint.
  1100. :param schema: Optional schema name to operate within. To control
  1101. quoting of the schema outside of the default behavior, use
  1102. the SQLAlchemy construct
  1103. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1104. """ # noqa: E501
  1105. ...
  1106. def drop_column(
  1107. self,
  1108. table_name: str,
  1109. column_name: str,
  1110. *,
  1111. schema: Optional[str] = None,
  1112. **kw: Any,
  1113. ) -> None:
  1114. """Issue a "drop column" instruction using the current
  1115. migration context.
  1116. e.g.::
  1117. drop_column("organization", "account_id")
  1118. :param table_name: name of table
  1119. :param column_name: name of column
  1120. :param schema: Optional schema name to operate within. To control
  1121. quoting of the schema outside of the default behavior, use
  1122. the SQLAlchemy construct
  1123. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1124. :param mssql_drop_check: Optional boolean. When ``True``, on
  1125. Microsoft SQL Server only, first
  1126. drop the CHECK constraint on the column using a
  1127. SQL-script-compatible
  1128. block that selects into a @variable from sys.check_constraints,
  1129. then exec's a separate DROP CONSTRAINT for that constraint.
  1130. :param mssql_drop_default: Optional boolean. When ``True``, on
  1131. Microsoft SQL Server only, first
  1132. drop the DEFAULT constraint on the column using a
  1133. SQL-script-compatible
  1134. block that selects into a @variable from sys.default_constraints,
  1135. then exec's a separate DROP CONSTRAINT for that default.
  1136. :param mssql_drop_foreign_key: Optional boolean. When ``True``, on
  1137. Microsoft SQL Server only, first
  1138. drop a single FOREIGN KEY constraint on the column using a
  1139. SQL-script-compatible
  1140. block that selects into a @variable from
  1141. sys.foreign_keys/sys.foreign_key_columns,
  1142. then exec's a separate DROP CONSTRAINT for that default. Only
  1143. works if the column has exactly one FK constraint which refers to
  1144. it, at the moment.
  1145. """ # noqa: E501
  1146. ...
  1147. def drop_constraint(
  1148. self,
  1149. constraint_name: str,
  1150. table_name: str,
  1151. type_: Optional[str] = None,
  1152. *,
  1153. schema: Optional[str] = None,
  1154. ) -> None:
  1155. r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
  1156. :param constraint_name: name of the constraint.
  1157. :param table_name: table name.
  1158. :param type\_: optional, required on MySQL. can be
  1159. 'foreignkey', 'primary', 'unique', or 'check'.
  1160. :param schema: Optional schema name to operate within. To control
  1161. quoting of the schema outside of the default behavior, use
  1162. the SQLAlchemy construct
  1163. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1164. """ # noqa: E501
  1165. ...
  1166. def drop_index(
  1167. self,
  1168. index_name: str,
  1169. table_name: Optional[str] = None,
  1170. *,
  1171. schema: Optional[str] = None,
  1172. if_exists: Optional[bool] = None,
  1173. **kw: Any,
  1174. ) -> None:
  1175. r"""Issue a "drop index" instruction using the current
  1176. migration context.
  1177. e.g.::
  1178. drop_index("accounts")
  1179. :param index_name: name of the index.
  1180. :param table_name: name of the owning table. Some
  1181. backends such as Microsoft SQL Server require this.
  1182. :param schema: Optional schema name to operate within. To control
  1183. quoting of the schema outside of the default behavior, use
  1184. the SQLAlchemy construct
  1185. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1186. :param if_exists: If True, adds IF EXISTS operator when
  1187. dropping the index.
  1188. .. versionadded:: 1.12.0
  1189. :param \**kw: Additional keyword arguments not mentioned above are
  1190. dialect specific, and passed in the form
  1191. ``<dialectname>_<argname>``.
  1192. See the documentation regarding an individual dialect at
  1193. :ref:`dialect_toplevel` for detail on documented arguments.
  1194. """ # noqa: E501
  1195. ...
  1196. def drop_table(
  1197. self, table_name: str, *, schema: Optional[str] = None, **kw: Any
  1198. ) -> None:
  1199. r"""Issue a "drop table" instruction using the current
  1200. migration context.
  1201. e.g.::
  1202. drop_table("accounts")
  1203. :param table_name: Name of the table
  1204. :param schema: Optional schema name to operate within. To control
  1205. quoting of the schema outside of the default behavior, use
  1206. the SQLAlchemy construct
  1207. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1208. :param \**kw: Other keyword arguments are passed to the underlying
  1209. :class:`sqlalchemy.schema.Table` object created for the command.
  1210. """ # noqa: E501
  1211. ...
  1212. def drop_table_comment(
  1213. self,
  1214. table_name: str,
  1215. *,
  1216. existing_comment: Optional[str] = None,
  1217. schema: Optional[str] = None,
  1218. ) -> None:
  1219. """Issue a "drop table comment" operation to
  1220. remove an existing comment set on a table.
  1221. :param table_name: string name of the target table.
  1222. :param existing_comment: An optional string value of a comment already
  1223. registered on the specified table.
  1224. .. seealso::
  1225. :meth:`.Operations.create_table_comment`
  1226. :paramref:`.Operations.alter_column.comment`
  1227. """ # noqa: E501
  1228. ...
  1229. def execute(
  1230. self,
  1231. sqltext: Union[Executable, str],
  1232. *,
  1233. execution_options: Optional[dict[str, Any]] = None,
  1234. ) -> None:
  1235. r"""Execute the given SQL using the current migration context.
  1236. The given SQL can be a plain string, e.g.::
  1237. op.execute("INSERT INTO table (foo) VALUES ('some value')")
  1238. Or it can be any kind of Core SQL Expression construct, such as
  1239. below where we use an update construct::
  1240. from sqlalchemy.sql import table, column
  1241. from sqlalchemy import String
  1242. from alembic import op
  1243. account = table("account", column("name", String))
  1244. op.execute(
  1245. account.update()
  1246. .where(account.c.name == op.inline_literal("account 1"))
  1247. .values({"name": op.inline_literal("account 2")})
  1248. )
  1249. Above, we made use of the SQLAlchemy
  1250. :func:`sqlalchemy.sql.expression.table` and
  1251. :func:`sqlalchemy.sql.expression.column` constructs to make a brief,
  1252. ad-hoc table construct just for our UPDATE statement. A full
  1253. :class:`~sqlalchemy.schema.Table` construct of course works perfectly
  1254. fine as well, though note it's a recommended practice to at least
  1255. ensure the definition of a table is self-contained within the migration
  1256. script, rather than imported from a module that may break compatibility
  1257. with older migrations.
  1258. In a SQL script context, the statement is emitted directly to the
  1259. output stream. There is *no* return result, however, as this
  1260. function is oriented towards generating a change script
  1261. that can run in "offline" mode. Additionally, parameterized
  1262. statements are discouraged here, as they *will not work* in offline
  1263. mode. Above, we use :meth:`.inline_literal` where parameters are
  1264. to be used.
  1265. For full interaction with a connected database where parameters can
  1266. also be used normally, use the "bind" available from the context::
  1267. from alembic import op
  1268. connection = op.get_bind()
  1269. connection.execute(
  1270. account.update()
  1271. .where(account.c.name == "account 1")
  1272. .values({"name": "account 2"})
  1273. )
  1274. Additionally, when passing the statement as a plain string, it is first
  1275. coerced into a :func:`sqlalchemy.sql.expression.text` construct
  1276. before being passed along. In the less likely case that the
  1277. literal SQL string contains a colon, it must be escaped with a
  1278. backslash, as::
  1279. op.execute(r"INSERT INTO table (foo) VALUES ('\:colon_value')")
  1280. :param sqltext: Any legal SQLAlchemy expression, including:
  1281. * a string
  1282. * a :func:`sqlalchemy.sql.expression.text` construct.
  1283. * a :func:`sqlalchemy.sql.expression.insert` construct.
  1284. * a :func:`sqlalchemy.sql.expression.update` construct.
  1285. * a :func:`sqlalchemy.sql.expression.delete` construct.
  1286. * Any "executable" described in SQLAlchemy Core documentation,
  1287. noting that no result set is returned.
  1288. .. note:: when passing a plain string, the statement is coerced into
  1289. a :func:`sqlalchemy.sql.expression.text` construct. This construct
  1290. considers symbols with colons, e.g. ``:foo`` to be bound parameters.
  1291. To avoid this, ensure that colon symbols are escaped, e.g.
  1292. ``\:foo``.
  1293. :param execution_options: Optional dictionary of
  1294. execution options, will be passed to
  1295. :meth:`sqlalchemy.engine.Connection.execution_options`.
  1296. """ # noqa: E501
  1297. ...
  1298. def rename_table(
  1299. self,
  1300. old_table_name: str,
  1301. new_table_name: str,
  1302. *,
  1303. schema: Optional[str] = None,
  1304. ) -> None:
  1305. """Emit an ALTER TABLE to rename a table.
  1306. :param old_table_name: old name.
  1307. :param new_table_name: new name.
  1308. :param schema: Optional schema name to operate within. To control
  1309. quoting of the schema outside of the default behavior, use
  1310. the SQLAlchemy construct
  1311. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1312. """ # noqa: E501
  1313. ...
  1314. # END STUB FUNCTIONS: op_cls
  1315. class BatchOperations(AbstractOperations):
  1316. """Modifies the interface :class:`.Operations` for batch mode.
  1317. This basically omits the ``table_name`` and ``schema`` parameters
  1318. from associated methods, as these are a given when running under batch
  1319. mode.
  1320. .. seealso::
  1321. :meth:`.Operations.batch_alter_table`
  1322. Note that as of 0.8, most of the methods on this class are produced
  1323. dynamically using the :meth:`.Operations.register_operation`
  1324. method.
  1325. """
  1326. impl: BatchOperationsImpl
  1327. def _noop(self, operation: Any) -> NoReturn:
  1328. raise NotImplementedError(
  1329. "The %s method does not apply to a batch table alter operation."
  1330. % operation
  1331. )
  1332. if TYPE_CHECKING:
  1333. # START STUB FUNCTIONS: batch_op
  1334. # ### the following stubs are generated by tools/write_pyi.py ###
  1335. # ### do not edit ###
  1336. def add_column(
  1337. self,
  1338. column: Column[Any],
  1339. *,
  1340. insert_before: Optional[str] = None,
  1341. insert_after: Optional[str] = None,
  1342. ) -> None:
  1343. """Issue an "add column" instruction using the current
  1344. batch migration context.
  1345. .. seealso::
  1346. :meth:`.Operations.add_column`
  1347. """ # noqa: E501
  1348. ...
  1349. def alter_column(
  1350. self,
  1351. column_name: str,
  1352. *,
  1353. nullable: Optional[bool] = None,
  1354. comment: Union[str, Literal[False], None] = False,
  1355. server_default: Any = False,
  1356. new_column_name: Optional[str] = None,
  1357. type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None,
  1358. existing_type: Union[
  1359. TypeEngine[Any], Type[TypeEngine[Any]], None
  1360. ] = None,
  1361. existing_server_default: Union[
  1362. str, bool, Identity, Computed, None
  1363. ] = False,
  1364. existing_nullable: Optional[bool] = None,
  1365. existing_comment: Optional[str] = None,
  1366. insert_before: Optional[str] = None,
  1367. insert_after: Optional[str] = None,
  1368. **kw: Any,
  1369. ) -> None:
  1370. """Issue an "alter column" instruction using the current
  1371. batch migration context.
  1372. Parameters are the same as that of :meth:`.Operations.alter_column`,
  1373. as well as the following option(s):
  1374. :param insert_before: String name of an existing column which this
  1375. column should be placed before, when creating the new table.
  1376. :param insert_after: String name of an existing column which this
  1377. column should be placed after, when creating the new table. If
  1378. both :paramref:`.BatchOperations.alter_column.insert_before`
  1379. and :paramref:`.BatchOperations.alter_column.insert_after` are
  1380. omitted, the column is inserted after the last existing column
  1381. in the table.
  1382. .. seealso::
  1383. :meth:`.Operations.alter_column`
  1384. """ # noqa: E501
  1385. ...
  1386. def create_check_constraint(
  1387. self,
  1388. constraint_name: str,
  1389. condition: Union[str, ColumnElement[bool], TextClause],
  1390. **kw: Any,
  1391. ) -> None:
  1392. """Issue a "create check constraint" instruction using the
  1393. current batch migration context.
  1394. The batch form of this call omits the ``source`` and ``schema``
  1395. arguments from the call.
  1396. .. seealso::
  1397. :meth:`.Operations.create_check_constraint`
  1398. """ # noqa: E501
  1399. ...
  1400. def create_exclude_constraint(
  1401. self, constraint_name: str, *elements: Any, **kw: Any
  1402. ) -> Optional[Table]:
  1403. """Issue a "create exclude constraint" instruction using the
  1404. current batch migration context.
  1405. .. note:: This method is Postgresql specific, and additionally
  1406. requires at least SQLAlchemy 1.0.
  1407. .. seealso::
  1408. :meth:`.Operations.create_exclude_constraint`
  1409. """ # noqa: E501
  1410. ...
  1411. def create_foreign_key(
  1412. self,
  1413. constraint_name: str,
  1414. referent_table: str,
  1415. local_cols: List[str],
  1416. remote_cols: List[str],
  1417. *,
  1418. referent_schema: Optional[str] = None,
  1419. onupdate: Optional[str] = None,
  1420. ondelete: Optional[str] = None,
  1421. deferrable: Optional[bool] = None,
  1422. initially: Optional[str] = None,
  1423. match: Optional[str] = None,
  1424. **dialect_kw: Any,
  1425. ) -> None:
  1426. """Issue a "create foreign key" instruction using the
  1427. current batch migration context.
  1428. The batch form of this call omits the ``source`` and ``source_schema``
  1429. arguments from the call.
  1430. e.g.::
  1431. with batch_alter_table("address") as batch_op:
  1432. batch_op.create_foreign_key(
  1433. "fk_user_address",
  1434. "user",
  1435. ["user_id"],
  1436. ["id"],
  1437. )
  1438. .. seealso::
  1439. :meth:`.Operations.create_foreign_key`
  1440. """ # noqa: E501
  1441. ...
  1442. def create_index(
  1443. self, index_name: str, columns: List[str], **kw: Any
  1444. ) -> None:
  1445. """Issue a "create index" instruction using the
  1446. current batch migration context.
  1447. .. seealso::
  1448. :meth:`.Operations.create_index`
  1449. """ # noqa: E501
  1450. ...
  1451. def create_primary_key(
  1452. self, constraint_name: str, columns: List[str]
  1453. ) -> None:
  1454. """Issue a "create primary key" instruction using the
  1455. current batch migration context.
  1456. The batch form of this call omits the ``table_name`` and ``schema``
  1457. arguments from the call.
  1458. .. seealso::
  1459. :meth:`.Operations.create_primary_key`
  1460. """ # noqa: E501
  1461. ...
  1462. def create_table_comment(
  1463. self,
  1464. comment: Optional[str],
  1465. *,
  1466. existing_comment: Optional[str] = None,
  1467. ) -> None:
  1468. """Emit a COMMENT ON operation to set the comment for a table
  1469. using the current batch migration context.
  1470. :param comment: string value of the comment being registered against
  1471. the specified table.
  1472. :param existing_comment: String value of a comment
  1473. already registered on the specified table, used within autogenerate
  1474. so that the operation is reversible, but not required for direct
  1475. use.
  1476. """ # noqa: E501
  1477. ...
  1478. def create_unique_constraint(
  1479. self, constraint_name: str, columns: Sequence[str], **kw: Any
  1480. ) -> Any:
  1481. """Issue a "create unique constraint" instruction using the
  1482. current batch migration context.
  1483. The batch form of this call omits the ``source`` and ``schema``
  1484. arguments from the call.
  1485. .. seealso::
  1486. :meth:`.Operations.create_unique_constraint`
  1487. """ # noqa: E501
  1488. ...
  1489. def drop_column(self, column_name: str, **kw: Any) -> None:
  1490. """Issue a "drop column" instruction using the current
  1491. batch migration context.
  1492. .. seealso::
  1493. :meth:`.Operations.drop_column`
  1494. """ # noqa: E501
  1495. ...
  1496. def drop_constraint(
  1497. self, constraint_name: str, type_: Optional[str] = None
  1498. ) -> None:
  1499. """Issue a "drop constraint" instruction using the
  1500. current batch migration context.
  1501. The batch form of this call omits the ``table_name`` and ``schema``
  1502. arguments from the call.
  1503. .. seealso::
  1504. :meth:`.Operations.drop_constraint`
  1505. """ # noqa: E501
  1506. ...
  1507. def drop_index(self, index_name: str, **kw: Any) -> None:
  1508. """Issue a "drop index" instruction using the
  1509. current batch migration context.
  1510. .. seealso::
  1511. :meth:`.Operations.drop_index`
  1512. """ # noqa: E501
  1513. ...
  1514. def drop_table_comment(
  1515. self, *, existing_comment: Optional[str] = None
  1516. ) -> None:
  1517. """Issue a "drop table comment" operation to
  1518. remove an existing comment set on a table using the current
  1519. batch operations context.
  1520. :param existing_comment: An optional string value of a comment already
  1521. registered on the specified table.
  1522. """ # noqa: E501
  1523. ...
  1524. def execute(
  1525. self,
  1526. sqltext: Union[Executable, str],
  1527. *,
  1528. execution_options: Optional[dict[str, Any]] = None,
  1529. ) -> None:
  1530. """Execute the given SQL using the current migration context.
  1531. .. seealso::
  1532. :meth:`.Operations.execute`
  1533. """ # noqa: E501
  1534. ...
  1535. # END STUB FUNCTIONS: batch_op