1
0

env.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. from logging.config import fileConfig
  2. from alembic import context
  3. from open_webui.models.auths import Auth
  4. from open_webui.env import DATABASE_URL, DATABASE_PASSWORD
  5. from sqlalchemy import engine_from_config, pool, create_engine
  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. if config.config_file_name is not None:
  12. fileConfig(config.config_file_name, disable_existing_loggers=False)
  13. # add your model's MetaData object here
  14. # for 'autogenerate' support
  15. # from myapp import mymodel
  16. # target_metadata = mymodel.Base.metadata
  17. target_metadata = Auth.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. DB_URL = DATABASE_URL
  23. if DB_URL:
  24. config.set_main_option("sqlalchemy.url", DB_URL.replace("%", "%%"))
  25. def run_migrations_offline() -> None:
  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(
  36. url=url,
  37. target_metadata=target_metadata,
  38. literal_binds=True,
  39. dialect_opts={"paramstyle": "named"},
  40. )
  41. with context.begin_transaction():
  42. context.run_migrations()
  43. def run_migrations_online() -> None:
  44. """Run migrations in 'online' mode.
  45. In this scenario we need to create an Engine
  46. and associate a connection with the context.
  47. """
  48. # Handle SQLCipher URLs
  49. if DB_URL and DB_URL.startswith("sqlite+sqlcipher://"):
  50. if not DATABASE_PASSWORD or DATABASE_PASSWORD.strip() == "":
  51. raise ValueError(
  52. "DATABASE_PASSWORD is required when using sqlite+sqlcipher:// URLs"
  53. )
  54. # Extract database path from SQLCipher URL
  55. db_path = DB_URL.replace("sqlite+sqlcipher://", "")
  56. if db_path.startswith("/"):
  57. db_path = db_path[1:] # Remove leading slash for relative paths
  58. # Create a custom creator function that uses sqlcipher3
  59. def create_sqlcipher_connection():
  60. import sqlcipher3
  61. conn = sqlcipher3.connect(db_path, check_same_thread=False)
  62. conn.execute(f"PRAGMA key = '{DATABASE_PASSWORD}'")
  63. return conn
  64. connectable = create_engine(
  65. "sqlite://", # Dummy URL since we're using creator
  66. creator=create_sqlcipher_connection,
  67. echo=False,
  68. )
  69. else:
  70. # Standard database connection (existing logic)
  71. connectable = engine_from_config(
  72. config.get_section(config.config_ini_section, {}),
  73. prefix="sqlalchemy.",
  74. poolclass=pool.NullPool,
  75. )
  76. with connectable.connect() as connection:
  77. context.configure(connection=connection, target_metadata=target_metadata)
  78. with context.begin_transaction():
  79. context.run_migrations()
  80. if context.is_offline_mode():
  81. run_migrations_offline()
  82. else:
  83. run_migrations_online()