aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Migration.cs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--OpenSim/Data/Migration.cs77
1 files changed, 37 insertions, 40 deletions
diff --git a/OpenSim/Data/Migration.cs b/OpenSim/Data/Migration.cs
index 75d5307..fe4dfc4 100644
--- a/OpenSim/Data/Migration.cs
+++ b/OpenSim/Data/Migration.cs
@@ -76,6 +76,7 @@ namespace OpenSim.Data
76 private DbConnection _conn; 76 private DbConnection _conn;
77 private string _subtype; 77 private string _subtype;
78 private Assembly _assem; 78 private Assembly _assem;
79 private Regex _match;
79 80
80 private static readonly string _migrations_create = "create table migrations(name varchar(100), version int)"; 81 private static readonly string _migrations_create = "create table migrations(name varchar(100), version int)";
81 private static readonly string _migrations_init = "insert into migrations values('migrations', 1)"; 82 private static readonly string _migrations_init = "insert into migrations values('migrations', 1)";
@@ -86,7 +87,16 @@ namespace OpenSim.Data
86 _type = type; 87 _type = type;
87 _conn = conn; 88 _conn = conn;
88 _assem = assem; 89 _assem = assem;
89 90 _match = new Regex(@"\.(\d\d\d)_" + _type + @"\.sql");
91 Initialize();
92 }
93
94 public Migration(DbConnection conn, Assembly assem, string subtype, string type)
95 {
96 _type = type;
97 _conn = conn;
98 _assem = assem;
99 _match = new Regex(subtype + @"\.(\d\d\d)_" + _type + @"\.sql");
90 Initialize(); 100 Initialize();
91 } 101 }
92 102
@@ -109,46 +119,37 @@ namespace OpenSim.Data
109 public void Update() 119 public void Update()
110 { 120 {
111 int version = 0; 121 int version = 0;
112 int newversion = 0;
113 version = FindVersion(_type); 122 version = FindVersion(_type);
114 123
115 List<string> migrations = GetMigrationsAfter(version); 124 SortedList<int, string> migrations = GetMigrationsAfter(version);
116 DbCommand cmd = _conn.CreateCommand(); 125 DbCommand cmd = _conn.CreateCommand();
117 foreach (string m in migrations) 126 foreach (KeyValuePair<int, string> kvp in migrations)
118 { 127 {
119 cmd.CommandText = m; 128 int newversion = kvp.Key;
129 cmd.CommandText = kvp.Value;
120 cmd.ExecuteNonQuery(); 130 cmd.ExecuteNonQuery();
121 } 131
122 132 if (version == 0) {
123 newversion = MaxVersion();
124 if (newversion > version)
125 {
126 if (version == 0)
127 {
128 InsertVersion(_type, newversion); 133 InsertVersion(_type, newversion);
129 } 134 } else {
130 else
131 {
132 UpdateVersion(_type, newversion); 135 UpdateVersion(_type, newversion);
133 } 136 }
137 version = newversion;
134 } 138 }
135 } 139 }
136 140
137 private int MaxVersion() 141 private int MaxVersion()
138 { 142 {
139 int max = 0; 143 int max = 0;
140
141 string[] names = _assem.GetManifestResourceNames(); 144 string[] names = _assem.GetManifestResourceNames();
142 List<string> migrations = new List<string>();
143 Regex r = new Regex(@"\.(\d\d\d)_" + _type + @"\.sql");
144 145
145 foreach (string s in names) 146 foreach (string s in names)
146 { 147 {
147 Match m = r.Match(s); 148 Match m = _match.Match(s);
148 if (m.Success) 149 if (m.Success)
149 { 150 {
150 int MigrationVersion = int.Parse(m.Groups[1].ToString()); 151 int MigrationVersion = int.Parse(m.Groups[1].ToString());
151 if (MigrationVersion > max) 152 if ( MigrationVersion > max )
152 max = MigrationVersion; 153 max = MigrationVersion;
153 } 154 }
154 } 155 }
@@ -159,8 +160,7 @@ namespace OpenSim.Data
159 { 160 {
160 int version = 0; 161 int version = 0;
161 DbCommand cmd = _conn.CreateCommand(); 162 DbCommand cmd = _conn.CreateCommand();
162 try 163 try {
163 {
164 cmd.CommandText = "select version from migrations where name='" + type + "' limit 1"; 164 cmd.CommandText = "select version from migrations where name='" + type + "' limit 1";
165 using (IDataReader reader = cmd.ExecuteReader()) 165 using (IDataReader reader = cmd.ExecuteReader())
166 { 166 {
@@ -170,9 +170,7 @@ namespace OpenSim.Data
170 } 170 }
171 reader.Close(); 171 reader.Close();
172 } 172 }
173 } 173 } catch {
174 catch
175 {
176 // Something went wrong, so we're version 0 174 // Something went wrong, so we're version 0
177 } 175 }
178 return version; 176 return version;
@@ -194,42 +192,41 @@ namespace OpenSim.Data
194 cmd.ExecuteNonQuery(); 192 cmd.ExecuteNonQuery();
195 } 193 }
196 194
197 private List<string> GetAllMigrations() 195 private SortedList<int, string> GetAllMigrations()
198 { 196 {
199 return GetMigrationsAfter(0); 197 return GetMigrationsAfter(0);
200 } 198 }
201 199
202 private List<string> GetMigrationsAfter(int version) 200 private SortedList<int, string> GetMigrationsAfter(int after)
203 { 201 {
204 string[] names = _assem.GetManifestResourceNames(); 202 string[] names = _assem.GetManifestResourceNames();
205 List<string> migrations = new List<string>(); 203 SortedList<int, string> migrations = new SortedList<int, string>();
206
207 Regex r = new Regex(@"^(\d\d\d)_" + _type + @"\.sql");
208 204
209 foreach (string s in names) 205 foreach (string s in names)
210 { 206 {
211 Match m = r.Match(s); 207 Match m = _match.Match(s);
212 if (m.Success) 208 if (m.Success)
213 { 209 {
214 m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString()); 210 m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString());
215 int MigrationVersion = int.Parse(m.Groups[1].ToString()); 211 int version = int.Parse(m.Groups[1].ToString());
216 using (Stream resource = _assem.GetManifestResourceStream(s)) 212 if (version > after) {
217 { 213 using (Stream resource = _assem.GetManifestResourceStream(s))
218 using (StreamReader resourceReader = new StreamReader(resource))
219 { 214 {
220 string resourceString = resourceReader.ReadToEnd(); 215 using (StreamReader resourceReader = new StreamReader(resource))
221 migrations.Add(resourceString); 216 {
217 string resourceString = resourceReader.ReadToEnd();
218 migrations.Add(version, resourceString);
219 }
222 } 220 }
223 } 221 }
224 } 222 }
225 } 223 }
226 224
227 // TODO: once this is working, get rid of this 225 // TODO: once this is working, get rid of this
228 if (migrations.Count < 1) 226 if (migrations.Count < 1) {
229 {
230 m_log.InfoFormat("Resource '{0}' was not found", _type); 227 m_log.InfoFormat("Resource '{0}' was not found", _type);
231 } 228 }
232 return migrations; 229 return migrations;
233 } 230 }
234 } 231 }
235} 232} \ No newline at end of file