Digitale bierlijst

env.py 2.1KB

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