check in working migration code fore SQLite. This

is now using migrations instead of the old model to
create tables.  Tested for existing old tables, 
and for creating new ones.
0.6.0-stable
Sean Dague 2008-06-11 21:01:33 +00:00
parent 68b98aecab
commit 6c1fce6147
5 changed files with 260 additions and 198 deletions

View File

@ -159,7 +159,13 @@ namespace OpenSim.Data
public int Version public int Version
{ {
get { return FindVersion(_type); } get { return FindVersion(_type); }
set { UpdateVersion(_type, value); } set {
if (Version < 1) {
InsertVersion(_type, value);
} else {
UpdateVersion(_type, value);
}
}
} }
private int FindVersion(string type) private int FindVersion(string type)

View File

@ -64,7 +64,19 @@ namespace OpenSim.Data.SQLite
} }
m_conn = new SqliteConnection(dbconnect); m_conn = new SqliteConnection(dbconnect);
m_conn.Open(); m_conn.Open();
TestTables(m_conn);
Assembly assem = GetType().Assembly;
Migration m = new Migration(m_conn, assem, "AssetStore");
// TODO: remove this next line after changeset 6000,
// people should have all gotten into the migration swing
// again.
TestTables(m_conn, m);
m.Update();
return; return;
} }
@ -258,7 +270,7 @@ namespace OpenSim.Data.SQLite
pcmd.ExecuteNonQuery(); pcmd.ExecuteNonQuery();
} }
private static bool TestTables(SqliteConnection conn) private static bool TestTables(SqliteConnection conn, Migration m)
{ {
SqliteCommand cmd = new SqliteCommand(assetSelect, conn); SqliteCommand cmd = new SqliteCommand(assetSelect, conn);
SqliteDataAdapter pDa = new SqliteDataAdapter(cmd); SqliteDataAdapter pDa = new SqliteDataAdapter(cmd);
@ -270,8 +282,14 @@ namespace OpenSim.Data.SQLite
catch (SqliteSyntaxException) catch (SqliteSyntaxException)
{ {
m_log.Info("[ASSET DB]: SQLite Database doesn't exist... creating"); m_log.Info("[ASSET DB]: SQLite Database doesn't exist... creating");
InitDB(conn); return false;
} }
// if the tables are here, and we don't have a migration,
// set it to 1, as we're migrating off of legacy bits
if (m.Version == 0)
m.Version = 1;
return true; return true;
} }

View File

@ -61,7 +61,12 @@ namespace OpenSim.Data.SQLite
conn.Open(); conn.Open();
TestTables(conn); Assembly assem = GetType().Assembly;
Migration m = new Migration(conn, assem, "InventoryStore");
// TODO: remove this line after changeset 6000
TestTables(conn, m);
m.Update();
SqliteCommand itemsSelectCmd = new SqliteCommand(invItemsSelect, conn); SqliteCommand itemsSelectCmd = new SqliteCommand(invItemsSelect, conn);
invItemsDa = new SqliteDataAdapter(itemsSelectCmd); invItemsDa = new SqliteDataAdapter(itemsSelectCmd);
@ -672,7 +677,7 @@ namespace OpenSim.Data.SQLite
scmd.ExecuteNonQuery(); scmd.ExecuteNonQuery();
} }
private static bool TestTables(SqliteConnection conn) private static bool TestTables(SqliteConnection conn, Migration m)
{ {
SqliteCommand invItemsSelectCmd = new SqliteCommand(invItemsSelect, conn); SqliteCommand invItemsSelectCmd = new SqliteCommand(invItemsSelect, conn);
SqliteDataAdapter pDa = new SqliteDataAdapter(invItemsSelectCmd); SqliteDataAdapter pDa = new SqliteDataAdapter(invItemsSelectCmd);
@ -688,63 +693,68 @@ namespace OpenSim.Data.SQLite
catch (SqliteSyntaxException) catch (SqliteSyntaxException)
{ {
m_log.Info("[INVENTORY DB]: SQLite Database doesn't exist... creating"); m_log.Info("[INVENTORY DB]: SQLite Database doesn't exist... creating");
InitDB(conn); return false;
} }
pDa.Fill(tmpDS, "inventoryitems"); if (m.Version == 0)
sDa.Fill(tmpDS, "inventoryfolders"); m.Version = 1;
// Very clumsy way of checking whether we need to upgrade the database table version and then updating. Only
// putting up with this because this code should be blown away soon by nhibernate...
conn.Open();
SqliteCommand cmd;
try
{
cmd = new SqliteCommand("select salePrice from inventoryitems limit 1;", conn);
cmd.ExecuteNonQuery();
}
catch (SqliteSyntaxException)
{
m_log.Info("[INVENTORY DB]: Upgrading sqlite inventory database to version 2");
cmd = new SqliteCommand("alter table inventoryitems add column salePrice integer default 99;", conn);
cmd.ExecuteNonQuery();
cmd = new SqliteCommand("alter table inventoryitems add column saleType integer default 0;", conn);
cmd.ExecuteNonQuery();
cmd = new SqliteCommand("alter table inventoryitems add column creationDate integer default 2000;", conn);
cmd.ExecuteNonQuery();
cmd = new SqliteCommand("alter table inventoryitems add column groupID varchar(255) default '00000000-0000-0000-0000-000000000000';", conn);
cmd.ExecuteNonQuery();
cmd = new SqliteCommand("alter table inventoryitems add column groupOwned integer default 0;", conn);
cmd.ExecuteNonQuery();
cmd = new SqliteCommand("alter table inventoryitems add column flags integer default 0;", conn);
cmd.ExecuteNonQuery();
pDa.Fill(tmpDS, "inventoryitems");
}
finally
{
conn.Close();
}
foreach (DataColumn col in createInventoryItemsTable().Columns)
{
if (! tmpDS.Tables["inventoryitems"].Columns.Contains(col.ColumnName))
{
m_log.Info("[INVENTORY DB]: Missing required column:" + col.ColumnName);
return false;
}
}
foreach (DataColumn col in createInventoryFoldersTable().Columns)
{
if (! tmpDS.Tables["inventoryfolders"].Columns.Contains(col.ColumnName))
{
m_log.Info("[INVENTORY DB]: Missing required column:" + col.ColumnName);
return false;
}
}
return true; return true;
// pDa.Fill(tmpDS, "inventoryitems");
// sDa.Fill(tmpDS, "inventoryfolders");
// // Very clumsy way of checking whether we need to upgrade the database table version and then updating. Only
// // putting up with this because this code should be blown away soon by nhibernate...
// conn.Open();
// SqliteCommand cmd;
// try
// {
// cmd = new SqliteCommand("select salePrice from inventoryitems limit 1;", conn);
// cmd.ExecuteNonQuery();
// }
// catch (SqliteSyntaxException)
// {
// m_log.Info("[INVENTORY DB]: Upgrading sqlite inventory database to version 2");
// cmd = new SqliteCommand("alter table inventoryitems add column salePrice integer default 99;", conn);
// cmd.ExecuteNonQuery();
// cmd = new SqliteCommand("alter table inventoryitems add column saleType integer default 0;", conn);
// cmd.ExecuteNonQuery();
// cmd = new SqliteCommand("alter table inventoryitems add column creationDate integer default 2000;", conn);
// cmd.ExecuteNonQuery();
// cmd = new SqliteCommand("alter table inventoryitems add column groupID varchar(255) default '00000000-0000-0000-0000-000000000000';", conn);
// cmd.ExecuteNonQuery();
// cmd = new SqliteCommand("alter table inventoryitems add column groupOwned integer default 0;", conn);
// cmd.ExecuteNonQuery();
// cmd = new SqliteCommand("alter table inventoryitems add column flags integer default 0;", conn);
// cmd.ExecuteNonQuery();
// pDa.Fill(tmpDS, "inventoryitems");
// }
// finally
// {
// conn.Close();
// }
// foreach (DataColumn col in createInventoryItemsTable().Columns)
// {
// if (! tmpDS.Tables["inventoryitems"].Columns.Contains(col.ColumnName))
// {
// m_log.Info("[INVENTORY DB]: Missing required column:" + col.ColumnName);
// return false;
// }
// }
// foreach (DataColumn col in createInventoryFoldersTable().Columns)
// {
// if (! tmpDS.Tables["inventoryfolders"].Columns.Contains(col.ColumnName))
// {
// m_log.Info("[INVENTORY DB]: Missing required column:" + col.ColumnName);
// return false;
// }
// }
// return true;
} }
} }
} }

View File

@ -84,6 +84,8 @@ namespace OpenSim.Data.SQLite
m_conn = new SqliteConnection(m_connectionString); m_conn = new SqliteConnection(m_connectionString);
m_conn.Open(); m_conn.Open();
SqliteCommand primSelectCmd = new SqliteCommand(primSelect, m_conn); SqliteCommand primSelectCmd = new SqliteCommand(primSelect, m_conn);
primDa = new SqliteDataAdapter(primSelectCmd); primDa = new SqliteDataAdapter(primSelectCmd);
// SqliteCommandBuilder primCb = new SqliteCommandBuilder(primDa); // SqliteCommandBuilder primCb = new SqliteCommandBuilder(primDa);
@ -104,10 +106,15 @@ namespace OpenSim.Data.SQLite
SqliteCommand landAccessListSelectCmd = new SqliteCommand(landAccessListSelect, m_conn); SqliteCommand landAccessListSelectCmd = new SqliteCommand(landAccessListSelect, m_conn);
landAccessListDa = new SqliteDataAdapter(landAccessListSelectCmd); landAccessListDa = new SqliteDataAdapter(landAccessListSelectCmd);
// We fill the data set, now we've got copies in memory for the information // This actually does the roll forward assembly stuff
// TODO: see if the linkage actually holds. Assembly assem = GetType().Assembly;
// primDa.FillSchema(ds, SchemaType.Source, "PrimSchema"); Migration m = new Migration(m_conn, assem, "RegionStore");
TestTables(m_conn);
// TODO: After rev 6000, remove this. People should have
// been rolled onto the new migration code by then.
TestTables(m_conn, m);
m.Update();
lock (ds) lock (ds)
{ {
@ -1520,81 +1527,81 @@ namespace OpenSim.Data.SQLite
/// Create the necessary database tables. /// Create the necessary database tables.
/// </summary> /// </summary>
/// <param name="conn"></param> /// <param name="conn"></param>
private void InitDB(SqliteConnection conn) // private void InitDB(SqliteConnection conn)
{ // {
string createPrims = defineTable(createPrimTable()); // string createPrims = defineTable(createPrimTable());
string createShapes = defineTable(createShapeTable()); // string createShapes = defineTable(createShapeTable());
string createItems = defineTable(createItemsTable()); // string createItems = defineTable(createItemsTable());
string createTerrain = defineTable(createTerrainTable()); // string createTerrain = defineTable(createTerrainTable());
string createLand = defineTable(createLandTable()); // string createLand = defineTable(createLandTable());
string createLandAccessList = defineTable(createLandAccessListTable()); // string createLandAccessList = defineTable(createLandAccessListTable());
SqliteCommand pcmd = new SqliteCommand(createPrims, conn); // SqliteCommand pcmd = new SqliteCommand(createPrims, conn);
SqliteCommand scmd = new SqliteCommand(createShapes, conn); // SqliteCommand scmd = new SqliteCommand(createShapes, conn);
SqliteCommand icmd = new SqliteCommand(createItems, conn); // SqliteCommand icmd = new SqliteCommand(createItems, conn);
SqliteCommand tcmd = new SqliteCommand(createTerrain, conn); // SqliteCommand tcmd = new SqliteCommand(createTerrain, conn);
SqliteCommand lcmd = new SqliteCommand(createLand, conn); // SqliteCommand lcmd = new SqliteCommand(createLand, conn);
SqliteCommand lalcmd = new SqliteCommand(createLandAccessList, conn); // SqliteCommand lalcmd = new SqliteCommand(createLandAccessList, conn);
try // try
{ // {
pcmd.ExecuteNonQuery(); // pcmd.ExecuteNonQuery();
} // }
catch (SqliteSyntaxException) // catch (SqliteSyntaxException)
{ // {
m_log.Warn("[REGION DB]: Primitives Table Already Exists"); // m_log.Warn("[REGION DB]: Primitives Table Already Exists");
} // }
try // try
{ // {
scmd.ExecuteNonQuery(); // scmd.ExecuteNonQuery();
} // }
catch (SqliteSyntaxException) // catch (SqliteSyntaxException)
{ // {
m_log.Warn("[REGION DB]: Shapes Table Already Exists"); // m_log.Warn("[REGION DB]: Shapes Table Already Exists");
} // }
if (persistPrimInventories) // if (persistPrimInventories)
{ // {
try // try
{ // {
icmd.ExecuteNonQuery(); // icmd.ExecuteNonQuery();
} // }
catch (SqliteSyntaxException) // catch (SqliteSyntaxException)
{ // {
m_log.Warn("[REGION DB]: Primitives Inventory Table Already Exists"); // m_log.Warn("[REGION DB]: Primitives Inventory Table Already Exists");
} // }
} // }
try // try
{ // {
tcmd.ExecuteNonQuery(); // tcmd.ExecuteNonQuery();
} // }
catch (SqliteSyntaxException) // catch (SqliteSyntaxException)
{ // {
m_log.Warn("[REGION DB]: Terrain Table Already Exists"); // m_log.Warn("[REGION DB]: Terrain Table Already Exists");
} // }
try // try
{ // {
lcmd.ExecuteNonQuery(); // lcmd.ExecuteNonQuery();
} // }
catch (SqliteSyntaxException) // catch (SqliteSyntaxException)
{ // {
m_log.Warn("[REGION DB]: Land Table Already Exists"); // m_log.Warn("[REGION DB]: Land Table Already Exists");
} // }
try // try
{ // {
lalcmd.ExecuteNonQuery(); // lalcmd.ExecuteNonQuery();
} // }
catch (SqliteSyntaxException) // catch (SqliteSyntaxException)
{ // {
m_log.Warn("[SQLITE]: LandAccessList Table Already Exists"); // m_log.Warn("[SQLITE]: LandAccessList Table Already Exists");
} // }
} // }
private bool TestTables(SqliteConnection conn) private bool TestTables(SqliteConnection conn, Migration m)
{ {
SqliteCommand primSelectCmd = new SqliteCommand(primSelect, conn); SqliteCommand primSelectCmd = new SqliteCommand(primSelect, conn);
SqliteDataAdapter pDa = new SqliteDataAdapter(primSelectCmd); SqliteDataAdapter pDa = new SqliteDataAdapter(primSelectCmd);
@ -1630,65 +1637,72 @@ namespace OpenSim.Data.SQLite
catch (SqliteSyntaxException) catch (SqliteSyntaxException)
{ {
m_log.Info("[DATASTORE]: SQLite Database doesn't exist... creating"); m_log.Info("[DATASTORE]: SQLite Database doesn't exist... creating");
InitDB(conn); return false;
} }
pDa.Fill(tmpDS, "prims"); // if we've gotten this far, and our version is still 0,
sDa.Fill(tmpDS, "primshapes"); // it's because the migration was never done, so
// initialize to 1 just to sync up to where we should be.
if (persistPrimInventories) if (m.Version == 0)
iDa.Fill(tmpDS, "primitems"); m.Version = 1;
tDa.Fill(tmpDS, "terrain"); // pDa.Fill(tmpDS, "prims");
lDa.Fill(tmpDS, "land"); // sDa.Fill(tmpDS, "primshapes");
lalDa.Fill(tmpDS, "landaccesslist");
foreach (DataColumn col in createPrimTable().Columns) // if (persistPrimInventories)
{ // iDa.Fill(tmpDS, "primitems");
if (!tmpDS.Tables["prims"].Columns.Contains(col.ColumnName))
{
m_log.Info("[REGION DB]: Missing required column:" + col.ColumnName);
return false;
}
}
foreach (DataColumn col in createShapeTable().Columns) // tDa.Fill(tmpDS, "terrain");
{ // lDa.Fill(tmpDS, "land");
if (!tmpDS.Tables["primshapes"].Columns.Contains(col.ColumnName)) // lalDa.Fill(tmpDS, "landaccesslist");
{
m_log.Info("[REGION DB]: Missing required column:" + col.ColumnName);
return false;
}
}
// XXX primitems should probably go here eventually // foreach (DataColumn col in createPrimTable().Columns)
// {
// if (!tmpDS.Tables["prims"].Columns.Contains(col.ColumnName))
// {
// m_log.Info("[REGION DB]: Missing required column:" + col.ColumnName);
// return false;
// }
// }
foreach (DataColumn col in createTerrainTable().Columns) // foreach (DataColumn col in createShapeTable().Columns)
{ // {
if (!tmpDS.Tables["terrain"].Columns.Contains(col.ColumnName)) // if (!tmpDS.Tables["primshapes"].Columns.Contains(col.ColumnName))
{ // {
m_log.Info("[REGION DB]: Missing require column:" + col.ColumnName); // m_log.Info("[REGION DB]: Missing required column:" + col.ColumnName);
return false; // return false;
} // }
} // }
foreach (DataColumn col in createLandTable().Columns) // // XXX primitems should probably go here eventually
{
if (!tmpDS.Tables["land"].Columns.Contains(col.ColumnName))
{
m_log.Info("[REGION DB]: Missing require column:" + col.ColumnName);
return false;
}
}
foreach (DataColumn col in createLandAccessListTable().Columns) // foreach (DataColumn col in createTerrainTable().Columns)
{ // {
if (!tmpDS.Tables["landaccesslist"].Columns.Contains(col.ColumnName)) // if (!tmpDS.Tables["terrain"].Columns.Contains(col.ColumnName))
{ // {
m_log.Info("[DATASTORE]: Missing require column:" + col.ColumnName); // m_log.Info("[REGION DB]: Missing require column:" + col.ColumnName);
return false; // return false;
} // }
} // }
// foreach (DataColumn col in createLandTable().Columns)
// {
// if (!tmpDS.Tables["land"].Columns.Contains(col.ColumnName))
// {
// m_log.Info("[REGION DB]: Missing require column:" + col.ColumnName);
// return false;
// }
// }
// foreach (DataColumn col in createLandAccessListTable().Columns)
// {
// if (!tmpDS.Tables["landaccesslist"].Columns.Contains(col.ColumnName))
// {
// m_log.Info("[DATASTORE]: Missing require column:" + col.ColumnName);
// return false;
// }
// }
return true; return true;
} }

View File

@ -72,12 +72,20 @@ namespace OpenSim.Data.SQLite
connect = "URI=file:userprofiles.db,version=3"; connect = "URI=file:userprofiles.db,version=3";
SqliteConnection conn = new SqliteConnection(connect); SqliteConnection conn = new SqliteConnection(connect);
TestTables(conn);
// This sucks, but It doesn't seem to work with the dataset Syncing :P // This sucks, but It doesn't seem to work with the dataset Syncing :P
g_conn = conn; g_conn = conn;
g_conn.Open(); g_conn.Open();
Assembly assem = GetType().Assembly;
Migration m = new Migration(g_conn, assem, "UserStore");
// TODO: remove this after rev 6000
TestTables(conn, m);
m.Update();
ds = new DataSet(); ds = new DataSet();
da = new SqliteDataAdapter(new SqliteCommand(userSelect, conn)); da = new SqliteDataAdapter(new SqliteCommand(userSelect, conn));
daf = new SqliteDataAdapter(new SqliteCommand(userFriendsSelect, conn)); daf = new SqliteDataAdapter(new SqliteCommand(userFriendsSelect, conn));
@ -824,7 +832,7 @@ namespace OpenSim.Data.SQLite
conn.Close(); conn.Close();
} }
private static bool TestTables(SqliteConnection conn) private static bool TestTables(SqliteConnection conn, Migration m)
{ {
SqliteCommand cmd = new SqliteCommand(userSelect, conn); SqliteCommand cmd = new SqliteCommand(userSelect, conn);
SqliteCommand fcmd = new SqliteCommand(userFriendsSelect, conn); SqliteCommand fcmd = new SqliteCommand(userFriendsSelect, conn);
@ -842,26 +850,32 @@ namespace OpenSim.Data.SQLite
catch (SqliteSyntaxException) catch (SqliteSyntaxException)
{ {
m_log.Info("[USER DB]: SQLite Database doesn't exist... creating"); m_log.Info("[USER DB]: SQLite Database doesn't exist... creating");
InitDB(conn); return false;
}
conn.Open();
try
{
cmd = new SqliteCommand("select webLoginKey from users limit 1;", conn);
cmd.ExecuteNonQuery();
}
catch (SqliteSyntaxException)
{
cmd = new SqliteCommand("alter table users add column webLoginKey text default '00000000-0000-0000-0000-000000000000';", conn);
cmd.ExecuteNonQuery();
pDa.Fill(tmpDS, "users");
}
finally
{
conn.Close();
} }
if (m.Version == 0)
m.Version = 1;
return true; return true;
// conn.Open();
// try
// {
// cmd = new SqliteCommand("select webLoginKey from users limit 1;", conn);
// cmd.ExecuteNonQuery();
// }
// catch (SqliteSyntaxException)
// {
// cmd = new SqliteCommand("alter table users add column webLoginKey text default '00000000-0000-0000-0000-000000000000';", conn);
// cmd.ExecuteNonQuery();
// pDa.Fill(tmpDS, "users");
// }
// finally
// {
// conn.Close();
// }
// return true;
} }
} }
} }