PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/api/model/alembic/env.py

https://gitlab.com/fdemian/Shelob
Python | 86 lines | 63 code | 10 blank | 13 comment | 0 complexity | c4e41d7f1cc48603cca1f01096c57020 MD5 | raw file
  1. from __future__ import with_statement
  2. from alembic import context
  3. from sqlalchemy import engine_from_config, pool
  4. from logging.config import fileConfig
  5. from tornado.options import define, options, parse_config_file
  6. def get_database_url():
  7. config_file_path = "../../config.ini"
  8. define('database_user', type=str, group='application', help='Database name.')
  9. define('database_name', type=str, group='application', help='Database name.')
  10. define('database_port', type=str, group='application', help='Database port.')
  11. define('database_password', type=str, group='application', help='Database password.')
  12. parse_config_file(config_file_path)
  13. name = options.database_name
  14. user = options.database_user
  15. port = options.database_port
  16. password = options.database_password
  17. return 'postgresql+psycopg2://' + user + ":" + password + "@localhost:" + port + "/" + name
  18. # this is the Alembic Config object, which provides
  19. # access to the values within the .ini file in use.
  20. config = context.config
  21. # Interpret the config file for Python logging.
  22. # This line sets up loggers basically.
  23. fileConfig(config.config_file_name)
  24. # add your model's MetaData object here
  25. # for 'autogenerate' support
  26. # from myapp import mymodel
  27. # target_metadata = mymodel.Base.metadata
  28. target_metadata = None
  29. # other values from the config, defined by the needs of env.py,
  30. # can be acquired:
  31. # my_important_option = config.get_main_option("my_important_option")
  32. # ... etc.
  33. def run_migrations_offline():
  34. """Run migrations in 'offline' mode.
  35. This configures the context with just a URL
  36. and not an Engine, though an Engine is acceptable
  37. here as well. By skipping the Engine creation
  38. we don't even need a DBAPI to be available.
  39. Calls to context.execute() here emit the given string to the
  40. script output.
  41. """
  42. #url = config.get_main_option("sqlalchemy.url")
  43. url = get_database_url()
  44. context.configure(url=url, target_metadata=target_metadata, literal_binds=True)
  45. print("::::::")
  46. with context.begin_transaction():
  47. context.run_migrations()
  48. def run_migrations_online():
  49. """Run migrations in 'online' mode.
  50. In this scenario we need to create an Engine
  51. and associate a connection with the context.
  52. """
  53. config_section = config.get_section(config.config_ini_section)
  54. config_section["sqlalchemy.url"] = get_database_url()
  55. connectable = engine_from_config(
  56. config_section,
  57. prefix='sqlalchemy.',
  58. poolclass=pool.NullPool)
  59. with connectable.connect() as connection:
  60. context.configure(connection=connection, target_metadata=target_metadata)
  61. with context.begin_transaction():
  62. context.run_migrations()
  63. if context.is_offline_mode():
  64. run_migrations_offline()
  65. else:
  66. run_migrations_online()