diff --git a/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSize.cs b/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSizeTests.cs similarity index 59% rename from src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSize.cs rename to src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSizeTests.cs index 69f81f4..d2a3701 100644 --- a/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSize.cs +++ b/src/Migrator.Tests/Providers/PostgreSQL/PostgreSQLTransformationProvider_GetColumnContentSizeTests.cs @@ -1,3 +1,4 @@ +using System; using System.Data; using DotNetProjects.Migrator.Framework; using NUnit.Framework; @@ -9,11 +10,10 @@ namespace Migrator.Tests.Providers.PostgreSQL; public class PostgreSQLTransformationProvider_GetColumnContentSizeTests : PostgreSQLTransformationProviderTestBase { [Test] - public void GetColumnContentSize_DefaultValues_Succeeds() + public void GetColumnContentSize_UseStringColumn_MaxContentLengthIsCorrect() { // Arrange const string testTableName = "testtable"; - const string stringColumnName = "stringcolumn"; Provider.AddTable(testTableName, @@ -30,4 +30,22 @@ public void GetColumnContentSize_DefaultValues_Succeeds() // Assert Assert.That(columnContentSize, Is.EqualTo(4444)); } + + [Test] + public void GetColumnContentSize_UseOnNonStringColumn_ThrowsSpeakingException() + { + // Arrange + const string testTableName = "testtable"; + const string stringColumnName = "nonstringcolumn"; + + Provider.AddTable(testTableName, + new Column(stringColumnName, DbType.Int32) + ); + + // Act + var exception = Assert.Throws(() => Provider.GetColumnContentSize(testTableName, stringColumnName)); + + // Assert + Assert.That(exception.Message, Does.Contain("is not of type string")); + } } diff --git a/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs b/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs index f1b7ede..2c7da7f 100644 --- a/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs +++ b/src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs @@ -251,6 +251,23 @@ public override string[] GetTables() public override int GetColumnContentSize(string table, string columnName) { + if (!TableExists(table)) + { + throw new Exception($"Table '{table}' not found."); + } + + if (!ColumnExists(table, columnName, true)) + { + throw new Exception($"Column '{columnName}' does not exist"); + } + + var column = GetColumnByName(table, columnName); + + if (column.MigratorDbType != MigratorDbType.String) + { + throw new Exception($"Column '{columnName}' in table {table} is not of type string"); + } + var result = ExecuteScalar($"SELECT MAX(LENGTH({QuoteColumnNameIfRequired(columnName)})) FROM {QuoteTableNameIfRequired(table)}"); if (result == DBNull.Value)