base.py 73 KB

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