gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
3.0KB

  1. ##########################################################################
  2. #
  3. # pgAdmin 4 - PostgreSQL Tools
  4. #
  5. # Copyright (C) 2013 - 2020, The pgAdmin Development Team
  6. # This software is released under the PostgreSQL Licence
  7. #
  8. ##########################################################################
  9. from __future__ import with_statement
  10. from alembic import context
  11. from sqlalchemy import engine_from_config, pool
  12. import logging
  13. # this is the Alembic Config object, which provides
  14. # access to the values within the .ini file in use.
  15. config = context.config
  16. # Interpret the config file for Python logging.
  17. # This line sets up loggers basically.
  18. logger = logging.getLogger('alembic.env')
  19. # add your model's MetaData object here
  20. # for 'autogenerate' support
  21. # from myapp import mymodel
  22. # target_metadata = mymodel.Base.metadata
  23. from flask import current_app
  24. config.set_main_option('sqlalchemy.url',
  25. current_app.config.get('SQLALCHEMY_DATABASE_URI'))
  26. target_metadata = current_app.extensions['migrate'].db.metadata
  27. # other values from the config, defined by the needs of env.py,
  28. # can be acquired:
  29. # my_important_option = config.get_main_option("my_important_option")
  30. # ... etc.
  31. def run_migrations_offline():
  32. """Run migrations in 'offline' mode.
  33. This configures the context with just a URL
  34. and not an Engine, though an Engine is acceptable
  35. here as well. By skipping the Engine creation
  36. we don't even need a DBAPI to be available.
  37. Calls to context.execute() here emit the given string to the
  38. script output.
  39. """
  40. url = config.get_main_option("sqlalchemy.url")
  41. context.configure(url=url)
  42. with context.begin_transaction():
  43. context.run_migrations()
  44. def run_migrations_online():
  45. """Run migrations in 'online' mode.
  46. In this scenario we need to create an Engine
  47. and associate a connection with the context.
  48. """
  49. # this callback is used to prevent an auto-migration from being generated
  50. # when there are no changes to the schema
  51. # reference: http://alembic.readthedocs.org/en/latest/cookbook.html
  52. def process_revision_directives(context, revision, directives):
  53. if getattr(config.cmd_opts, 'autogenerate', False):
  54. script = directives[0]
  55. if script.upgrade_ops.is_empty():
  56. directives[:] = []
  57. logger.info('No changes in schema detected.')
  58. engine = engine_from_config(config.get_section(config.config_ini_section),
  59. prefix='sqlalchemy.',
  60. poolclass=pool.NullPool)
  61. connection = engine.connect()
  62. context.configure(connection=connection,
  63. target_metadata=target_metadata,
  64. process_revision_directives=process_revision_directives,
  65. **current_app.extensions['migrate'].configure_args)
  66. try:
  67. with context.begin_transaction():
  68. context.run_migrations()
  69. finally:
  70. connection.close()
  71. if context.is_offline_mode():
  72. run_migrations_offline()
  73. else:
  74. run_migrations_online()
上海开阖软件有限公司 沪ICP备12045867号-1