update of migration code to be more sane on version

tracking, and support sub types that we'll need for
nhibernate.
0.6.0-stable
Sean Dague 2008-06-10 22:57:20 +00:00
parent fb9a338bc4
commit 68cda63761
1 changed files with 37 additions and 40 deletions

View File

@ -76,6 +76,7 @@ namespace OpenSim.Data
private DbConnection _conn; private DbConnection _conn;
private string _subtype; private string _subtype;
private Assembly _assem; private Assembly _assem;
private Regex _match;
private static readonly string _migrations_create = "create table migrations(name varchar(100), version int)"; private static readonly string _migrations_create = "create table migrations(name varchar(100), version int)";
private static readonly string _migrations_init = "insert into migrations values('migrations', 1)"; private static readonly string _migrations_init = "insert into migrations values('migrations', 1)";
@ -86,7 +87,16 @@ namespace OpenSim.Data
_type = type; _type = type;
_conn = conn; _conn = conn;
_assem = assem; _assem = assem;
_match = new Regex(@"\.(\d\d\d)_" + _type + @"\.sql");
Initialize();
}
public Migration(DbConnection conn, Assembly assem, string subtype, string type)
{
_type = type;
_conn = conn;
_assem = assem;
_match = new Regex(subtype + @"\.(\d\d\d)_" + _type + @"\.sql");
Initialize(); Initialize();
} }
@ -109,46 +119,37 @@ namespace OpenSim.Data
public void Update() public void Update()
{ {
int version = 0; int version = 0;
int newversion = 0;
version = FindVersion(_type); version = FindVersion(_type);
List<string> migrations = GetMigrationsAfter(version); SortedList<int, string> migrations = GetMigrationsAfter(version);
DbCommand cmd = _conn.CreateCommand(); DbCommand cmd = _conn.CreateCommand();
foreach (string m in migrations) foreach (KeyValuePair<int, string> kvp in migrations)
{ {
cmd.CommandText = m; int newversion = kvp.Key;
cmd.CommandText = kvp.Value;
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
}
if (version == 0) {
newversion = MaxVersion();
if (newversion > version)
{
if (version == 0)
{
InsertVersion(_type, newversion); InsertVersion(_type, newversion);
} } else {
else
{
UpdateVersion(_type, newversion); UpdateVersion(_type, newversion);
} }
version = newversion;
} }
} }
private int MaxVersion() private int MaxVersion()
{ {
int max = 0; int max = 0;
string[] names = _assem.GetManifestResourceNames(); string[] names = _assem.GetManifestResourceNames();
List<string> migrations = new List<string>();
Regex r = new Regex(@"\.(\d\d\d)_" + _type + @"\.sql");
foreach (string s in names) foreach (string s in names)
{ {
Match m = r.Match(s); Match m = _match.Match(s);
if (m.Success) if (m.Success)
{ {
int MigrationVersion = int.Parse(m.Groups[1].ToString()); int MigrationVersion = int.Parse(m.Groups[1].ToString());
if (MigrationVersion > max) if ( MigrationVersion > max )
max = MigrationVersion; max = MigrationVersion;
} }
} }
@ -159,8 +160,7 @@ namespace OpenSim.Data
{ {
int version = 0; int version = 0;
DbCommand cmd = _conn.CreateCommand(); DbCommand cmd = _conn.CreateCommand();
try try {
{
cmd.CommandText = "select version from migrations where name='" + type + "' limit 1"; cmd.CommandText = "select version from migrations where name='" + type + "' limit 1";
using (IDataReader reader = cmd.ExecuteReader()) using (IDataReader reader = cmd.ExecuteReader())
{ {
@ -170,9 +170,7 @@ namespace OpenSim.Data
} }
reader.Close(); reader.Close();
} }
} } catch {
catch
{
// Something went wrong, so we're version 0 // Something went wrong, so we're version 0
} }
return version; return version;
@ -194,42 +192,41 @@ namespace OpenSim.Data
cmd.ExecuteNonQuery(); cmd.ExecuteNonQuery();
} }
private List<string> GetAllMigrations() private SortedList<int, string> GetAllMigrations()
{ {
return GetMigrationsAfter(0); return GetMigrationsAfter(0);
} }
private List<string> GetMigrationsAfter(int version) private SortedList<int, string> GetMigrationsAfter(int after)
{ {
string[] names = _assem.GetManifestResourceNames(); string[] names = _assem.GetManifestResourceNames();
List<string> migrations = new List<string>(); SortedList<int, string> migrations = new SortedList<int, string>();
Regex r = new Regex(@"^(\d\d\d)_" + _type + @"\.sql");
foreach (string s in names) foreach (string s in names)
{ {
Match m = r.Match(s); Match m = _match.Match(s);
if (m.Success) if (m.Success)
{ {
m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString()); m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString());
int MigrationVersion = int.Parse(m.Groups[1].ToString()); int version = int.Parse(m.Groups[1].ToString());
using (Stream resource = _assem.GetManifestResourceStream(s)) if (version > after) {
{ using (Stream resource = _assem.GetManifestResourceStream(s))
using (StreamReader resourceReader = new StreamReader(resource))
{ {
string resourceString = resourceReader.ReadToEnd(); using (StreamReader resourceReader = new StreamReader(resource))
migrations.Add(resourceString); {
string resourceString = resourceReader.ReadToEnd();
migrations.Add(version, resourceString);
}
} }
} }
} }
} }
// TODO: once this is working, get rid of this // TODO: once this is working, get rid of this
if (migrations.Count < 1) if (migrations.Count < 1) {
{
m_log.InfoFormat("Resource '{0}' was not found", _type); m_log.InfoFormat("Resource '{0}' was not found", _type);
} }
return migrations; return migrations;
} }
} }
} }