Skip to content

Throw if GetColumnContentSize column is not of type string. #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Data;
using DotNetProjects.Migrator.Framework;
using NUnit.Framework;
Expand All @@ -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,
Expand All @@ -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<Exception>(() => Provider.GetColumnContentSize(testTableName, stringColumnName));

// Assert
Assert.That(exception.Message, Does.Contain("is not of type string"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,9 +172,9 @@
{
var oldColumn = GetColumnByName(table, column.Name);

var isUniqueSet = column.ColumnProperty.IsSet(ColumnProperty.Unique);

Check warning on line 175 in src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

'ColumnProperty.Unique' is obsolete: 'Use method 'AddUniqueConstraint' instead. This is marked being obsolete since you cannot add a name for the constraint which makes it difficult to remove the constraint again.'

column.ColumnProperty = column.ColumnProperty.Clear(ColumnProperty.Unique);

Check warning on line 177 in src/Migrator/Providers/Impl/PostgreSQL/PostgreSQLTransformationProvider.cs

View workflow job for this annotation

GitHub Actions / build

'ColumnProperty.Unique' is obsolete: 'Use method 'AddUniqueConstraint' instead. This is marked being obsolete since you cannot add a name for the constraint which makes it difficult to remove the constraint again.'

var mapper = _dialect.GetAndMapColumnProperties(column);

Expand Down Expand Up @@ -251,6 +251,23 @@

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)
Expand Down
Loading