Skip to content

Commit

Permalink
init now creates database if missing.
Browse files Browse the repository at this point in the history
  • Loading branch information
drunkcod committed May 12, 2015
1 parent a8ac596 commit 6dacc77
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 24 deletions.
2 changes: 2 additions & 0 deletions DataBoss/DataBoss.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
<Compile Include="DataBossConfiguration.cs" />
<Compile Include="DataBossConsoleLogMigrationScope.cs" />
<Compile Include="DataBossDirectoryMigration.cs" />
<Compile Include="DataBossMigrationInfo.cs" />
<Compile Include="DataBossMigrationPath.cs" />
<Compile Include="DataBossMigrator.cs" />
<Compile Include="DataBossSqlMigrationScope.cs" />
<Compile Include="DataBossTextMigration.cs" />
Expand Down
13 changes: 13 additions & 0 deletions DataBoss/DataBossMigrationInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace DataBoss
{
public class DataBossMigrationInfo
{
public long Id;
public string Context;
public string Name;

public string FullId { get {
return string.IsNullOrEmpty(Context) ? Id.ToString() : string.Format("{0}.{1}", Context, Id);
} }
}
}
13 changes: 13 additions & 0 deletions DataBoss/DataBossMigrationPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System.Xml.Serialization;

namespace DataBoss
{
public class DataBossMigrationPath
{
[XmlAttribute("context")]
public string Context;

[XmlAttribute("path")]
public string Path;
}
}
44 changes: 21 additions & 23 deletions DataBoss/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,9 @@
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Xml.Serialization;

namespace DataBoss
{
public class DataBossMigrationPath
{
[XmlAttribute("context")]
public string Context;

[XmlAttribute("path")]
public string Path;
}

public class DataBossMigrationInfo
{
public long Id;
public string Context;
public string Name;

public string FullId { get {
return string.IsNullOrEmpty(Context) ? Id.ToString() : string.Format("{0}.{1}", Context, Id);
} }
}

public class Program
{
static string ProgramName { get { return Path.GetFileName(typeof(Program).Assembly.Location); } }
Expand Down Expand Up @@ -63,7 +42,6 @@ static int Main(string[] args) {
}

using(var db = new SqlConnection(cc.Value.GetConnectionString())) {
db.Open();
command(new Program(db), cc.Value);
}

Expand All @@ -87,6 +65,7 @@ static string GetUsageString() {
}

void Initialize(DataBossConfiguration config) {
EnsureDatabse(config.GetConnectionString());
using(var cmd = new SqlCommand(@"
if not exists(select * from sys.tables t where t.name = '__DataBossHistory') begin
create table __DataBossHistory(
Expand All @@ -108,13 +87,31 @@ add constraint PK_DataBossHistory primary key(
end
", db))
{
db.Open();
using(var r = cmd.ExecuteReader())
while(r.Read())
Console.WriteLine(r.GetValue(0));
}
}


static void EnsureDatabse(string connectionString) {
var qs = new SqlConnectionStringBuilder(connectionString);
var dbName = qs.InitialCatalog;
qs.Remove("Initial Catalog");
using(var db = new SqlConnection(qs.ConnectionString)) {
db.Open();
using(var cmd = new SqlCommand("select db_id(@db)" ,db)) {
cmd.Parameters.AddWithValue("@db", dbName);
if(cmd.ExecuteScalar() is DBNull) {
cmd.CommandText = "create database [" + dbName + "]";
cmd.ExecuteNonQuery();
}
}
}
}

void Status(DataBossConfiguration config) {
db.Open();
var pending = GetPendingMigrations(config);
if(pending.Count != 0) {
Console.WriteLine("Pending migrations:");
Expand All @@ -124,6 +121,7 @@ void Status(DataBossConfiguration config) {
}

void Update(DataBossConfiguration config) {
db.Open();
var pending = GetPendingMigrations(config);
Console.WriteLine("{0} pending migrations found.", pending.Count);

Expand Down
2 changes: 1 addition & 1 deletion DataBoss/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
[assembly: AssemblyCulture("")]
[assembly: ComVisible(false)]
[assembly: Guid("100edf18-fcc4-4676-9ddb-b480ac700ae4")]
[assembly: AssemblyVersion("0.0.0.4")]
[assembly: AssemblyVersion("0.0.0.5")]

0 comments on commit 6dacc77

Please sign in to comment.