core: MariaDB database migration failed. Schema 46->47
The problem
After upgrade to 2024.9.0 database migration fails.
What version of Home Assistant Core has the issue?
core-2024.9.0
What was the last working version of Home Assistant Core?
core-2024.8.3
What type of installation are you running?
Home Assistant OS
Integration causing the issue
recorder
Link to integration documentation on our website
No response
Diagnostics information
No response
Example YAML snippet
No response
Anything in the logs that might be useful for us?
2024-09-05 13:30:39.050 WARNING (Recorder) [homeassistant.components.recorder.migration] The database is about to upgrade from schema version 46 to 47
2024-09-05 13:30:39.063 WARNING (Recorder) [homeassistant.components.recorder.migration] Adding foreign key constraint to states.old_state_id. Note: this can take several minutes on large databases and slow machines. Please be patient!
2024-09-05 13:34:46.676 ERROR (Recorder) [homeassistant.components.recorder.migration] Could not update foreign options in states table
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
self.dialect.do_execute(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 924, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
db.query(q)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
_mysql.connection.query(self, query)
MySQLdb.OperationalError: (1034, "Index for table 'homeassistant/#sql-alter-1-d' is corrupt; try to repair it")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 753, in _add_constraint
connection.execute(add_constraint)
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
return meth(
^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/sql/ddl.py", line 180, in _execute_on_connection
return connection._execute_ddl(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1529, in _execute_ddl
ret = self._execute_context(
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
return self._exec_single_context(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1986, in _exec_single_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2353, in _handle_dbapi_exception
raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
self.dialect.do_execute(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 924, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
db.query(q)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
_mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1034, "Index for table 'homeassistant/#sql-alter-1-d' is corrupt; try to repair it")
[SQL: ALTER TABLE states ADD FOREIGN KEY(old_state_id) REFERENCES states (state_id)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2024-09-05 13:34:46.684 ERROR (Recorder) [homeassistant.helpers.recorder] Error executing query
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
self.dialect.do_execute(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 924, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
db.query(q)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
_mysql.connection.query(self, query)
MySQLdb.OperationalError: (1034, "Index for table 'homeassistant/#sql-alter-1-d' is corrupt; try to repair it")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/helpers/recorder.py", line 101, in session_scope
yield session
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 753, in _add_constraint
connection.execute(add_constraint)
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
return meth(
^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/sql/ddl.py", line 180, in _execute_on_connection
return connection._execute_ddl(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1529, in _execute_ddl
ret = self._execute_context(
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
return self._exec_single_context(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1986, in _exec_single_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2353, in _handle_dbapi_exception
raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
self.dialect.do_execute(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 924, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
db.query(q)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
_mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1034, "Index for table 'homeassistant/#sql-alter-1-d' is corrupt; try to repair it")
[SQL: ALTER TABLE states ADD FOREIGN KEY(old_state_id) REFERENCES states (state_id)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
2024-09-05 13:34:46.688 ERROR (Recorder) [homeassistant.components.recorder.core] Database error during schema migration
Traceback (most recent call last):
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
self.dialect.do_execute(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 924, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
db.query(q)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
_mysql.connection.query(self, query)
MySQLdb.OperationalError: (1034, "Index for table 'homeassistant/#sql-alter-1-d' is corrupt; try to repair it")
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/usr/src/homeassistant/homeassistant/components/recorder/core.py", line 981, in _migrate_schema
new_schema_status = migrator(
^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 334, in migrate_schema_live
schema_status = _migrate_schema(
^^^^^^^^^^^^^^^^
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 300, in _migrate_schema
_apply_update(instance, hass, engine, session_maker, new_version, start_version)
File "/usr/src/homeassistant/homeassistant/components/recorder/util.py", line 703, in wrapper
job(instance, *args, **kwargs)
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 883, in _apply_update
migrator_cls(instance, hass, engine, session_maker, old_version).apply_update()
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 929, in apply_update
self._apply_update()
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 1761, in _apply_update
_restore_foreign_key_constraints(
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 721, in _restore_foreign_key_constraints
_add_constraint(session_maker, add_constraint, table, column)
File "/usr/src/homeassistant/homeassistant/components/recorder/migration.py", line 753, in _add_constraint
connection.execute(add_constraint)
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1418, in execute
return meth(
^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/sql/ddl.py", line 180, in _execute_on_connection
return connection._execute_ddl(
^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1529, in _execute_ddl
ret = self._execute_context(
^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1846, in _execute_context
return self._exec_single_context(
^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1986, in _exec_single_context
self._handle_dbapi_exception(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 2353, in _handle_dbapi_exception
raise sqlalchemy_exception.with_traceback(exc_info[2]) from e
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1967, in _exec_single_context
self.dialect.do_execute(
File "/usr/local/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 924, in do_execute
cursor.execute(statement, parameters)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 179, in execute
res = self._query(mogrified_query)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.12/site-packages/MySQLdb/cursors.py", line 330, in _query
db.query(q)
File "/usr/local/lib/python3.12/site-packages/MySQLdb/connections.py", line 261, in query
_mysql.connection.query(self, query)
sqlalchemy.exc.OperationalError: (MySQLdb.OperationalError) (1034, "Index for table 'homeassistant/#sql-alter-1-d' is corrupt; try to repair it")
[SQL: ALTER TABLE states ADD FOREIGN KEY(old_state_id) REFERENCES states (state_id)]
(Background on this error at: https://sqlalche.me/e/20/e3q8)
Additional information
Have tried restarting everything. Ran mariadb-check and everything is fine there. MariaDB version 11.5.2
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 6
- Comments: 74
What I did is:
SET GLOBAL innodb_buffer_pool_size=536870912;to set the innodb_buffer_pool_size to 512mb.show global variables like 'innodb_buffer%';to see if the command above worked (last row).START TRANSACTION;to be able to rollback in case of somethings goes wrong.USE 'homeassistant';to select the HA database.ALTER TABLE states ADD FOREIGN KEY(attributes_id) REFERENCES state_attributes (attributes_id);or the query that you find in the second-last row of the error log.COMMIT;to save the work.After this restart HA to let the upgrade finish itself.
This procedure also solved my problem.
If you are using HAOS and the MariaDB add-on, follow these steps to access MySQL:
Log in from the OS console prompt:
Verify if Docker is running:
Access the Docker prompt:
Follow the MySQL commands provided above.
Why did you close the issue? There are still many people with the problem, I think that from HA something better should be done.
Hello, i like to get rid of the error but i have no success till now. Installed portainer, yes i know i should not do that but i have no other chance with hassos. i went into mariadb container and changed buffer_pool:
@15.09. my mariadb had 32GByte. After first migration fail something went terrible wrong and my database size increased from 32Gbyte to 46Gbyte. Further i have 200Gbyte free space left.
The second attempt fails although the buffer was increased to 4GByte. with:
my thought would be to simply drop the table as long as there is no dependency on the energy dashboard?
i have already lost 1 year of data from the energy dashboard because i merged two consumption sensors into one. nobody could help me how to correct this.
the data is important to me, i don’t want to lose my long-term data again.
Can someone confirm that i can simply drop and migrate without worries? My recorder settings:
Okay…Wow… Went into console with mysql client directly.
show global variables like 'innodb_buffer%';seems that setting
innodb_buffer_pool_size=4Gin/etc/my.cnf.d/mariadb-server.cnfhad no effect. after setting it directly with mysql client toSET GLOBAL innodb_buffer_pool_size=2147483648;it was applied:amazing horrible…waiting for ALTER TABLE now.
FINALLY after 3 retries in 4 weeks and 14GByte additional failed migration garbage data…i was able to do the database migration step with this, many thx:
SET GLOBAL innodb_buffer_pool_size=2147483648;to set the innodb_buffer_pool_size to 2GByte.show global variables like 'innodb_buffer%';to see if the command above worked (last row).START TRANSACTION;to be able to rollback in case of somethings goes wrong.USE 'homeassistant';to select the HA database.ALTER TABLE states ADD FOREIGN KEY(attributes_id) REFERENCES state_attributes (attributes_id);or the query that you find in the second-last row of the error log.COMMIT;to save the work.Take care, if you set 2Gbyte for innodb_buffer_pool_size you should have 2 GByte free RAM. Otherwise docker container mariadb will crash while migration.
After around 3 days I finally got a migration error with the lock table size error. This is good news for me. Will try the innodb buffer solution found by others.
Ok fair point, I reopen and let them close.
I can confirm this procedure allowed me to upgrade from database version 46 to 47. Thanks!
You surely need to run the failed command to fix the state of the database. So, I’d try something like:
SET GLOBAL innodb_buffer_pool_size=536870912;to increase the innodb_buffer_pool_size to 512 MiB.SHOW GLOBAL VARIABLES LIKE "innodb_buffer_pool_%";to see if the change has been applied successfully.START TRANSACTION;(do it in transaction, so you can runROLLBACK;if some problem occurrs).USE homeassistant;to select the Home Assistant database.ALTER TABLE states MODIFY event_id BIGINT;to apply the command that crashed when automatic database migration was running.COMMIT;to save the work orROLLBACK;if the operation wasn’t successful.For a MariaDB based database refer to this issue for a possible workaround: https://github.com/home-assistant/addons/issues/3754
This solved my problem 🙏 After setting the innodb_buffer_pool_size from 128 to 512MB migration succeeded, also it was much faster migrating
It’s not because of the size of the disk neither, i have 120gb of free space and a 30gb db, is 4x time the database size. From what i read the cause is the “innodb_buffer_pool_size” the cause, and i’m trying this hoping to resolve.