Created
June 10, 2013 17:14
-
-
Save svallory/5750525 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
namespace Sparrow.Migrations | |
{ | |
using System; | |
using System.Data.Entity.Migrations; | |
using System.IO; | |
public abstract class ScriptMigration : DbMigration | |
{ | |
public string MigrationsDir | |
{ | |
get | |
{ | |
return Path.Combine(DevEnv.ProjectDirectories.Infra, "Migrations"); | |
} | |
} | |
private string migrationName; | |
public string MigrationName | |
{ | |
get | |
{ | |
if (!string.IsNullOrEmpty(migrationName)) | |
return migrationName; | |
if(!Directory.Exists(MigrationsDir)) | |
throw new Exception(string.Format("The migrations directory could not be found. {0}. This path is based on DevEnv.ProjectDirectories.Infra.", MigrationsDir)); | |
var files = Directory.GetFiles(this.MigrationsDir, "*.cs", SearchOption.TopDirectoryOnly); | |
var className = this.GetType().Name; | |
foreach (var file in files) | |
{ | |
if (file.EndsWith(className + ".cs")) | |
{ | |
return this.migrationName = Path.GetFileNameWithoutExtension(file); | |
} | |
} | |
throw new Exception(string.Format( | |
"Couldn't find the file for the migration {0}. " + | |
"The file name of a migration MUST follow the format [timestamp]_[classname].cs. " + | |
"For this migration the correct file name is {1}", | |
this.GetType().Name, | |
"999999999999999_" + this.GetType().Name + ".cs")); | |
} | |
} | |
public string ScriptPath | |
{ | |
get | |
{ | |
var mig = this.MigrationsDir; | |
return Path.Combine(this.MigrationsDir, "Scripts", this.MigrationName + ".sql"); | |
} | |
} | |
public override void Up() | |
{ | |
using (var ctx = new MigrationCtx()) | |
{ | |
ctx.Database.ExecuteSqlCommand(File.ReadAllText(this.ScriptPath)); | |
} | |
} | |
public override void Down() | |
{ | |
throw new Exception("Sparrow doesn't support reverting migrations"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment