Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Simon Dingley 1474 posts 3431 karma points c-trib
    Aug 16, 2016 @ 12:35
    Simon Dingley
    0

    Checking existing Column Size in a custom Migration

    I am working on a migration and need to check the size/length of an existing varchar column in a class inheriting from DatabaseSchemaResult.

    I can check the column exists:

    this.ValidColumns.Contains("myTableName,description")
    

    Great!

    I then attempt to check the size:

    this.TableDefinitions.FirstOrDefault(x => x.Name == "myTableName")
                       .Columns.FirstOrDefault(c => c.Name == "description")
                       .Size != 500)
    

    and this is where it fails since the Size is zero for all columns?

    Can anyone point me in the right direction please?

    Thanks, Simon

  • Simon Dingley 1474 posts 3431 karma points c-trib
    Nov 07, 2016 @ 09:48
    Simon Dingley
    100

    In order to provide a solution for anyone else looking to do this here is what was implemented:

    An extension method was added for Umbraco.Core.Persistence.Database objects as follows:

    internal static int GetDbTableColumnSize(this Database database, string tableName, string columnName)
    {
        var sql = new Sql("SELECT character_maximum_length FROM information_schema.columns WHERE table_name = @table AND column_name = @column", new { @table = tableName, @column = columnName });
        return database.ExecuteScalar<int>(sql);
    }
    

    Which was then used as follows:

    var size = database.GetDbTableColumnSize("tableName", "columnName");
    
Please Sign in or register to post replies

Write your reply to:

Draft