Skip to content

Commit

Permalink
Merge pull request #27 from Zocdoc/mvs-schemazen-create-tables
Browse files Browse the repository at this point in the history
Mvs schemazen create tables
  • Loading branch information
marcio-santos-zocdoc authored Dec 13, 2016
2 parents fe679c1 + 61bda32 commit 5780cec
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
3 changes: 2 additions & 1 deletion console/Create.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ public override int Run(string[] remainingArguments) {
};

try {
createCommand.Execute(DatabaseFilesPath);
createCommand.CreateDatabase(DatabaseFilesPath);
createCommand.CreateTables(DatabaseFilesPath);
} catch (BatchSqlFileException ex) {
_logger.Log(TraceLevel.Info, Environment.NewLine + "Create completed with the following errors:");
foreach (var e in ex.Exceptions) {
Expand Down
16 changes: 13 additions & 3 deletions model/Command/CreateCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace SchemaZen.Library.Command {
public class CreateCommand : BaseCommand {

public void Execute(string databaseFilesPath)
public void CreateDatabase(string databaseFilesPath)
{
var db = CreateDatabase();
if (!Directory.Exists(db.Dir))
Expand All @@ -20,8 +20,18 @@ public void Execute(string databaseFilesPath)
throw new InvalidOperationException(msg);
}

db.CreateFromDir(Overwrite, databaseFilesPath, Logger.Log);
Logger.Log(TraceLevel.Info, Environment.NewLine + "Database created successfully.");
db.CreateDBFromDir(databaseFilesPath, Logger.Log);
}

public void CreateTables(string databaseFilesPath)
{
var db = CreateDatabase();
if (!Directory.Exists(db.Dir))
{
throw new FileNotFoundException(string.Format("Snapshot dir {0} does not exist.", db.Dir));
}

db.CreateDbObjectsFromDir(databaseFilesPath, Logger.Log);
}
}
}
81 changes: 45 additions & 36 deletions model/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1420,43 +1420,11 @@ public void ImportData(Action<TraceLevel, string> log = null) {
log(TraceLevel.Info, string.Format("Data imported successfully for {0}", DatabaseName));
}

public void CreateFromDir(bool overwrite, string databaseFilesPath = null, Action<TraceLevel, string> log = null) {
public void CreateDbObjectsFromDir(string databaseFilesPath = null, Action<TraceLevel, string> log = null) {
if (log == null) log = (tl, s) => { };

if (DBHelper.DbExists(Connection)) {
log(TraceLevel.Verbose, string.Format("Dropping existing database for {0}", DatabaseName));
DBHelper.DropDb(Connection);
log(TraceLevel.Verbose, string.Format("Existing database dropped for {0}", DatabaseName));
}

log(TraceLevel.Info, string.Format("Creating database {0}", DatabaseName));
//create database
DBHelper.CreateDb(Connection, databaseFilesPath);

//run scripts
if (File.Exists(Dir + "/props.sql")) {
log(TraceLevel.Verbose, string.Format("Setting database properties {0}", DatabaseName));
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/props.sql"));
} catch (SqlBatchException ex) {
throw new SqlFileException(Dir + "/props.sql", ex);
}

// COLLATE can cause connection to be reset
// so clear the pool so we get a new connection
DBHelper.ClearPool(Connection);
}

if (File.Exists(Dir + "/schemas.sql")) {
log(TraceLevel.Verbose, string.Format("Creating database schemas for {0}", DatabaseName));
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
} catch (SqlBatchException ex) {
throw new SqlFileException(Dir + "/schemas.sql", ex);
}
}

log(TraceLevel.Info, string.Format("Creating database objects for {0}", DatabaseName));
log(TraceLevel.Info, "Creating database objects...");
// create db objects

// resolve dependencies by trying over and over
Expand Down Expand Up @@ -1518,9 +1486,50 @@ public void CreateFromDir(bool overwrite, string databaseFilesPath = null, Actio
ex.Exceptions = errors;
throw ex;
}
}

private List<string> GetScripts() {
log(TraceLevel.Info, Environment.NewLine + "Database objects created successfully.");
}

public void CreateDBFromDir(string databaseFilesPath, Action<TraceLevel, string> log) {
if (log == null) log = (tl, s) => { };

if (DBHelper.DbExists(Connection)) {
log(TraceLevel.Verbose, string.Format("Dropping existing database for {0}", DatabaseName));
DBHelper.DropDb(Connection);
log(TraceLevel.Verbose, string.Format("Existing database dropped for {0}", DatabaseName));
}

log(TraceLevel.Info, string.Format("Creating database {0}", DatabaseName));
//create database
DBHelper.CreateDb(Connection, databaseFilesPath);

//run scripts
if (File.Exists(Dir + "/props.sql")) {
log(TraceLevel.Verbose, string.Format("Setting database properties {0}", DatabaseName));
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/props.sql"));
} catch (SqlBatchException ex) {
throw new SqlFileException(Dir + "/props.sql", ex);
}

// COLLATE can cause connection to be reset
// so clear the pool so we get a new connection
DBHelper.ClearPool(Connection);
}

if (File.Exists(Dir + "/schemas.sql")) {
log(TraceLevel.Verbose, string.Format("Creating database schemas for {0}", DatabaseName));
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
} catch (SqlBatchException ex) {
throw new SqlFileException(Dir + "/schemas.sql", ex);
}
}

log(TraceLevel.Info, Environment.NewLine + string.Format("Database {0} created successfully.", DatabaseName));
}

private List<string> GetScripts() {
var scripts = new List<string>();
foreach (
var dirPath in _dirs.Where(dir => dir != "foreign_keys").Select(dir => Dir + "/" + dir).Where(Directory.Exists)) {
Expand Down
2 changes: 1 addition & 1 deletion model/Schemazen.Library.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<package >
<metadata>
<id>Schemazen.Library</id>
<version>1.0.66</version>
<version>1.0.69</version>
<title>Schemazen.Library</title>
<authors>sethreno</authors>
<owners>sethreno</owners>
Expand Down
2 changes: 1 addition & 1 deletion test/DatabaseTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ public void TestScriptToDir() {
var copy = new Database("ScriptToDirTestCopy");
copy.Dir = db.Dir;
copy.Connection = ConfigHelper.TestDB.Replace("database=TESTDB", "database=" + copy.Name);
copy.CreateFromDir(true);
copy.CreateDbObjectsFromDir();
copy.Load();
TestCompare(db, copy);
}
Expand Down

0 comments on commit 5780cec

Please sign in to comment.