Skip to content

Commit 285b97f

Browse files
committed
Take away clustered spec
SQL Azure requires "clustered", while SQL CE requires "nonclustered". Let's just go with the default.
1 parent 5f539a7 commit 285b97f

File tree

1 file changed

+145
-145
lines changed

1 file changed

+145
-145
lines changed
Lines changed: 145 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,146 +1,146 @@
1-
using System;
2-
using System.Collections.Generic;
3-
using System.Data;
4-
using System.Data.Common;
5-
using System.Data.SqlClient;
6-
using DbUp.Engine;
7-
using DbUp.Engine.Output;
8-
9-
namespace DbUp.Support.SqlServer
10-
{
11-
/// <summary>
12-
/// An implementation of the <see cref="IJournal"/> interface which tracks version numbers for a
13-
/// SQL Server database using a table called dbo.SchemaVersions.
14-
/// </summary>
15-
public sealed class SqlTableJournal : IJournal
16-
{
17-
private readonly Func<IDbConnection> connectionFactory;
18-
private readonly string tableName;
19-
private readonly string schemaTableName;
20-
private readonly IUpgradeLog log;
21-
22-
/// <summary>
23-
/// Initializes a new instance of the <see cref="SqlTableJournal"/> class.
24-
/// </summary>
25-
/// <param name="connectionFactory">The connection factory.</param>
26-
/// <param name="schema">The schema that contains the table.</param>
27-
/// <param name="table">The table name.</param>
28-
/// <param name="logger">The log.</param>
29-
/// <example>
30-
/// var journal = new TableJournal("Server=server;Database=database;Trusted_Connection=True", "dbo", "MyVersionTable");
31-
/// </example>
32-
public SqlTableJournal(Func<IDbConnection> connectionFactory, string schema, string table, IUpgradeLog logger)
33-
{
34-
this.connectionFactory = connectionFactory;
35-
schemaTableName = tableName = table;
36-
if (!string.IsNullOrEmpty(schema))
37-
schemaTableName = schema + "." + tableName;
38-
log = logger;
39-
}
40-
41-
/// <summary>
42-
/// Recalls the version number of the database.
43-
/// </summary>
44-
/// <returns>All executed scripts.</returns>
45-
public string[] GetExecutedScripts()
46-
{
47-
log.WriteInformation("Fetching list of already executed scripts.");
48-
var exists = DoesTableExist();
49-
if (!exists)
50-
{
51-
log.WriteInformation(string.Format("The {0} table could not be found. The database is assumed to be at version 0.", schemaTableName));
52-
return new string[0];
53-
}
54-
55-
var scripts = new List<string>();
56-
using (var connection = connectionFactory())
57-
using (var command = connection.CreateCommand())
58-
{
59-
command.CommandText = string.Format("select [ScriptName] from {0} order by [ScriptName]", schemaTableName);
60-
command.CommandType = CommandType.Text;
61-
connection.Open();
62-
63-
using(var reader = command.ExecuteReader())
64-
{
65-
while (reader.Read())
66-
scripts.Add((string) reader[0]);
67-
}
68-
}
69-
return scripts.ToArray();
70-
}
71-
72-
/// <summary>
73-
/// Records a database upgrade for a database specified in a given connection string.
74-
/// </summary>
75-
/// <param name="script">The script.</param>
76-
public void StoreExecutedScript(SqlScript script)
77-
{
78-
var exists = DoesTableExist();
79-
if (!exists)
80-
{
81-
log.WriteInformation(string.Format("Creating the {0} table", schemaTableName));
82-
83-
using (var connection = connectionFactory())
84-
using (var command = connection.CreateCommand())
85-
{
86-
command.CommandText = string.Format(
87-
@"create table {0} (
88-
[Id] int identity(1,1) not null constraint PK_SchemaVersions_Id primary key clustered ,
89-
[ScriptName] nvarchar(255) not null,
90-
[Applied] datetime not null
91-
)", schemaTableName);
92-
93-
command.CommandType = CommandType.Text;
94-
connection.Open();
95-
96-
command.ExecuteNonQuery();
97-
}
98-
99-
log.WriteInformation(string.Format("The {0} table has been created", schemaTableName));
100-
}
101-
102-
103-
using (var connection = connectionFactory())
104-
using (var command = connection.CreateCommand())
105-
{
106-
command.CommandText = string.Format("insert into {0} (ScriptName, Applied) values (@scriptName, '{1}')", schemaTableName, DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss"));
107-
108-
var param = command.CreateParameter();
109-
param.ParameterName = "scriptName";
110-
param.Value = script.Name;
111-
command.Parameters.Add(param);
112-
113-
command.CommandType = CommandType.Text;
114-
connection.Open();
115-
116-
command.ExecuteNonQuery();
117-
}
118-
}
119-
120-
private bool DoesTableExist()
121-
{
122-
try
123-
{
124-
using (var connection = connectionFactory())
125-
{
126-
using (var command = connection.CreateCommand())
127-
{
128-
command.CommandText = string.Format("select count(*) from {0}", schemaTableName);
129-
command.CommandType = CommandType.Text;
130-
connection.Open();
131-
command.ExecuteScalar();
132-
return true;
133-
}
134-
}
135-
}
136-
catch (SqlException)
137-
{
138-
return false;
139-
}
140-
catch (DbException)
141-
{
142-
return false;
143-
}
144-
}
145-
}
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Data;
4+
using System.Data.Common;
5+
using System.Data.SqlClient;
6+
using DbUp.Engine;
7+
using DbUp.Engine.Output;
8+
9+
namespace DbUp.Support.SqlServer
10+
{
11+
/// <summary>
12+
/// An implementation of the <see cref="IJournal"/> interface which tracks version numbers for a
13+
/// SQL Server database using a table called dbo.SchemaVersions.
14+
/// </summary>
15+
public sealed class SqlTableJournal : IJournal
16+
{
17+
private readonly Func<IDbConnection> connectionFactory;
18+
private readonly string tableName;
19+
private readonly string schemaTableName;
20+
private readonly IUpgradeLog log;
21+
22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="SqlTableJournal"/> class.
24+
/// </summary>
25+
/// <param name="connectionFactory">The connection factory.</param>
26+
/// <param name="schema">The schema that contains the table.</param>
27+
/// <param name="table">The table name.</param>
28+
/// <param name="logger">The log.</param>
29+
/// <example>
30+
/// var journal = new TableJournal("Server=server;Database=database;Trusted_Connection=True", "dbo", "MyVersionTable");
31+
/// </example>
32+
public SqlTableJournal(Func<IDbConnection> connectionFactory, string schema, string table, IUpgradeLog logger)
33+
{
34+
this.connectionFactory = connectionFactory;
35+
schemaTableName = tableName = table;
36+
if (!string.IsNullOrEmpty(schema))
37+
schemaTableName = schema + "." + tableName;
38+
log = logger;
39+
}
40+
41+
/// <summary>
42+
/// Recalls the version number of the database.
43+
/// </summary>
44+
/// <returns>All executed scripts.</returns>
45+
public string[] GetExecutedScripts()
46+
{
47+
log.WriteInformation("Fetching list of already executed scripts.");
48+
var exists = DoesTableExist();
49+
if (!exists)
50+
{
51+
log.WriteInformation(string.Format("The {0} table could not be found. The database is assumed to be at version 0.", schemaTableName));
52+
return new string[0];
53+
}
54+
55+
var scripts = new List<string>();
56+
using (var connection = connectionFactory())
57+
using (var command = connection.CreateCommand())
58+
{
59+
command.CommandText = string.Format("select [ScriptName] from {0} order by [ScriptName]", schemaTableName);
60+
command.CommandType = CommandType.Text;
61+
connection.Open();
62+
63+
using(var reader = command.ExecuteReader())
64+
{
65+
while (reader.Read())
66+
scripts.Add((string) reader[0]);
67+
}
68+
}
69+
return scripts.ToArray();
70+
}
71+
72+
/// <summary>
73+
/// Records a database upgrade for a database specified in a given connection string.
74+
/// </summary>
75+
/// <param name="script">The script.</param>
76+
public void StoreExecutedScript(SqlScript script)
77+
{
78+
var exists = DoesTableExist();
79+
if (!exists)
80+
{
81+
log.WriteInformation(string.Format("Creating the {0} table", schemaTableName));
82+
83+
using (var connection = connectionFactory())
84+
using (var command = connection.CreateCommand())
85+
{
86+
command.CommandText = string.Format(
87+
@"create table {0} (
88+
[Id] int identity(1,1) not null constraint PK_SchemaVersions_Id primary key,
89+
[ScriptName] nvarchar(255) not null,
90+
[Applied] datetime not null
91+
)", schemaTableName);
92+
93+
command.CommandType = CommandType.Text;
94+
connection.Open();
95+
96+
command.ExecuteNonQuery();
97+
}
98+
99+
log.WriteInformation(string.Format("The {0} table has been created", schemaTableName));
100+
}
101+
102+
103+
using (var connection = connectionFactory())
104+
using (var command = connection.CreateCommand())
105+
{
106+
command.CommandText = string.Format("insert into {0} (ScriptName, Applied) values (@scriptName, '{1}')", schemaTableName, DateTime.UtcNow.ToString("yyyy-MM-dd hh:mm:ss"));
107+
108+
var param = command.CreateParameter();
109+
param.ParameterName = "scriptName";
110+
param.Value = script.Name;
111+
command.Parameters.Add(param);
112+
113+
command.CommandType = CommandType.Text;
114+
connection.Open();
115+
116+
command.ExecuteNonQuery();
117+
}
118+
}
119+
120+
private bool DoesTableExist()
121+
{
122+
try
123+
{
124+
using (var connection = connectionFactory())
125+
{
126+
using (var command = connection.CreateCommand())
127+
{
128+
command.CommandText = string.Format("select count(*) from {0}", schemaTableName);
129+
command.CommandType = CommandType.Text;
130+
connection.Open();
131+
command.ExecuteScalar();
132+
return true;
133+
}
134+
}
135+
}
136+
catch (SqlException)
137+
{
138+
return false;
139+
}
140+
catch (DbException)
141+
{
142+
return false;
143+
}
144+
}
145+
}
146146
}

0 commit comments

Comments
 (0)