env.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from __future__ import with_statement
  2. import os
  3. from alembic import context
  4. from sqlalchemy import engine_from_config, pool
  5. from logging.config import fileConfig
  6. # this is the Alembic Config object, which provides
  7. # access to the values within the .ini file in use.
  8. config = context.config
  9. # Interpret the config file for Python logging.
  10. # This line sets up loggers basically.
  11. fileConfig(config.config_file_name)
  12. # add your model's MetaData object here
  13. # for 'autogenerate' support
  14. # from myapp import mymodel
  15. # target_metadata = mymodel.Base.metadata
  16. # target_metadata = None
  17. from app.db.base import Base # noqa
  18. target_metadata = Base.metadata
  19. # other values from the config, defined by the needs of env.py,
  20. # can be acquired:
  21. # my_important_option = config.get_main_option("my_important_option")
  22. # ... etc.
  23. '''
  24. def get_url():
  25. user = os.getenv("POSTGRES_USER", "postgres")
  26. password = os.getenv("POSTGRES_PASSWORD", "")
  27. server = os.getenv("POSTGRES_SERVER", "db")
  28. db = os.getenv("POSTGRES_DB", "app")
  29. return f"postgresql://{user}:{password}@{server}/{db}"
  30. '''
  31. def get_url():
  32. user = "choozmo"
  33. password = "pAssw0rd"
  34. host = "db.ptt.cx:3306"
  35. db_name = "video_maker"
  36. return f'mysql://{user}:{password}@{host}/{db_name}'
  37. def run_migrations_offline():
  38. """Run migrations in 'offline' mode.
  39. This configures the context with just a URL
  40. and not an Engine, though an Engine is acceptable
  41. here as well. By skipping the Engine creation
  42. we don't even need a DBAPI to be available.
  43. Calls to context.execute() here emit the given string to the
  44. script output.
  45. """
  46. url = get_url()
  47. context.configure(
  48. url=url, target_metadata=target_metadata, literal_binds=True, compare_type=True
  49. )
  50. with context.begin_transaction():
  51. context.run_migrations()
  52. def run_migrations_online():
  53. """Run migrations in 'online' mode.
  54. In this scenario we need to create an Engine
  55. and associate a connection with the context.
  56. """
  57. configuration = config.get_section(config.config_ini_section)
  58. configuration["sqlalchemy.url"] = get_url()
  59. connectable = engine_from_config(
  60. configuration, prefix="sqlalchemy.", poolclass=pool.NullPool,
  61. )
  62. with connectable.connect() as connection:
  63. context.configure(
  64. connection=connection, target_metadata=target_metadata, compare_type=True
  65. )
  66. with context.begin_transaction():
  67. context.run_migrations()
  68. if context.is_offline_mode():
  69. run_migrations_offline()
  70. else:
  71. run_migrations_online()