aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Data/Migration.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Data/Migration.cs')
-rw-r--r--OpenSim/Data/Migration.cs108
1 files changed, 62 insertions, 46 deletions
diff --git a/OpenSim/Data/Migration.cs b/OpenSim/Data/Migration.cs
index e51dc22..06defe4 100644
--- a/OpenSim/Data/Migration.cs
+++ b/OpenSim/Data/Migration.cs
@@ -53,8 +53,8 @@ namespace OpenSim.Data
53 /// When a database driver starts up, it specifies a resource that 53 /// When a database driver starts up, it specifies a resource that
54 /// needs to be brought up to the current revision. For instance: 54 /// needs to be brought up to the current revision. For instance:
55 /// 55 ///
56 /// Migration um = new Migration(Assembly, DbConnection, "Users"); 56 /// Migration um = new Migration(DbConnection, Assembly, "Users");
57 /// um.Upgrade(); 57 /// um.Update();
58 /// 58 ///
59 /// This works out which version Users is at, and applies all the 59 /// This works out which version Users is at, and applies all the
60 /// revisions past it to it. If there is no users table, all 60 /// revisions past it to it. If there is no users table, all
@@ -110,10 +110,11 @@ namespace OpenSim.Data
110 return; 110 return;
111 111
112 // If not, create the migration tables 112 // If not, create the migration tables
113 DbCommand cmd = _conn.CreateCommand(); 113 using (DbCommand cmd = _conn.CreateCommand())
114 cmd.CommandText = _migrations_create; 114 {
115 cmd.ExecuteNonQuery(); 115 cmd.CommandText = _migrations_create;
116 cmd.Dispose(); 116 cmd.ExecuteNonQuery();
117 }
117 118
118 InsertVersion("migrations", 1); 119 InsertVersion("migrations", 1);
119 } 120 }
@@ -128,28 +129,39 @@ namespace OpenSim.Data
128 return; 129 return;
129 130
130 // to prevent people from killing long migrations. 131 // to prevent people from killing long migrations.
131 m_log.InfoFormat("[MIGRATIONS] Upgrading {0} to latest revision.", _type); 132 m_log.InfoFormat("[MIGRATIONS] Upgrading {0} to latest revision {1}.", _type, migrations.Keys[migrations.Count - 1]);
132 m_log.Info("[MIGRATIONS] NOTE: this may take a while, don't interupt this process!"); 133 m_log.Info("[MIGRATIONS] NOTE: this may take a while, don't interupt this process!");
133 134
134 DbCommand cmd = _conn.CreateCommand(); 135 using (DbCommand cmd = _conn.CreateCommand())
135 foreach (KeyValuePair<int, string> kvp in migrations)
136 { 136 {
137 int newversion = kvp.Key; 137 foreach (KeyValuePair<int, string> kvp in migrations)
138 cmd.CommandText = kvp.Value;
139 // we need to up the command timeout to infinite as we might be doing long migrations.
140 cmd.CommandTimeout = 0;
141 cmd.ExecuteNonQuery();
142
143 if (version == 0)
144 {
145 InsertVersion(_type, newversion);
146 }
147 else
148 { 138 {
149 UpdateVersion(_type, newversion); 139 int newversion = kvp.Key;
140 cmd.CommandText = kvp.Value;
141 // we need to up the command timeout to infinite as we might be doing long migrations.
142 cmd.CommandTimeout = 0;
143 try
144 {
145 cmd.ExecuteNonQuery();
146 }
147 catch (Exception e)
148 {
149 m_log.DebugFormat("[MIGRATIONS] Cmd was {0}", cmd.CommandText);
150 m_log.DebugFormat("[MIGRATIONS]: An error has occurred in the migration {0}.\n This may mean you could see errors trying to run OpenSim. If you see database related errors, you will need to fix the issue manually. Continuing.", e.Message);
151 cmd.CommandText = "ROLLBACK;";
152 cmd.ExecuteNonQuery();
153 }
154
155 if (version == 0)
156 {
157 InsertVersion(_type, newversion);
158 }
159 else
160 {
161 UpdateVersion(_type, newversion);
162 }
163 version = newversion;
150 } 164 }
151 version = newversion;
152 cmd.Dispose();
153 } 165 }
154 } 166 }
155 167
@@ -189,43 +201,46 @@ namespace OpenSim.Data
189 protected virtual int FindVersion(DbConnection conn, string type) 201 protected virtual int FindVersion(DbConnection conn, string type)
190 { 202 {
191 int version = 0; 203 int version = 0;
192 DbCommand cmd = conn.CreateCommand(); 204 using (DbCommand cmd = conn.CreateCommand())
193 try
194 { 205 {
195 cmd.CommandText = "select version from migrations where name='" + type +"' order by version desc"; 206 try
196 using (IDataReader reader = cmd.ExecuteReader())
197 { 207 {
198 if (reader.Read()) 208 cmd.CommandText = "select version from migrations where name='" + type + "' order by version desc";
209 using (IDataReader reader = cmd.ExecuteReader())
199 { 210 {
200 version = Convert.ToInt32(reader["version"]); 211 if (reader.Read())
212 {
213 version = Convert.ToInt32(reader["version"]);
214 }
215 reader.Close();
201 } 216 }
202 reader.Close(); 217 }
218 catch
219 {
220 // Something went wrong, so we're version 0
203 } 221 }
204 } 222 }
205 catch
206 {
207 // Something went wrong, so we're version 0
208 }
209 cmd.Dispose();
210 return version; 223 return version;
211 } 224 }
212 225
213 private void InsertVersion(string type, int version) 226 private void InsertVersion(string type, int version)
214 { 227 {
215 DbCommand cmd = _conn.CreateCommand(); 228 using (DbCommand cmd = _conn.CreateCommand())
216 cmd.CommandText = "insert into migrations(name, version) values('" + type + "', " + version + ")"; 229 {
217 m_log.InfoFormat("[MIGRATIONS]: Creating {0} at version {1}", type, version); 230 cmd.CommandText = "insert into migrations(name, version) values('" + type + "', " + version + ")";
218 cmd.ExecuteNonQuery(); 231 m_log.InfoFormat("[MIGRATIONS]: Creating {0} at version {1}", type, version);
219 cmd.Dispose(); 232 cmd.ExecuteNonQuery();
233 }
220 } 234 }
221 235
222 private void UpdateVersion(string type, int version) 236 private void UpdateVersion(string type, int version)
223 { 237 {
224 DbCommand cmd = _conn.CreateCommand(); 238 using (DbCommand cmd = _conn.CreateCommand())
225 cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'"; 239 {
226 m_log.InfoFormat("[MIGRATIONS]: Updating {0} to version {1}", type, version); 240 cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'";
227 cmd.ExecuteNonQuery(); 241 m_log.InfoFormat("[MIGRATIONS]: Updating {0} to version {1}", type, version);
228 cmd.Dispose(); 242 cmd.ExecuteNonQuery();
243 }
229 } 244 }
230 245
231 // private SortedList<int, string> GetAllMigrations() 246 // private SortedList<int, string> GetAllMigrations()
@@ -246,7 +261,8 @@ namespace OpenSim.Data
246 if (m.Success) 261 if (m.Success)
247 { 262 {
248 int version = int.Parse(m.Groups[1].ToString()); 263 int version = int.Parse(m.Groups[1].ToString());
249 if (version > after) { 264 if (version > after)
265 {
250 using (Stream resource = _assem.GetManifestResourceStream(s)) 266 using (Stream resource = _assem.GetManifestResourceStream(s))
251 { 267 {
252 using (StreamReader resourceReader = new StreamReader(resource)) 268 using (StreamReader resourceReader = new StreamReader(resource))