base.py 73 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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. ) -> None:
  495. """Issue an "add column" instruction using the current
  496. migration context.
  497. e.g.::
  498. from alembic import op
  499. from sqlalchemy import Column, String
  500. op.add_column("organization", Column("name", String()))
  501. The :meth:`.Operations.add_column` method typically corresponds
  502. to the SQL command "ALTER TABLE... ADD COLUMN". Within the scope
  503. of this command, the column's name, datatype, nullability,
  504. and optional server-generated defaults may be indicated.
  505. .. note::
  506. With the exception of NOT NULL constraints or single-column FOREIGN
  507. KEY constraints, other kinds of constraints such as PRIMARY KEY,
  508. UNIQUE or CHECK constraints **cannot** be generated using this
  509. method; for these constraints, refer to operations such as
  510. :meth:`.Operations.create_primary_key` and
  511. :meth:`.Operations.create_check_constraint`. In particular, the
  512. following :class:`~sqlalchemy.schema.Column` parameters are
  513. **ignored**:
  514. * :paramref:`~sqlalchemy.schema.Column.primary_key` - SQL databases
  515. typically do not support an ALTER operation that can add
  516. individual columns one at a time to an existing primary key
  517. constraint, therefore it's less ambiguous to use the
  518. :meth:`.Operations.create_primary_key` method, which assumes no
  519. existing primary key constraint is present.
  520. * :paramref:`~sqlalchemy.schema.Column.unique` - use the
  521. :meth:`.Operations.create_unique_constraint` method
  522. * :paramref:`~sqlalchemy.schema.Column.index` - use the
  523. :meth:`.Operations.create_index` method
  524. The provided :class:`~sqlalchemy.schema.Column` object may include a
  525. :class:`~sqlalchemy.schema.ForeignKey` constraint directive,
  526. referencing a remote table name. For this specific type of constraint,
  527. Alembic will automatically emit a second ALTER statement in order to
  528. add the single-column FOREIGN KEY constraint separately::
  529. from alembic import op
  530. from sqlalchemy import Column, INTEGER, ForeignKey
  531. op.add_column(
  532. "organization",
  533. Column("account_id", INTEGER, ForeignKey("accounts.id")),
  534. )
  535. The column argument passed to :meth:`.Operations.add_column` is a
  536. :class:`~sqlalchemy.schema.Column` construct, used in the same way it's
  537. used in SQLAlchemy. In particular, values or functions to be indicated
  538. as producing the column's default value on the database side are
  539. specified using the ``server_default`` parameter, and not ``default``
  540. which only specifies Python-side defaults::
  541. from alembic import op
  542. from sqlalchemy import Column, TIMESTAMP, func
  543. # specify "DEFAULT NOW" along with the column add
  544. op.add_column(
  545. "account",
  546. Column("timestamp", TIMESTAMP, server_default=func.now()),
  547. )
  548. :param table_name: String name of the parent table.
  549. :param column: a :class:`sqlalchemy.schema.Column` object
  550. representing the new column.
  551. :param schema: Optional schema name to operate within. To control
  552. quoting of the schema outside of the default behavior, use
  553. the SQLAlchemy construct
  554. :class:`~sqlalchemy.sql.elements.quoted_name`.
  555. """ # noqa: E501
  556. ...
  557. def alter_column(
  558. self,
  559. table_name: str,
  560. column_name: str,
  561. *,
  562. nullable: Optional[bool] = None,
  563. comment: Union[str, Literal[False], None] = False,
  564. server_default: Union[
  565. str, bool, Identity, Computed, TextClause
  566. ] = False,
  567. new_column_name: Optional[str] = None,
  568. type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None,
  569. existing_type: Union[
  570. TypeEngine[Any], Type[TypeEngine[Any]], None
  571. ] = None,
  572. existing_server_default: Union[
  573. str, bool, Identity, Computed, TextClause, None
  574. ] = False,
  575. existing_nullable: Optional[bool] = None,
  576. existing_comment: Optional[str] = None,
  577. schema: Optional[str] = None,
  578. **kw: Any,
  579. ) -> None:
  580. r"""Issue an "alter column" instruction using the
  581. current migration context.
  582. Generally, only that aspect of the column which
  583. is being changed, i.e. name, type, nullability,
  584. default, needs to be specified. Multiple changes
  585. can also be specified at once and the backend should
  586. "do the right thing", emitting each change either
  587. separately or together as the backend allows.
  588. MySQL has special requirements here, since MySQL
  589. cannot ALTER a column without a full specification.
  590. When producing MySQL-compatible migration files,
  591. it is recommended that the ``existing_type``,
  592. ``existing_server_default``, and ``existing_nullable``
  593. parameters be present, if not being altered.
  594. Type changes which are against the SQLAlchemy
  595. "schema" types :class:`~sqlalchemy.types.Boolean`
  596. and :class:`~sqlalchemy.types.Enum` may also
  597. add or drop constraints which accompany those
  598. types on backends that don't support them natively.
  599. The ``existing_type`` argument is
  600. used in this case to identify and remove a previous
  601. constraint that was bound to the type object.
  602. :param table_name: string name of the target table.
  603. :param column_name: string name of the target column,
  604. as it exists before the operation begins.
  605. :param nullable: Optional; specify ``True`` or ``False``
  606. to alter the column's nullability.
  607. :param server_default: Optional; specify a string
  608. SQL expression, :func:`~sqlalchemy.sql.expression.text`,
  609. or :class:`~sqlalchemy.schema.DefaultClause` to indicate
  610. an alteration to the column's default value.
  611. Set to ``None`` to have the default removed.
  612. :param comment: optional string text of a new comment to add to the
  613. column.
  614. :param new_column_name: Optional; specify a string name here to
  615. indicate the new name within a column rename operation.
  616. :param type\_: Optional; a :class:`~sqlalchemy.types.TypeEngine`
  617. type object to specify a change to the column's type.
  618. For SQLAlchemy types that also indicate a constraint (i.e.
  619. :class:`~sqlalchemy.types.Boolean`, :class:`~sqlalchemy.types.Enum`),
  620. the constraint is also generated.
  621. :param autoincrement: set the ``AUTO_INCREMENT`` flag of the column;
  622. currently understood by the MySQL dialect.
  623. :param existing_type: Optional; a
  624. :class:`~sqlalchemy.types.TypeEngine`
  625. type object to specify the previous type. This
  626. is required for all MySQL column alter operations that
  627. don't otherwise specify a new type, as well as for
  628. when nullability is being changed on a SQL Server
  629. column. It is also used if the type is a so-called
  630. SQLAlchemy "schema" type which may define a constraint (i.e.
  631. :class:`~sqlalchemy.types.Boolean`,
  632. :class:`~sqlalchemy.types.Enum`),
  633. so that the constraint can be dropped.
  634. :param existing_server_default: Optional; The existing
  635. default value of the column. Required on MySQL if
  636. an existing default is not being changed; else MySQL
  637. removes the default.
  638. :param existing_nullable: Optional; the existing nullability
  639. of the column. Required on MySQL if the existing nullability
  640. is not being changed; else MySQL sets this to NULL.
  641. :param existing_autoincrement: Optional; the existing autoincrement
  642. of the column. Used for MySQL's system of altering a column
  643. that specifies ``AUTO_INCREMENT``.
  644. :param existing_comment: string text of the existing comment on the
  645. column to be maintained. Required on MySQL if the existing comment
  646. on the column is not being changed.
  647. :param schema: Optional schema name to operate within. To control
  648. quoting of the schema outside of the default behavior, use
  649. the SQLAlchemy construct
  650. :class:`~sqlalchemy.sql.elements.quoted_name`.
  651. :param postgresql_using: String argument which will indicate a
  652. SQL expression to render within the Postgresql-specific USING clause
  653. within ALTER COLUMN. This string is taken directly as raw SQL which
  654. must explicitly include any necessary quoting or escaping of tokens
  655. within the expression.
  656. """ # noqa: E501
  657. ...
  658. def bulk_insert(
  659. self,
  660. table: Union[Table, TableClause],
  661. rows: List[Dict[str, Any]],
  662. *,
  663. multiinsert: bool = True,
  664. ) -> None:
  665. """Issue a "bulk insert" operation using the current
  666. migration context.
  667. This provides a means of representing an INSERT of multiple rows
  668. which works equally well in the context of executing on a live
  669. connection as well as that of generating a SQL script. In the
  670. case of a SQL script, the values are rendered inline into the
  671. statement.
  672. e.g.::
  673. from alembic import op
  674. from datetime import date
  675. from sqlalchemy.sql import table, column
  676. from sqlalchemy import String, Integer, Date
  677. # Create an ad-hoc table to use for the insert statement.
  678. accounts_table = table(
  679. "account",
  680. column("id", Integer),
  681. column("name", String),
  682. column("create_date", Date),
  683. )
  684. op.bulk_insert(
  685. accounts_table,
  686. [
  687. {
  688. "id": 1,
  689. "name": "John Smith",
  690. "create_date": date(2010, 10, 5),
  691. },
  692. {
  693. "id": 2,
  694. "name": "Ed Williams",
  695. "create_date": date(2007, 5, 27),
  696. },
  697. {
  698. "id": 3,
  699. "name": "Wendy Jones",
  700. "create_date": date(2008, 8, 15),
  701. },
  702. ],
  703. )
  704. When using --sql mode, some datatypes may not render inline
  705. automatically, such as dates and other special types. When this
  706. issue is present, :meth:`.Operations.inline_literal` may be used::
  707. op.bulk_insert(
  708. accounts_table,
  709. [
  710. {
  711. "id": 1,
  712. "name": "John Smith",
  713. "create_date": op.inline_literal("2010-10-05"),
  714. },
  715. {
  716. "id": 2,
  717. "name": "Ed Williams",
  718. "create_date": op.inline_literal("2007-05-27"),
  719. },
  720. {
  721. "id": 3,
  722. "name": "Wendy Jones",
  723. "create_date": op.inline_literal("2008-08-15"),
  724. },
  725. ],
  726. multiinsert=False,
  727. )
  728. When using :meth:`.Operations.inline_literal` in conjunction with
  729. :meth:`.Operations.bulk_insert`, in order for the statement to work
  730. in "online" (e.g. non --sql) mode, the
  731. :paramref:`~.Operations.bulk_insert.multiinsert`
  732. flag should be set to ``False``, which will have the effect of
  733. individual INSERT statements being emitted to the database, each
  734. with a distinct VALUES clause, so that the "inline" values can
  735. still be rendered, rather than attempting to pass the values
  736. as bound parameters.
  737. :param table: a table object which represents the target of the INSERT.
  738. :param rows: a list of dictionaries indicating rows.
  739. :param multiinsert: when at its default of True and --sql mode is not
  740. enabled, the INSERT statement will be executed using
  741. "executemany()" style, where all elements in the list of
  742. dictionaries are passed as bound parameters in a single
  743. list. Setting this to False results in individual INSERT
  744. statements being emitted per parameter set, and is needed
  745. in those cases where non-literal values are present in the
  746. parameter sets.
  747. """ # noqa: E501
  748. ...
  749. def create_check_constraint(
  750. self,
  751. constraint_name: Optional[str],
  752. table_name: str,
  753. condition: Union[str, ColumnElement[bool], TextClause],
  754. *,
  755. schema: Optional[str] = None,
  756. **kw: Any,
  757. ) -> None:
  758. """Issue a "create check constraint" instruction using the
  759. current migration context.
  760. e.g.::
  761. from alembic import op
  762. from sqlalchemy.sql import column, func
  763. op.create_check_constraint(
  764. "ck_user_name_len",
  765. "user",
  766. func.len(column("name")) > 5,
  767. )
  768. CHECK constraints are usually against a SQL expression, so ad-hoc
  769. table metadata is usually needed. The function will convert the given
  770. arguments into a :class:`sqlalchemy.schema.CheckConstraint` bound
  771. to an anonymous table in order to emit the CREATE statement.
  772. :param name: Name of the check constraint. The name is necessary
  773. so that an ALTER statement can be emitted. For setups that
  774. use an automated naming scheme such as that described at
  775. :ref:`sqla:constraint_naming_conventions`,
  776. ``name`` here can be ``None``, as the event listener will
  777. apply the name to the constraint object when it is associated
  778. with the table.
  779. :param table_name: String name of the source table.
  780. :param condition: SQL expression that's the condition of the
  781. constraint. Can be a string or SQLAlchemy expression language
  782. structure.
  783. :param deferrable: optional bool. If set, emit DEFERRABLE or
  784. NOT DEFERRABLE when issuing DDL for this constraint.
  785. :param initially: optional string. If set, emit INITIALLY <value>
  786. when issuing DDL for this constraint.
  787. :param schema: Optional schema name to operate within. To control
  788. quoting of the schema outside of the default behavior, use
  789. the SQLAlchemy construct
  790. :class:`~sqlalchemy.sql.elements.quoted_name`.
  791. """ # noqa: E501
  792. ...
  793. def create_exclude_constraint(
  794. self,
  795. constraint_name: str,
  796. table_name: str,
  797. *elements: Any,
  798. **kw: Any,
  799. ) -> Optional[Table]:
  800. """Issue an alter to create an EXCLUDE constraint using the
  801. current migration context.
  802. .. note:: This method is Postgresql specific, and additionally
  803. requires at least SQLAlchemy 1.0.
  804. e.g.::
  805. from alembic import op
  806. op.create_exclude_constraint(
  807. "user_excl",
  808. "user",
  809. ("period", "&&"),
  810. ("group", "="),
  811. where=("group != 'some group'"),
  812. )
  813. Note that the expressions work the same way as that of
  814. the ``ExcludeConstraint`` object itself; if plain strings are
  815. passed, quoting rules must be applied manually.
  816. :param name: Name of the constraint.
  817. :param table_name: String name of the source table.
  818. :param elements: exclude conditions.
  819. :param where: SQL expression or SQL string with optional WHERE
  820. clause.
  821. :param deferrable: optional bool. If set, emit DEFERRABLE or
  822. NOT DEFERRABLE when issuing DDL for this constraint.
  823. :param initially: optional string. If set, emit INITIALLY <value>
  824. when issuing DDL for this constraint.
  825. :param schema: Optional schema name to operate within.
  826. """ # noqa: E501
  827. ...
  828. def create_foreign_key(
  829. self,
  830. constraint_name: Optional[str],
  831. source_table: str,
  832. referent_table: str,
  833. local_cols: List[str],
  834. remote_cols: List[str],
  835. *,
  836. onupdate: Optional[str] = None,
  837. ondelete: Optional[str] = None,
  838. deferrable: Optional[bool] = None,
  839. initially: Optional[str] = None,
  840. match: Optional[str] = None,
  841. source_schema: Optional[str] = None,
  842. referent_schema: Optional[str] = None,
  843. **dialect_kw: Any,
  844. ) -> None:
  845. """Issue a "create foreign key" instruction using the
  846. current migration context.
  847. e.g.::
  848. from alembic import op
  849. op.create_foreign_key(
  850. "fk_user_address",
  851. "address",
  852. "user",
  853. ["user_id"],
  854. ["id"],
  855. )
  856. This internally generates a :class:`~sqlalchemy.schema.Table` object
  857. containing the necessary columns, then generates a new
  858. :class:`~sqlalchemy.schema.ForeignKeyConstraint`
  859. object which it then associates with the
  860. :class:`~sqlalchemy.schema.Table`.
  861. Any event listeners associated with this action will be fired
  862. off normally. The :class:`~sqlalchemy.schema.AddConstraint`
  863. construct is ultimately used to generate the ALTER statement.
  864. :param constraint_name: Name of the foreign key constraint. The name
  865. is necessary so that an ALTER statement can be emitted. For setups
  866. that use an automated naming scheme such as that described at
  867. :ref:`sqla:constraint_naming_conventions`,
  868. ``name`` here can be ``None``, as the event listener will
  869. apply the name to the constraint object when it is associated
  870. with the table.
  871. :param source_table: String name of the source table.
  872. :param referent_table: String name of the destination table.
  873. :param local_cols: a list of string column names in the
  874. source table.
  875. :param remote_cols: a list of string column names in the
  876. remote table.
  877. :param onupdate: Optional string. If set, emit ON UPDATE <value> when
  878. issuing DDL for this constraint. Typical values include CASCADE,
  879. DELETE and RESTRICT.
  880. :param ondelete: Optional string. If set, emit ON DELETE <value> when
  881. issuing DDL for this constraint. Typical values include CASCADE,
  882. DELETE and RESTRICT.
  883. :param deferrable: optional bool. If set, emit DEFERRABLE or NOT
  884. DEFERRABLE when issuing DDL for this constraint.
  885. :param source_schema: Optional schema name of the source table.
  886. :param referent_schema: Optional schema name of the destination table.
  887. """ # noqa: E501
  888. ...
  889. def create_index(
  890. self,
  891. index_name: Optional[str],
  892. table_name: str,
  893. columns: Sequence[Union[str, TextClause, ColumnElement[Any]]],
  894. *,
  895. schema: Optional[str] = None,
  896. unique: bool = False,
  897. if_not_exists: Optional[bool] = None,
  898. **kw: Any,
  899. ) -> None:
  900. r"""Issue a "create index" instruction using the current
  901. migration context.
  902. e.g.::
  903. from alembic import op
  904. op.create_index("ik_test", "t1", ["foo", "bar"])
  905. Functional indexes can be produced by using the
  906. :func:`sqlalchemy.sql.expression.text` construct::
  907. from alembic import op
  908. from sqlalchemy import text
  909. op.create_index("ik_test", "t1", [text("lower(foo)")])
  910. :param index_name: name of the index.
  911. :param table_name: name of the owning table.
  912. :param columns: a list consisting of string column names and/or
  913. :func:`~sqlalchemy.sql.expression.text` constructs.
  914. :param schema: Optional schema name to operate within. To control
  915. quoting of the schema outside of the default behavior, use
  916. the SQLAlchemy construct
  917. :class:`~sqlalchemy.sql.elements.quoted_name`.
  918. :param unique: If True, create a unique index.
  919. :param quote: Force quoting of this column's name on or off,
  920. corresponding to ``True`` or ``False``. When left at its default
  921. of ``None``, the column identifier will be quoted according to
  922. whether the name is case sensitive (identifiers with at least one
  923. upper case character are treated as case sensitive), or if it's a
  924. reserved word. This flag is only needed to force quoting of a
  925. reserved word which is not known by the SQLAlchemy dialect.
  926. :param if_not_exists: If True, adds IF NOT EXISTS operator when
  927. creating the new index.
  928. .. versionadded:: 1.12.0
  929. :param \**kw: Additional keyword arguments not mentioned above are
  930. dialect specific, and passed in the form
  931. ``<dialectname>_<argname>``.
  932. See the documentation regarding an individual dialect at
  933. :ref:`dialect_toplevel` for detail on documented arguments.
  934. """ # noqa: E501
  935. ...
  936. def create_primary_key(
  937. self,
  938. constraint_name: Optional[str],
  939. table_name: str,
  940. columns: List[str],
  941. *,
  942. schema: Optional[str] = None,
  943. ) -> None:
  944. """Issue a "create primary key" instruction using the current
  945. migration context.
  946. e.g.::
  947. from alembic import op
  948. op.create_primary_key("pk_my_table", "my_table", ["id", "version"])
  949. This internally generates a :class:`~sqlalchemy.schema.Table` object
  950. containing the necessary columns, then generates a new
  951. :class:`~sqlalchemy.schema.PrimaryKeyConstraint`
  952. object which it then associates with the
  953. :class:`~sqlalchemy.schema.Table`.
  954. Any event listeners associated with this action will be fired
  955. off normally. The :class:`~sqlalchemy.schema.AddConstraint`
  956. construct is ultimately used to generate the ALTER statement.
  957. :param constraint_name: Name of the primary key constraint. The name
  958. is necessary so that an ALTER statement can be emitted. For setups
  959. that use an automated naming scheme such as that described at
  960. :ref:`sqla:constraint_naming_conventions`
  961. ``name`` here can be ``None``, as the event listener will
  962. apply the name to the constraint object when it is associated
  963. with the table.
  964. :param table_name: String name of the target table.
  965. :param columns: a list of string column names to be applied to the
  966. primary key constraint.
  967. :param schema: Optional schema name to operate within. To control
  968. quoting of the schema outside of the default behavior, use
  969. the SQLAlchemy construct
  970. :class:`~sqlalchemy.sql.elements.quoted_name`.
  971. """ # noqa: E501
  972. ...
  973. def create_table(
  974. self,
  975. table_name: str,
  976. *columns: SchemaItem,
  977. if_not_exists: Optional[bool] = None,
  978. **kw: Any,
  979. ) -> Table:
  980. r"""Issue a "create table" instruction using the current migration
  981. context.
  982. This directive receives an argument list similar to that of the
  983. traditional :class:`sqlalchemy.schema.Table` construct, but without the
  984. metadata::
  985. from sqlalchemy import INTEGER, VARCHAR, NVARCHAR, Column
  986. from alembic import op
  987. op.create_table(
  988. "account",
  989. Column("id", INTEGER, primary_key=True),
  990. Column("name", VARCHAR(50), nullable=False),
  991. Column("description", NVARCHAR(200)),
  992. Column("timestamp", TIMESTAMP, server_default=func.now()),
  993. )
  994. Note that :meth:`.create_table` accepts
  995. :class:`~sqlalchemy.schema.Column`
  996. constructs directly from the SQLAlchemy library. In particular,
  997. default values to be created on the database side are
  998. specified using the ``server_default`` parameter, and not
  999. ``default`` which only specifies Python-side defaults::
  1000. from alembic import op
  1001. from sqlalchemy import Column, TIMESTAMP, func
  1002. # specify "DEFAULT NOW" along with the "timestamp" column
  1003. op.create_table(
  1004. "account",
  1005. Column("id", INTEGER, primary_key=True),
  1006. Column("timestamp", TIMESTAMP, server_default=func.now()),
  1007. )
  1008. The function also returns a newly created
  1009. :class:`~sqlalchemy.schema.Table` object, corresponding to the table
  1010. specification given, which is suitable for
  1011. immediate SQL operations, in particular
  1012. :meth:`.Operations.bulk_insert`::
  1013. from sqlalchemy import INTEGER, VARCHAR, NVARCHAR, Column
  1014. from alembic import op
  1015. account_table = op.create_table(
  1016. "account",
  1017. Column("id", INTEGER, primary_key=True),
  1018. Column("name", VARCHAR(50), nullable=False),
  1019. Column("description", NVARCHAR(200)),
  1020. Column("timestamp", TIMESTAMP, server_default=func.now()),
  1021. )
  1022. op.bulk_insert(
  1023. account_table,
  1024. [
  1025. {"name": "A1", "description": "account 1"},
  1026. {"name": "A2", "description": "account 2"},
  1027. ],
  1028. )
  1029. :param table_name: Name of the table
  1030. :param \*columns: collection of :class:`~sqlalchemy.schema.Column`
  1031. objects within
  1032. the table, as well as optional :class:`~sqlalchemy.schema.Constraint`
  1033. objects
  1034. and :class:`~.sqlalchemy.schema.Index` objects.
  1035. :param schema: Optional schema name to operate within. To control
  1036. quoting of the schema outside of the default behavior, use
  1037. the SQLAlchemy construct
  1038. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1039. :param if_not_exists: If True, adds IF NOT EXISTS operator when
  1040. creating the new table.
  1041. .. versionadded:: 1.13.3
  1042. :param \**kw: Other keyword arguments are passed to the underlying
  1043. :class:`sqlalchemy.schema.Table` object created for the command.
  1044. :return: the :class:`~sqlalchemy.schema.Table` object corresponding
  1045. to the parameters given.
  1046. """ # noqa: E501
  1047. ...
  1048. def create_table_comment(
  1049. self,
  1050. table_name: str,
  1051. comment: Optional[str],
  1052. *,
  1053. existing_comment: Optional[str] = None,
  1054. schema: Optional[str] = None,
  1055. ) -> None:
  1056. """Emit a COMMENT ON operation to set the comment for a table.
  1057. :param table_name: string name of the target table.
  1058. :param comment: string value of the comment being registered against
  1059. the specified table.
  1060. :param existing_comment: String value of a comment
  1061. already registered on the specified table, used within autogenerate
  1062. so that the operation is reversible, but not required for direct
  1063. use.
  1064. .. seealso::
  1065. :meth:`.Operations.drop_table_comment`
  1066. :paramref:`.Operations.alter_column.comment`
  1067. """ # noqa: E501
  1068. ...
  1069. def create_unique_constraint(
  1070. self,
  1071. constraint_name: Optional[str],
  1072. table_name: str,
  1073. columns: Sequence[str],
  1074. *,
  1075. schema: Optional[str] = None,
  1076. **kw: Any,
  1077. ) -> Any:
  1078. """Issue a "create unique constraint" instruction using the
  1079. current migration context.
  1080. e.g.::
  1081. from alembic import op
  1082. op.create_unique_constraint("uq_user_name", "user", ["name"])
  1083. This internally generates a :class:`~sqlalchemy.schema.Table` object
  1084. containing the necessary columns, then generates a new
  1085. :class:`~sqlalchemy.schema.UniqueConstraint`
  1086. object which it then associates with the
  1087. :class:`~sqlalchemy.schema.Table`.
  1088. Any event listeners associated with this action will be fired
  1089. off normally. The :class:`~sqlalchemy.schema.AddConstraint`
  1090. construct is ultimately used to generate the ALTER statement.
  1091. :param name: Name of the unique constraint. The name is necessary
  1092. so that an ALTER statement can be emitted. For setups that
  1093. use an automated naming scheme such as that described at
  1094. :ref:`sqla:constraint_naming_conventions`,
  1095. ``name`` here can be ``None``, as the event listener will
  1096. apply the name to the constraint object when it is associated
  1097. with the table.
  1098. :param table_name: String name of the source table.
  1099. :param columns: a list of string column names in the
  1100. source table.
  1101. :param deferrable: optional bool. If set, emit DEFERRABLE or
  1102. NOT DEFERRABLE when issuing DDL for this constraint.
  1103. :param initially: optional string. If set, emit INITIALLY <value>
  1104. when issuing DDL for this constraint.
  1105. :param schema: Optional schema name to operate within. To control
  1106. quoting of the schema outside of the default behavior, use
  1107. the SQLAlchemy construct
  1108. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1109. """ # noqa: E501
  1110. ...
  1111. def drop_column(
  1112. self,
  1113. table_name: str,
  1114. column_name: str,
  1115. *,
  1116. schema: Optional[str] = None,
  1117. **kw: Any,
  1118. ) -> None:
  1119. """Issue a "drop column" instruction using the current
  1120. migration context.
  1121. e.g.::
  1122. drop_column("organization", "account_id")
  1123. :param table_name: name of table
  1124. :param column_name: name of column
  1125. :param schema: Optional schema name to operate within. To control
  1126. quoting of the schema outside of the default behavior, use
  1127. the SQLAlchemy construct
  1128. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1129. :param mssql_drop_check: Optional boolean. When ``True``, on
  1130. Microsoft SQL Server only, first
  1131. drop the CHECK constraint on the column using a
  1132. SQL-script-compatible
  1133. block that selects into a @variable from sys.check_constraints,
  1134. then exec's a separate DROP CONSTRAINT for that constraint.
  1135. :param mssql_drop_default: Optional boolean. When ``True``, on
  1136. Microsoft SQL Server only, first
  1137. drop the DEFAULT constraint on the column using a
  1138. SQL-script-compatible
  1139. block that selects into a @variable from sys.default_constraints,
  1140. then exec's a separate DROP CONSTRAINT for that default.
  1141. :param mssql_drop_foreign_key: Optional boolean. When ``True``, on
  1142. Microsoft SQL Server only, first
  1143. drop a single FOREIGN KEY constraint on the column using a
  1144. SQL-script-compatible
  1145. block that selects into a @variable from
  1146. sys.foreign_keys/sys.foreign_key_columns,
  1147. then exec's a separate DROP CONSTRAINT for that default. Only
  1148. works if the column has exactly one FK constraint which refers to
  1149. it, at the moment.
  1150. """ # noqa: E501
  1151. ...
  1152. def drop_constraint(
  1153. self,
  1154. constraint_name: str,
  1155. table_name: str,
  1156. type_: Optional[str] = None,
  1157. *,
  1158. schema: Optional[str] = None,
  1159. ) -> None:
  1160. r"""Drop a constraint of the given name, typically via DROP CONSTRAINT.
  1161. :param constraint_name: name of the constraint.
  1162. :param table_name: table name.
  1163. :param type\_: optional, required on MySQL. can be
  1164. 'foreignkey', 'primary', 'unique', or 'check'.
  1165. :param schema: Optional schema name to operate within. To control
  1166. quoting of the schema outside of the default behavior, use
  1167. the SQLAlchemy construct
  1168. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1169. """ # noqa: E501
  1170. ...
  1171. def drop_index(
  1172. self,
  1173. index_name: str,
  1174. table_name: Optional[str] = None,
  1175. *,
  1176. schema: Optional[str] = None,
  1177. if_exists: Optional[bool] = None,
  1178. **kw: Any,
  1179. ) -> None:
  1180. r"""Issue a "drop index" instruction using the current
  1181. migration context.
  1182. e.g.::
  1183. drop_index("accounts")
  1184. :param index_name: name of the index.
  1185. :param table_name: name of the owning table. Some
  1186. backends such as Microsoft SQL Server require this.
  1187. :param schema: Optional schema name to operate within. To control
  1188. quoting of the schema outside of the default behavior, use
  1189. the SQLAlchemy construct
  1190. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1191. :param if_exists: If True, adds IF EXISTS operator when
  1192. dropping the index.
  1193. .. versionadded:: 1.12.0
  1194. :param \**kw: Additional keyword arguments not mentioned above are
  1195. dialect specific, and passed in the form
  1196. ``<dialectname>_<argname>``.
  1197. See the documentation regarding an individual dialect at
  1198. :ref:`dialect_toplevel` for detail on documented arguments.
  1199. """ # noqa: E501
  1200. ...
  1201. def drop_table(
  1202. self,
  1203. table_name: str,
  1204. *,
  1205. schema: Optional[str] = None,
  1206. if_exists: Optional[bool] = None,
  1207. **kw: Any,
  1208. ) -> None:
  1209. r"""Issue a "drop table" instruction using the current
  1210. migration context.
  1211. e.g.::
  1212. drop_table("accounts")
  1213. :param table_name: Name of the table
  1214. :param schema: Optional schema name to operate within. To control
  1215. quoting of the schema outside of the default behavior, use
  1216. the SQLAlchemy construct
  1217. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1218. :param if_exists: If True, adds IF EXISTS operator when
  1219. dropping the table.
  1220. .. versionadded:: 1.13.3
  1221. :param \**kw: Other keyword arguments are passed to the underlying
  1222. :class:`sqlalchemy.schema.Table` object created for the command.
  1223. """ # noqa: E501
  1224. ...
  1225. def drop_table_comment(
  1226. self,
  1227. table_name: str,
  1228. *,
  1229. existing_comment: Optional[str] = None,
  1230. schema: Optional[str] = None,
  1231. ) -> None:
  1232. """Issue a "drop table comment" operation to
  1233. remove an existing comment set on a table.
  1234. :param table_name: string name of the target table.
  1235. :param existing_comment: An optional string value of a comment already
  1236. registered on the specified table.
  1237. .. seealso::
  1238. :meth:`.Operations.create_table_comment`
  1239. :paramref:`.Operations.alter_column.comment`
  1240. """ # noqa: E501
  1241. ...
  1242. def execute(
  1243. self,
  1244. sqltext: Union[Executable, str],
  1245. *,
  1246. execution_options: Optional[dict[str, Any]] = None,
  1247. ) -> None:
  1248. r"""Execute the given SQL using the current migration context.
  1249. The given SQL can be a plain string, e.g.::
  1250. op.execute("INSERT INTO table (foo) VALUES ('some value')")
  1251. Or it can be any kind of Core SQL Expression construct, such as
  1252. below where we use an update construct::
  1253. from sqlalchemy.sql import table, column
  1254. from sqlalchemy import String
  1255. from alembic import op
  1256. account = table("account", column("name", String))
  1257. op.execute(
  1258. account.update()
  1259. .where(account.c.name == op.inline_literal("account 1"))
  1260. .values({"name": op.inline_literal("account 2")})
  1261. )
  1262. Above, we made use of the SQLAlchemy
  1263. :func:`sqlalchemy.sql.expression.table` and
  1264. :func:`sqlalchemy.sql.expression.column` constructs to make a brief,
  1265. ad-hoc table construct just for our UPDATE statement. A full
  1266. :class:`~sqlalchemy.schema.Table` construct of course works perfectly
  1267. fine as well, though note it's a recommended practice to at least
  1268. ensure the definition of a table is self-contained within the migration
  1269. script, rather than imported from a module that may break compatibility
  1270. with older migrations.
  1271. In a SQL script context, the statement is emitted directly to the
  1272. output stream. There is *no* return result, however, as this
  1273. function is oriented towards generating a change script
  1274. that can run in "offline" mode. Additionally, parameterized
  1275. statements are discouraged here, as they *will not work* in offline
  1276. mode. Above, we use :meth:`.inline_literal` where parameters are
  1277. to be used.
  1278. For full interaction with a connected database where parameters can
  1279. also be used normally, use the "bind" available from the context::
  1280. from alembic import op
  1281. connection = op.get_bind()
  1282. connection.execute(
  1283. account.update()
  1284. .where(account.c.name == "account 1")
  1285. .values({"name": "account 2"})
  1286. )
  1287. Additionally, when passing the statement as a plain string, it is first
  1288. coerced into a :func:`sqlalchemy.sql.expression.text` construct
  1289. before being passed along. In the less likely case that the
  1290. literal SQL string contains a colon, it must be escaped with a
  1291. backslash, as::
  1292. op.execute(r"INSERT INTO table (foo) VALUES ('\:colon_value')")
  1293. :param sqltext: Any legal SQLAlchemy expression, including:
  1294. * a string
  1295. * a :func:`sqlalchemy.sql.expression.text` construct.
  1296. * a :func:`sqlalchemy.sql.expression.insert` construct.
  1297. * a :func:`sqlalchemy.sql.expression.update` construct.
  1298. * a :func:`sqlalchemy.sql.expression.delete` construct.
  1299. * Any "executable" described in SQLAlchemy Core documentation,
  1300. noting that no result set is returned.
  1301. .. note:: when passing a plain string, the statement is coerced into
  1302. a :func:`sqlalchemy.sql.expression.text` construct. This construct
  1303. considers symbols with colons, e.g. ``:foo`` to be bound parameters.
  1304. To avoid this, ensure that colon symbols are escaped, e.g.
  1305. ``\:foo``.
  1306. :param execution_options: Optional dictionary of
  1307. execution options, will be passed to
  1308. :meth:`sqlalchemy.engine.Connection.execution_options`.
  1309. """ # noqa: E501
  1310. ...
  1311. def rename_table(
  1312. self,
  1313. old_table_name: str,
  1314. new_table_name: str,
  1315. *,
  1316. schema: Optional[str] = None,
  1317. ) -> None:
  1318. """Emit an ALTER TABLE to rename a table.
  1319. :param old_table_name: old name.
  1320. :param new_table_name: new name.
  1321. :param schema: Optional schema name to operate within. To control
  1322. quoting of the schema outside of the default behavior, use
  1323. the SQLAlchemy construct
  1324. :class:`~sqlalchemy.sql.elements.quoted_name`.
  1325. """ # noqa: E501
  1326. ...
  1327. # END STUB FUNCTIONS: op_cls
  1328. class BatchOperations(AbstractOperations):
  1329. """Modifies the interface :class:`.Operations` for batch mode.
  1330. This basically omits the ``table_name`` and ``schema`` parameters
  1331. from associated methods, as these are a given when running under batch
  1332. mode.
  1333. .. seealso::
  1334. :meth:`.Operations.batch_alter_table`
  1335. Note that as of 0.8, most of the methods on this class are produced
  1336. dynamically using the :meth:`.Operations.register_operation`
  1337. method.
  1338. """
  1339. impl: BatchOperationsImpl
  1340. def _noop(self, operation: Any) -> NoReturn:
  1341. raise NotImplementedError(
  1342. "The %s method does not apply to a batch table alter operation."
  1343. % operation
  1344. )
  1345. if TYPE_CHECKING:
  1346. # START STUB FUNCTIONS: batch_op
  1347. # ### the following stubs are generated by tools/write_pyi.py ###
  1348. # ### do not edit ###
  1349. def add_column(
  1350. self,
  1351. column: Column[Any],
  1352. *,
  1353. insert_before: Optional[str] = None,
  1354. insert_after: Optional[str] = None,
  1355. ) -> None:
  1356. """Issue an "add column" instruction using the current
  1357. batch migration context.
  1358. .. seealso::
  1359. :meth:`.Operations.add_column`
  1360. """ # noqa: E501
  1361. ...
  1362. def alter_column(
  1363. self,
  1364. column_name: str,
  1365. *,
  1366. nullable: Optional[bool] = None,
  1367. comment: Union[str, Literal[False], None] = False,
  1368. server_default: Any = False,
  1369. new_column_name: Optional[str] = None,
  1370. type_: Union[TypeEngine[Any], Type[TypeEngine[Any]], None] = None,
  1371. existing_type: Union[
  1372. TypeEngine[Any], Type[TypeEngine[Any]], None
  1373. ] = None,
  1374. existing_server_default: Union[
  1375. str, bool, Identity, Computed, None
  1376. ] = False,
  1377. existing_nullable: Optional[bool] = None,
  1378. existing_comment: Optional[str] = None,
  1379. insert_before: Optional[str] = None,
  1380. insert_after: Optional[str] = None,
  1381. **kw: Any,
  1382. ) -> None:
  1383. """Issue an "alter column" instruction using the current
  1384. batch migration context.
  1385. Parameters are the same as that of :meth:`.Operations.alter_column`,
  1386. as well as the following option(s):
  1387. :param insert_before: String name of an existing column which this
  1388. column should be placed before, when creating the new table.
  1389. :param insert_after: String name of an existing column which this
  1390. column should be placed after, when creating the new table. If
  1391. both :paramref:`.BatchOperations.alter_column.insert_before`
  1392. and :paramref:`.BatchOperations.alter_column.insert_after` are
  1393. omitted, the column is inserted after the last existing column
  1394. in the table.
  1395. .. seealso::
  1396. :meth:`.Operations.alter_column`
  1397. """ # noqa: E501
  1398. ...
  1399. def create_check_constraint(
  1400. self,
  1401. constraint_name: str,
  1402. condition: Union[str, ColumnElement[bool], TextClause],
  1403. **kw: Any,
  1404. ) -> None:
  1405. """Issue a "create check constraint" instruction using the
  1406. current batch migration context.
  1407. The batch form of this call omits the ``source`` and ``schema``
  1408. arguments from the call.
  1409. .. seealso::
  1410. :meth:`.Operations.create_check_constraint`
  1411. """ # noqa: E501
  1412. ...
  1413. def create_exclude_constraint(
  1414. self, constraint_name: str, *elements: Any, **kw: Any
  1415. ) -> Optional[Table]:
  1416. """Issue a "create exclude constraint" instruction using the
  1417. current batch migration context.
  1418. .. note:: This method is Postgresql specific, and additionally
  1419. requires at least SQLAlchemy 1.0.
  1420. .. seealso::
  1421. :meth:`.Operations.create_exclude_constraint`
  1422. """ # noqa: E501
  1423. ...
  1424. def create_foreign_key(
  1425. self,
  1426. constraint_name: Optional[str],
  1427. referent_table: str,
  1428. local_cols: List[str],
  1429. remote_cols: List[str],
  1430. *,
  1431. referent_schema: Optional[str] = None,
  1432. onupdate: Optional[str] = None,
  1433. ondelete: Optional[str] = None,
  1434. deferrable: Optional[bool] = None,
  1435. initially: Optional[str] = None,
  1436. match: Optional[str] = None,
  1437. **dialect_kw: Any,
  1438. ) -> None:
  1439. """Issue a "create foreign key" instruction using the
  1440. current batch migration context.
  1441. The batch form of this call omits the ``source`` and ``source_schema``
  1442. arguments from the call.
  1443. e.g.::
  1444. with batch_alter_table("address") as batch_op:
  1445. batch_op.create_foreign_key(
  1446. "fk_user_address",
  1447. "user",
  1448. ["user_id"],
  1449. ["id"],
  1450. )
  1451. .. seealso::
  1452. :meth:`.Operations.create_foreign_key`
  1453. """ # noqa: E501
  1454. ...
  1455. def create_index(
  1456. self, index_name: str, columns: List[str], **kw: Any
  1457. ) -> None:
  1458. """Issue a "create index" instruction using the
  1459. current batch migration context.
  1460. .. seealso::
  1461. :meth:`.Operations.create_index`
  1462. """ # noqa: E501
  1463. ...
  1464. def create_primary_key(
  1465. self, constraint_name: Optional[str], columns: List[str]
  1466. ) -> None:
  1467. """Issue a "create primary key" instruction using the
  1468. current batch migration context.
  1469. The batch form of this call omits the ``table_name`` and ``schema``
  1470. arguments from the call.
  1471. .. seealso::
  1472. :meth:`.Operations.create_primary_key`
  1473. """ # noqa: E501
  1474. ...
  1475. def create_table_comment(
  1476. self,
  1477. comment: Optional[str],
  1478. *,
  1479. existing_comment: Optional[str] = None,
  1480. ) -> None:
  1481. """Emit a COMMENT ON operation to set the comment for a table
  1482. using the current batch migration context.
  1483. :param comment: string value of the comment being registered against
  1484. the specified table.
  1485. :param existing_comment: String value of a comment
  1486. already registered on the specified table, used within autogenerate
  1487. so that the operation is reversible, but not required for direct
  1488. use.
  1489. """ # noqa: E501
  1490. ...
  1491. def create_unique_constraint(
  1492. self, constraint_name: str, columns: Sequence[str], **kw: Any
  1493. ) -> Any:
  1494. """Issue a "create unique constraint" instruction using the
  1495. current batch migration context.
  1496. The batch form of this call omits the ``source`` and ``schema``
  1497. arguments from the call.
  1498. .. seealso::
  1499. :meth:`.Operations.create_unique_constraint`
  1500. """ # noqa: E501
  1501. ...
  1502. def drop_column(self, column_name: str, **kw: Any) -> None:
  1503. """Issue a "drop column" instruction using the current
  1504. batch migration context.
  1505. .. seealso::
  1506. :meth:`.Operations.drop_column`
  1507. """ # noqa: E501
  1508. ...
  1509. def drop_constraint(
  1510. self, constraint_name: str, type_: Optional[str] = None
  1511. ) -> None:
  1512. """Issue a "drop constraint" instruction using the
  1513. current batch migration context.
  1514. The batch form of this call omits the ``table_name`` and ``schema``
  1515. arguments from the call.
  1516. .. seealso::
  1517. :meth:`.Operations.drop_constraint`
  1518. """ # noqa: E501
  1519. ...
  1520. def drop_index(self, index_name: str, **kw: Any) -> None:
  1521. """Issue a "drop index" instruction using the
  1522. current batch migration context.
  1523. .. seealso::
  1524. :meth:`.Operations.drop_index`
  1525. """ # noqa: E501
  1526. ...
  1527. def drop_table_comment(
  1528. self, *, existing_comment: Optional[str] = None
  1529. ) -> None:
  1530. """Issue a "drop table comment" operation to
  1531. remove an existing comment set on a table using the current
  1532. batch operations context.
  1533. :param existing_comment: An optional string value of a comment already
  1534. registered on the specified table.
  1535. """ # noqa: E501
  1536. ...
  1537. def execute(
  1538. self,
  1539. sqltext: Union[Executable, str],
  1540. *,
  1541. execution_options: Optional[dict[str, Any]] = None,
  1542. ) -> None:
  1543. """Execute the given SQL using the current migration context.
  1544. .. seealso::
  1545. :meth:`.Operations.execute`
  1546. """ # noqa: E501
  1547. ...
  1548. # END STUB FUNCTIONS: batch_op