Skip to content

Commit 8a971ac

Browse files
JaBistDuNarrischJaBistDuNarrisch
authored andcommitted
Override GetColumnContentSize for Postgre SQL
1 parent 94ad5ec commit 8a971ac

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System.Data;
2+
using DotNetProjects.Migrator.Framework;
3+
using NUnit.Framework;
4+
5+
namespace Migrator.Tests.Providers.PostgreSQL;
6+
7+
[TestFixture]
8+
[Category("Postgre")]
9+
public class PostgreSQLTransformationProvider_GetColumnContentSizeTests : PostgreSQLTransformationProviderTestBase
10+
{
11+
private const decimal DecimalDefaultValue = 14.56565m;
12+
13+
[Test]
14+
public void GetColumnContentSize_DefaultValues_Succeeds()
15+
{
16+
// Arrange
17+
const string testTableName = "testtable";
18+
19+
const string stringColumnName = "stringcolumn";
20+
21+
Provider.AddTable(testTableName,
22+
new Column(stringColumnName, DbType.String, 5000)
23+
);
24+
25+
Provider.Insert(testTableName, [stringColumnName], [new string('A', 44)]);
26+
Provider.Insert(testTableName, [stringColumnName], [new string('B', 444)]);
27+
Provider.Insert(testTableName, [stringColumnName], [new string('C', 4444)]);
28+
29+
// Act
30+
var columnContentSize = Provider.GetColumnContentSize(testTableName, stringColumnName);
31+
32+
// Assert
33+
Assert.That(columnContentSize, Is.EqualTo(4444));
34+
}
35+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,18 @@ public override string[] GetTables()
249249
return tables.ToArray();
250250
}
251251

252+
public override int GetColumnContentSize(string table, string columnName)
253+
{
254+
var result = ExecuteScalar($"SELECT MAX(LENGTH({QuoteColumnNameIfRequired(columnName)})) FROM {QuoteTableNameIfRequired(table)}");
255+
256+
if (result == DBNull.Value)
257+
{
258+
return 0;
259+
}
260+
261+
return Convert.ToInt32(result);
262+
}
263+
252264
public override Column[] GetColumns(string table)
253265
{
254266
var stringBuilder = new StringBuilder();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ FROM sys.[indexes] Ind
276276

277277
if (!reader.IsDBNull(6))
278278
{
279-
idx.KeyColumns = (reader.GetString(6).Split(','));
279+
idx.KeyColumns = reader.GetString(6).Split(',');
280280
}
281281
if (!reader.IsDBNull(7))
282282
{
283-
idx.IncludeColumns = (reader.GetString(7).Split(','));
283+
idx.IncludeColumns = reader.GetString(7).Split(',');
284284
}
285285

286286
retVal.Add(idx);
@@ -293,7 +293,7 @@ FROM sys.[indexes] Ind
293293

294294
public override int GetColumnContentSize(string table, string columnName)
295295
{
296-
var result = this.ExecuteScalar("SELECT MAX(LEN(" + this.QuoteColumnNameIfRequired(columnName) + ")) FROM " + this.QuoteTableNameIfRequired(table));
296+
var result = ExecuteScalar("SELECT MAX(LEN(" + this.QuoteColumnNameIfRequired(columnName) + ")) FROM " + this.QuoteTableNameIfRequired(table));
297297

298298
if (result == DBNull.Value)
299299
{

0 commit comments

Comments
 (0)