public void MigrateTables()
{
// If we dont need updated, what are we doing here?
if (!NeedsUpdated) return;
// MD5 Hash Update, only update to date so i will make this better in the future
if(Version.Major == 2 && Version.Minor == 0)
{
// In this update, we hash all of the passwords to MD5!
using (DbTransaction Transaction = base.BeginTransaction())
{
try
{
// Set the column length to 32, since this is the MD5 hash length
if (base.DatabaseEngine == DatabaseEngine.Mysql)
base.Execute("ALTER TABLE accounts MODIFY COLUMN password VARCHAR(32) NOT NULL");
// Here we use the QueryReader method to pull 1 row at a time, using memory efficiently
foreach (Dictionary<string, object> Row in base.Query("SELECT id, password FROM accounts"))
{
base.Execute("UPDATE accounts SET password=@P0 WHERE id=@P1",
Row["password"].ToString().GetMD5Hash(false),
Row["id"]
);
}
// Delete old version data
base.Execute("DELETE FROM _version");
// Run the alter table command on MySql, SQLite does not support this
if (base.DatabaseEngine == DatabaseEngine.Mysql)
{
base.Execute("ALTER TABLE _version MODIFY dbver VARCHAR(4)");
}
else
{
base.Execute("DROP TABLE IF EXISTS \"main\".\"_version\"");
base.Execute("CREATE TABLE \"main\".\"_version\"(\"dbver\" TEXT NOT NULL DEFAULT 0, \"dbdate\" INT NOT NULL DEFAULT 0, PRIMARY KEY (\"dbver\"));");
}
// Insert New Data
base.Execute("INSERT INTO _version(dbver, dbdate) VALUES (@P0, @P1)", "2.1", DateTime.UtcNow.ToUnixTimestamp());
Transaction.Commit();
// Set new version
Version = new Version(2, 1);
}
catch
{
Transaction.Rollback();
throw;
}
}
}
}