/
MigrateApi.cs
173 lines (152 loc) · 6.35 KB
/
MigrateApi.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
using System;
using System.Data;
using System.Linq;
using FluentNHibernate.Cfg.Db;
using FluentNHibernate.Cfg;
using NHibernate;
using System.Collections.Generic;
using Sector.Entities;
using System.IO;
using Sector.Mappings;
using NHibernate.Tool.hbm2ddl;
namespace Sector
{
public class MigrateApi : IMigrateApi
{
private ISectorDb sectorDb;
public MigrateApi(ISectorDb sectorDb)
{
this.sectorDb = sectorDb;
}
public bool IsVersionControlled(IRepository repository)
{
// First make sure we're not already under version control.
bool alreadyVersioned = true;
using (ISession session = sectorDb.DbFactory.OpenSession())
{
try
{
MigrateVersion mgv = session.QueryOver<MigrateVersion>()
.Where(m => m.RepositoryId == repository.RepositoryId)
.SingleOrDefault();
if (mgv == null)
alreadyVersioned = false;
}
catch (Exception)
{
alreadyVersioned = false;
}
}
return alreadyVersioned;
}
public void VersionControl(IRepository repository)
{
// Create the migration table first.
sectorDb.CreateMigrationTable();
Console.WriteLine("Versioning the db, setting version to 0");
using (ISession session = sectorDb.DbFactory.OpenSession())
using (ITransaction transaction = session.BeginTransaction())
{
MigrateVersion mgv = new MigrateVersion(repositoryId: repository.RepositoryId,
repositoryPath: repository.RepositoryPath,
version: 0);
session.Save(mgv);
transaction.Commit();
}
}
private int GetDbVersion(IRepository repository, ISession session)
{
MigrateVersion mgv = session.QueryOver<MigrateVersion>()
.Where(m => m.RepositoryId == repository.RepositoryId)
.SingleOrDefault();
if (mgv == null)
throw new SectorException("Unable to fetch the db version");
return mgv.Version;
}
public int GetDbVersion(IRepository repository)
{
using (ISession session = sectorDb.DbFactory.OpenSession())
{
return GetDbVersion(repository, session);
}
}
public void Upgrade(IRepository repository, int version)
{
using (ISession session = sectorDb.DbFactory.OpenSession())
{
int dbVersion = GetDbVersion(repository, session);
if (dbVersion >= version)
{
// Already up higher than this so do nothing.
return;
}
int highestAvailable = repository.GetVersion();
if (version > highestAvailable)
{
throw new SectorException("Version requested higher than latest available");
}
if (!repository.HasVersion(version))
{
throw new SectorException("Version requested not available in the repository");
}
int steps = version - dbVersion;
foreach (var upVersion in Enumerable.Range(dbVersion + 1, steps))
{
using (ITransaction transaction = session.BeginTransaction())
using (var sqlCommand = session.Connection.CreateCommand())
{
// Run the SQL for the next version.
sqlCommand.CommandText = repository.GetUpgradeSql(upVersion);
sqlCommand.ExecuteNonQuery();
// Upgrade the version info and then commit the transaction.
MigrateVersion mgv = session.QueryOver<MigrateVersion>()
.Where(m => m.RepositoryId == repository.RepositoryId)
.SingleOrDefault();
mgv.Version = upVersion;
transaction.Commit();
}
}
}
}
public void Downgrade(IRepository repository, int version)
{
using (ISession session = sectorDb.DbFactory.OpenSession())
{
int dbVersion = GetDbVersion(repository, session);
if (dbVersion <= version)
{
// Already lower or same as current version.
return;
}
if (version < 0)
{
throw new SectorException("Version cannot be less than 0");
}
// Example downgrade from 4 back to 0:
// 4_downgrade set version to 3
// 3_downgrade set version to 2,
// 2_downgrade set version to 1
// 1_downgrade set version = 0
// Means we need to go over range 1, 4 in reverse order, here we iterate
// over range 1, 4 reversed to { 4, 1 } == 4, 3, 2, 1 and end with dbVersion 0
int steps = dbVersion - version;
foreach (var downVersion in Enumerable.Range(version + 1, steps).Reverse())
{
using (ITransaction transaction = session.BeginTransaction())
using (var sqlCommand = session.Connection.CreateCommand())
{
// Run the SQL for the next version.
sqlCommand.CommandText = repository.GetDowngradeSql(downVersion);
sqlCommand.ExecuteNonQuery();
// Upgrade the version info and then commit the transaction.
MigrateVersion mgv = session.QueryOver<MigrateVersion>()
.Where(m => m.RepositoryId == repository.RepositoryId)
.SingleOrDefault();
mgv.Version = downVersion - 1;
transaction.Commit();
}
}
}
}
}
}