Digitale bierlijst

env.py 2.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # other values from the config, defined by the needs of env.py,
  13. # can be acquired:
  14. # my_important_option = config.get_main_option("my_important_option")
  15. # ... etc.
  16. from piket_server.flask import CONFIG_DIR, DB_URL, db
  17. # add your model's MetaData object here
  18. # for 'autogenerate' support
  19. # from myapp import mymodel
  20. # target_metadata = mymodel.Base.metadata
  21. target_metadata = db.Model.metadata
  22. os.makedirs(os.path.expanduser(CONFIG_DIR), mode=0o744, exist_ok=True)
  23. config.file_config["alembic"]["sqlalchemy.url"] = DB_URL
  24. def run_migrations_offline() -> None:
  25. """Run migrations in 'offline' mode.
  26. This configures the context with just a URL
  27. and not an Engine, though an Engine is acceptable
  28. here as well. By skipping the Engine creation
  29. we don't even need a DBAPI to be available.
  30. Calls to context.execute() here emit the given string to the
  31. script output.
  32. """
  33. url = config.get_main_option("sqlalchemy.url")
  34. context.configure(url=url, target_metadata=target_metadata, literal_binds=True)
  35. with context.begin_transaction():
  36. context.run_migrations()
  37. def run_migrations_online() -> None:
  38. """Run migrations in 'online' mode.
  39. In this scenario we need to create an Engine
  40. and associate a connection with the context.
  41. """
  42. connectable = engine_from_config(
  43. config.get_section(config.config_ini_section),
  44. prefix="sqlalchemy.",
  45. poolclass=pool.NullPool,
  46. )
  47. with connectable.connect() as connection:
  48. context.configure(connection=connection, target_metadata=target_metadata)
  49. with context.begin_transaction():
  50. context.run_migrations()
  51. if context.is_offline_mode():
  52. run_migrations_offline()
  53. else:
  54. run_migrations_online()