Skip to content

Commit 8360f7d

Browse files
Ja bist du narrischJa bist du narrisch
authored andcommitted
All SQLite and SQL Server Tests are green #72 -#75
1 parent 605fda2 commit 8360f7d

File tree

7 files changed

+44
-23
lines changed

7 files changed

+44
-23
lines changed

src/Migrator.Tests/Providers/Base/TransformationProviderBase.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,11 @@ public void RemoveColumnWithDefault()
236236
[Test]
237237
public void RemoveUnexistingColumn()
238238
{
239-
var exception1 = Assert.Throws<Exception>(() => Provider.RemoveColumn("TestTwo", "abc"));
240-
var exception2 = Assert.Throws<Exception>(() => Provider.RemoveColumn("abc", "abc"));
239+
var exception1 = Assert.Throws<MigrationException>(() => Provider.RemoveColumn("TestTwo", "abc"));
240+
var exception2 = Assert.Throws<MigrationException>(() => Provider.RemoveColumn("abc", "abc"));
241241

242-
Assert.That(exception1.Message, Is.EqualTo("Column does not exist"));
243-
Assert.That(exception2.Message, Is.EqualTo("Table does not exist"));
242+
Assert.That(exception1.Message, Is.EqualTo("The table 'TestTwo' does not have a column named 'abc'"));
243+
Assert.That(exception2.Message, Is.EqualTo("The table 'abc' does not exist"));
244244
}
245245

246246
/// <summary>

src/Migrator.Tests/Providers/SQLServer/Base/SQLServerTransformationProviderTestBase.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,13 @@ public abstract class SQLServerTransformationProviderTestBase : TransformationPr
1616
public void SetUp()
1717
{
1818
var configReader = new ConfigurationReader();
19-
var connectionString = configReader.GetDatabaseConnectionConfigById(DatabaseConnectionConfigIds.SQLiteConnectionConfigId)
19+
var connectionString = configReader.GetDatabaseConnectionConfigById(DatabaseConnectionConfigIds.SQLServerConnectionConfigId)
2020
.ConnectionString;
2121

22+
2223
DbProviderFactories.RegisterFactory("Microsoft.Data.SqlClient", () => Microsoft.Data.SqlClient.SqlClientFactory.Instance);
2324

24-
Provider = new SqlServerTransformationProvider(new SqlServerDialect(), connectionString, null, "default", null);
25+
Provider = new SqlServerTransformationProvider(new SqlServerDialect(), connectionString, "dbo", "default", "Microsoft.Data.SqlClient");
2526
Provider.BeginTransaction();
2627

2728
AddDefaultTable();

src/Migrator.Tests/Providers/SQLite/SQLiteTransformationProviderGenericTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using DotNetProjects.Migrator.Providers.Impl.SQLite;
22
using Migrator.Providers.SQLite;
33
using Migrator.Tests.Settings;
4+
using Migrator.Tests.Settings.Config;
45
using NUnit.Framework;
56

67
namespace Migrator.Tests.Providers.SQLite.Base;
@@ -13,7 +14,7 @@ public class SQLiteTransformationProviderGenericTests : TransformationProviderBa
1314
public void SetUp()
1415
{
1516
var configReader = new ConfigurationReader();
16-
var connectionString = configReader.GetDatabaseConnectionConfigById("SQLiteConnectionString")
17+
var connectionString = configReader.GetDatabaseConnectionConfigById(DatabaseConnectionConfigIds.SQLiteConnectionConfigId)
1718
.ConnectionString;
1819

1920
Provider = new SQLiteTransformationProvider(new SQLiteDialect(), connectionString, "default", null);

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -396,19 +396,19 @@ public override void RemoveColumn(string tableName, string column)
396396

397397
if (!TableExists(tableName))
398398
{
399-
throw new Exception("Table does not exist");
399+
throw new MigrationException($"The table '{tableName}' does not exist");
400400
}
401401

402402
if (!ColumnExists(tableName, column))
403403
{
404-
throw new Exception("Column does not exist");
404+
throw new MigrationException($"The table '{tableName}' does not have a column named '{column}'");
405405
}
406406

407407
var sqliteInfoMainTable = GetSQLiteTableInfo(tableName);
408408

409409
if (!sqliteInfoMainTable.ColumnMappings.Any(x => x.OldName == column))
410410
{
411-
throw new Exception("Column not found");
411+
throw new MigrationException("Column not found");
412412
}
413413

414414
// We throw if all of the conditions are fulfilled:

src/Migrator/Providers/Impl/SqlServer/SqlServerDialect.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public SqlServerDialect()
4545
RegisterColumnType(DbType.StringFixedLength, int.MaxValue - 1, "NCHAR($l)");
4646
RegisterColumnType(DbType.StringFixedLength, int.MaxValue, "NCHAR(max)");
4747
RegisterColumnType(DbType.String, "NVARCHAR(255)");
48-
RegisterColumnType(DbType.String, int.MaxValue - 1, "NVARCHAR($l)");
48+
RegisterColumnType(DbType.String, 4000, "NVARCHAR($l)");
4949
RegisterColumnType(DbType.String, int.MaxValue, "NVARCHAR(max)");
5050
//RegisterColumnType(DbType.String, 1073741823, "NTEXT");
5151
RegisterColumnType(DbType.Time, "DATETIME");

src/Migrator/Providers/Impl/SqlServer/SqlServerTransformationProvider.cs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,15 @@ public override void AddColumn(string table, string sqlColumn)
9090
table = _dialect.TableNameNeedsQuote ? _dialect.Quote(table) : table;
9191
ExecuteNonQuery(string.Format("ALTER TABLE {0} ADD {1}", table, sqlColumn));
9292
}
93+
9394
public override void AddPrimaryKeyNonClustered(string name, string table, params string[] columns)
9495
{
95-
string nonclusteredString = "NONCLUSTERED";
96+
var nonclusteredString = "NONCLUSTERED";
9697
ExecuteNonQuery(
97-
String.Format("ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY {2} ({3}) ", table, name, nonclusteredString,
98-
String.Join(",", QuoteColumnNamesIfRequired(columns))));
98+
string.Format("ALTER TABLE {0} ADD CONSTRAINT {1} PRIMARY KEY {2} ({3}) ", table, name, nonclusteredString,
99+
string.Join(",", QuoteColumnNamesIfRequired(columns))));
99100
}
101+
100102
public override void AddIndex(string table, Index index)
101103
{
102104
var name = QuoteConstraintNameIfRequired(index.Name);
@@ -108,7 +110,7 @@ public override void AddIndex(string table, Index index)
108110
if (index.IncludeColumns != null && index.IncludeColumns.Length > 0)
109111
{
110112
var include = QuoteColumnNamesIfRequired(index.IncludeColumns);
111-
ExecuteNonQuery(String.Format("CREATE {0}{1} INDEX {2} ON {3} ({4}) INCLUDE ({5})", (index.Unique ? "UNIQUE " : ""), (index.Clustered ? "CLUSTERED" : "NONCLUSTERED"), name, table, string.Join(", ", columns), string.Join(", ", include)));
113+
ExecuteNonQuery(string.Format("CREATE {0}{1} INDEX {2} ON {3} ({4}) INCLUDE ({5})", (index.Unique ? "UNIQUE " : ""), (index.Clustered ? "CLUSTERED" : "NONCLUSTERED"), name, table, string.Join(", ", columns), string.Join(", ", include)));
112114
}
113115
else
114116
{
@@ -132,7 +134,7 @@ public override void ChangeColumn(string table, Column column)
132134

133135
base.ChangeColumn(table, column);
134136

135-
ColumnPropertiesMapper mapper = _dialect.GetAndMapColumnPropertiesWithoutDefault(column);
137+
var mapper = _dialect.GetAndMapColumnPropertiesWithoutDefault(column);
136138
ExecuteNonQuery(string.Format("ALTER TABLE {0} ADD CONSTRAINT {1} {2} FOR {3}", this.QuoteTableNameIfRequired(table), "DF_" + table + "_" + column.Name, _dialect.Default(def), this.QuoteColumnNameIfRequired(column.Name)));
137139

138140
if (notNull)
@@ -147,11 +149,14 @@ public override void ChangeColumn(string table, Column column)
147149
public override bool ColumnExists(string table, string column)
148150
{
149151
string schema;
152+
150153
if (!TableExists(table))
151154
{
152155
return false;
153156
}
157+
154158
int firstIndex = table.IndexOf(".");
159+
155160
if (firstIndex >= 0)
156161
{
157162
schema = table.Substring(0, firstIndex);
@@ -161,6 +166,7 @@ public override bool ColumnExists(string table, string column)
161166
{
162167
schema = _defaultSchema;
163168
}
169+
164170
using (var cmd = CreateCommand())
165171
using (
166172
IDataReader reader = base.ExecuteQuery(cmd, string.Format("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '{0}' AND TABLE_NAME='{1}' AND COLUMN_NAME='{2}'", schema, table, column)))
@@ -185,14 +191,17 @@ public override bool TableExists(string table)
185191
int firstIndex = table.IndexOf(".");
186192
if (firstIndex >= 0)
187193
{
188-
schema = table.Substring(0, firstIndex);
189-
table = table.Substring(firstIndex + 1);
194+
schema = table.Substring(0, firstIndex).Trim();
195+
table = table.Substring(firstIndex + 1).Trim();
190196
}
191197
else
192198
{
193199
schema = _defaultSchema;
194200
}
195201

202+
schema = schema.StartsWith("[") && schema.EndsWith("]") ? schema.Substring(1, schema.Length - 2) : schema;
203+
table = table.StartsWith("[") && table.EndsWith("]") ? table.Substring(1, table.Length - 2) : table;
204+
196205
using (var cmd = CreateCommand())
197206
using (IDataReader reader = base.ExecuteQuery(cmd, string.Format("SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME='{0}' AND TABLE_SCHEMA='{1}'", table, schema)))
198207
{
@@ -453,6 +462,11 @@ public override void RemoveColumn(string table, string column)
453462

454463
public override void RenameColumn(string tableName, string oldColumnName, string newColumnName)
455464
{
465+
if (!TableExists(tableName))
466+
{
467+
throw new MigrationException($"The table '{tableName}' does not exist");
468+
}
469+
456470
if (ColumnExists(tableName, newColumnName))
457471
throw new MigrationException(String.Format("Table '{0}' has column named '{1}' already", tableName, newColumnName));
458472

src/Migrator/Providers/TransformationProvider.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -443,16 +443,21 @@ public virtual void RenameColumn(string tableName, string oldColumnName, string
443443
ExecuteNonQuery(String.Format("ALTER TABLE {0} RENAME COLUMN {1} TO {2}", tableName, Dialect.Quote(column.Name), quotedNewColumnName));
444444
}
445445

446-
public virtual void RemoveColumn(string table, string column)
446+
public virtual void RemoveColumn(string tableName, string column)
447447
{
448-
if (!ColumnExists(table, column, true))
448+
if (!TableExists(tableName))
449449
{
450-
throw new MigrationException(string.Format("The table '{0}' does not have a column named '{1}'", table, column));
450+
throw new MigrationException($"The table '{tableName}' does not exist");
451451
}
452452

453-
var existingColumn = GetColumnByName(table, column);
453+
if (!ColumnExists(tableName, column, true))
454+
{
455+
throw new MigrationException(string.Format("The table '{0}' does not have a column named '{1}'", tableName, column));
456+
}
457+
458+
var existingColumn = GetColumnByName(tableName, column);
454459

455-
ExecuteNonQuery(String.Format("ALTER TABLE {0} DROP COLUMN {1} ", table, Dialect.Quote(existingColumn.Name)));
460+
ExecuteNonQuery(String.Format("ALTER TABLE {0} DROP COLUMN {1} ", tableName, Dialect.Quote(existingColumn.Name)));
456461
}
457462

458463
public virtual bool ColumnExists(string table, string column)

0 commit comments

Comments
 (0)