LinuxDoku / migratordotnet

Automatically exported from code.google.com/p/migratordotnet
0 stars 0 forks source link

SQLite.NET problems with adding primary key #141

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
---What steps will reproduce the problem?
1. Create SQLite DB. Create any table without primary key.
2. Create migration with adding primary key. 

---What is the expected output? What do you see instead?
SQLite don't support adding primary key on existing table. You need to 
recreate table instead. 

---Please use labels and text to provide additional information.

add next code to SQLiteTransformationProvider.cs
        /// <summary>
        /// Append a primary key to a table.
        /// </summary>
        /// <param name="name">Constraint name</param>
        /// <param name="table">Table name</param>
        /// <param name="columns">Primary column names</param>
        public override void AddPrimaryKey(string name, string table, 
params string[] columns)
        {
            if (ConstraintExists(table, name))
            {
                Logger.Warn("Primary key {0} already exists", name);
                return;
            }

            string[] origColDefs = GetColumnDefs(table);
            List<string> colDefs = new List<string>();
            //TODO: if we don't commit changes yet, column may not exist
            //foreach (string keyCol in columns)
            //{
            //    bool founded = false;
            //    foreach (string col in origColDefs)
            //    {
            //        if (col.ToLower().Equals(keyCol.ToLower()))
            //        {
            //            founded = true;
            //        }
            //    }
            //    if (!founded)
            //    {
            //        Logger.Warn("Column {0} not founded. Can't create 
primary key with not existing column", keyCol);
            //        return;
            //    }
            //}
            foreach (string col in origColDefs)
                colDefs.Add(col);
            colDefs.Add(String.Format("primary key({0})", String.Join(",", 
columns)));
            string[] newColDefs = colDefs.ToArray();
            string colDefsSql = String.Join(",", newColDefs);
            string[] colNames = ParseSqlForColumnNames(origColDefs);
            string colNamesSql = String.Join(",", colNames);

            AddTable(table + "_temp", null, colDefsSql);
            ExecuteQuery(String.Format("INSERT INTO {0}_temp SELECT {1} 
FROM {0}", table, colNamesSql));
            RemoveTable(table);
            ExecuteQuery(String.Format("ALTER TABLE {0}_temp RENAME TO 
{0}", table));
        }

Original issue reported on code.google.com by it.advis...@gmail.com on 12 Mar 2010 at 4:43