Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug]: Migration can crash if migrationsMeta table exists but is empty #3588

Closed
nichwall opened this issue Nov 7, 2024 · 2 comments · Fixed by #3589
Closed

[Bug]: Migration can crash if migrationsMeta table exists but is empty #3588

nichwall opened this issue Nov 7, 2024 · 2 comments · Fixed by #3589
Labels
bug Something isn't working

Comments

@nichwall
Copy link
Contributor

nichwall commented Nov 7, 2024

What happened?

A user in Discord reported a crash when upgrading from server version 2.12.3 to 2.16.2. Upon request, they tried upgrading to a few different versions, including 2.13.3 and 2.14.0. They were able to upgrade to all versions before 2.14.0, but the migration failed and crashed for all servers version at 2.14.0 and above.

The user provided their database to me and I was able to reproduce the crash. Upon investigation, it looks like they somehow got an empty migrationsMeta table. I am not sure if this is a result of the failed upgrade to 2.16.2 or something else.

What did you expect to happen?

Migration to be handled when the table is empty. It appears to be a problem with how isDatabaseNew is handled. Instead of only checking if the table exists, we should also check that the version row exists.

async checkOrCreateMigrationsMetaTable() {
const queryInterface = this.sequelize.getQueryInterface()
let migrationsMetaTableExists = await queryInterface.tableExists(MigrationManager.MIGRATIONS_META_TABLE)
if (this.isDatabaseNew && migrationsMetaTableExists) {
// This can happen if database was initialized with force: true
await queryInterface.dropTable(MigrationManager.MIGRATIONS_META_TABLE)
migrationsMetaTableExists = false
}
if (!migrationsMetaTableExists) {
await queryInterface.createTable(MigrationManager.MIGRATIONS_META_TABLE, {
key: {
type: DataTypes.STRING,
allowNull: false
},
value: {
type: DataTypes.STRING,
allowNull: false
}
})
await this.sequelize.query("INSERT INTO :migrationsMeta (key, value) VALUES ('version', :version), ('maxVersion', '0.0.0')", {
replacements: { version: this.isDatabaseNew ? this.serverVersion : '0.0.0', migrationsMeta: MigrationManager.MIGRATIONS_META_TABLE },
type: Sequelize.QueryTypes.INSERT
})
Logger.debug(`[MigrationManager] Created migrationsMeta table: "${MigrationManager.MIGRATIONS_META_TABLE}"`)
}
}

Steps to reproduce the issue

  1. Create migrationsMeta table in an existing database with no rows
  2. Attempt to upgrade to a version greater than or equal to 2.14.0

Audiobookshelf version

v2.16.2

How are you running audiobookshelf?

Built from source

What OS is your Audiobookshelf server hosted from?

Linux

If the issue is being seen in the UI, what browsers are you seeing the problem on?

None

Logs

[2024-11-06 21:53:19.790] ERROR: [Database] Failed to run migrations TypeError: Cannot read properties of undefined (reading 'version')
    at MigrationManager.fetchVersionsFromDatabase (/home/chell/data/Documents/git/audiobookshelf/server/managers/MigrationManager.js:178:14)
    at async MigrationManager.init (/home/chell/data/Documents/git/audiobookshelf/server/managers/MigrationManager.js:46:5)
    at async Database.init (/home/chell/data/Documents/git/audiobookshelf/server/Database.js:183:7)
    at async Server.init (/home/chell/data/Documents/git/audiobookshelf/server/Server.js:128:5)
    at async Server.start (/home/chell/data/Documents/git/audiobookshelf/server/Server.js:191:5)
[2024-11-06 21:53:19.793] FATAL: [Server] Unhandled rejection: Error: Database migration failed
    at Database.init (/home/chell/data/Documents/git/audiobookshelf/server/Database.js:187:13)
    at async Server.init (/home/chell/data/Documents/git/audiobookshelf/server/Server.js:128:5)
    at async Server.start (/home/chell/data/Documents/git/audiobookshelf/server/Server.js:191:5) 
promise: Promise {
  <rejected> Error: Database migration failed
      at Database.init (/home/chell/data/Documents/git/audiobookshelf/server/Database.js:187:13)
      at async Server.init (/home/chell/data/Documents/git/audiobookshelf/server/Server.js:128:5)
      at async Server.start (/home/chell/data/Documents/git/audiobookshelf/server/Server.js:191:5)
}

Additional Notes

No response

@ggladchun-nasuni
Copy link

Confirming @nichwall root cause. Manually dropped migrationData and was able to upgrade

@advplyr advplyr added the awaiting release Issue is resolved and will be in the next release label Nov 17, 2024
Copy link

Fixed in v2.17.0.

@github-actions github-actions bot removed the awaiting release Issue is resolved and will be in the next release label Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants