Issue 141 in migratordotnet: SQLite.NET problems with adding primary key

2 views
Skip to first unread message

codesite...@google.com

unread,
Mar 11, 2010, 11:43:52 PM3/11/10
to migratordo...@googlegroups.com
Status: New
Owner: ----

New issue 141 by it.adviser.ua: SQLite.NET problems with adding primary key
http://code.google.com/p/migratordotnet/issues/detail?id=141

---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));
}


--
You received this message because you are listed in the owner
or CC fields of this issue, or because you starred this issue.
You may adjust your issue notification preferences at:
http://code.google.com/hosting/settings

Reply all
Reply to author
Forward
0 new messages