Skip to content

Commit 4db3e55

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Throw if GetColumnContentSize column is not of type string.
1 parent 96a9030 commit 4db3e55

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSize.cs renamed to src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSizeTests.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Data;
23
using DotNetProjects.Migrator.Framework;
34
using NUnit.Framework;
@@ -9,11 +10,10 @@ namespace Migrator.Tests.Providers.PostgreSQL;
910
public class PostgreSQLTransformationProvider_GetColumnContentSizeTests : PostgreSQLTransformationProviderTestBase
1011
{
1112
[Test]
12-
public void GetColumnContentSize_DefaultValues_Succeeds()
13+
public void GetColumnContentSize_UseStringColumn_MaxContentLengthIsCorrect()
1314
{
1415
// Arrange
1516
const string testTableName = "testtable";
16-
1717
const string stringColumnName = "stringcolumn";
1818

1919
Provider.AddTable(testTableName,
@@ -30,4 +30,22 @@ public void GetColumnContentSize_DefaultValues_Succeeds()
3030
// Assert
3131
Assert.That(columnContentSize, Is.EqualTo(4444));
3232
}
33+
34+
[Test]
35+
public void GetColumnContentSize_UseOnNonStringColumn_ThrowsSpeakingException()
36+
{
37+
// Arrange
38+
const string testTableName = "testtable";
39+
const string stringColumnName = "nonstringcolumn";
40+
41+
Provider.AddTable(testTableName,
42+
new Column(stringColumnName, DbType.Int32)
43+
);
44+
45+
// Act
46+
var exception = Assert.Throws<Exception>(() => Provider.GetColumnContentSize(testTableName, stringColumnName));
47+
48+
// Assert
49+
Assert.That(exception.Message, Does.Contain("is not of type string"));
50+
}
3351
}

src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,23 @@ public override string[] GetTables()
251251

252252
public override int GetColumnContentSize(string table, string columnName)
253253
{
254+
if (!TableExists(table))
255+
{
256+
throw new Exception($"Table '{table}' not found.");
257+
}
258+
259+
if (!ColumnExists(table, columnName, true))
260+
{
261+
throw new Exception($"Column '{columnName}' does not exist");
262+
}
263+
264+
var column = GetColumnByName(table, columnName);
265+
266+
if (column.MigratorDbType != MigratorDbType.String)
267+
{
268+
throw new Exception($"Column '{columnName}' in table {table} is not of type string");
269+
}
270+
254271
var result = ExecuteScalar($"SELECT MAX(LENGTH({QuoteColumnNameIfRequired(columnName)})) FROM {QuoteTableNameIfRequired(table)}");
255272

256273
if (result == DBNull.Value)

0 commit comments

Comments
 (0)