With OrmLite, is there a way to automatically update table schema when my POCO is modified?

…衆ロ難τιáo~ 提交于 2019-12-03 12:57:38

No there is no current support for Auto Migration of RDBMS Schema's vs POCOs in ServiceStack's OrmLite.

There are currently a few threads being discussed in OrmLite's issues that are exploring the different ways to add this.

I created an extension method to automatically add missing columns to my tables. Been working great so far. Caveat: the code for getting the column names is SQL Server specific.

namespace System.Data
{
    public static class IDbConnectionExtensions
    {
        private static List<string> GetColumnNames(IDbConnection db, string tableName)
        {
            var columns = new List<string>();
            using (var cmd = db.CreateCommand())
            {
                cmd.CommandText = "exec sp_columns " + tableName;
                var reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    var ordinal = reader.GetOrdinal("COLUMN_NAME");
                    columns.Add(reader.GetString(ordinal));
                }
                reader.Close();
            }
            return columns;
        }

        public static void AlterTable<T>(this IDbConnection db) where T : new()
        {
            var model = ModelDefinition<T>.Definition;

            // just create the table if it doesn't already exist
            if (db.TableExists(model.ModelName) == false)
            {
                db.CreateTable<T>(overwrite: false);
                return;
            }

            // find each of the missing fields
            var columns = GetColumnNames(db, model.ModelName);
            var missing = ModelDefinition<T>.Definition.FieldDefinitions
                .Where(field => columns.Contains(field.FieldName) == false)
                .ToList();

            // add a new column for each missing field
            foreach (var field in missing)
            {
                var alterSql = string.Format("ALTER TABLE {0} ADD {1} {2}", 
                    model.ModelName,
                    field.FieldName, 
                    db.GetDialectProvider().GetColumnTypeDefinition(field.FieldType)
                    );
                Console.WriteLine(alterSql);
                db.ExecuteSql(alterSql);
            }
        }
    }
}
user44

Here is a slightly modified version of code from cornelha to work with PostgreSQL. Removed this fragment

        //private static List<string> GetColumnNames(object poco)
        //{
        //    var list = new List<string>();
        //    foreach (var prop in poco.GetType().GetProperties())
        //    {
        //        list.Add(prop.Name);
        //    }
        //    return list;
        //}

and used IOrmLiteDialectProvider.NamingStrategy.GetTableName and IOrmLiteDialectProvider.NamingStrategy.GetColumnName methods to convert table and column names from PascalNotation to this_kind_of_notation used by OrmLite when creating tables in PostgreSQL.

    public static class IDbConnectionExtensions
    {
        private static List<string> GetColumnNames(IDbConnection db, string tableName, IOrmLiteDialectProvider provider)
        {
            var columns = new List<string>();
            using (var cmd = db.CreateCommand())
            {
                cmd.CommandText = getCommandText(tableName, provider);
                var tbl = new DataTable();
                tbl.Load(cmd.ExecuteReader());
                for (int i = 0; i < tbl.Columns.Count; i++)
                {
                    columns.Add(tbl.Columns[i].ColumnName);
                }

            }
            return columns;
        }

        private static string getCommandText(string tableName, IOrmLiteDialectProvider provider)
        {

            if (provider == PostgreSqlDialect.Provider)

                return string.Format("select * from {0} limit 1", tableName);
            else return string.Format("select top 1 * from {0}", tableName);
        }

        public static void AlterTable<T>(this IDbConnection db, IOrmLiteDialectProvider provider) where T : new()
        {
            var model = ModelDefinition<T>.Definition;
            var table = new T();
            var namingStrategy = provider.NamingStrategy;
            // just create the table if it doesn't already exist
            var tableName = namingStrategy.GetTableName(model.ModelName);
            if (db.TableExists(tableName) == false)
            {
                db.CreateTable<T>(overwrite: false);
                return;
            }

            // find each of the missing fields
            var columns = GetColumnNames(db, model.ModelName, provider);
            var missing = ModelDefinition<T>.Definition.FieldDefinitions
                                            .Where(field => columns.Contains(namingStrategy.GetColumnName(field.FieldName)) == false)
                                            .ToList();

            // add a new column for each missing field
            foreach (var field in missing)
            {
                var columnName = namingStrategy.GetColumnName(field.FieldName);
                var alterSql = string.Format("ALTER TABLE {0} ADD COLUMN {1} {2}",
                                             tableName,
                                             columnName,
                                             db.GetDialectProvider().GetColumnTypeDefinition(field.FieldType)
                    );
                Console.WriteLine(alterSql);
                db.ExecuteSql(alterSql);
            }
        }
    }

I implemented an UpdateTable function. The basic idea is:

  1. Rename current table on database.
  2. Let OrmLite create the new schema.
  3. Copy the relevant data from the old table to the new.
  4. Drop the old table.

Github Repo: https://github.com/peheje/Extending-NServiceKit.OrmLite

Condensed code:

public interface ISqlProvider
    {
        string RenameTableSql(string currentName, string newName);
        string GetColumnNamesSql(string tableName);
        string InsertIntoSql(string intoTableName, string fromTableName, string commaSeparatedColumns);
        string DropTableSql(string tableName);
    }

public static void UpdateTable<T>(IDbConnection connection, ISqlProvider sqlProvider) where T : new()
        {
            connection.CreateTableIfNotExists<T>();
            var model = ModelDefinition<T>.Definition;
            string tableName = model.Name;
            string tableNameTmp = tableName + "Tmp";
            string renameTableSql = sqlProvider.RenameTableSql(tableName, tableNameTmp);
            connection.ExecuteNonQuery(renameTableSql);

            connection.CreateTable<T>();

            string getModelColumnsSql = sqlProvider.GetColumnNamesSql(tableName);
            var modelColumns = connection.SqlList<string>(getModelColumnsSql);
            string getDbColumnsSql = sqlProvider.GetColumnNamesSql(tableNameTmp);
            var dbColumns = connection.SqlList<string>(getDbColumnsSql);

            List<string> activeFields = dbColumns.Where(dbColumn => modelColumns.Contains(dbColumn)).ToList();

            string activeFieldsCommaSep = ListToCommaSeparatedString(activeFields);
            string insertIntoSql = sqlProvider.InsertIntoSql(tableName, tableNameTmp, activeFieldsCommaSep);

            connection.ExecuteSql(insertIntoSql);

            string dropTableSql = sqlProvider.DropTableSql(tableNameTmp);
            //connection.ExecuteSql(dropTableSql);  //maybe you want to clean up yourself, else uncomment
        }

        private static String ListToCommaSeparatedString(List<String> source)
        {
            var sb = new StringBuilder();
            for (int i = 0; i < source.Count; i++)
            {
                sb.Append(source[i]);
                if (i < source.Count - 1)
                {
                    sb.Append(", ");
                }
            }
            return sb.ToString();
        }
    }

MySql implementation:

public class MySqlProvider : ISqlProvider
    {
        public string RenameTableSql(string currentName, string newName)
        {
            return "RENAME TABLE `" + currentName + "` TO `" + newName + "`;";
        }

        public string GetColumnNamesSql(string tableName)
        {
            return "SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" + tableName + "';";
        }

        public string InsertIntoSql(string intoTableName, string fromTableName, string commaSeparatedColumns)
        {
            return "INSERT INTO `" + intoTableName + "` (" + commaSeparatedColumns + ") SELECT " + commaSeparatedColumns + " FROM `" + fromTableName + "`;";
        }

        public string DropTableSql(string tableName)
        {
            return "DROP TABLE `" + tableName + "`;";
        }
    }

Usage:

 using (var db = dbFactory.OpenDbConnection())
 {
     DbUpdate.UpdateTable<SimpleData>(db, new MySqlProvider());
 }

Haven't tested with FKs. Can't handle renaming properties.

I needed to implement something similiar and found the post by Scott very helpful. I decided to make a small change which will make it much more agnostic. Since I only use Sqlite and MSSQL, I made the getCommand method very simple, but can be extended. I used a simple datatable to get the columns. This solution works perfectly for my requirements.

    public static class IDbConnectionExtensions
{
    private static List<string> GetColumnNames(IDbConnection db, string tableName,IOrmLiteDialectProvider provider)
    {
        var columns = new List<string>();
        using (var cmd = db.CreateCommand())
        {
            cmd.CommandText = getCommandText(tableName, provider);
            var tbl = new DataTable();
            tbl.Load(cmd.ExecuteReader());
            for (int i = 0; i < tbl.Columns.Count; i++)
            {
                columns.Add(tbl.Columns[i].ColumnName);
            }

        }
        return columns;
    }

    private static string getCommandText(string tableName,  IOrmLiteDialectProvider provider)
    {

        if(provider ==  SqliteDialect.Provider)

        return string.Format("select * from {0} limit 1", tableName);
        else return string.Format("select top 1 * from {0}", tableName);
    }

    private static List<string> GetColumnNames(object poco)
    {
        var list = new List<string>();
        foreach (var prop in poco.GetType().GetProperties())
        {
            list.Add(prop.Name);
        }
        return list;
    }

    public static void AlterTable<T>(this IDbConnection db, IOrmLiteDialectProvider provider) where T : new()
    {
        var model = ModelDefinition<T>.Definition;
        var table = new T();
        // just create the table if it doesn't already exist
        if (db.TableExists(model.ModelName) == false)
        {
            db.CreateTable<T>(overwrite: false);
            return;
        }

        // find each of the missing fields
        var columns = GetColumnNames(db, model.ModelName,provider);
        var missing = ModelDefinition<T>.Definition.FieldDefinitions
                                        .Where(field => columns.Contains(field.FieldName) == false)
                                        .ToList();

        // add a new column for each missing field
        foreach (var field in missing)
        {
            var alterSql = string.Format("ALTER TABLE {0} ADD {1} {2}",
                                         model.ModelName,
                                         field.FieldName,
                                         db.GetDialectProvider().GetColumnTypeDefinition(field.FieldType)
                );
            Console.WriteLine(alterSql);
            db.ExecuteSql(alterSql);
        }
    }
}

So I took user44 answer, and modified the AlterTable method to make it a bit more efficient. Instead of looping and running one SQL query per field/column, I merge it into one with some simple text parsing (MySQL commands!).

        public static void AlterTable<T>(this IDbConnection db, IOrmLiteDialectProvider provider) where T : new()
        {
            var model = ModelDefinition<T>.Definition;
            var table = new T();
            var namingStrategy = provider.NamingStrategy;
            // just create the table if it doesn't already exist
            var tableName = namingStrategy.GetTableName(model.ModelName);
            if (db.TableExists(tableName) == false)
            {
                db.CreateTable<T>(overwrite: false);
                return;
            }

            // find each of the missing fields
            var columns = GetColumnNames(db, model.ModelName, provider);
            var missing = ModelDefinition<T>.Definition.FieldDefinitions
                                            .Where(field => columns.Contains(namingStrategy.GetColumnName(field.FieldName)) == false)
                                            .ToList();
            string alterSql = "";
            string addSql = "";
            // add a new column for each missing field
            foreach (var field in missing)
            {
                var alt = db.GetDialectProvider().ToAddColumnStatement(typeof(T), field); // Should be made more efficient, one query for all changes instead of many
                int index = alt.IndexOf("ADD ");
                alterSql = alt.Substring(0, index);
                addSql += alt.Substring(alt.IndexOf("ADD COLUMN")).Replace(";", "") + ", ";
            }
            if (addSql.Length > 2)
                addSql = addSql.Substring(0, addSql.Length - 2);
            string fullSql = alterSql + addSql;
            Console.WriteLine(fullSql);
            db.ExecuteSql(fullSql);
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!