Fix database migration by avoiding DROP COLUMN AND ADD COLUMN

This commit is contained in:
Bart De Vries 2021-06-29 20:52:40 +02:00
parent 3a24ea6f65
commit acef37fa58
1 changed files with 11 additions and 6 deletions

View File

@ -80,13 +80,18 @@ bool Database::migrateTo2()
bool Database::migrateTo3()
{
qDebug() << "Migrating database to version 3";
TRUE_OR_RETURN(execute(QStringLiteral("ALTER TABLE Enclosures ADD COLUMN downloaded_temp BOOL DEFAULT 0;")));
TRUE_OR_RETURN(execute(QStringLiteral("UPDATE Enclosures SET downloaded_temp=1 WHERE downloaded=1;")));
TRUE_OR_RETURN(execute(QStringLiteral("ALTER TABLE Enclosures DROP COLUMN downloaded;")));
TRUE_OR_RETURN(execute(QStringLiteral("ALTER TABLE Enclosures ADD COLUMN downloaded INTEGER DEFAULT 0;")));
TRUE_OR_RETURN(execute(QStringLiteral("UPDATE Enclosures SET downloaded=3 WHERE downloaded_temp=1;")));
TRUE_OR_RETURN(execute(QStringLiteral("ALTER TABLE Enclosures DROP COLUMN downloaded_temp;")));
TRUE_OR_RETURN(execute(QStringLiteral("BEGIN TRANSACTION;")));
TRUE_OR_RETURN(
execute(QStringLiteral("CREATE TABLE IF NOT EXISTS Enclosurestemp (feed TEXT, id TEXT, duration INTEGER, size INTEGER, title TEXT, type TEXT, url "
"TEXT, playposition INTEGER, downloaded INTEGER);")));
TRUE_OR_RETURN(
execute(QStringLiteral("INSERT INTO Enclosurestemp (feed, id, duration, size, title, type, url, playposition, downloaded) SELECT feed, id, duration, "
"size, title, type, url, playposition, downloaded FROM Enclosures;")));
TRUE_OR_RETURN(execute(QStringLiteral("UPDATE Enclosurestemp SET downloaded=3 WHERE downloaded=1;")));
TRUE_OR_RETURN(execute(QStringLiteral("DROP TABLE Enclosures;")));
TRUE_OR_RETURN(execute(QStringLiteral("ALTER TABLE Enclosurestemp RENAME TO Enclosures;")));
TRUE_OR_RETURN(execute(QStringLiteral("PRAGMA user_version = 3;")));
TRUE_OR_RETURN(execute(QStringLiteral("COMMIT;")));
return true;
}