Skip to content

Commit

Permalink
Fixed composite foreign keys bug (Issue 885) (#977)
Browse files Browse the repository at this point in the history
* Fixed composite foreign keys bug

* Update Generator.cs

Inverted condition and add spacing

* Added test for scripting of composite foreign keys

* Changed primary key value to avoid testing unique constraint
  • Loading branch information
gsv-helbling committed Feb 14, 2024
1 parent 6135b1d commit 310a8ca
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 4 deletions.
8 changes: 4 additions & 4 deletions src/API/Generator/Generator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1604,10 +1604,10 @@ private void GenerateTriggerForForeignKey(string prefix, string triggerType, Con

if (column.IsNullable == YesNoOption.YES)
{
_sbScript.Append(string.Join(" ", constraint.Columns.Select(x => $"NEW.{x} IS NOT NULL AND").ToArray()));
_sbScript.Append(string.Join("", constraint.Columns.Select(x => $"NEW.{x} IS NOT NULL AND ").ToArray()));
}

_sbScript.Append($"(SELECT {string.Join(", ", constraint.UniqueColumns.ToArray())} FROM {foreignTableName} WHERE ");
_sbScript.Append($"NOT EXISTS (SELECT * FROM {foreignTableName} WHERE ");

for (int i = 0; i < constraint.Columns.Count; i++)
{
Expand All @@ -1627,7 +1627,7 @@ private void GenerateTriggerForForeignKey(string prefix, string triggerType, Con
}
}

_sbScript.Append(") IS NULL;");
_sbScript.Append(");");
_sbScript.AppendLine(" END;");
}

Expand Down Expand Up @@ -2335,4 +2335,4 @@ private void GenerateTrigger(Trigger trigger)
}
#endregion
}
}
}
50 changes: 50 additions & 0 deletions src/API/SqlCeScripting40/Tests/GeneratorTest/Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Data.SqlServerCe;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

Expand Down Expand Up @@ -116,6 +119,53 @@ public void TestServerExportToSqlite()
}
}

[Test]
public void TestCompactExportToSqliteCompositeForeignKey()
{
// Script SQL CE DB with composite foreign key
var sourceConnectionString = new SqlCeConnectionStringBuilder
{
DataSource = Path.Combine(dbPath, "composite_foreign_key.sdf"),
MaxDatabaseSize = 4091
}.ToString();
var path = @"C:\temp\composite_foreign_key.sql";

IList<string> generatedFiles;
using (var sourceRepository = new DB4Repository(sourceConnectionString))
{
var generator = new Generator4(sourceRepository, path, false, false, true);
generator.ScriptDatabaseToFile(Scope.SchemaDataSQLite);
generatedFiles = generator.GeneratedFiles.ToList();
}

// Generate SQLite DB and test data insertion
var targetConnectionString = new SQLiteConnectionStringBuilder
{
DataSource = ":memory:",
Version = 3
}.ToString();
using (var targetRepository = new SQLiteRepository(targetConnectionString))
{
foreach (var generatedFile in generatedFiles)
{
targetRepository.ExecuteSqlFile(generatedFile);
}

// The owner table has only one entry with ID (0, 0)
targetRepository.ExecuteSql(
"INSERT INTO [Product] (ProductId, OwnerId1, OwnerId2) Values (0, 0, 0); GO;");
Assert.Throws<SQLiteException>(() =>
targetRepository.ExecuteSql(
"INSERT INTO [Product] (ProductId, OwnerId1, OwnerId2) Values (1, 0, 1); GO;"));
Assert.Throws<SQLiteException>(() =>
targetRepository.ExecuteSql(
"INSERT INTO [Product] (ProductId, OwnerId1, OwnerId2) Values (2, 1, 0); GO;"));
Assert.Throws<SQLiteException>(() =>
targetRepository.ExecuteSql(
"INSERT INTO [Product] (ProductId, OwnerId1, OwnerId2) Values (3, 1, 1); GO;"));
}
}

[Test]
public void TestServerExportDotInTable()
{
Expand Down
5 changes: 5 additions & 0 deletions src/API/SqlCeScripting40/Tests/Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
<ItemGroup>
<Content Include="composite_foreign_key.sdf">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
Expand Down
Binary file not shown.

0 comments on commit 310a8ca

Please sign in to comment.