Skip to content

Commit d548bbd

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
Updated FK handling in SQLite
1 parent ffe341e commit d548bbd

File tree

7 files changed

+323
-65
lines changed

7 files changed

+323
-65
lines changed

src/Migrator.Tests/Providers/SQLiteTransformationProviderTest.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,15 @@ public void SetUp()
3939
[Test]
4040
public void AddForeignKey()
4141
{
42+
// Arrange
4243
AddTableWithPrimaryKey();
43-
_provider.AddForeignKey("Will not be used by SQLite", "Test", "Id", "TestTwo", "TestId", ForeignKeyConstraintType.SetDefault);
44+
45+
// Act
46+
_provider.AddForeignKey("FK name is not supported by SQLite", foreignTable: "Test", foreignColumn: "Id", primaryTable: "TestTwo", primaryColumn: "TestId", ForeignKeyConstraintType.Cascade);
47+
48+
// Assert
49+
var foreignKeyConstraints = ((SQLiteTransformationProvider)_provider).GetForeignKeyConstraints("Test");
50+
var tableSQLCreateScript = ((SQLiteTransformationProvider)_provider).GetSqlCreateTableScript("Test");
4451
}
4552

4653

src/Migrator/Framework/ForeignKeyConstraint.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ public class ForeignKeyConstraint : IDbField
77
public ForeignKeyConstraint()
88
{ }
99

10-
public ForeignKeyConstraint(string name, string table, string[] columns, string pkTable, string[] pkColumns, string stringId = null)
10+
public ForeignKeyConstraint(string name, string table, string[] columns, string pkTable, string[] pkColumns)
1111
{
12-
StringId = stringId;
1312
Name = name;
1413
Table = table;
1514
Columns = columns;
@@ -19,12 +18,27 @@ public ForeignKeyConstraint(string name, string table, string[] columns, string
1918

2019
/// <summary>
2120
/// Gets or sets the Id of the FK. This is not the name of the FK.
22-
/// SQLite:
21+
/// Currently used for SQLite
2322
/// </summary>
24-
public string StringId { get; set; }
23+
public int? Id { get; set; }
2524
public string Name { get; set; }
2625
public string Table { get; set; }
2726
public string[] Columns { get; set; }
2827
public string PkTable { get; set; }
2928
public string[] PkColumns { get; set; }
29+
30+
/// <summary>
31+
/// Gets or sets the on update text. Currently only used for SQLite.
32+
/// </summary>
33+
public string OnDelete { get; set; }
34+
35+
/// <summary>
36+
/// Gets or sets the on update text. Currently only used for SQLite.
37+
/// </summary>
38+
public string OnUpdate { get; set; }
39+
40+
/// <summary>
41+
/// /// Gets or sets the match text. Currently only used for SQLite.
42+
/// </summary>
43+
public string Match { get; set; }
3044
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace DotNetProjects.Migrator.Providers.Impl.SQLite.Models;
2+
3+
public class MappingInfo
4+
{
5+
/// <summary>
6+
/// Gets or sets the old name.
7+
/// </summary>
8+
public string OldName { get; set; }
9+
10+
/// <summary>
11+
/// Gets or sets the new name.
12+
/// </summary>
13+
public string NewName { get; set; }
14+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
namespace DotNetProjects.Migrator.Providers.Impl.SQLite.Models;
2+
3+
/// <summary>
4+
/// Represents a row of pragma_foreign_key_list() in SQLite.
5+
/// </summary>
6+
public class PragmaForeignKeyListItem
7+
{
8+
/// <summary>
9+
/// Gets or sets the foreign key id. Name: id
10+
/// </summary>
11+
public int Id { get; set; }
12+
13+
/// <summary>
14+
/// Gets or sets the sequence number of the foreign key. Name: seq
15+
/// </summary>
16+
public int Seq { get; set; }
17+
18+
/// <summary>
19+
/// Gets or sets the name of the referenced table. Name: table
20+
/// </summary>
21+
public string Table { get; set; }
22+
23+
/// <summary>
24+
/// Gets or sets the column in the current table that acts as the FK. Name: from
25+
/// </summary>
26+
public string From { get; set; }
27+
28+
/// <summary>
29+
/// Gets or sets the column in the referenced table. Name: to
30+
/// </summary>
31+
public string To { get; set; }
32+
33+
/// <summary>
34+
/// Gets or sets on update. Name: on_update
35+
/// </summary>
36+
public string OnUpdate { get; set; }
37+
38+
/// <summary>
39+
/// Gets or sets on delete. Name: on_delete
40+
/// </summary>
41+
public string OnDelete { get; set; }
42+
43+
/// <summary>
44+
/// Gets or sets match. Name: match
45+
/// </summary>
46+
public string Match { get; set; }
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System.Collections.Generic;
2+
using DotNetProjects.Migrator.Framework;
3+
using Migrator.Framework;
4+
5+
namespace DotNetProjects.Migrator.Providers.Impl.SQLite.Models;
6+
7+
public class SQLiteTableInfo
8+
{
9+
/// <summary>
10+
/// Gets or sets the table name.
11+
/// </summary>
12+
public MappingInfo TableNameMapping { get; set; }
13+
14+
/// <summary>
15+
/// Gets or sets the columns of a table
16+
/// </summary>
17+
public List<Column> Columns { get; set; } = [];
18+
19+
/// <summary>
20+
/// Gets or sets the indexes of a table.
21+
/// </summary>
22+
public List<Index> Indexes { get; set; } = [];
23+
24+
/// <summary>
25+
/// Gets or sets the foreign keys of a table.
26+
/// </summary>
27+
public List<ForeignKeyConstraint> ForeignKeys { get; set; } = [];
28+
29+
/// <summary>
30+
/// Gets or sets the column mappings.
31+
/// </summary>
32+
public List<MappingInfo> ColumnMappings { get; set; } = [];
33+
}

0 commit comments

Comments
 (0)