Skip to content

Commit 99a623f

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
override some AddColumn methods in SQLite
1 parent e6b8b9d commit 99a623f

File tree

3 files changed

+88
-8
lines changed

3 files changed

+88
-8
lines changed

src/Migrator.Tests/Providers/Generic/TransformationProviderGenericMiscConstraintBase.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Linq;
44
using DotNetProjects.Migrator.Framework;
55
using DotNetProjects.Migrator.Providers.Impl.SQLite;
6-
using DryIoc;
76
using NUnit.Framework;
87

98
namespace Migrator.Tests.Providers.Generic;
@@ -35,7 +34,7 @@ public void AddMultipleUniqueConstraint()
3534
Provider.AddUniqueConstraint("UN_Test_TestTwo", "TestTwo", "Id", "TestId");
3635
}
3736

38-
public void AddCheckConstraint()
37+
public void AddTestCheckConstraint()
3938
{
4039
Provider.AddCheckConstraint("CK_TestTwo_TestId", "TestTwo", "TestId>5");
4140
}
@@ -91,8 +90,10 @@ public virtual void CanAddMultipleUniqueConstraint()
9190
[Test]
9291
public virtual void CanAddCheckConstraint()
9392
{
94-
AddCheckConstraint();
95-
Assert.That(Provider.ConstraintExists("TestTwo", "CK_TestTwo_TestId"), Is.True);
93+
AddTestCheckConstraint();
94+
var constraintExists = Provider.ConstraintExists("TestTwo", "CK_TestTwo_TestId");
95+
96+
Assert.That(constraintExists, Is.True);
9697
}
9798

9899
[Test]
@@ -115,7 +116,7 @@ public void RemoveUniqueConstraint()
115116
[Test]
116117
public virtual void RemoveCheckConstraint()
117118
{
118-
AddCheckConstraint();
119+
AddTestCheckConstraint();
119120
Provider.RemoveConstraint("TestTwo", "CK_TestTwo_TestId");
120121
Assert.That(Provider.ConstraintExists("TestTwo", "CK_TestTwo_TestId"), Is.False);
121122
}

src/Migrator/Providers/Impl/SQLite/SQLiteTransformationProvider.cs

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,7 @@ public void RecreateTable(SQLiteTableInfo sqliteTableInfo)
751751

752752
var dbFields = columnDbFields.Concat(foreignKeyDbFields)
753753
.Concat(uniqueDbFields)
754+
.Concat(checkConstraintDbFields)
754755
.ToArray();
755756

756757
// ToHashSet() not available in older .NET versions so we create it old-fashioned.
@@ -811,6 +812,12 @@ public void RecreateTable(SQLiteTableInfo sqliteTableInfo)
811812
}
812813
}
813814

815+
[Obsolete]
816+
public override void AddTable(string table, string engine, string columns)
817+
{
818+
throw new NotSupportedException();
819+
}
820+
814821
public override void AddColumn(string table, Column column)
815822
{
816823
if (!TableExists(table))
@@ -819,6 +826,7 @@ public override void AddColumn(string table, Column column)
819826
}
820827

821828
var sqliteInfo = GetSQLiteTableInfo(table);
829+
822830
if (sqliteInfo.ColumnMappings.Select(x => x.OldName).ToList().Contains(column.Name))
823831
{
824832
throw new Exception("Column already exists.");
@@ -830,6 +838,61 @@ public override void AddColumn(string table, Column column)
830838
RecreateTable(sqliteInfo);
831839
}
832840

841+
public override void AddColumn(string table, string columnName, DbType type, ColumnProperty property)
842+
{
843+
var column = new Column(columnName, type, property);
844+
845+
AddColumn(table, column);
846+
}
847+
848+
public override void AddColumn(string table, string columnName, MigratorDbType type, ColumnProperty property)
849+
{
850+
var column = new Column(columnName, type, property);
851+
852+
AddColumn(table, column);
853+
}
854+
855+
public override void AddColumn(string table, string columnName, DbType type)
856+
{
857+
var column = new Column(columnName, type);
858+
859+
AddColumn(table, column);
860+
}
861+
862+
public override void AddColumn(string table, string columnName, MigratorDbType type)
863+
{
864+
var column = new Column(columnName, type);
865+
866+
AddColumn(table, column);
867+
}
868+
869+
public override void AddColumn(string table, string columnName, DbType type, int size, ColumnProperty property)
870+
{
871+
var column = new Column(columnName, type, size, property);
872+
873+
AddColumn(table, column);
874+
}
875+
876+
public override void AddColumn(string table, string columnName, MigratorDbType type, int size, ColumnProperty property)
877+
{
878+
var column = new Column(columnName, type, size, property);
879+
880+
AddColumn(table, column);
881+
}
882+
883+
public override void AddColumn(string table, string columnName, DbType type, object defaultValue)
884+
{
885+
var column = new Column(columnName, type, defaultValue);
886+
887+
AddColumn(table, column);
888+
}
889+
890+
public override void AddColumn(string table, string sqlColumn)
891+
{
892+
var column = new Column(sqlColumn);
893+
AddColumn(table, column);
894+
}
895+
833896
public override void ChangeColumn(string table, Column column)
834897
{
835898
if (!TableExists(table))
@@ -910,9 +973,12 @@ public override string[] GetConstraints(string table)
910973
.Select(x => x.Name)
911974
.ToList();
912975

913-
// TODO add PK and CHECK
976+
var checkConstraints = sqliteInfo.CheckConstraints
977+
.Select(x => x.Name)
978+
.ToList();
914979

915980
var names = foreignKeyNames.Concat(uniqueConstraints)
981+
.Concat(checkConstraints)
916982
.Where(x => !string.IsNullOrWhiteSpace(x))
917983
.ToArray();
918984

@@ -1471,6 +1537,16 @@ public List<PragmaTableInfoItem> GetPragmaTableInfoItems(string tableNameNotQuot
14711537
return pragmaTableInfoItems;
14721538
}
14731539

1540+
public override void AddCheckConstraint(string constraintName, string tableName, string checkSql)
1541+
{
1542+
var sqliteTableInfo = GetSQLiteTableInfo(tableName);
1543+
1544+
var checkConstraint = new CheckConstraint(constraintName, checkSql);
1545+
sqliteTableInfo.CheckConstraints.Add(checkConstraint);
1546+
1547+
RecreateTable(sqliteTableInfo);
1548+
}
1549+
14741550
public List<CheckConstraint> GetCheckConstraints(string tableName)
14751551
{
14761552
if (!TableExists(tableName))

src/Migrator/Providers/TransformationProvider.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ public virtual void AddTable(string name, string engine, params IDbField[] field
425425
var compoundPrimaryKey = pks.Count > 1;
426426

427427
var columnProviders = new List<ColumnPropertiesMapper>(columns.Count());
428+
428429
foreach (var column in columns)
429430
{
430431
// Remove the primary key notation if compound primary key because we'll add it back later
@@ -447,15 +448,17 @@ public virtual void AddTable(string name, string engine, params IDbField[] field
447448
}
448449

449450
var indexes = fields.Where(x => x is Index).Cast<Index>().ToArray();
451+
450452
foreach (var index in indexes)
451453
{
452454
AddIndex(name, index);
453455
}
454456

455457
var foreignKeys = fields.Where(x => x is ForeignKeyConstraint).Cast<ForeignKeyConstraint>().ToArray();
458+
456459
foreach (var foreignKey in foreignKeys)
457460
{
458-
this.AddForeignKey(name, foreignKey);
461+
AddForeignKey(name, foreignKey);
459462
}
460463
}
461464

@@ -647,7 +650,7 @@ public virtual void AddColumn(string table, string column, MigratorDbType type,
647650
/// AddColumn(string, string, Type, int, ColumnProperty, object)
648651
/// </see>
649652
/// </summary>
650-
public void AddColumn(string table, string column, DbType type)
653+
public virtual void AddColumn(string table, string column, DbType type)
651654
{
652655
AddColumn(table, column, type, 0, ColumnProperty.Null, null);
653656
}

0 commit comments

Comments
 (0)