ddl.py 44 KB

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