ddl.py 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377
  1. # sql/ddl.py
  2. # Copyright (C) 2009-2024 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: https://www.opensource.org/licenses/mit-license.php
  7. # mypy: allow-untyped-defs, allow-untyped-calls
  8. """
  9. Provides the hierarchy of DDL-defining schema items as well as routines
  10. to invoke them for a create/drop call.
  11. """
  12. from __future__ import annotations
  13. import contextlib
  14. import typing
  15. from typing import Any
  16. from typing import Callable
  17. from typing import Iterable
  18. from typing import List
  19. from typing import Optional
  20. from typing import Sequence as typing_Sequence
  21. from typing import Tuple
  22. from . import roles
  23. from .base import _generative
  24. from .base import Executable
  25. from .base import SchemaVisitor
  26. from .elements import ClauseElement
  27. from .. import exc
  28. from .. import util
  29. from ..util import topological
  30. from ..util.typing import Protocol
  31. from ..util.typing import Self
  32. if typing.TYPE_CHECKING:
  33. from .compiler import Compiled
  34. from .compiler import DDLCompiler
  35. from .elements import BindParameter
  36. from .schema import Constraint
  37. from .schema import ForeignKeyConstraint
  38. from .schema import SchemaItem
  39. from .schema import Sequence
  40. from .schema import Table
  41. from .selectable import TableClause
  42. from ..engine.base import Connection
  43. from ..engine.interfaces import CacheStats
  44. from ..engine.interfaces import CompiledCacheType
  45. from ..engine.interfaces import Dialect
  46. from ..engine.interfaces import SchemaTranslateMapType
  47. class BaseDDLElement(ClauseElement):
  48. """The root of DDL constructs, including those that are sub-elements
  49. within the "create table" and other processes.
  50. .. versionadded:: 2.0
  51. """
  52. _hierarchy_supports_caching = False
  53. """disable cache warnings for all _DDLCompiles subclasses. """
  54. def _compiler(self, dialect, **kw):
  55. """Return a compiler appropriate for this ClauseElement, given a
  56. Dialect."""
  57. return dialect.ddl_compiler(dialect, self, **kw)
  58. def _compile_w_cache(
  59. self,
  60. dialect: Dialect,
  61. *,
  62. compiled_cache: Optional[CompiledCacheType],
  63. column_keys: List[str],
  64. for_executemany: bool = False,
  65. schema_translate_map: Optional[SchemaTranslateMapType] = None,
  66. **kw: Any,
  67. ) -> Tuple[
  68. Compiled, Optional[typing_Sequence[BindParameter[Any]]], CacheStats
  69. ]:
  70. raise NotImplementedError()
  71. class DDLIfCallable(Protocol):
  72. def __call__(
  73. self,
  74. ddl: BaseDDLElement,
  75. target: SchemaItem,
  76. bind: Optional[Connection],
  77. tables: Optional[List[Table]] = None,
  78. state: Optional[Any] = None,
  79. *,
  80. dialect: Dialect,
  81. compiler: Optional[DDLCompiler] = ...,
  82. checkfirst: bool,
  83. ) -> bool:
  84. ...
  85. class DDLIf(typing.NamedTuple):
  86. dialect: Optional[str]
  87. callable_: Optional[DDLIfCallable]
  88. state: Optional[Any]
  89. def _should_execute(
  90. self,
  91. ddl: BaseDDLElement,
  92. target: SchemaItem,
  93. bind: Optional[Connection],
  94. compiler: Optional[DDLCompiler] = None,
  95. **kw: Any,
  96. ) -> bool:
  97. if bind is not None:
  98. dialect = bind.dialect
  99. elif compiler is not None:
  100. dialect = compiler.dialect
  101. else:
  102. assert False, "compiler or dialect is required"
  103. if isinstance(self.dialect, str):
  104. if self.dialect != dialect.name:
  105. return False
  106. elif isinstance(self.dialect, (tuple, list, set)):
  107. if dialect.name not in self.dialect:
  108. return False
  109. if self.callable_ is not None and not self.callable_(
  110. ddl,
  111. target,
  112. bind,
  113. state=self.state,
  114. dialect=dialect,
  115. compiler=compiler,
  116. **kw,
  117. ):
  118. return False
  119. return True
  120. class ExecutableDDLElement(roles.DDLRole, Executable, BaseDDLElement):
  121. """Base class for standalone executable DDL expression constructs.
  122. This class is the base for the general purpose :class:`.DDL` class,
  123. as well as the various create/drop clause constructs such as
  124. :class:`.CreateTable`, :class:`.DropTable`, :class:`.AddConstraint`,
  125. etc.
  126. .. versionchanged:: 2.0 :class:`.ExecutableDDLElement` is renamed from
  127. :class:`.DDLElement`, which still exists for backwards compatibility.
  128. :class:`.ExecutableDDLElement` integrates closely with SQLAlchemy events,
  129. introduced in :ref:`event_toplevel`. An instance of one is
  130. itself an event receiving callable::
  131. event.listen(
  132. users,
  133. 'after_create',
  134. AddConstraint(constraint).execute_if(dialect='postgresql')
  135. )
  136. .. seealso::
  137. :class:`.DDL`
  138. :class:`.DDLEvents`
  139. :ref:`event_toplevel`
  140. :ref:`schema_ddl_sequences`
  141. """
  142. _ddl_if: Optional[DDLIf] = None
  143. target: Optional[SchemaItem] = None
  144. def _execute_on_connection(
  145. self, connection, distilled_params, execution_options
  146. ):
  147. return connection._execute_ddl(
  148. self, distilled_params, execution_options
  149. )
  150. @_generative
  151. def against(self, target: SchemaItem) -> Self:
  152. """Return a copy of this :class:`_schema.ExecutableDDLElement` which
  153. will include the given target.
  154. This essentially applies the given item to the ``.target`` attribute of
  155. the returned :class:`_schema.ExecutableDDLElement` object. This target
  156. is then usable by event handlers and compilation routines in order to
  157. provide services such as tokenization of a DDL string in terms of a
  158. particular :class:`_schema.Table`.
  159. When a :class:`_schema.ExecutableDDLElement` object is established as
  160. an event handler for the :meth:`_events.DDLEvents.before_create` or
  161. :meth:`_events.DDLEvents.after_create` events, and the event then
  162. occurs for a given target such as a :class:`_schema.Constraint` or
  163. :class:`_schema.Table`, that target is established with a copy of the
  164. :class:`_schema.ExecutableDDLElement` object using this method, which
  165. then proceeds to the :meth:`_schema.ExecutableDDLElement.execute`
  166. method in order to invoke the actual DDL instruction.
  167. :param target: a :class:`_schema.SchemaItem` that will be the subject
  168. of a DDL operation.
  169. :return: a copy of this :class:`_schema.ExecutableDDLElement` with the
  170. ``.target`` attribute assigned to the given
  171. :class:`_schema.SchemaItem`.
  172. .. seealso::
  173. :class:`_schema.DDL` - uses tokenization against the "target" when
  174. processing the DDL string.
  175. """
  176. self.target = target
  177. return self
  178. @_generative
  179. def execute_if(
  180. self,
  181. dialect: Optional[str] = None,
  182. callable_: Optional[DDLIfCallable] = None,
  183. state: Optional[Any] = None,
  184. ) -> Self:
  185. r"""Return a callable that will execute this
  186. :class:`_ddl.ExecutableDDLElement` conditionally within an event
  187. handler.
  188. Used to provide a wrapper for event listening::
  189. event.listen(
  190. metadata,
  191. 'before_create',
  192. DDL("my_ddl").execute_if(dialect='postgresql')
  193. )
  194. :param dialect: May be a string or tuple of strings.
  195. If a string, it will be compared to the name of the
  196. executing database dialect::
  197. DDL('something').execute_if(dialect='postgresql')
  198. If a tuple, specifies multiple dialect names::
  199. DDL('something').execute_if(dialect=('postgresql', 'mysql'))
  200. :param callable\_: A callable, which will be invoked with
  201. three positional arguments as well as optional keyword
  202. arguments:
  203. :ddl:
  204. This DDL element.
  205. :target:
  206. The :class:`_schema.Table` or :class:`_schema.MetaData`
  207. object which is the
  208. target of this event. May be None if the DDL is executed
  209. explicitly.
  210. :bind:
  211. The :class:`_engine.Connection` being used for DDL execution.
  212. May be None if this construct is being created inline within
  213. a table, in which case ``compiler`` will be present.
  214. :tables:
  215. Optional keyword argument - a list of Table objects which are to
  216. be created/ dropped within a MetaData.create_all() or drop_all()
  217. method call.
  218. :dialect: keyword argument, but always present - the
  219. :class:`.Dialect` involved in the operation.
  220. :compiler: keyword argument. Will be ``None`` for an engine
  221. level DDL invocation, but will refer to a :class:`.DDLCompiler`
  222. if this DDL element is being created inline within a table.
  223. :state:
  224. Optional keyword argument - will be the ``state`` argument
  225. passed to this function.
  226. :checkfirst:
  227. Keyword argument, will be True if the 'checkfirst' flag was
  228. set during the call to ``create()``, ``create_all()``,
  229. ``drop()``, ``drop_all()``.
  230. If the callable returns a True value, the DDL statement will be
  231. executed.
  232. :param state: any value which will be passed to the callable\_
  233. as the ``state`` keyword argument.
  234. .. seealso::
  235. :meth:`.SchemaItem.ddl_if`
  236. :class:`.DDLEvents`
  237. :ref:`event_toplevel`
  238. """
  239. self._ddl_if = DDLIf(dialect, callable_, state)
  240. return self
  241. def _should_execute(self, target, bind, **kw):
  242. if self._ddl_if is None:
  243. return True
  244. else:
  245. return self._ddl_if._should_execute(self, target, bind, **kw)
  246. def _invoke_with(self, bind):
  247. if self._should_execute(self.target, bind):
  248. return bind.execute(self)
  249. def __call__(self, target, bind, **kw):
  250. """Execute the DDL as a ddl_listener."""
  251. self.against(target)._invoke_with(bind)
  252. def _generate(self):
  253. s = self.__class__.__new__(self.__class__)
  254. s.__dict__ = self.__dict__.copy()
  255. return s
  256. DDLElement = ExecutableDDLElement
  257. """:class:`.DDLElement` is renamed to :class:`.ExecutableDDLElement`."""
  258. class DDL(ExecutableDDLElement):
  259. """A literal DDL statement.
  260. Specifies literal SQL DDL to be executed by the database. DDL objects
  261. function as DDL event listeners, and can be subscribed to those events
  262. listed in :class:`.DDLEvents`, using either :class:`_schema.Table` or
  263. :class:`_schema.MetaData` objects as targets.
  264. Basic templating support allows
  265. a single DDL instance to handle repetitive tasks for multiple tables.
  266. Examples::
  267. from sqlalchemy import event, DDL
  268. tbl = Table('users', metadata, Column('uid', Integer))
  269. event.listen(tbl, 'before_create', DDL('DROP TRIGGER users_trigger'))
  270. spow = DDL('ALTER TABLE %(table)s SET secretpowers TRUE')
  271. event.listen(tbl, 'after_create', spow.execute_if(dialect='somedb'))
  272. drop_spow = DDL('ALTER TABLE users SET secretpowers FALSE')
  273. connection.execute(drop_spow)
  274. When operating on Table events, the following ``statement``
  275. string substitutions are available::
  276. %(table)s - the Table name, with any required quoting applied
  277. %(schema)s - the schema name, with any required quoting applied
  278. %(fullname)s - the Table name including schema, quoted if needed
  279. The DDL's "context", if any, will be combined with the standard
  280. substitutions noted above. Keys present in the context will override
  281. the standard substitutions.
  282. """
  283. __visit_name__ = "ddl"
  284. def __init__(self, statement, context=None):
  285. """Create a DDL statement.
  286. :param statement:
  287. A string or unicode string to be executed. Statements will be
  288. processed with Python's string formatting operator using
  289. a fixed set of string substitutions, as well as additional
  290. substitutions provided by the optional :paramref:`.DDL.context`
  291. parameter.
  292. A literal '%' in a statement must be escaped as '%%'.
  293. SQL bind parameters are not available in DDL statements.
  294. :param context:
  295. Optional dictionary, defaults to None. These values will be
  296. available for use in string substitutions on the DDL statement.
  297. .. seealso::
  298. :class:`.DDLEvents`
  299. :ref:`event_toplevel`
  300. """
  301. if not isinstance(statement, str):
  302. raise exc.ArgumentError(
  303. "Expected a string or unicode SQL statement, got '%r'"
  304. % statement
  305. )
  306. self.statement = statement
  307. self.context = context or {}
  308. def __repr__(self):
  309. parts = [repr(self.statement)]
  310. if self.context:
  311. parts.append(f"context={self.context}")
  312. return "<%s@%s; %s>" % (
  313. type(self).__name__,
  314. id(self),
  315. ", ".join(parts),
  316. )
  317. class _CreateDropBase(ExecutableDDLElement):
  318. """Base class for DDL constructs that represent CREATE and DROP or
  319. equivalents.
  320. The common theme of _CreateDropBase is a single
  321. ``element`` attribute which refers to the element
  322. to be created or dropped.
  323. """
  324. def __init__(
  325. self,
  326. element,
  327. ):
  328. self.element = self.target = element
  329. self._ddl_if = getattr(element, "_ddl_if", None)
  330. @property
  331. def stringify_dialect(self):
  332. return self.element.create_drop_stringify_dialect
  333. def _create_rule_disable(self, compiler):
  334. """Allow disable of _create_rule using a callable.
  335. Pass to _create_rule using
  336. util.portable_instancemethod(self._create_rule_disable)
  337. to retain serializability.
  338. """
  339. return False
  340. class _CreateBase(_CreateDropBase):
  341. def __init__(self, element, if_not_exists=False):
  342. super().__init__(element)
  343. self.if_not_exists = if_not_exists
  344. class _DropBase(_CreateDropBase):
  345. def __init__(self, element, if_exists=False):
  346. super().__init__(element)
  347. self.if_exists = if_exists
  348. class CreateSchema(_CreateBase):
  349. """Represent a CREATE SCHEMA statement.
  350. The argument here is the string name of the schema.
  351. """
  352. __visit_name__ = "create_schema"
  353. stringify_dialect = "default"
  354. def __init__(
  355. self,
  356. name,
  357. if_not_exists=False,
  358. ):
  359. """Create a new :class:`.CreateSchema` construct."""
  360. super().__init__(element=name, if_not_exists=if_not_exists)
  361. class DropSchema(_DropBase):
  362. """Represent a DROP SCHEMA statement.
  363. The argument here is the string name of the schema.
  364. """
  365. __visit_name__ = "drop_schema"
  366. stringify_dialect = "default"
  367. def __init__(
  368. self,
  369. name,
  370. cascade=False,
  371. if_exists=False,
  372. ):
  373. """Create a new :class:`.DropSchema` construct."""
  374. super().__init__(element=name, if_exists=if_exists)
  375. self.cascade = cascade
  376. class CreateTable(_CreateBase):
  377. """Represent a CREATE TABLE statement."""
  378. __visit_name__ = "create_table"
  379. def __init__(
  380. self,
  381. element: Table,
  382. include_foreign_key_constraints: Optional[
  383. typing_Sequence[ForeignKeyConstraint]
  384. ] = None,
  385. if_not_exists: bool = False,
  386. ):
  387. """Create a :class:`.CreateTable` construct.
  388. :param element: a :class:`_schema.Table` that's the subject
  389. of the CREATE
  390. :param on: See the description for 'on' in :class:`.DDL`.
  391. :param include_foreign_key_constraints: optional sequence of
  392. :class:`_schema.ForeignKeyConstraint` objects that will be included
  393. inline within the CREATE construct; if omitted, all foreign key
  394. constraints that do not specify use_alter=True are included.
  395. :param if_not_exists: if True, an IF NOT EXISTS operator will be
  396. applied to the construct.
  397. .. versionadded:: 1.4.0b2
  398. """
  399. super().__init__(element, if_not_exists=if_not_exists)
  400. self.columns = [CreateColumn(column) for column in element.columns]
  401. self.include_foreign_key_constraints = include_foreign_key_constraints
  402. class _DropView(_DropBase):
  403. """Semi-public 'DROP VIEW' construct.
  404. Used by the test suite for dialect-agnostic drops of views.
  405. This object will eventually be part of a public "view" API.
  406. """
  407. __visit_name__ = "drop_view"
  408. class CreateConstraint(BaseDDLElement):
  409. def __init__(self, element: Constraint):
  410. self.element = element
  411. class CreateColumn(BaseDDLElement):
  412. """Represent a :class:`_schema.Column`
  413. as rendered in a CREATE TABLE statement,
  414. via the :class:`.CreateTable` construct.
  415. This is provided to support custom column DDL within the generation
  416. of CREATE TABLE statements, by using the
  417. compiler extension documented in :ref:`sqlalchemy.ext.compiler_toplevel`
  418. to extend :class:`.CreateColumn`.
  419. Typical integration is to examine the incoming :class:`_schema.Column`
  420. object, and to redirect compilation if a particular flag or condition
  421. is found::
  422. from sqlalchemy import schema
  423. from sqlalchemy.ext.compiler import compiles
  424. @compiles(schema.CreateColumn)
  425. def compile(element, compiler, **kw):
  426. column = element.element
  427. if "special" not in column.info:
  428. return compiler.visit_create_column(element, **kw)
  429. text = "%s SPECIAL DIRECTIVE %s" % (
  430. column.name,
  431. compiler.type_compiler.process(column.type)
  432. )
  433. default = compiler.get_column_default_string(column)
  434. if default is not None:
  435. text += " DEFAULT " + default
  436. if not column.nullable:
  437. text += " NOT NULL"
  438. if column.constraints:
  439. text += " ".join(
  440. compiler.process(const)
  441. for const in column.constraints)
  442. return text
  443. The above construct can be applied to a :class:`_schema.Table`
  444. as follows::
  445. from sqlalchemy import Table, Metadata, Column, Integer, String
  446. from sqlalchemy import schema
  447. metadata = MetaData()
  448. table = Table('mytable', MetaData(),
  449. Column('x', Integer, info={"special":True}, primary_key=True),
  450. Column('y', String(50)),
  451. Column('z', String(20), info={"special":True})
  452. )
  453. metadata.create_all(conn)
  454. Above, the directives we've added to the :attr:`_schema.Column.info`
  455. collection
  456. will be detected by our custom compilation scheme::
  457. CREATE TABLE mytable (
  458. x SPECIAL DIRECTIVE INTEGER NOT NULL,
  459. y VARCHAR(50),
  460. z SPECIAL DIRECTIVE VARCHAR(20),
  461. PRIMARY KEY (x)
  462. )
  463. The :class:`.CreateColumn` construct can also be used to skip certain
  464. columns when producing a ``CREATE TABLE``. This is accomplished by
  465. creating a compilation rule that conditionally returns ``None``.
  466. This is essentially how to produce the same effect as using the
  467. ``system=True`` argument on :class:`_schema.Column`, which marks a column
  468. as an implicitly-present "system" column.
  469. For example, suppose we wish to produce a :class:`_schema.Table`
  470. which skips
  471. rendering of the PostgreSQL ``xmin`` column against the PostgreSQL
  472. backend, but on other backends does render it, in anticipation of a
  473. triggered rule. A conditional compilation rule could skip this name only
  474. on PostgreSQL::
  475. from sqlalchemy.schema import CreateColumn
  476. @compiles(CreateColumn, "postgresql")
  477. def skip_xmin(element, compiler, **kw):
  478. if element.element.name == 'xmin':
  479. return None
  480. else:
  481. return compiler.visit_create_column(element, **kw)
  482. my_table = Table('mytable', metadata,
  483. Column('id', Integer, primary_key=True),
  484. Column('xmin', Integer)
  485. )
  486. Above, a :class:`.CreateTable` construct will generate a ``CREATE TABLE``
  487. which only includes the ``id`` column in the string; the ``xmin`` column
  488. will be omitted, but only against the PostgreSQL backend.
  489. """
  490. __visit_name__ = "create_column"
  491. def __init__(self, element):
  492. self.element = element
  493. class DropTable(_DropBase):
  494. """Represent a DROP TABLE statement."""
  495. __visit_name__ = "drop_table"
  496. def __init__(self, element: Table, if_exists: bool = False):
  497. """Create a :class:`.DropTable` construct.
  498. :param element: a :class:`_schema.Table` that's the subject
  499. of the DROP.
  500. :param on: See the description for 'on' in :class:`.DDL`.
  501. :param if_exists: if True, an IF EXISTS operator will be applied to the
  502. construct.
  503. .. versionadded:: 1.4.0b2
  504. """
  505. super().__init__(element, if_exists=if_exists)
  506. class CreateSequence(_CreateBase):
  507. """Represent a CREATE SEQUENCE statement."""
  508. __visit_name__ = "create_sequence"
  509. def __init__(self, element: Sequence, if_not_exists: bool = False):
  510. super().__init__(element, if_not_exists=if_not_exists)
  511. class DropSequence(_DropBase):
  512. """Represent a DROP SEQUENCE statement."""
  513. __visit_name__ = "drop_sequence"
  514. def __init__(self, element: Sequence, if_exists: bool = False):
  515. super().__init__(element, if_exists=if_exists)
  516. class CreateIndex(_CreateBase):
  517. """Represent a CREATE INDEX statement."""
  518. __visit_name__ = "create_index"
  519. def __init__(self, element, if_not_exists=False):
  520. """Create a :class:`.Createindex` construct.
  521. :param element: a :class:`_schema.Index` that's the subject
  522. of the CREATE.
  523. :param if_not_exists: if True, an IF NOT EXISTS operator will be
  524. applied to the construct.
  525. .. versionadded:: 1.4.0b2
  526. """
  527. super().__init__(element, if_not_exists=if_not_exists)
  528. class DropIndex(_DropBase):
  529. """Represent a DROP INDEX statement."""
  530. __visit_name__ = "drop_index"
  531. def __init__(self, element, if_exists=False):
  532. """Create a :class:`.DropIndex` construct.
  533. :param element: a :class:`_schema.Index` that's the subject
  534. of the DROP.
  535. :param if_exists: if True, an IF EXISTS operator will be applied to the
  536. construct.
  537. .. versionadded:: 1.4.0b2
  538. """
  539. super().__init__(element, if_exists=if_exists)
  540. class AddConstraint(_CreateBase):
  541. """Represent an ALTER TABLE ADD CONSTRAINT statement."""
  542. __visit_name__ = "add_constraint"
  543. def __init__(self, element):
  544. super().__init__(element)
  545. element._create_rule = util.portable_instancemethod(
  546. self._create_rule_disable
  547. )
  548. class DropConstraint(_DropBase):
  549. """Represent an ALTER TABLE DROP CONSTRAINT statement."""
  550. __visit_name__ = "drop_constraint"
  551. def __init__(self, element, cascade=False, if_exists=False, **kw):
  552. self.cascade = cascade
  553. super().__init__(element, if_exists=if_exists, **kw)
  554. element._create_rule = util.portable_instancemethod(
  555. self._create_rule_disable
  556. )
  557. class SetTableComment(_CreateDropBase):
  558. """Represent a COMMENT ON TABLE IS statement."""
  559. __visit_name__ = "set_table_comment"
  560. class DropTableComment(_CreateDropBase):
  561. """Represent a COMMENT ON TABLE '' statement.
  562. Note this varies a lot across database backends.
  563. """
  564. __visit_name__ = "drop_table_comment"
  565. class SetColumnComment(_CreateDropBase):
  566. """Represent a COMMENT ON COLUMN IS statement."""
  567. __visit_name__ = "set_column_comment"
  568. class DropColumnComment(_CreateDropBase):
  569. """Represent a COMMENT ON COLUMN IS NULL statement."""
  570. __visit_name__ = "drop_column_comment"
  571. class SetConstraintComment(_CreateDropBase):
  572. """Represent a COMMENT ON CONSTRAINT IS statement."""
  573. __visit_name__ = "set_constraint_comment"
  574. class DropConstraintComment(_CreateDropBase):
  575. """Represent a COMMENT ON CONSTRAINT IS NULL statement."""
  576. __visit_name__ = "drop_constraint_comment"
  577. class InvokeDDLBase(SchemaVisitor):
  578. def __init__(self, connection):
  579. self.connection = connection
  580. @contextlib.contextmanager
  581. def with_ddl_events(self, target, **kw):
  582. """helper context manager that will apply appropriate DDL events
  583. to a CREATE or DROP operation."""
  584. raise NotImplementedError()
  585. class InvokeCreateDDLBase(InvokeDDLBase):
  586. @contextlib.contextmanager
  587. def with_ddl_events(self, target, **kw):
  588. """helper context manager that will apply appropriate DDL events
  589. to a CREATE or DROP operation."""
  590. target.dispatch.before_create(
  591. target, self.connection, _ddl_runner=self, **kw
  592. )
  593. yield
  594. target.dispatch.after_create(
  595. target, self.connection, _ddl_runner=self, **kw
  596. )
  597. class InvokeDropDDLBase(InvokeDDLBase):
  598. @contextlib.contextmanager
  599. def with_ddl_events(self, target, **kw):
  600. """helper context manager that will apply appropriate DDL events
  601. to a CREATE or DROP operation."""
  602. target.dispatch.before_drop(
  603. target, self.connection, _ddl_runner=self, **kw
  604. )
  605. yield
  606. target.dispatch.after_drop(
  607. target, self.connection, _ddl_runner=self, **kw
  608. )
  609. class SchemaGenerator(InvokeCreateDDLBase):
  610. def __init__(
  611. self, dialect, connection, checkfirst=False, tables=None, **kwargs
  612. ):
  613. super().__init__(connection, **kwargs)
  614. self.checkfirst = checkfirst
  615. self.tables = tables
  616. self.preparer = dialect.identifier_preparer
  617. self.dialect = dialect
  618. self.memo = {}
  619. def _can_create_table(self, table):
  620. self.dialect.validate_identifier(table.name)
  621. effective_schema = self.connection.schema_for_object(table)
  622. if effective_schema:
  623. self.dialect.validate_identifier(effective_schema)
  624. return not self.checkfirst or not self.dialect.has_table(
  625. self.connection, table.name, schema=effective_schema
  626. )
  627. def _can_create_index(self, index):
  628. effective_schema = self.connection.schema_for_object(index.table)
  629. if effective_schema:
  630. self.dialect.validate_identifier(effective_schema)
  631. return not self.checkfirst or not self.dialect.has_index(
  632. self.connection,
  633. index.table.name,
  634. index.name,
  635. schema=effective_schema,
  636. )
  637. def _can_create_sequence(self, sequence):
  638. effective_schema = self.connection.schema_for_object(sequence)
  639. return self.dialect.supports_sequences and (
  640. (not self.dialect.sequences_optional or not sequence.optional)
  641. and (
  642. not self.checkfirst
  643. or not self.dialect.has_sequence(
  644. self.connection, sequence.name, schema=effective_schema
  645. )
  646. )
  647. )
  648. def visit_metadata(self, metadata):
  649. if self.tables is not None:
  650. tables = self.tables
  651. else:
  652. tables = list(metadata.tables.values())
  653. collection = sort_tables_and_constraints(
  654. [t for t in tables if self._can_create_table(t)]
  655. )
  656. seq_coll = [
  657. s
  658. for s in metadata._sequences.values()
  659. if s.column is None and self._can_create_sequence(s)
  660. ]
  661. event_collection = [t for (t, fks) in collection if t is not None]
  662. with self.with_ddl_events(
  663. metadata,
  664. tables=event_collection,
  665. checkfirst=self.checkfirst,
  666. ):
  667. for seq in seq_coll:
  668. self.traverse_single(seq, create_ok=True)
  669. for table, fkcs in collection:
  670. if table is not None:
  671. self.traverse_single(
  672. table,
  673. create_ok=True,
  674. include_foreign_key_constraints=fkcs,
  675. _is_metadata_operation=True,
  676. )
  677. else:
  678. for fkc in fkcs:
  679. self.traverse_single(fkc)
  680. def visit_table(
  681. self,
  682. table,
  683. create_ok=False,
  684. include_foreign_key_constraints=None,
  685. _is_metadata_operation=False,
  686. ):
  687. if not create_ok and not self._can_create_table(table):
  688. return
  689. with self.with_ddl_events(
  690. table,
  691. checkfirst=self.checkfirst,
  692. _is_metadata_operation=_is_metadata_operation,
  693. ):
  694. for column in table.columns:
  695. if column.default is not None:
  696. self.traverse_single(column.default)
  697. if not self.dialect.supports_alter:
  698. # e.g., don't omit any foreign key constraints
  699. include_foreign_key_constraints = None
  700. CreateTable(
  701. table,
  702. include_foreign_key_constraints=(
  703. include_foreign_key_constraints
  704. ),
  705. )._invoke_with(self.connection)
  706. if hasattr(table, "indexes"):
  707. for index in table.indexes:
  708. self.traverse_single(index, create_ok=True)
  709. if (
  710. self.dialect.supports_comments
  711. and not self.dialect.inline_comments
  712. ):
  713. if table.comment is not None:
  714. SetTableComment(table)._invoke_with(self.connection)
  715. for column in table.columns:
  716. if column.comment is not None:
  717. SetColumnComment(column)._invoke_with(self.connection)
  718. if self.dialect.supports_constraint_comments:
  719. for constraint in table.constraints:
  720. if constraint.comment is not None:
  721. self.connection.execute(
  722. SetConstraintComment(constraint)
  723. )
  724. def visit_foreign_key_constraint(self, constraint):
  725. if not self.dialect.supports_alter:
  726. return
  727. with self.with_ddl_events(constraint):
  728. AddConstraint(constraint)._invoke_with(self.connection)
  729. def visit_sequence(self, sequence, create_ok=False):
  730. if not create_ok and not self._can_create_sequence(sequence):
  731. return
  732. with self.with_ddl_events(sequence):
  733. CreateSequence(sequence)._invoke_with(self.connection)
  734. def visit_index(self, index, create_ok=False):
  735. if not create_ok and not self._can_create_index(index):
  736. return
  737. with self.with_ddl_events(index):
  738. CreateIndex(index)._invoke_with(self.connection)
  739. class SchemaDropper(InvokeDropDDLBase):
  740. def __init__(
  741. self, dialect, connection, checkfirst=False, tables=None, **kwargs
  742. ):
  743. super().__init__(connection, **kwargs)
  744. self.checkfirst = checkfirst
  745. self.tables = tables
  746. self.preparer = dialect.identifier_preparer
  747. self.dialect = dialect
  748. self.memo = {}
  749. def visit_metadata(self, metadata):
  750. if self.tables is not None:
  751. tables = self.tables
  752. else:
  753. tables = list(metadata.tables.values())
  754. try:
  755. unsorted_tables = [t for t in tables if self._can_drop_table(t)]
  756. collection = list(
  757. reversed(
  758. sort_tables_and_constraints(
  759. unsorted_tables,
  760. filter_fn=lambda constraint: False
  761. if not self.dialect.supports_alter
  762. or constraint.name is None
  763. else None,
  764. )
  765. )
  766. )
  767. except exc.CircularDependencyError as err2:
  768. if not self.dialect.supports_alter:
  769. util.warn(
  770. "Can't sort tables for DROP; an "
  771. "unresolvable foreign key "
  772. "dependency exists between tables: %s; and backend does "
  773. "not support ALTER. To restore at least a partial sort, "
  774. "apply use_alter=True to ForeignKey and "
  775. "ForeignKeyConstraint "
  776. "objects involved in the cycle to mark these as known "
  777. "cycles that will be ignored."
  778. % (", ".join(sorted([t.fullname for t in err2.cycles])))
  779. )
  780. collection = [(t, ()) for t in unsorted_tables]
  781. else:
  782. raise exc.CircularDependencyError(
  783. err2.args[0],
  784. err2.cycles,
  785. err2.edges,
  786. msg="Can't sort tables for DROP; an "
  787. "unresolvable foreign key "
  788. "dependency exists between tables: %s. Please ensure "
  789. "that the ForeignKey and ForeignKeyConstraint objects "
  790. "involved in the cycle have "
  791. "names so that they can be dropped using "
  792. "DROP CONSTRAINT."
  793. % (", ".join(sorted([t.fullname for t in err2.cycles]))),
  794. ) from err2
  795. seq_coll = [
  796. s
  797. for s in metadata._sequences.values()
  798. if self._can_drop_sequence(s)
  799. ]
  800. event_collection = [t for (t, fks) in collection if t is not None]
  801. with self.with_ddl_events(
  802. metadata,
  803. tables=event_collection,
  804. checkfirst=self.checkfirst,
  805. ):
  806. for table, fkcs in collection:
  807. if table is not None:
  808. self.traverse_single(
  809. table,
  810. drop_ok=True,
  811. _is_metadata_operation=True,
  812. _ignore_sequences=seq_coll,
  813. )
  814. else:
  815. for fkc in fkcs:
  816. self.traverse_single(fkc)
  817. for seq in seq_coll:
  818. self.traverse_single(seq, drop_ok=seq.column is None)
  819. def _can_drop_table(self, table):
  820. self.dialect.validate_identifier(table.name)
  821. effective_schema = self.connection.schema_for_object(table)
  822. if effective_schema:
  823. self.dialect.validate_identifier(effective_schema)
  824. return not self.checkfirst or self.dialect.has_table(
  825. self.connection, table.name, schema=effective_schema
  826. )
  827. def _can_drop_index(self, index):
  828. effective_schema = self.connection.schema_for_object(index.table)
  829. if effective_schema:
  830. self.dialect.validate_identifier(effective_schema)
  831. return not self.checkfirst or self.dialect.has_index(
  832. self.connection,
  833. index.table.name,
  834. index.name,
  835. schema=effective_schema,
  836. )
  837. def _can_drop_sequence(self, sequence):
  838. effective_schema = self.connection.schema_for_object(sequence)
  839. return self.dialect.supports_sequences and (
  840. (not self.dialect.sequences_optional or not sequence.optional)
  841. and (
  842. not self.checkfirst
  843. or self.dialect.has_sequence(
  844. self.connection, sequence.name, schema=effective_schema
  845. )
  846. )
  847. )
  848. def visit_index(self, index, drop_ok=False):
  849. if not drop_ok and not self._can_drop_index(index):
  850. return
  851. with self.with_ddl_events(index):
  852. DropIndex(index)(index, self.connection)
  853. def visit_table(
  854. self,
  855. table,
  856. drop_ok=False,
  857. _is_metadata_operation=False,
  858. _ignore_sequences=(),
  859. ):
  860. if not drop_ok and not self._can_drop_table(table):
  861. return
  862. with self.with_ddl_events(
  863. table,
  864. checkfirst=self.checkfirst,
  865. _is_metadata_operation=_is_metadata_operation,
  866. ):
  867. DropTable(table)._invoke_with(self.connection)
  868. # traverse client side defaults which may refer to server-side
  869. # sequences. noting that some of these client side defaults may
  870. # also be set up as server side defaults
  871. # (see https://docs.sqlalchemy.org/en/
  872. # latest/core/defaults.html
  873. # #associating-a-sequence-as-the-server-side-
  874. # default), so have to be dropped after the table is dropped.
  875. for column in table.columns:
  876. if (
  877. column.default is not None
  878. and column.default not in _ignore_sequences
  879. ):
  880. self.traverse_single(column.default)
  881. def visit_foreign_key_constraint(self, constraint):
  882. if not self.dialect.supports_alter:
  883. return
  884. with self.with_ddl_events(constraint):
  885. DropConstraint(constraint)._invoke_with(self.connection)
  886. def visit_sequence(self, sequence, drop_ok=False):
  887. if not drop_ok and not self._can_drop_sequence(sequence):
  888. return
  889. with self.with_ddl_events(sequence):
  890. DropSequence(sequence)._invoke_with(self.connection)
  891. def sort_tables(
  892. tables: Iterable[TableClause],
  893. skip_fn: Optional[Callable[[ForeignKeyConstraint], bool]] = None,
  894. extra_dependencies: Optional[
  895. typing_Sequence[Tuple[TableClause, TableClause]]
  896. ] = None,
  897. ) -> List[Table]:
  898. """Sort a collection of :class:`_schema.Table` objects based on
  899. dependency.
  900. This is a dependency-ordered sort which will emit :class:`_schema.Table`
  901. objects such that they will follow their dependent :class:`_schema.Table`
  902. objects.
  903. Tables are dependent on another based on the presence of
  904. :class:`_schema.ForeignKeyConstraint`
  905. objects as well as explicit dependencies
  906. added by :meth:`_schema.Table.add_is_dependent_on`.
  907. .. warning::
  908. The :func:`._schema.sort_tables` function cannot by itself
  909. accommodate automatic resolution of dependency cycles between
  910. tables, which are usually caused by mutually dependent foreign key
  911. constraints. When these cycles are detected, the foreign keys
  912. of these tables are omitted from consideration in the sort.
  913. A warning is emitted when this condition occurs, which will be an
  914. exception raise in a future release. Tables which are not part
  915. of the cycle will still be returned in dependency order.
  916. To resolve these cycles, the
  917. :paramref:`_schema.ForeignKeyConstraint.use_alter` parameter may be
  918. applied to those constraints which create a cycle. Alternatively,
  919. the :func:`_schema.sort_tables_and_constraints` function will
  920. automatically return foreign key constraints in a separate
  921. collection when cycles are detected so that they may be applied
  922. to a schema separately.
  923. .. versionchanged:: 1.3.17 - a warning is emitted when
  924. :func:`_schema.sort_tables` cannot perform a proper sort due to
  925. cyclical dependencies. This will be an exception in a future
  926. release. Additionally, the sort will continue to return
  927. other tables not involved in the cycle in dependency order
  928. which was not the case previously.
  929. :param tables: a sequence of :class:`_schema.Table` objects.
  930. :param skip_fn: optional callable which will be passed a
  931. :class:`_schema.ForeignKeyConstraint` object; if it returns True, this
  932. constraint will not be considered as a dependency. Note this is
  933. **different** from the same parameter in
  934. :func:`.sort_tables_and_constraints`, which is
  935. instead passed the owning :class:`_schema.ForeignKeyConstraint` object.
  936. :param extra_dependencies: a sequence of 2-tuples of tables which will
  937. also be considered as dependent on each other.
  938. .. seealso::
  939. :func:`.sort_tables_and_constraints`
  940. :attr:`_schema.MetaData.sorted_tables` - uses this function to sort
  941. """
  942. if skip_fn is not None:
  943. fixed_skip_fn = skip_fn
  944. def _skip_fn(fkc):
  945. for fk in fkc.elements:
  946. if fixed_skip_fn(fk):
  947. return True
  948. else:
  949. return None
  950. else:
  951. _skip_fn = None # type: ignore
  952. return [
  953. t
  954. for (t, fkcs) in sort_tables_and_constraints(
  955. tables,
  956. filter_fn=_skip_fn,
  957. extra_dependencies=extra_dependencies,
  958. _warn_for_cycles=True,
  959. )
  960. if t is not None
  961. ]
  962. def sort_tables_and_constraints(
  963. tables, filter_fn=None, extra_dependencies=None, _warn_for_cycles=False
  964. ):
  965. """Sort a collection of :class:`_schema.Table` /
  966. :class:`_schema.ForeignKeyConstraint`
  967. objects.
  968. This is a dependency-ordered sort which will emit tuples of
  969. ``(Table, [ForeignKeyConstraint, ...])`` such that each
  970. :class:`_schema.Table` follows its dependent :class:`_schema.Table`
  971. objects.
  972. Remaining :class:`_schema.ForeignKeyConstraint`
  973. objects that are separate due to
  974. dependency rules not satisfied by the sort are emitted afterwards
  975. as ``(None, [ForeignKeyConstraint ...])``.
  976. Tables are dependent on another based on the presence of
  977. :class:`_schema.ForeignKeyConstraint` objects, explicit dependencies
  978. added by :meth:`_schema.Table.add_is_dependent_on`,
  979. as well as dependencies
  980. stated here using the :paramref:`~.sort_tables_and_constraints.skip_fn`
  981. and/or :paramref:`~.sort_tables_and_constraints.extra_dependencies`
  982. parameters.
  983. :param tables: a sequence of :class:`_schema.Table` objects.
  984. :param filter_fn: optional callable which will be passed a
  985. :class:`_schema.ForeignKeyConstraint` object,
  986. and returns a value based on
  987. whether this constraint should definitely be included or excluded as
  988. an inline constraint, or neither. If it returns False, the constraint
  989. will definitely be included as a dependency that cannot be subject
  990. to ALTER; if True, it will **only** be included as an ALTER result at
  991. the end. Returning None means the constraint is included in the
  992. table-based result unless it is detected as part of a dependency cycle.
  993. :param extra_dependencies: a sequence of 2-tuples of tables which will
  994. also be considered as dependent on each other.
  995. .. seealso::
  996. :func:`.sort_tables`
  997. """
  998. fixed_dependencies = set()
  999. mutable_dependencies = set()
  1000. if extra_dependencies is not None:
  1001. fixed_dependencies.update(extra_dependencies)
  1002. remaining_fkcs = set()
  1003. for table in tables:
  1004. for fkc in table.foreign_key_constraints:
  1005. if fkc.use_alter is True:
  1006. remaining_fkcs.add(fkc)
  1007. continue
  1008. if filter_fn:
  1009. filtered = filter_fn(fkc)
  1010. if filtered is True:
  1011. remaining_fkcs.add(fkc)
  1012. continue
  1013. dependent_on = fkc.referred_table
  1014. if dependent_on is not table:
  1015. mutable_dependencies.add((dependent_on, table))
  1016. fixed_dependencies.update(
  1017. (parent, table) for parent in table._extra_dependencies
  1018. )
  1019. try:
  1020. candidate_sort = list(
  1021. topological.sort(
  1022. fixed_dependencies.union(mutable_dependencies),
  1023. tables,
  1024. )
  1025. )
  1026. except exc.CircularDependencyError as err:
  1027. if _warn_for_cycles:
  1028. util.warn(
  1029. "Cannot correctly sort tables; there are unresolvable cycles "
  1030. 'between tables "%s", which is usually caused by mutually '
  1031. "dependent foreign key constraints. Foreign key constraints "
  1032. "involving these tables will not be considered; this warning "
  1033. "may raise an error in a future release."
  1034. % (", ".join(sorted(t.fullname for t in err.cycles)),)
  1035. )
  1036. for edge in err.edges:
  1037. if edge in mutable_dependencies:
  1038. table = edge[1]
  1039. if table not in err.cycles:
  1040. continue
  1041. can_remove = [
  1042. fkc
  1043. for fkc in table.foreign_key_constraints
  1044. if filter_fn is None or filter_fn(fkc) is not False
  1045. ]
  1046. remaining_fkcs.update(can_remove)
  1047. for fkc in can_remove:
  1048. dependent_on = fkc.referred_table
  1049. if dependent_on is not table:
  1050. mutable_dependencies.discard((dependent_on, table))
  1051. candidate_sort = list(
  1052. topological.sort(
  1053. fixed_dependencies.union(mutable_dependencies),
  1054. tables,
  1055. )
  1056. )
  1057. return [
  1058. (table, table.foreign_key_constraints.difference(remaining_fkcs))
  1059. for table in candidate_sort
  1060. ] + [(None, list(remaining_fkcs))]