environment.py 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  1. from ..operations import Operations
  2. from .migration import MigrationContext
  3. from .. import util
  4. class EnvironmentContext(util.ModuleClsProxy):
  5. """A configurational facade made available in an ``env.py`` script.
  6. The :class:`.EnvironmentContext` acts as a *facade* to the more
  7. nuts-and-bolts objects of :class:`.MigrationContext` as well as certain
  8. aspects of :class:`.Config`,
  9. within the context of the ``env.py`` script that is invoked by
  10. most Alembic commands.
  11. :class:`.EnvironmentContext` is normally instantiated
  12. when a command in :mod:`alembic.command` is run. It then makes
  13. itself available in the ``alembic.context`` module for the scope
  14. of the command. From within an ``env.py`` script, the current
  15. :class:`.EnvironmentContext` is available by importing this module.
  16. :class:`.EnvironmentContext` also supports programmatic usage.
  17. At this level, it acts as a Python context manager, that is, is
  18. intended to be used using the
  19. ``with:`` statement. A typical use of :class:`.EnvironmentContext`::
  20. from alembic.config import Config
  21. from alembic.script import ScriptDirectory
  22. config = Config()
  23. config.set_main_option("script_location", "myapp:migrations")
  24. script = ScriptDirectory.from_config(config)
  25. def my_function(rev, context):
  26. '''do something with revision "rev", which
  27. will be the current database revision,
  28. and "context", which is the MigrationContext
  29. that the env.py will create'''
  30. with EnvironmentContext(
  31. config,
  32. script,
  33. fn = my_function,
  34. as_sql = False,
  35. starting_rev = 'base',
  36. destination_rev = 'head',
  37. tag = "sometag"
  38. ):
  39. script.run_env()
  40. The above script will invoke the ``env.py`` script
  41. within the migration environment. If and when ``env.py``
  42. calls :meth:`.MigrationContext.run_migrations`, the
  43. ``my_function()`` function above will be called
  44. by the :class:`.MigrationContext`, given the context
  45. itself as well as the current revision in the database.
  46. .. note::
  47. For most API usages other than full blown
  48. invocation of migration scripts, the :class:`.MigrationContext`
  49. and :class:`.ScriptDirectory` objects can be created and
  50. used directly. The :class:`.EnvironmentContext` object
  51. is *only* needed when you need to actually invoke the
  52. ``env.py`` module present in the migration environment.
  53. """
  54. _migration_context = None
  55. config = None
  56. """An instance of :class:`.Config` representing the
  57. configuration file contents as well as other variables
  58. set programmatically within it."""
  59. script = None
  60. """An instance of :class:`.ScriptDirectory` which provides
  61. programmatic access to version files within the ``versions/``
  62. directory.
  63. """
  64. def __init__(self, config, script, **kw):
  65. """Construct a new :class:`.EnvironmentContext`.
  66. :param config: a :class:`.Config` instance.
  67. :param script: a :class:`.ScriptDirectory` instance.
  68. :param \**kw: keyword options that will be ultimately
  69. passed along to the :class:`.MigrationContext` when
  70. :meth:`.EnvironmentContext.configure` is called.
  71. """
  72. self.config = config
  73. self.script = script
  74. self.context_opts = kw
  75. def __enter__(self):
  76. """Establish a context which provides a
  77. :class:`.EnvironmentContext` object to
  78. env.py scripts.
  79. The :class:`.EnvironmentContext` will
  80. be made available as ``from alembic import context``.
  81. """
  82. self._install_proxy()
  83. return self
  84. def __exit__(self, *arg, **kw):
  85. self._remove_proxy()
  86. def is_offline_mode(self):
  87. """Return True if the current migrations environment
  88. is running in "offline mode".
  89. This is ``True`` or ``False`` depending
  90. on the the ``--sql`` flag passed.
  91. This function does not require that the :class:`.MigrationContext`
  92. has been configured.
  93. """
  94. return self.context_opts.get('as_sql', False)
  95. def is_transactional_ddl(self):
  96. """Return True if the context is configured to expect a
  97. transactional DDL capable backend.
  98. This defaults to the type of database in use, and
  99. can be overridden by the ``transactional_ddl`` argument
  100. to :meth:`.configure`
  101. This function requires that a :class:`.MigrationContext`
  102. has first been made available via :meth:`.configure`.
  103. """
  104. return self.get_context().impl.transactional_ddl
  105. def requires_connection(self):
  106. return not self.is_offline_mode()
  107. def get_head_revision(self):
  108. """Return the hex identifier of the 'head' script revision.
  109. If the script directory has multiple heads, this
  110. method raises a :class:`.CommandError`;
  111. :meth:`.EnvironmentContext.get_head_revisions` should be preferred.
  112. This function does not require that the :class:`.MigrationContext`
  113. has been configured.
  114. .. seealso:: :meth:`.EnvironmentContext.get_head_revisions`
  115. """
  116. return self.script.as_revision_number("head")
  117. def get_head_revisions(self):
  118. """Return the hex identifier of the 'heads' script revision(s).
  119. This returns a tuple containing the version number of all
  120. heads in the script directory.
  121. This function does not require that the :class:`.MigrationContext`
  122. has been configured.
  123. .. versionadded:: 0.7.0
  124. """
  125. return self.script.as_revision_number("heads")
  126. def get_starting_revision_argument(self):
  127. """Return the 'starting revision' argument,
  128. if the revision was passed using ``start:end``.
  129. This is only meaningful in "offline" mode.
  130. Returns ``None`` if no value is available
  131. or was configured.
  132. This function does not require that the :class:`.MigrationContext`
  133. has been configured.
  134. """
  135. if self._migration_context is not None:
  136. return self.script.as_revision_number(
  137. self.get_context()._start_from_rev)
  138. elif 'starting_rev' in self.context_opts:
  139. return self.script.as_revision_number(
  140. self.context_opts['starting_rev'])
  141. else:
  142. # this should raise only in the case that a command
  143. # is being run where the "starting rev" is never applicable;
  144. # this is to catch scripts which rely upon this in
  145. # non-sql mode or similar
  146. raise util.CommandError(
  147. "No starting revision argument is available.")
  148. def get_revision_argument(self):
  149. """Get the 'destination' revision argument.
  150. This is typically the argument passed to the
  151. ``upgrade`` or ``downgrade`` command.
  152. If it was specified as ``head``, the actual
  153. version number is returned; if specified
  154. as ``base``, ``None`` is returned.
  155. This function does not require that the :class:`.MigrationContext`
  156. has been configured.
  157. """
  158. return self.script.as_revision_number(
  159. self.context_opts['destination_rev'])
  160. def get_tag_argument(self):
  161. """Return the value passed for the ``--tag`` argument, if any.
  162. The ``--tag`` argument is not used directly by Alembic,
  163. but is available for custom ``env.py`` configurations that
  164. wish to use it; particularly for offline generation scripts
  165. that wish to generate tagged filenames.
  166. This function does not require that the :class:`.MigrationContext`
  167. has been configured.
  168. .. seealso::
  169. :meth:`.EnvironmentContext.get_x_argument` - a newer and more
  170. open ended system of extending ``env.py`` scripts via the command
  171. line.
  172. """
  173. return self.context_opts.get('tag', None)
  174. def get_x_argument(self, as_dictionary=False):
  175. """Return the value(s) passed for the ``-x`` argument, if any.
  176. The ``-x`` argument is an open ended flag that allows any user-defined
  177. value or values to be passed on the command line, then available
  178. here for consumption by a custom ``env.py`` script.
  179. The return value is a list, returned directly from the ``argparse``
  180. structure. If ``as_dictionary=True`` is passed, the ``x`` arguments
  181. are parsed using ``key=value`` format into a dictionary that is
  182. then returned.
  183. For example, to support passing a database URL on the command line,
  184. the standard ``env.py`` script can be modified like this::
  185. cmd_line_url = context.get_x_argument(
  186. as_dictionary=True).get('dbname')
  187. if cmd_line_url:
  188. engine = create_engine(cmd_line_url)
  189. else:
  190. engine = engine_from_config(
  191. config.get_section(config.config_ini_section),
  192. prefix='sqlalchemy.',
  193. poolclass=pool.NullPool)
  194. This then takes effect by running the ``alembic`` script as::
  195. alembic -x dbname=postgresql://user:pass@host/dbname upgrade head
  196. This function does not require that the :class:`.MigrationContext`
  197. has been configured.
  198. .. versionadded:: 0.6.0
  199. .. seealso::
  200. :meth:`.EnvironmentContext.get_tag_argument`
  201. :attr:`.Config.cmd_opts`
  202. """
  203. if self.config.cmd_opts is not None:
  204. value = self.config.cmd_opts.x or []
  205. else:
  206. value = []
  207. if as_dictionary:
  208. value = dict(
  209. arg.split('=', 1) for arg in value
  210. )
  211. return value
  212. def configure(self,
  213. connection=None,
  214. url=None,
  215. dialect_name=None,
  216. transactional_ddl=None,
  217. transaction_per_migration=False,
  218. output_buffer=None,
  219. starting_rev=None,
  220. tag=None,
  221. template_args=None,
  222. render_as_batch=False,
  223. target_metadata=None,
  224. include_symbol=None,
  225. include_object=None,
  226. include_schemas=False,
  227. process_revision_directives=None,
  228. compare_type=False,
  229. compare_server_default=False,
  230. render_item=None,
  231. literal_binds=False,
  232. upgrade_token="upgrades",
  233. downgrade_token="downgrades",
  234. alembic_module_prefix="op.",
  235. sqlalchemy_module_prefix="sa.",
  236. user_module_prefix=None,
  237. **kw
  238. ):
  239. """Configure a :class:`.MigrationContext` within this
  240. :class:`.EnvironmentContext` which will provide database
  241. connectivity and other configuration to a series of
  242. migration scripts.
  243. Many methods on :class:`.EnvironmentContext` require that
  244. this method has been called in order to function, as they
  245. ultimately need to have database access or at least access
  246. to the dialect in use. Those which do are documented as such.
  247. The important thing needed by :meth:`.configure` is a
  248. means to determine what kind of database dialect is in use.
  249. An actual connection to that database is needed only if
  250. the :class:`.MigrationContext` is to be used in
  251. "online" mode.
  252. If the :meth:`.is_offline_mode` function returns ``True``,
  253. then no connection is needed here. Otherwise, the
  254. ``connection`` parameter should be present as an
  255. instance of :class:`sqlalchemy.engine.Connection`.
  256. This function is typically called from the ``env.py``
  257. script within a migration environment. It can be called
  258. multiple times for an invocation. The most recent
  259. :class:`~sqlalchemy.engine.Connection`
  260. for which it was called is the one that will be operated upon
  261. by the next call to :meth:`.run_migrations`.
  262. General parameters:
  263. :param connection: a :class:`~sqlalchemy.engine.Connection`
  264. to use
  265. for SQL execution in "online" mode. When present, is also
  266. used to determine the type of dialect in use.
  267. :param url: a string database url, or a
  268. :class:`sqlalchemy.engine.url.URL` object.
  269. The type of dialect to be used will be derived from this if
  270. ``connection`` is not passed.
  271. :param dialect_name: string name of a dialect, such as
  272. "postgresql", "mssql", etc.
  273. The type of dialect to be used will be derived from this if
  274. ``connection`` and ``url`` are not passed.
  275. :param transactional_ddl: Force the usage of "transactional"
  276. DDL on or off;
  277. this otherwise defaults to whether or not the dialect in
  278. use supports it.
  279. :param transaction_per_migration: if True, nest each migration script
  280. in a transaction rather than the full series of migrations to
  281. run.
  282. .. versionadded:: 0.6.5
  283. :param output_buffer: a file-like object that will be used
  284. for textual output
  285. when the ``--sql`` option is used to generate SQL scripts.
  286. Defaults to
  287. ``sys.stdout`` if not passed here and also not present on
  288. the :class:`.Config`
  289. object. The value here overrides that of the :class:`.Config`
  290. object.
  291. :param output_encoding: when using ``--sql`` to generate SQL
  292. scripts, apply this encoding to the string output.
  293. :param literal_binds: when using ``--sql`` to generate SQL
  294. scripts, pass through the ``literal_binds`` flag to the compiler
  295. so that any literal values that would ordinarily be bound
  296. parameters are converted to plain strings.
  297. .. warning:: Dialects can typically only handle simple datatypes
  298. like strings and numbers for auto-literal generation. Datatypes
  299. like dates, intervals, and others may still require manual
  300. formatting, typically using :meth:`.Operations.inline_literal`.
  301. .. note:: the ``literal_binds`` flag is ignored on SQLAlchemy
  302. versions prior to 0.8 where this feature is not supported.
  303. .. versionadded:: 0.7.6
  304. .. seealso::
  305. :meth:`.Operations.inline_literal`
  306. :param starting_rev: Override the "starting revision" argument
  307. when using ``--sql`` mode.
  308. :param tag: a string tag for usage by custom ``env.py`` scripts.
  309. Set via the ``--tag`` option, can be overridden here.
  310. :param template_args: dictionary of template arguments which
  311. will be added to the template argument environment when
  312. running the "revision" command. Note that the script environment
  313. is only run within the "revision" command if the --autogenerate
  314. option is used, or if the option "revision_environment=true"
  315. is present in the alembic.ini file.
  316. :param version_table: The name of the Alembic version table.
  317. The default is ``'alembic_version'``.
  318. :param version_table_schema: Optional schema to place version
  319. table within.
  320. :param version_table_pk: boolean, whether the Alembic version table
  321. should use a primary key constraint for the "value" column; this
  322. only takes effect when the table is first created.
  323. Defaults to True; setting to False should not be necessary and is
  324. here for backwards compatibility reasons.
  325. .. versionadded:: 0.8.10 Added the
  326. :paramref:`.EnvironmentContext.configure.version_table_pk`
  327. flag and additionally established that the Alembic version table
  328. has a primary key constraint by default.
  329. Parameters specific to the autogenerate feature, when
  330. ``alembic revision`` is run with the ``--autogenerate`` feature:
  331. :param target_metadata: a :class:`sqlalchemy.schema.MetaData`
  332. object, or a sequence of :class:`~sqlalchemy.schema.MetaData`
  333. objects, that will be consulted during autogeneration.
  334. The tables present in each :class:`~sqlalchemy.schema.MetaData`
  335. will be compared against
  336. what is locally available on the target
  337. :class:`~sqlalchemy.engine.Connection`
  338. to produce candidate upgrade/downgrade operations.
  339. .. versionchanged:: 0.9.0 the
  340. :paramref:`.EnvironmentContext.configure.target_metadata`
  341. parameter may now be passed a sequence of
  342. :class:`~sqlalchemy.schema.MetaData` objects to support
  343. autogeneration of multiple :class:`~sqlalchemy.schema.MetaData`
  344. collections.
  345. :param compare_type: Indicates type comparison behavior during
  346. an autogenerate
  347. operation. Defaults to ``False`` which disables type
  348. comparison. Set to
  349. ``True`` to turn on default type comparison, which has varied
  350. accuracy depending on backend. See :ref:`compare_types`
  351. for an example as well as information on other type
  352. comparison options.
  353. .. seealso::
  354. :ref:`compare_types`
  355. :paramref:`.EnvironmentContext.configure.compare_server_default`
  356. :param compare_server_default: Indicates server default comparison
  357. behavior during
  358. an autogenerate operation. Defaults to ``False`` which disables
  359. server default
  360. comparison. Set to ``True`` to turn on server default comparison,
  361. which has
  362. varied accuracy depending on backend.
  363. To customize server default comparison behavior, a callable may
  364. be specified
  365. which can filter server default comparisons during an
  366. autogenerate operation.
  367. defaults during an autogenerate operation. The format of this
  368. callable is::
  369. def my_compare_server_default(context, inspected_column,
  370. metadata_column, inspected_default, metadata_default,
  371. rendered_metadata_default):
  372. # return True if the defaults are different,
  373. # False if not, or None to allow the default implementation
  374. # to compare these defaults
  375. return None
  376. context.configure(
  377. # ...
  378. compare_server_default = my_compare_server_default
  379. )
  380. ``inspected_column`` is a dictionary structure as returned by
  381. :meth:`sqlalchemy.engine.reflection.Inspector.get_columns`, whereas
  382. ``metadata_column`` is a :class:`sqlalchemy.schema.Column` from
  383. the local model environment.
  384. A return value of ``None`` indicates to allow default server default
  385. comparison
  386. to proceed. Note that some backends such as Postgresql actually
  387. execute
  388. the two defaults on the database side to compare for equivalence.
  389. .. seealso::
  390. :paramref:`.EnvironmentContext.configure.compare_type`
  391. :param include_object: A callable function which is given
  392. the chance to return ``True`` or ``False`` for any object,
  393. indicating if the given object should be considered in the
  394. autogenerate sweep.
  395. The function accepts the following positional arguments:
  396. * ``object``: a :class:`~sqlalchemy.schema.SchemaItem` object such
  397. as a :class:`~sqlalchemy.schema.Table`,
  398. :class:`~sqlalchemy.schema.Column`,
  399. :class:`~sqlalchemy.schema.Index`
  400. :class:`~sqlalchemy.schema.UniqueConstraint`,
  401. or :class:`~sqlalchemy.schema.ForeignKeyConstraint` object
  402. * ``name``: the name of the object. This is typically available
  403. via ``object.name``.
  404. * ``type``: a string describing the type of object; currently
  405. ``"table"``, ``"column"``, ``"index"``, ``"unique_constraint"``,
  406. or ``"foreign_key_constraint"``
  407. .. versionadded:: 0.7.0 Support for indexes and unique constraints
  408. within the
  409. :paramref:`~.EnvironmentContext.configure.include_object` hook.
  410. .. versionadded:: 0.7.1 Support for foreign keys within the
  411. :paramref:`~.EnvironmentContext.configure.include_object` hook.
  412. * ``reflected``: ``True`` if the given object was produced based on
  413. table reflection, ``False`` if it's from a local :class:`.MetaData`
  414. object.
  415. * ``compare_to``: the object being compared against, if available,
  416. else ``None``.
  417. E.g.::
  418. def include_object(object, name, type_, reflected, compare_to):
  419. if (type_ == "column" and
  420. not reflected and
  421. object.info.get("skip_autogenerate", False)):
  422. return False
  423. else:
  424. return True
  425. context.configure(
  426. # ...
  427. include_object = include_object
  428. )
  429. :paramref:`.EnvironmentContext.configure.include_object` can also
  430. be used to filter on specific schemas to include or omit, when
  431. the :paramref:`.EnvironmentContext.configure.include_schemas`
  432. flag is set to ``True``. The :attr:`.Table.schema` attribute
  433. on each :class:`.Table` object reflected will indicate the name of the
  434. schema from which the :class:`.Table` originates.
  435. .. versionadded:: 0.6.0
  436. .. seealso::
  437. :paramref:`.EnvironmentContext.configure.include_schemas`
  438. :param include_symbol: A callable function which, given a table name
  439. and schema name (may be ``None``), returns ``True`` or ``False``,
  440. indicating if the given table should be considered in the
  441. autogenerate sweep.
  442. .. deprecated:: 0.6.0
  443. :paramref:`.EnvironmentContext.configure.include_symbol`
  444. is superceded by the more generic
  445. :paramref:`.EnvironmentContext.configure.include_object`
  446. parameter.
  447. E.g.::
  448. def include_symbol(tablename, schema):
  449. return tablename not in ("skip_table_one", "skip_table_two")
  450. context.configure(
  451. # ...
  452. include_symbol = include_symbol
  453. )
  454. .. seealso::
  455. :paramref:`.EnvironmentContext.configure.include_schemas`
  456. :paramref:`.EnvironmentContext.configure.include_object`
  457. :param render_as_batch: if True, commands which alter elements
  458. within a table will be placed under a ``with batch_alter_table():``
  459. directive, so that batch migrations will take place.
  460. .. versionadded:: 0.7.0
  461. .. seealso::
  462. :ref:`batch_migrations`
  463. :param include_schemas: If True, autogenerate will scan across
  464. all schemas located by the SQLAlchemy
  465. :meth:`~sqlalchemy.engine.reflection.Inspector.get_schema_names`
  466. method, and include all differences in tables found across all
  467. those schemas. When using this option, you may want to also
  468. use the :paramref:`.EnvironmentContext.configure.include_object`
  469. option to specify a callable which
  470. can filter the tables/schemas that get included.
  471. .. seealso::
  472. :paramref:`.EnvironmentContext.configure.include_object`
  473. :param render_item: Callable that can be used to override how
  474. any schema item, i.e. column, constraint, type,
  475. etc., is rendered for autogenerate. The callable receives a
  476. string describing the type of object, the object, and
  477. the autogen context. If it returns False, the
  478. default rendering method will be used. If it returns None,
  479. the item will not be rendered in the context of a Table
  480. construct, that is, can be used to skip columns or constraints
  481. within op.create_table()::
  482. def my_render_column(type_, col, autogen_context):
  483. if type_ == "column" and isinstance(col, MySpecialCol):
  484. return repr(col)
  485. else:
  486. return False
  487. context.configure(
  488. # ...
  489. render_item = my_render_column
  490. )
  491. Available values for the type string include: ``"column"``,
  492. ``"primary_key"``, ``"foreign_key"``, ``"unique"``, ``"check"``,
  493. ``"type"``, ``"server_default"``.
  494. .. seealso::
  495. :ref:`autogen_render_types`
  496. :param upgrade_token: When autogenerate completes, the text of the
  497. candidate upgrade operations will be present in this template
  498. variable when ``script.py.mako`` is rendered. Defaults to
  499. ``upgrades``.
  500. :param downgrade_token: When autogenerate completes, the text of the
  501. candidate downgrade operations will be present in this
  502. template variable when ``script.py.mako`` is rendered. Defaults to
  503. ``downgrades``.
  504. :param alembic_module_prefix: When autogenerate refers to Alembic
  505. :mod:`alembic.operations` constructs, this prefix will be used
  506. (i.e. ``op.create_table``) Defaults to "``op.``".
  507. Can be ``None`` to indicate no prefix.
  508. :param sqlalchemy_module_prefix: When autogenerate refers to
  509. SQLAlchemy
  510. :class:`~sqlalchemy.schema.Column` or type classes, this prefix
  511. will be used
  512. (i.e. ``sa.Column("somename", sa.Integer)``) Defaults to "``sa.``".
  513. Can be ``None`` to indicate no prefix.
  514. Note that when dialect-specific types are rendered, autogenerate
  515. will render them using the dialect module name, i.e. ``mssql.BIT()``,
  516. ``postgresql.UUID()``.
  517. :param user_module_prefix: When autogenerate refers to a SQLAlchemy
  518. type (e.g. :class:`.TypeEngine`) where the module name is not
  519. under the ``sqlalchemy`` namespace, this prefix will be used
  520. within autogenerate. If left at its default of
  521. ``None``, the ``__module__`` attribute of the type is used to
  522. render the import module. It's a good practice to set this
  523. and to have all custom types be available from a fixed module space,
  524. in order to future-proof migration files against reorganizations
  525. in modules.
  526. .. versionchanged:: 0.7.0
  527. :paramref:`.EnvironmentContext.configure.user_module_prefix`
  528. no longer defaults to the value of
  529. :paramref:`.EnvironmentContext.configure.sqlalchemy_module_prefix`
  530. when left at ``None``; the ``__module__`` attribute is now used.
  531. .. versionadded:: 0.6.3 added
  532. :paramref:`.EnvironmentContext.configure.user_module_prefix`
  533. .. seealso::
  534. :ref:`autogen_module_prefix`
  535. :param process_revision_directives: a callable function that will
  536. be passed a structure representing the end result of an autogenerate
  537. or plain "revision" operation, which can be manipulated to affect
  538. how the ``alembic revision`` command ultimately outputs new
  539. revision scripts. The structure of the callable is::
  540. def process_revision_directives(context, revision, directives):
  541. pass
  542. The ``directives`` parameter is a Python list containing
  543. a single :class:`.MigrationScript` directive, which represents
  544. the revision file to be generated. This list as well as its
  545. contents may be freely modified to produce any set of commands.
  546. The section :ref:`customizing_revision` shows an example of
  547. doing this. The ``context`` parameter is the
  548. :class:`.MigrationContext` in use,
  549. and ``revision`` is a tuple of revision identifiers representing the
  550. current revision of the database.
  551. The callable is invoked at all times when the ``--autogenerate``
  552. option is passed to ``alembic revision``. If ``--autogenerate``
  553. is not passed, the callable is invoked only if the
  554. ``revision_environment`` variable is set to True in the Alembic
  555. configuration, in which case the given ``directives`` collection
  556. will contain empty :class:`.UpgradeOps` and :class:`.DowngradeOps`
  557. collections for ``.upgrade_ops`` and ``.downgrade_ops``. The
  558. ``--autogenerate`` option itself can be inferred by inspecting
  559. ``context.config.cmd_opts.autogenerate``.
  560. The callable function may optionally be an instance of
  561. a :class:`.Rewriter` object. This is a helper object that
  562. assists in the production of autogenerate-stream rewriter functions.
  563. .. versionadded:: 0.8.0
  564. .. versionchanged:: 0.8.1 - The
  565. :paramref:`.EnvironmentContext.configure.process_revision_directives`
  566. hook can append op directives into :class:`.UpgradeOps` and
  567. :class:`.DowngradeOps` which will be rendered in Python regardless
  568. of whether the ``--autogenerate`` option is in use or not;
  569. the ``revision_environment`` configuration variable should be
  570. set to "true" in the config to enable this.
  571. .. seealso::
  572. :ref:`customizing_revision`
  573. :ref:`autogen_rewriter`
  574. :paramref:`.command.revision.process_revision_directives`
  575. Parameters specific to individual backends:
  576. :param mssql_batch_separator: The "batch separator" which will
  577. be placed between each statement when generating offline SQL Server
  578. migrations. Defaults to ``GO``. Note this is in addition to the
  579. customary semicolon ``;`` at the end of each statement; SQL Server
  580. considers the "batch separator" to denote the end of an
  581. individual statement execution, and cannot group certain
  582. dependent operations in one step.
  583. :param oracle_batch_separator: The "batch separator" which will
  584. be placed between each statement when generating offline
  585. Oracle migrations. Defaults to ``/``. Oracle doesn't add a
  586. semicolon between statements like most other backends.
  587. """
  588. opts = self.context_opts
  589. if transactional_ddl is not None:
  590. opts["transactional_ddl"] = transactional_ddl
  591. if output_buffer is not None:
  592. opts["output_buffer"] = output_buffer
  593. elif self.config.output_buffer is not None:
  594. opts["output_buffer"] = self.config.output_buffer
  595. if starting_rev:
  596. opts['starting_rev'] = starting_rev
  597. if tag:
  598. opts['tag'] = tag
  599. if template_args and 'template_args' in opts:
  600. opts['template_args'].update(template_args)
  601. opts["transaction_per_migration"] = transaction_per_migration
  602. opts['target_metadata'] = target_metadata
  603. opts['include_symbol'] = include_symbol
  604. opts['include_object'] = include_object
  605. opts['include_schemas'] = include_schemas
  606. opts['render_as_batch'] = render_as_batch
  607. opts['upgrade_token'] = upgrade_token
  608. opts['downgrade_token'] = downgrade_token
  609. opts['sqlalchemy_module_prefix'] = sqlalchemy_module_prefix
  610. opts['alembic_module_prefix'] = alembic_module_prefix
  611. opts['user_module_prefix'] = user_module_prefix
  612. opts['literal_binds'] = literal_binds
  613. opts['process_revision_directives'] = process_revision_directives
  614. if render_item is not None:
  615. opts['render_item'] = render_item
  616. if compare_type is not None:
  617. opts['compare_type'] = compare_type
  618. if compare_server_default is not None:
  619. opts['compare_server_default'] = compare_server_default
  620. opts['script'] = self.script
  621. opts.update(kw)
  622. self._migration_context = MigrationContext.configure(
  623. connection=connection,
  624. url=url,
  625. dialect_name=dialect_name,
  626. environment_context=self,
  627. opts=opts
  628. )
  629. def run_migrations(self, **kw):
  630. """Run migrations as determined by the current command line
  631. configuration
  632. as well as versioning information present (or not) in the current
  633. database connection (if one is present).
  634. The function accepts optional ``**kw`` arguments. If these are
  635. passed, they are sent directly to the ``upgrade()`` and
  636. ``downgrade()``
  637. functions within each target revision file. By modifying the
  638. ``script.py.mako`` file so that the ``upgrade()`` and ``downgrade()``
  639. functions accept arguments, parameters can be passed here so that
  640. contextual information, usually information to identify a particular
  641. database in use, can be passed from a custom ``env.py`` script
  642. to the migration functions.
  643. This function requires that a :class:`.MigrationContext` has
  644. first been made available via :meth:`.configure`.
  645. """
  646. with Operations.context(self._migration_context):
  647. self.get_context().run_migrations(**kw)
  648. def execute(self, sql, execution_options=None):
  649. """Execute the given SQL using the current change context.
  650. The behavior of :meth:`.execute` is the same
  651. as that of :meth:`.Operations.execute`. Please see that
  652. function's documentation for full detail including
  653. caveats and limitations.
  654. This function requires that a :class:`.MigrationContext` has
  655. first been made available via :meth:`.configure`.
  656. """
  657. self.get_context().execute(sql,
  658. execution_options=execution_options)
  659. def static_output(self, text):
  660. """Emit text directly to the "offline" SQL stream.
  661. Typically this is for emitting comments that
  662. start with --. The statement is not treated
  663. as a SQL execution, no ; or batch separator
  664. is added, etc.
  665. """
  666. self.get_context().impl.static_output(text)
  667. def begin_transaction(self):
  668. """Return a context manager that will
  669. enclose an operation within a "transaction",
  670. as defined by the environment's offline
  671. and transactional DDL settings.
  672. e.g.::
  673. with context.begin_transaction():
  674. context.run_migrations()
  675. :meth:`.begin_transaction` is intended to
  676. "do the right thing" regardless of
  677. calling context:
  678. * If :meth:`.is_transactional_ddl` is ``False``,
  679. returns a "do nothing" context manager
  680. which otherwise produces no transactional
  681. state or directives.
  682. * If :meth:`.is_offline_mode` is ``True``,
  683. returns a context manager that will
  684. invoke the :meth:`.DefaultImpl.emit_begin`
  685. and :meth:`.DefaultImpl.emit_commit`
  686. methods, which will produce the string
  687. directives ``BEGIN`` and ``COMMIT`` on
  688. the output stream, as rendered by the
  689. target backend (e.g. SQL Server would
  690. emit ``BEGIN TRANSACTION``).
  691. * Otherwise, calls :meth:`sqlalchemy.engine.Connection.begin`
  692. on the current online connection, which
  693. returns a :class:`sqlalchemy.engine.Transaction`
  694. object. This object demarcates a real
  695. transaction and is itself a context manager,
  696. which will roll back if an exception
  697. is raised.
  698. Note that a custom ``env.py`` script which
  699. has more specific transactional needs can of course
  700. manipulate the :class:`~sqlalchemy.engine.Connection`
  701. directly to produce transactional state in "online"
  702. mode.
  703. """
  704. return self.get_context().begin_transaction()
  705. def get_context(self):
  706. """Return the current :class:`.MigrationContext` object.
  707. If :meth:`.EnvironmentContext.configure` has not been
  708. called yet, raises an exception.
  709. """
  710. if self._migration_context is None:
  711. raise Exception("No context has been configured yet.")
  712. return self._migration_context
  713. def get_bind(self):
  714. """Return the current 'bind'.
  715. In "online" mode, this is the
  716. :class:`sqlalchemy.engine.Connection` currently being used
  717. to emit SQL to the database.
  718. This function requires that a :class:`.MigrationContext`
  719. has first been made available via :meth:`.configure`.
  720. """
  721. return self.get_context().bind
  722. def get_impl(self):
  723. return self.get_context().impl