From 34faa417c1d7c48d86236ed8f29bd44518322373 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 18 Mar 2021 01:21:47 +0100 Subject: [PATCH] Fixed: Database migration failure when database was manually repaired in a certain way fixes #4390 --- .../SqliteSchemaDumperFixture.cs | 1 + .../Migration/Framework/SqliteSchemaDumper.cs | 18 ++++++++++++++++++ .../Migration/Framework/SqliteSyntaxReader.cs | 4 ++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs b/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs index 4a5fc0fda..e80fc4623 100644 --- a/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs @@ -25,6 +25,7 @@ namespace NzbDrone.Core.Test.Datastore.SqliteSchemaDumperTests [TestCase(@"CREATE TABLE ""Test """"Table"" (""My""""Id"" INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "Test \"Table", "My\"Id")] [TestCase(@"CREATE TABLE [Test Table] ([My Id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT)", "Test Table", "My Id")] [TestCase(@" CREATE TABLE `Test ``Table` ( `My`` Id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT ) ", "Test `Table", "My` Id")] + [TestCase(@"CREATE TABLE TestTable (MyId INTEGER NOT NULL, PRIMARY KEY(""MyId"" AUTOINCREMENT))", "TestTable", "MyId")] public void should_parse_table_language_flavors(string sql, string tableName, string columnName) { var result = Subject.ReadTableSchema(sql); diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSchemaDumper.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSchemaDumper.cs index c1f786f79..c97c0ea4b 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSchemaDumper.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSchemaDumper.cs @@ -1,5 +1,6 @@ using System.Collections.Generic; using System.Data; +using System.Linq; using FluentMigrator.Model; using FluentMigrator.Runner; using FluentMigrator.Runner.Processors.SQLite; @@ -67,6 +68,23 @@ namespace NzbDrone.Core.Datastore.Migration.Framework if (columnReader.Read() == SqliteSyntaxReader.TokenType.StringToken) { + if (columnReader.ValueToUpper == "PRIMARY") + { + columnReader.SkipTillToken(SqliteSyntaxReader.TokenType.ListStart); + if (columnReader.Read() == SqliteSyntaxReader.TokenType.Identifier) + { + var pk = table.Columns.First(v => v.Name == columnReader.Value); + pk.IsPrimaryKey = true; + pk.IsNullable = true; + pk.IsUnique = true; + if (columnReader.Buffer.ToUpperInvariant().Contains("AUTOINCREMENT")) + { + pk.IsIdentity = true; + } + continue; + } + } + if (columnReader.ValueToUpper == "CONSTRAINT" || columnReader.ValueToUpper == "PRIMARY" || columnReader.ValueToUpper == "UNIQUE" || columnReader.ValueToUpper == "CHECK" || columnReader.ValueToUpper == "FOREIGN") diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSyntaxReader.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSyntaxReader.cs index 703aff012..fd7db0215 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSyntaxReader.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSyntaxReader.cs @@ -148,9 +148,9 @@ namespace NzbDrone.Core.Datastore.Migration.Framework { var start = Index; var end = start + 1; - while (end < Buffer.Length && (char.IsLetter(Buffer[end]) || char.IsNumber(Buffer[end]) || Buffer[end] == '_' || Buffer[end] == '(')) end++; + while (end < Buffer.Length && (char.IsLetter(Buffer[end]) || char.IsNumber(Buffer[end]) || Buffer[end] == '_')) end++; - if (end >= Buffer.Length || Buffer[end] == ',' || Buffer[end] == ')' || char.IsWhiteSpace(Buffer[end])) + if (end >= Buffer.Length || Buffer[end] == ',' || Buffer[end] == '(' || Buffer[end] == ')' || char.IsWhiteSpace(Buffer[end])) { Index = end; }