Annotation of /trunk/ModularRex/Tools/MigrationTool/RegionMigration.cs
Parent Directory
|
Revision Log
Revision 180 - (view) (download)
| 1 : | mikkopa | 177 | using System; |
| 2 : | using System.Collections.Generic; | ||
| 3 : | using System.Text; | ||
| 4 : | using Mono.Data.SqliteClient; | ||
| 5 : | using System.Reflection; | ||
| 6 : | using OpenSim.Data; | ||
| 7 : | using log4net; | ||
| 8 : | mikkopa | 179 | using ModularRex.RexFramework; |
| 9 : | using OpenMetaverse; | ||
| 10 : | using ModularRex.NHibernate; | ||
| 11 : | mikkopa | 177 | |
| 12 : | namespace ModularRex.Tools.MigrationTool | ||
| 13 : | { | ||
| 14 : | class RegionMigration | ||
| 15 : | { | ||
| 16 : | private static readonly ILog m_log = LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType); | ||
| 17 : | |||
| 18 : | protected string m_connectionString = String.Empty; | ||
| 19 : | mikkopa | 179 | protected string m_rexConnectionString = String.Empty; |
| 20 : | mikkopa | 177 | |
| 21 : | private const string addAuthbyerID = "ALTER TABLE land ADD COLUMN AuthbuyerID varchar(36) NOT NULL default '00000000-0000-0000-0000-000000000000'"; | ||
| 22 : | |||
| 23 : | mikkopa | 179 | public RegionMigration(string connectionString, string rexConnectionString) |
| 24 : | mikkopa | 177 | { |
| 25 : | // default to something sensible | ||
| 26 : | if (connectionString == "") | ||
| 27 : | { | ||
| 28 : | m_connectionString = "URI=file:OpenSim.db,version=3"; | ||
| 29 : | } | ||
| 30 : | else | ||
| 31 : | { | ||
| 32 : | m_connectionString = connectionString; | ||
| 33 : | } | ||
| 34 : | mikkopa | 179 | |
| 35 : | if (rexConnectionString == String.Empty) | ||
| 36 : | { | ||
| 37 : | m_rexConnectionString = "SQLiteDialect;SQLite20Driver;Data Source=RexObjects.db;Version=3"; | ||
| 38 : | } | ||
| 39 : | else | ||
| 40 : | { | ||
| 41 : | m_rexConnectionString = rexConnectionString; | ||
| 42 : | } | ||
| 43 : | mikkopa | 177 | } |
| 44 : | |||
| 45 : | public bool Convert() | ||
| 46 : | { | ||
| 47 : | try | ||
| 48 : | { | ||
| 49 : | SqliteConnection conn = new SqliteConnection(m_connectionString); | ||
| 50 : | conn.Open(); | ||
| 51 : | |||
| 52 : | Assembly assem = GetType().Assembly; | ||
| 53 : | Migration m = new Migration(conn, assem, "RegionStore"); | ||
| 54 : | |||
| 55 : | int version = m.Version; | ||
| 56 : | |||
| 57 : | mikkopa | 179 | if (version <= 14) |
| 58 : | mikkopa | 177 | { |
| 59 : | if (version == 0) | ||
| 60 : | { | ||
| 61 : | mikkopa | 179 | //read rex tables and add to rex database |
| 62 : | mikkopa | 180 | m_log.Info("[regionstore] converting rex tables to rexobjectproperties"); |
| 63 : | if (!ConvertLegacyRexDataToModreX()) | ||
| 64 : | mikkopa | 179 | { |
| 65 : | mikkopa | 180 | conn.Close(); |
| 66 : | mikkopa | 179 | return false; |
| 67 : | } | ||
| 68 : | |||
| 69 : | m_log.Info("[RegionStore] Update region migrations"); | ||
| 70 : | mikkopa | 177 | //Add new field to Land table |
| 71 : | SqliteCommand addAuthbyerIDCmd = new SqliteCommand(addAuthbyerID, conn); | ||
| 72 : | addAuthbyerIDCmd.ExecuteNonQuery(); | ||
| 73 : | |||
| 74 : | //Change migration to version 1 | ||
| 75 : | m.Version = 1; | ||
| 76 : | } | ||
| 77 : | |||
| 78 : | //Run migrations up to 9 | ||
| 79 : | mikkopa | 179 | //Note: this run migrations only to point nine since only those files exist in application resources. |
| 80 : | mikkopa | 177 | m.Update(); |
| 81 : | |||
| 82 : | //Skip over 10. Change version to 10 | ||
| 83 : | //This skips adding of the ClickAction since that already exists in 0.4 database | ||
| 84 : | mikkopa | 179 | //m.Version = 10; |
| 85 : | mikkopa | 177 | } |
| 86 : | |||
| 87 : | conn.Close(); | ||
| 88 : | return true; | ||
| 89 : | } | ||
| 90 : | catch (Exception e) | ||
| 91 : | { | ||
| 92 : | m_log.ErrorFormat("[RegionStore] Migration failed. Reason: {0}", e); | ||
| 93 : | return false; | ||
| 94 : | } | ||
| 95 : | } | ||
| 96 : | mikkopa | 179 | |
| 97 : | protected bool ConvertLegacyRexDataToModreX() | ||
| 98 : | { | ||
| 99 : | NHibernateRexLegacyData legacydata = new NHibernateRexLegacyData(); | ||
| 100 : | |||
| 101 : | //convert connnection string to NHibernate style | ||
| 102 : | //parse string like this: "URI=file:OpenSim.db,version=3" | ||
| 103 : | //and convert it to like this: "SQLiteDialect;SQLite20Driver;Data Source=RexObjects.db;Version=3" | ||
| 104 : | string arg1 = String.Empty; | ||
| 105 : | string arg2 = String.Empty; | ||
| 106 : | |||
| 107 : | string[] components = m_connectionString.Split(','); | ||
| 108 : | if (components[0].StartsWith("URI=file:")) | ||
| 109 : | { | ||
| 110 : | arg1 = components[0].Substring(9); | ||
| 111 : | } | ||
| 112 : | else | ||
| 113 : | { | ||
| 114 : | m_log.ErrorFormat("[MODREXOBJECTS]: Error parseing connection string {0}", m_connectionString); | ||
| 115 : | return false; | ||
| 116 : | } | ||
| 117 : | if (components[1].StartsWith("version=")) | ||
| 118 : | { | ||
| 119 : | arg2 = components[1].Substring(8); | ||
| 120 : | } | ||
| 121 : | else | ||
| 122 : | { | ||
| 123 : | m_log.ErrorFormat("[MODREXOBJECTS]: Error parseing connection string {0}", m_connectionString); | ||
| 124 : | return false; | ||
| 125 : | } | ||
| 126 : | |||
| 127 : | StringBuilder sb = new StringBuilder(); | ||
| 128 : | sb.AppendFormat("SQLiteDialect;SQLite20Driver;Data Source={0};Version={1}", arg1, arg2); | ||
| 129 : | |||
| 130 : | legacydata.Initialise(sb.ToString()); | ||
| 131 : | if (!legacydata.Inizialized) | ||
| 132 : | { | ||
| 133 : | m_log.Info("[MODREXOBJECTS]: Legacy database failed to initialize."); | ||
| 134 : | return false; | ||
| 135 : | } | ||
| 136 : | |||
| 137 : | List<RexLegacyPrimData> rexprimdata = legacydata.LoadAllRexPrimData(); | ||
| 138 : | m_log.Info("[MODREXOBJECTS]: Legacy rexprimdata objects loaded:" + rexprimdata.Count.ToString()); | ||
| 139 : | |||
| 140 : | List<RexLegacyPrimMaterialData> rexprimmaterialdata = legacydata.LoadAllRexPrimMaterialData(); | ||
| 141 : | m_log.Info("[MODREXOBJECTS]: Legacy rexprimmaterialdata objects loaded:" + rexprimmaterialdata.Count.ToString()); | ||
| 142 : | |||
| 143 : | Dictionary<UUID, RexObjectProperties> rexObjects = new Dictionary<UUID, RexObjectProperties>(); | ||
| 144 : | |||
| 145 : | foreach (RexLegacyPrimData prim in rexprimdata) | ||
| 146 : | { | ||
| 147 : | RexObjectProperties p = new RexObjectProperties(); | ||
| 148 : | p.SetRexPrimDataFromLegacyData(prim); | ||
| 149 : | rexObjects.Add(prim.UUID, p); | ||
| 150 : | } | ||
| 151 : | |||
| 152 : | foreach (RexLegacyPrimMaterialData primmat in rexprimmaterialdata) | ||
| 153 : | { | ||
| 154 : | if (rexObjects.ContainsKey(primmat.UUID) && rexObjects[primmat.UUID] != null) | ||
| 155 : | { | ||
| 156 : | rexObjects[primmat.UUID].RexMaterials.AddMaterial((uint)primmat.MaterialIndex, primmat.MaterialUUID); | ||
| 157 : | } | ||
| 158 : | else | ||
| 159 : | { | ||
| 160 : | m_log.WarnFormat("[MODREXOBJECTS]: Could not find RexObjectData for prim {0} while adding materials. Creating", primmat.UUID); | ||
| 161 : | RexObjectProperties p = new RexObjectProperties(); | ||
| 162 : | p.ParentObjectID = primmat.UUID; | ||
| 163 : | p.RexMaterials.AddMaterial((uint)primmat.MaterialIndex, primmat.MaterialUUID); | ||
| 164 : | rexObjects.Add(primmat.UUID, p); | ||
| 165 : | } | ||
| 166 : | } | ||
| 167 : | |||
| 168 : | //Add rex object to database | ||
| 169 : | NHibernateRexObjectData rexObjectManager = new NHibernateRexObjectData(); | ||
| 170 : | rexObjectManager.Initialise(m_rexConnectionString); | ||
| 171 : | foreach (KeyValuePair<UUID, RexObjectProperties> data in rexObjects) | ||
| 172 : | { | ||
| 173 : | rexObjectManager.StoreObject(data.Value); | ||
| 174 : | } | ||
| 175 : | |||
| 176 : | return true; | ||
| 177 : | } | ||
| 178 : | mikkopa | 177 | } |
| 179 : | } |
| ViewVC Help | |
| Powered by ViewVC 1.0.0 |

