diff options
author | Sean Dague | 2008-06-09 19:37:13 +0000 |
---|---|---|
committer | Sean Dague | 2008-06-09 19:37:13 +0000 |
commit | 2e2dde71f2525a0a5d1b4ce8d9d98b8df24ee3af (patch) | |
tree | c6340b9cff1dd0df56dbdc2a2f7f056e0a7a0128 /OpenSim/Data/Migrations | |
parent | more work in progress migration code, still a while before (diff) | |
download | opensim-SC_OLD-2e2dde71f2525a0a5d1b4ce8d9d98b8df24ee3af.zip opensim-SC_OLD-2e2dde71f2525a0a5d1b4ce8d9d98b8df24ee3af.tar.gz opensim-SC_OLD-2e2dde71f2525a0a5d1b4ce8d9d98b8df24ee3af.tar.bz2 opensim-SC_OLD-2e2dde71f2525a0a5d1b4ce8d9d98b8df24ee3af.tar.xz |
fill out some more migration facilities
Diffstat (limited to 'OpenSim/Data/Migrations')
-rw-r--r-- | OpenSim/Data/Migrations/Migration.cs | 69 |
1 files changed, 50 insertions, 19 deletions
diff --git a/OpenSim/Data/Migrations/Migration.cs b/OpenSim/Data/Migrations/Migration.cs index 939935c..aaf6dab 100644 --- a/OpenSim/Data/Migrations/Migration.cs +++ b/OpenSim/Data/Migrations/Migration.cs | |||
@@ -71,29 +71,33 @@ namespace OpenSim.Data.Migrations | |||
71 | private string _type; | 71 | private string _type; |
72 | private DbConnection _conn; | 72 | private DbConnection _conn; |
73 | private string _subtype; | 73 | private string _subtype; |
74 | private Assembly _assem; | ||
74 | 75 | ||
75 | private static readonly string _migrations_create = "create table migrations(name varchar(100), version int)"; | 76 | 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_init = "insert into migrations values('migrations', 1)"; |
77 | private static readonly string _migrations_find = "select version from migrations where name='migrations'"; | 78 | private static readonly string _migrations_find = "select version from migrations where name='migrations'"; |
78 | 79 | ||
79 | public Migration(DbConnection conn, string type) | 80 | public Migration(DbConnection conn, Assembly assem, string type) |
80 | { | 81 | { |
81 | _type = type; | 82 | _type = type; |
82 | _conn = conn; | 83 | _conn = conn; |
83 | 84 | _assem = assem; | |
84 | } | 85 | } |
85 | 86 | ||
86 | private void Initialize() | 87 | private void Initialize() |
87 | { | 88 | { |
89 | // clever, eh, we figure out which migrations version we are | ||
90 | int migration_version = FindVersion("migrations"); | ||
91 | |||
92 | if (migration_version > 0) | ||
93 | return; | ||
94 | |||
95 | // If not, create the migration tables | ||
88 | DbCommand cmd = _conn.CreateCommand(); | 96 | 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; | 97 | cmd.CommandText = _migrations_create; |
93 | cmd.ExecuteNonQuery(); | 98 | cmd.ExecuteNonQuery(); |
94 | 99 | ||
95 | cmd.CommandText = _migrations_init; | 100 | UpdateVersion("migrations", 1); |
96 | cmd.ExecuteNonQuery(); | ||
97 | } | 101 | } |
98 | 102 | ||
99 | public void Update() | 103 | public void Update() |
@@ -102,23 +106,55 @@ namespace OpenSim.Data.Migrations | |||
102 | version = FindVersion(_type); | 106 | version = FindVersion(_type); |
103 | 107 | ||
104 | List<string> migrations = GetMigrationsAfter(version); | 108 | List<string> migrations = GetMigrationsAfter(version); |
109 | DbCommand cmd = _conn.CreateCommand(); | ||
105 | foreach (string m in migrations) | 110 | foreach (string m in migrations) |
106 | { | 111 | { |
107 | // TODO: update each | 112 | cmd.CommandText = m; |
113 | cmd.ExecuteNonQuery(); | ||
108 | } | 114 | } |
115 | UpdateVersion(_type, MaxVersion()); | ||
116 | } | ||
109 | 117 | ||
110 | // TODO: find the last revision by number and populate it back | 118 | private int MaxVersion() |
119 | { | ||
120 | int max = 0; | ||
121 | |||
122 | string[] names = _assem.GetManifestResourceNames(); | ||
123 | List<string> migrations = new List<string>(); | ||
124 | Regex r = new Regex(@"^(\d\d\d)_" + _type + @"\.sql"); | ||
125 | |||
126 | foreach (string s in names) | ||
127 | { | ||
128 | Match m = r.Match(s); | ||
129 | int MigrationVersion = int.Parse(m.Groups[1].ToString()); | ||
130 | if ( MigrationVersion > max ) | ||
131 | max = MigrationVersion; | ||
132 | } | ||
133 | return max; | ||
111 | } | 134 | } |
112 | 135 | ||
113 | private int FindVersion(string _type) | 136 | private int FindVersion(string type) |
114 | { | 137 | { |
115 | int version = 0; | 138 | int version = 0; |
116 | DbCommand cmd = _conn.CreateCommand(); | 139 | DbCommand cmd = _conn.CreateCommand(); |
117 | cmd.CommandText = "select version from migrations where name='" + _type + "' limit 1"; | 140 | cmd.CommandText = "select version from migrations where name='" + type + "' limit 1"; |
118 | 141 | using (IDataReader reader = cmd.ExecuteReader()) | |
142 | { | ||
143 | if (reader.Read()) | ||
144 | { | ||
145 | version = Convert.ToInt32(reader["version"]); | ||
146 | } | ||
147 | reader.Close(); | ||
148 | } | ||
119 | return version; | 149 | return version; |
120 | } | 150 | } |
121 | 151 | ||
152 | private void UpdateVersion(string type, int version) | ||
153 | { | ||
154 | DbCommand cmd = _conn.CreateCommand(); | ||
155 | cmd.CommandText = "update migrations set version=" + version + " where name='" + type + "'"; | ||
156 | cmd.ExecuteNonQuery(); | ||
157 | } | ||
122 | 158 | ||
123 | private List<string> GetAllMigrations() | 159 | private List<string> GetAllMigrations() |
124 | { | 160 | { |
@@ -127,8 +163,7 @@ namespace OpenSim.Data.Migrations | |||
127 | 163 | ||
128 | private List<string> GetMigrationsAfter(int version) | 164 | private List<string> GetMigrationsAfter(int version) |
129 | { | 165 | { |
130 | Assembly assem = GetType().Assembly; | 166 | string[] names = _assem.GetManifestResourceNames(); |
131 | string[] names = assem.GetManifestResourceNames(); | ||
132 | List<string> migrations = new List<string>(); | 167 | List<string> migrations = new List<string>(); |
133 | 168 | ||
134 | Regex r = new Regex(@"^(\d\d\d)_" + _type + @"\.sql"); | 169 | Regex r = new Regex(@"^(\d\d\d)_" + _type + @"\.sql"); |
@@ -141,7 +176,7 @@ namespace OpenSim.Data.Migrations | |||
141 | Match m = r.Match(s); | 176 | Match m = r.Match(s); |
142 | m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString()); | 177 | m_log.Info("MIGRATION: Match: " + m.Groups[1].ToString()); |
143 | int MigrationVersion = int.Parse(m.Groups[1].ToString()); | 178 | int MigrationVersion = int.Parse(m.Groups[1].ToString()); |
144 | using (Stream resource = assem.GetManifestResourceStream(s)) | 179 | using (Stream resource = _assem.GetManifestResourceStream(s)) |
145 | { | 180 | { |
146 | using (StreamReader resourceReader = new StreamReader(resource)) | 181 | using (StreamReader resourceReader = new StreamReader(resource)) |
147 | { | 182 | { |
@@ -159,9 +194,5 @@ namespace OpenSim.Data.Migrations | |||
159 | 194 | ||
160 | return migrations; | 195 | return migrations; |
161 | } | 196 | } |
162 | |||
163 | |||
164 | } | 197 | } |
165 | |||
166 | |||
167 | } \ No newline at end of file | 198 | } \ No newline at end of file |