[🐛 Bug]: DB migrations fails when upgrade to 0.27.1
Describe the bug Start of the keep-backend (when backend version upgrades to 0.27.1) fails with:
keep-backend-1 | {"asctime": "2024-10-23 09:38:00,337", "message": "Running migrations...", "levelname": "INFO", "name": "keep.api.core.db_on_start", "filename": "db_on_start.py", "otelTraceID": null, "otelSpanID": null, "otelServiceName": null, "threadName": "MainThread", "process": 1, "module": "db_on_start"}
keep-backend-1 | INFO [alembic.runtime.migration] Context impl SQLiteImpl.
keep-backend-1 | INFO [alembic.runtime.migration] Will assume non-transactional DDL.
keep-backend-1 | INFO [alembic.runtime.migration] Running upgrade 83c1020be97d -> 8438f041ee0e, add pulling_enabled
keep-backend-1 | Traceback (most recent call last):
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
keep-backend-1 | self.dialect.do_execute(
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
keep-backend-1 | cursor.execute(statement, parameters)
keep-backend-1 | sqlite3.OperationalError: Cannot add a NOT NULL column with default value NULL
keep-backend-1 |
keep-backend-1 | The above exception was the direct cause of the following exception:
keep-backend-1 |
keep-backend-1 | Traceback (most recent call last):
keep-backend-1 | File "/venv/bin/gunicorn", line 8, in <module>
keep-backend-1 | sys.exit(run())
keep-backend-1 | ^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/gunicorn/app/wsgiapp.py", line 67, in run
keep-backend-1 | WSGIApplication("%(prog)s [OPTIONS] [APP_MODULE]", prog=prog).run()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/gunicorn/app/base.py", line 236, in run
keep-backend-1 | super().run()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/gunicorn/app/base.py", line 72, in run
keep-backend-1 | Arbiter(self).run()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/gunicorn/arbiter.py", line 198, in run
keep-backend-1 | self.start()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/gunicorn/arbiter.py", line 138, in start
keep-backend-1 | self.cfg.on_starting(self)
keep-backend-1 | File "/venv/lib/python3.11/site-packages/keep/api/config.py", line 20, in on_starting
keep-backend-1 | migrate_db()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/keep/api/core/db_on_start.py", line 183, in migrate_db
keep-backend-1 | alembic.command.upgrade(config, "head")
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/command.py", line 406, in upgrade
keep-backend-1 | script.run_env()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/script/base.py", line 582, in run_env
keep-backend-1 | util.load_python_file(self.dir, "env.py")
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 95, in load_python_file
keep-backend-1 | module = load_module_py(module_id, path)
keep-backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/util/pyfiles.py", line 113, in load_module_py
keep-backend-1 | spec.loader.exec_module(module) # type: ignore
keep-backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
keep-backend-1 | File "<frozen importlib._bootstrap_external>", line 940, in exec_module
keep-backend-1 | File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
keep-backend-1 | File "/venv/lib/python3.11/site-packages/keep/api/core/../models/db/migrations/env.py", line 96, in <module>
keep-backend-1 | loop.run_until_complete(task)
keep-backend-1 | File "/usr/local/lib/python3.11/asyncio/base_events.py", line 653, in run_until_complete
keep-backend-1 | return future.result()
keep-backend-1 | ^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/keep/api/core/../models/db/migrations/env.py", line 87, in run_migrations_online
keep-backend-1 | do_run_migrations(connectable.connect())
keep-backend-1 | File "/venv/lib/python3.11/site-packages/keep/api/core/../models/db/migrations/env.py", line 76, in do_run_migrations
keep-backend-1 | context.run_migrations()
keep-backend-1 | File "<string>", line 8, in run_migrations
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/runtime/environment.py", line 946, in run_migrations
keep-backend-1 | self.get_context().run_migrations(**kw)
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/runtime/migration.py", line 628, in run_migrations
keep-backend-1 | step.migration_fn(**kw)
keep-backend-1 | File "/venv/lib/python3.11/site-packages/keep/api/models/db/migrations/versions/2024-10-22-10-38_8438f041ee0e.py", line 21, in upgrade
keep-backend-1 | with op.batch_alter_table("provider", schema=None) as batch_op:
keep-backend-1 | File "/usr/local/lib/python3.11/contextlib.py", line 144, in __exit__
keep-backend-1 | next(self.gen)
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/operations/base.py", line 398, in batch_alter_table
keep-backend-1 | impl.flush()
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/operations/batch.py", line 116, in flush
keep-backend-1 | fn(*arg, **kw)
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/ddl/impl.py", line 337, in add_column
keep-backend-1 | self._exec(base.AddColumn(table_name, column, schema=schema))
keep-backend-1 | File "/venv/lib/python3.11/site-packages/alembic/ddl/impl.py", line 210, in _exec
keep-backend-1 | return conn.execute(construct, params)
keep-backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/future/engine.py", line 280, in execute
keep-backend-1 | return self._execute_20(
keep-backend-1 | ^^^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1705, in _execute_20
keep-backend-1 | return meth(self, args_10style, kwargs_10style, execution_options)
keep-backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/sql/ddl.py", line 80, in _execute_on_connection
keep-backend-1 | return connection._execute_ddl(
keep-backend-1 | ^^^^^^^^^^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1472, in _execute_ddl
keep-backend-1 | ret = self._execute_context(
keep-backend-1 | ^^^^^^^^^^^^^^^^^^^^^^
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1943, in _execute_context
keep-backend-1 | self._handle_dbapi_exception(
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 2124, in _handle_dbapi_exception
keep-backend-1 | util.raise_(
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/util/compat.py", line 208, in raise_
keep-backend-1 | raise exception
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/base.py", line 1900, in _execute_context
keep-backend-1 | self.dialect.do_execute(
keep-backend-1 | File "/venv/lib/python3.11/site-packages/sqlalchemy/engine/default.py", line 736, in do_execute
keep-backend-1 | cursor.execute(statement, parameters)
keep-backend-1 | sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) Cannot add a NOT NULL column with default value NULL
keep-backend-1 | [SQL: ALTER TABLE provider ADD COLUMN pulling_enabled BOOLEAN NOT NULL]
keep-backend-1 | (Background on this error at: https://sqlalche.me/e/14/e3q8)
To Reproduce try to upgrade 0.26.1 to 0.27.1
Expected behavior Normal start
Additional context
Looks like this: SQL: ALTER TABLE provider ADD COLUMN pulling_enabled BOOLEAN NOT NULL is root of the problem
This line:
https://github.com/keephq/keep/blob/029068697218e904fd66822874284481ebaa7df0/keep/api/models/db/migrations/versions/2024-10-22-10-38_8438f041ee0e.py#L23
is autogenerated based from this line: https://github.com/keephq/keep/blob/029068697218e904fd66822874284481ebaa7df0/keep/api/models/db/provider.py#L23
looks like alembic/sqlalchemy fails to generate valid migration (add not null column with default value to already existing table) for this form of definition of model.
may be it should be implemented like this? https://github.com/keephq/keep/blob/029068697218e904fd66822874284481ebaa7df0/keep/api/models/db/provider.py#L25
which generates: https://github.com/keephq/keep/blob/029068697218e904fd66822874284481ebaa7df0/keep/api/models/db/migrations/versions/2024-09-13-10-48_938b1aa62d5c.py#L31
that looks more conforming to https://github.com/miguelgrinberg/Flask-Migrate/issues/265 (server_default=sa.false())
Hey, @imorph! I'm here to help you with any bugs or questions you have. Let's squash those bugs together!
The error you're encountering is due to SQLite's restriction on adding a NOT NULL column without a default value. To resolve this, you should modify the migration script to include a default value for the pulling_enabled column. You can achieve this by setting server_default=sa.true() in the migration script, which aligns with the default value defined in the model:
with op.batch_alter_table("provider", schema=None) as batch_op:
batch_op.add_column(sa.Column(
"pulling_enabled",
sa.Boolean(),
nullable=False,
server_default=sa.true()
))
This change ensures that the migration script provides a default value for existing rows, allowing the migration to succeed without violating SQLite's constraints.
To continue talking to Dosu, mention @dosu.
on it @imorph
released 0.27.2 with the fix
fyi @imorph