diff options
author | Sean Dague | 2008-06-09 19:11:49 +0000 |
---|---|---|
committer | Sean Dague | 2008-06-09 19:11:49 +0000 |
commit | c30e9ff0f15f08f96025b216c7d37a94b73709c2 (patch) | |
tree | 118097b90021555d7eb031a4040e6832f27c9e74 /OpenSim/Data | |
parent | start in on the shell for a generic database versioning module. My (diff) | |
download | opensim-SC_OLD-c30e9ff0f15f08f96025b216c7d37a94b73709c2.zip opensim-SC_OLD-c30e9ff0f15f08f96025b216c7d37a94b73709c2.tar.gz opensim-SC_OLD-c30e9ff0f15f08f96025b216c7d37a94b73709c2.tar.bz2 opensim-SC_OLD-c30e9ff0f15f08f96025b216c7d37a94b73709c2.tar.xz |
more work in progress migration code, still a while before
this becomes useful
Diffstat (limited to 'OpenSim/Data')
-rw-r--r-- | OpenSim/Data/Migrations/Migration.cs | 98 |
1 files changed, 94 insertions, 4 deletions
diff --git a/OpenSim/Data/Migrations/Migration.cs b/OpenSim/Data/Migrations/Migration.cs index 0ae56ae..939935c 100644 --- a/OpenSim/Data/Migrations/Migration.cs +++ b/OpenSim/Data/Migrations/Migration.cs | |||
@@ -28,8 +28,11 @@ | |||
28 | using System; | 28 | using System; |
29 | using System.Collections.Generic; | 29 | using System.Collections.Generic; |
30 | using System.Data; | 30 | using System.Data; |
31 | using System.Data.Common; | ||
31 | using System.IO; | 32 | using System.IO; |
32 | using System.Reflection; | 33 | using System.Reflection; |
34 | using System.Text.RegularExpressions; | ||
35 | using log4net; | ||
33 | 36 | ||
34 | namespace OpenSim.Data.Migrations | 37 | namespace OpenSim.Data.Migrations |
35 | { | 38 | { |
@@ -50,8 +53,8 @@ namespace OpenSim.Data.Migrations | |||
50 | /// When a database driver starts up, it specifies a resource that | 53 | /// When a database driver starts up, it specifies a resource that |
51 | /// needs to be brought up to the current revision. For instance: | 54 | /// needs to be brought up to the current revision. For instance: |
52 | /// | 55 | /// |
53 | /// Migration um = new Migration("Users"); | 56 | /// Migration um = new Migration(DbConnection, "Users"); |
54 | /// um.Upgrade(dbconnection); | 57 | /// um.Upgrade(); |
55 | /// | 58 | /// |
56 | /// 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 |
57 | /// 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 |
@@ -63,11 +66,98 @@ namespace OpenSim.Data.Migrations | |||
63 | 66 | ||
64 | public class Migration | 67 | public class Migration |
65 | { | 68 | { |
66 | private string _type; | 69 | private static readonly log4net.ILog m_log = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); |
67 | 70 | ||
68 | public Migration(string type) | 71 | private string _type; |
72 | private DbConnection _conn; | ||
73 | private string _subtype; | ||
74 | |||
75 | private static readonly string _migrations_create = "create table migrations(name varchar(100), version int)"; | ||
76 | private static readonly string _migrations_init = "insert into migrations values('migrations', 1)"; | ||
77 | private static readonly string _migrations_find = "select version from migrations where name='migrations'"; | ||
78 | |||
79 | public Migration(DbConnection conn, string type) | ||
69 | { | 80 | { |
70 | _type = type; | 81 | _type = type; |
82 | _conn = conn; | ||
83 | |||
84 | } | ||
85 | |||
86 | private void Initialize() | ||
87 | { | ||
88 | DbCommand cmd = _conn.CreateCommand(); | ||
89 | cmd.CommandText = _migrations_find; | ||
90 | // TODO: generic way to get that text | ||
91 | // if ( not found ) | ||
92 | cmd.CommandText = _migrations_create; | ||
93 | cmd.ExecuteNonQuery(); | ||
94 | |||
95 | cmd.CommandText = _migrations_init; | ||
96 | cmd.ExecuteNonQuery(); | ||
97 | } | ||
98 | |||
99 | public void Update() | ||
100 | { | ||
101 | int version = 0; | ||
102 | version = FindVersion(_type); | ||
103 | |||
104 | List<string> migrations = GetMigrationsAfter(version); | ||
105 | foreach (string m in migrations) | ||
106 | { | ||
107 | // TODO: update each | ||
108 | } | ||
109 | |||
110 | // TODO: find the last revision by number and populate it back | ||
111 | } | ||
112 | |||
113 | private int FindVersion(string _type) | ||
114 | { | ||
115 | int version = 0; | ||
116 | DbCommand cmd = _conn.CreateCommand(); | ||
117 | cmd.CommandText = "select version from migrations where name='" + _type + "' limit 1"; | ||
118 | |||
119 | return version; | ||
120 | } | ||
121 | |||
122 | |||
123 | private List<string> GetAllMigrations() | ||
124 | { | ||
125 | return GetMigrationsAfter(0); | ||
126 | } | ||
127 | |||
128 | private List<string> GetMigrationsAfter(int version) | ||
129 | { | ||
130 | Assembly assem = GetType().Assembly; | ||
131 | string[] names = assem.GetManifestResourceNames(); | ||
132 | List<string> migrations = new List<string>(); | ||
133 | |||
134 | Regex r = new Regex(@"^(\d\d\d)_" + _type + @"\.sql"); | ||
135 | |||
136 | foreach (string s in names) | ||
137 | { | ||
138 | m_log.Info("MIGRATION: Resources: " + s); | ||
139 | if (s.EndsWith(_type + @"\.sql")) | ||
140 | { | ||
141 | Match m = r.Match(s); | ||
142 | m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString()); | ||
143 | int MigrationVersion = int.Parse(m.Groups[1].ToString()); | ||
144 | using (Stream resource = assem.GetManifestResourceStream(s)) | ||
145 | { | ||
146 | using (StreamReader resourceReader = new StreamReader(resource)) | ||
147 | { | ||
148 | string resourceString = resourceReader.ReadToEnd(); | ||
149 | migrations.Add(resourceString); | ||
150 | } | ||
151 | } | ||
152 | } | ||
153 | } | ||
154 | |||
155 | // TODO: once this is working, get rid of this | ||
156 | if (migrations.Count < 1) { | ||
157 | throw new Exception(string.Format("Resource '{0}' was not found", _type)); | ||
158 | } | ||
159 | |||
160 | return migrations; | ||
71 | } | 161 | } |
72 | 162 | ||
73 | 163 | ||