config.py 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. from argparse import ArgumentParser
  2. from .util.compat import SafeConfigParser
  3. import inspect
  4. import os
  5. import sys
  6. from . import command
  7. from . import util
  8. from . import package_dir
  9. from .util import compat
  10. class Config(object):
  11. """Represent an Alembic configuration.
  12. Within an ``env.py`` script, this is available
  13. via the :attr:`.EnvironmentContext.config` attribute,
  14. which in turn is available at ``alembic.context``::
  15. from alembic import context
  16. some_param = context.config.get_main_option("my option")
  17. When invoking Alembic programatically, a new
  18. :class:`.Config` can be created by passing
  19. the name of an .ini file to the constructor::
  20. from alembic.config import Config
  21. alembic_cfg = Config("/path/to/yourapp/alembic.ini")
  22. With a :class:`.Config` object, you can then
  23. run Alembic commands programmatically using the directives
  24. in :mod:`alembic.command`.
  25. The :class:`.Config` object can also be constructed without
  26. a filename. Values can be set programmatically, and
  27. new sections will be created as needed::
  28. from alembic.config import Config
  29. alembic_cfg = Config()
  30. alembic_cfg.set_main_option("script_location", "myapp:migrations")
  31. alembic_cfg.set_main_option("url", "postgresql://foo/bar")
  32. alembic_cfg.set_section_option("mysection", "foo", "bar")
  33. .. warning::
  34. When using programmatic configuration, make sure the
  35. ``env.py`` file in use is compatible with the target configuration;
  36. including that the call to Python ``logging.fileConfig()`` is
  37. omitted if the programmatic configuration doesn't actually include
  38. logging directives.
  39. For passing non-string values to environments, such as connections and
  40. engines, use the :attr:`.Config.attributes` dictionary::
  41. with engine.begin() as connection:
  42. alembic_cfg.attributes['connection'] = connection
  43. command.upgrade(alembic_cfg, "head")
  44. :param file_: name of the .ini file to open.
  45. :param ini_section: name of the main Alembic section within the
  46. .ini file
  47. :param output_buffer: optional file-like input buffer which
  48. will be passed to the :class:`.MigrationContext` - used to redirect
  49. the output of "offline generation" when using Alembic programmatically.
  50. :param stdout: buffer where the "print" output of commands will be sent.
  51. Defaults to ``sys.stdout``.
  52. .. versionadded:: 0.4
  53. :param config_args: A dictionary of keys and values that will be used
  54. for substitution in the alembic config file. The dictionary as given
  55. is **copied** to a new one, stored locally as the attribute
  56. ``.config_args``. When the :attr:`.Config.file_config` attribute is
  57. first invoked, the replacement variable ``here`` will be added to this
  58. dictionary before the dictionary is passed to ``SafeConfigParser()``
  59. to parse the .ini file.
  60. .. versionadded:: 0.7.0
  61. :param attributes: optional dictionary of arbitrary Python keys/values,
  62. which will be populated into the :attr:`.Config.attributes` dictionary.
  63. .. versionadded:: 0.7.5
  64. .. seealso::
  65. :ref:`connection_sharing`
  66. """
  67. def __init__(self, file_=None, ini_section='alembic', output_buffer=None,
  68. stdout=sys.stdout, cmd_opts=None,
  69. config_args=util.immutabledict(), attributes=None):
  70. """Construct a new :class:`.Config`
  71. """
  72. self.config_file_name = file_
  73. self.config_ini_section = ini_section
  74. self.output_buffer = output_buffer
  75. self.stdout = stdout
  76. self.cmd_opts = cmd_opts
  77. self.config_args = dict(config_args)
  78. if attributes:
  79. self.attributes.update(attributes)
  80. cmd_opts = None
  81. """The command-line options passed to the ``alembic`` script.
  82. Within an ``env.py`` script this can be accessed via the
  83. :attr:`.EnvironmentContext.config` attribute.
  84. .. versionadded:: 0.6.0
  85. .. seealso::
  86. :meth:`.EnvironmentContext.get_x_argument`
  87. """
  88. config_file_name = None
  89. """Filesystem path to the .ini file in use."""
  90. config_ini_section = None
  91. """Name of the config file section to read basic configuration
  92. from. Defaults to ``alembic``, that is the ``[alembic]`` section
  93. of the .ini file. This value is modified using the ``-n/--name``
  94. option to the Alembic runnier.
  95. """
  96. @util.memoized_property
  97. def attributes(self):
  98. """A Python dictionary for storage of additional state.
  99. This is a utility dictionary which can include not just strings but
  100. engines, connections, schema objects, or anything else.
  101. Use this to pass objects into an env.py script, such as passing
  102. a :class:`sqlalchemy.engine.base.Connection` when calling
  103. commands from :mod:`alembic.command` programmatically.
  104. .. versionadded:: 0.7.5
  105. .. seealso::
  106. :ref:`connection_sharing`
  107. :paramref:`.Config.attributes`
  108. """
  109. return {}
  110. def print_stdout(self, text, *arg):
  111. """Render a message to standard out."""
  112. util.write_outstream(
  113. self.stdout,
  114. (compat.text_type(text) % arg),
  115. "\n"
  116. )
  117. @util.memoized_property
  118. def file_config(self):
  119. """Return the underlying ``ConfigParser`` object.
  120. Direct access to the .ini file is available here,
  121. though the :meth:`.Config.get_section` and
  122. :meth:`.Config.get_main_option`
  123. methods provide a possibly simpler interface.
  124. """
  125. if self.config_file_name:
  126. here = os.path.abspath(os.path.dirname(self.config_file_name))
  127. else:
  128. here = ""
  129. self.config_args['here'] = here
  130. file_config = SafeConfigParser(self.config_args)
  131. if self.config_file_name:
  132. file_config.read([self.config_file_name])
  133. else:
  134. file_config.add_section(self.config_ini_section)
  135. return file_config
  136. def get_template_directory(self):
  137. """Return the directory where Alembic setup templates are found.
  138. This method is used by the alembic ``init`` and ``list_templates``
  139. commands.
  140. """
  141. return os.path.join(package_dir, 'templates')
  142. def get_section(self, name):
  143. """Return all the configuration options from a given .ini file section
  144. as a dictionary.
  145. """
  146. return dict(self.file_config.items(name))
  147. def set_main_option(self, name, value):
  148. """Set an option programmatically within the 'main' section.
  149. This overrides whatever was in the .ini file.
  150. :param name: name of the value
  151. :param value: the value. Note that this value is passed to
  152. ``ConfigParser.set``, which supports variable interpolation using
  153. pyformat (e.g. ``%(some_value)s``). A raw percent sign not part of
  154. an interpolation symbol must therefore be escaped, e.g. ``%%``.
  155. The given value may refer to another value already in the file
  156. using the interpolation format.
  157. """
  158. self.set_section_option(self.config_ini_section, name, value)
  159. def remove_main_option(self, name):
  160. self.file_config.remove_option(self.config_ini_section, name)
  161. def set_section_option(self, section, name, value):
  162. """Set an option programmatically within the given section.
  163. The section is created if it doesn't exist already.
  164. The value here will override whatever was in the .ini
  165. file.
  166. :param section: name of the section
  167. :param name: name of the value
  168. :param value: the value. Note that this value is passed to
  169. ``ConfigParser.set``, which supports variable interpolation using
  170. pyformat (e.g. ``%(some_value)s``). A raw percent sign not part of
  171. an interpolation symbol must therefore be escaped, e.g. ``%%``.
  172. The given value may refer to another value already in the file
  173. using the interpolation format.
  174. """
  175. if not self.file_config.has_section(section):
  176. self.file_config.add_section(section)
  177. self.file_config.set(section, name, value)
  178. def get_section_option(self, section, name, default=None):
  179. """Return an option from the given section of the .ini file.
  180. """
  181. if not self.file_config.has_section(section):
  182. raise util.CommandError("No config file %r found, or file has no "
  183. "'[%s]' section" %
  184. (self.config_file_name, section))
  185. if self.file_config.has_option(section, name):
  186. return self.file_config.get(section, name)
  187. else:
  188. return default
  189. def get_main_option(self, name, default=None):
  190. """Return an option from the 'main' section of the .ini file.
  191. This defaults to being a key from the ``[alembic]``
  192. section, unless the ``-n/--name`` flag were used to
  193. indicate a different section.
  194. """
  195. return self.get_section_option(self.config_ini_section, name, default)
  196. class CommandLine(object):
  197. def __init__(self, prog=None):
  198. self._generate_args(prog)
  199. def _generate_args(self, prog):
  200. def add_options(parser, positional, kwargs):
  201. kwargs_opts = {
  202. 'template': (
  203. "-t", "--template",
  204. dict(
  205. default='generic',
  206. type=str,
  207. help="Setup template for use with 'init'"
  208. )
  209. ),
  210. 'message': (
  211. "-m", "--message",
  212. dict(
  213. type=str,
  214. help="Message string to use with 'revision'")
  215. ),
  216. 'sql': (
  217. "--sql",
  218. dict(
  219. action="store_true",
  220. help="Don't emit SQL to database - dump to "
  221. "standard output/file instead"
  222. )
  223. ),
  224. 'tag': (
  225. "--tag",
  226. dict(
  227. type=str,
  228. help="Arbitrary 'tag' name - can be used by "
  229. "custom env.py scripts.")
  230. ),
  231. 'head': (
  232. "--head",
  233. dict(
  234. type=str,
  235. help="Specify head revision or <branchname>@head "
  236. "to base new revision on."
  237. )
  238. ),
  239. 'splice': (
  240. "--splice",
  241. dict(
  242. action="store_true",
  243. help="Allow a non-head revision as the "
  244. "'head' to splice onto"
  245. )
  246. ),
  247. 'depends_on': (
  248. "--depends-on",
  249. dict(
  250. action="append",
  251. help="Specify one or more revision identifiers "
  252. "which this revision should depend on."
  253. )
  254. ),
  255. 'rev_id': (
  256. "--rev-id",
  257. dict(
  258. type=str,
  259. help="Specify a hardcoded revision id instead of "
  260. "generating one"
  261. )
  262. ),
  263. 'version_path': (
  264. "--version-path",
  265. dict(
  266. type=str,
  267. help="Specify specific path from config for "
  268. "version file"
  269. )
  270. ),
  271. 'branch_label': (
  272. "--branch-label",
  273. dict(
  274. type=str,
  275. help="Specify a branch label to apply to the "
  276. "new revision"
  277. )
  278. ),
  279. 'verbose': (
  280. "-v", "--verbose",
  281. dict(
  282. action="store_true",
  283. help="Use more verbose output"
  284. )
  285. ),
  286. 'resolve_dependencies': (
  287. '--resolve-dependencies',
  288. dict(
  289. action="store_true",
  290. help="Treat dependency versions as down revisions"
  291. )
  292. ),
  293. 'autogenerate': (
  294. "--autogenerate",
  295. dict(
  296. action="store_true",
  297. help="Populate revision script with candidate "
  298. "migration operations, based on comparison "
  299. "of database to model.")
  300. ),
  301. 'head_only': (
  302. "--head-only",
  303. dict(
  304. action="store_true",
  305. help="Deprecated. Use --verbose for "
  306. "additional output")
  307. ),
  308. 'rev_range': (
  309. "-r", "--rev-range",
  310. dict(
  311. action="store",
  312. help="Specify a revision range; "
  313. "format is [start]:[end]")
  314. )
  315. }
  316. positional_help = {
  317. 'directory': "location of scripts directory",
  318. 'revision': "revision identifier",
  319. 'revisions': "one or more revisions, or 'heads' for all heads"
  320. }
  321. for arg in kwargs:
  322. if arg in kwargs_opts:
  323. args = kwargs_opts[arg]
  324. args, kw = args[0:-1], args[-1]
  325. parser.add_argument(*args, **kw)
  326. for arg in positional:
  327. if arg == "revisions":
  328. subparser.add_argument(
  329. arg, nargs='+', help=positional_help.get(arg))
  330. else:
  331. subparser.add_argument(arg, help=positional_help.get(arg))
  332. parser = ArgumentParser(prog=prog)
  333. parser.add_argument("-c", "--config",
  334. type=str,
  335. default="alembic.ini",
  336. help="Alternate config file")
  337. parser.add_argument("-n", "--name",
  338. type=str,
  339. default="alembic",
  340. help="Name of section in .ini file to "
  341. "use for Alembic config")
  342. parser.add_argument("-x", action="append",
  343. help="Additional arguments consumed by "
  344. "custom env.py scripts, e.g. -x "
  345. "setting1=somesetting -x setting2=somesetting")
  346. parser.add_argument("--raiseerr", action="store_true",
  347. help="Raise a full stack trace on error")
  348. subparsers = parser.add_subparsers()
  349. for fn in [getattr(command, n) for n in dir(command)]:
  350. if inspect.isfunction(fn) and \
  351. fn.__name__[0] != '_' and \
  352. fn.__module__ == 'alembic.command':
  353. spec = inspect.getargspec(fn)
  354. if spec[3]:
  355. positional = spec[0][1:-len(spec[3])]
  356. kwarg = spec[0][-len(spec[3]):]
  357. else:
  358. positional = spec[0][1:]
  359. kwarg = []
  360. subparser = subparsers.add_parser(
  361. fn.__name__,
  362. help=fn.__doc__)
  363. add_options(subparser, positional, kwarg)
  364. subparser.set_defaults(cmd=(fn, positional, kwarg))
  365. self.parser = parser
  366. def run_cmd(self, config, options):
  367. fn, positional, kwarg = options.cmd
  368. try:
  369. fn(config,
  370. *[getattr(options, k, None) for k in positional],
  371. **dict((k, getattr(options, k, None)) for k in kwarg)
  372. )
  373. except util.CommandError as e:
  374. if options.raiseerr:
  375. raise
  376. else:
  377. util.err(str(e))
  378. def main(self, argv=None):
  379. options = self.parser.parse_args(argv)
  380. if not hasattr(options, "cmd"):
  381. # see http://bugs.python.org/issue9253, argparse
  382. # behavior changed incompatibly in py3.3
  383. self.parser.error("too few arguments")
  384. else:
  385. cfg = Config(file_=options.config,
  386. ini_section=options.name, cmd_opts=options)
  387. self.run_cmd(cfg, options)
  388. def main(argv=None, prog=None, **kwargs):
  389. """The console runner function for Alembic."""
  390. CommandLine(prog=prog).main(argv=argv)
  391. if __name__ == '__main__':
  392. main()