From 3a7e5c9201719a50d06110867086bf4403c811c8 Mon Sep 17 00:00:00 2001 From: Qstick Date: Thu, 1 Apr 2021 22:12:54 -0400 Subject: [PATCH] Fixed: Database migration failure when database was manually repaired in a certain way Fixes #2094 --- .../SqliteSchemaDumperFixture.cs | 3 ++- .../Migration/Framework/SqliteSchemaDumper.cs | 21 ++++++++++++++++++- .../Migration/Framework/SqliteSyntaxReader.cs | 4 ++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs b/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs index 64efa19ba..0baecd9aa 100644 --- a/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/SqliteSchemaDumperTests/SqliteSchemaDumperFixture.cs @@ -1,4 +1,4 @@ -using System.Data; +using System.Data; using System.Linq; using FluentAssertions; using NUnit.Framework; @@ -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 57b984c5a..4ae4e6e68 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.Collections.Generic; using System.Data; +using System.Linq; using FluentMigrator.Model; using FluentMigrator.Runner.Processors.SQLite; @@ -66,6 +67,24 @@ 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 4940eac93..c41425ab7 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSyntaxReader.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/SqliteSyntaxReader.cs @@ -154,12 +154,12 @@ 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] == '(')) + 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; }