Skip to content

Commit

Permalink
- Split Create command in create databases and create tables
Browse files Browse the repository at this point in the history
  • Loading branch information
marcio-santos-zocdoc committed Dec 12, 2016
1 parent 3fb8078 commit 0f16154
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 40 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.CreateDatabases(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: 14 additions & 2 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 CreateDatabases(string databaseFilesPath)
{
var db = CreateDatabase();
if (!Directory.Exists(db.Dir))
Expand All @@ -20,7 +20,19 @@ public void Execute(string databaseFilesPath)
throw new InvalidOperationException(msg);
}

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

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);
Logger.Log(TraceLevel.Info, Environment.NewLine + "Database created successfully.");
}
}
Expand Down
75 changes: 40 additions & 35 deletions model/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,43 +1411,11 @@ public void ImportData(Action<TraceLevel, string> log = null) {
log(TraceLevel.Info, "Data imported successfully.");
}

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, "Dropping existing database...");
DBHelper.DropDb(Connection);
log(TraceLevel.Verbose, "Existing database dropped.");
}

log(TraceLevel.Info, "Creating database...");
//create database
DBHelper.CreateDb(Connection, databaseFilesPath);

//run scripts
if (File.Exists(Dir + "/props.sql")) {
log(TraceLevel.Verbose, "Setting database properties...");
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, "Creating database schemas...");
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
} catch (SqlBatchException ex) {
throw new SqlFileException(Dir + "/schemas.sql", ex);
}
}

log(TraceLevel.Info, "Creating database objects...");
log(TraceLevel.Info, "Creating database objects...");
// create db objects

// resolve dependencies by trying over and over
Expand Down Expand Up @@ -1511,7 +1479,44 @@ public void CreateFromDir(bool overwrite, string databaseFilesPath = null, Actio
}
}

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

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

log(TraceLevel.Info, "Creating database...");
//create database
DBHelper.CreateDb(Connection, databaseFilesPath);

//run scripts
if (File.Exists(Dir + "/props.sql")) {
log(TraceLevel.Verbose, "Setting database properties...");
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, "Creating database schemas...");
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(Dir + "/schemas.sql"));
} catch (SqlBatchException ex) {
throw new SqlFileException(Dir + "/schemas.sql", ex);
}
}
}

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.1</version>
<version>1.0.69</version>
<title>Script and create SQL Server objects quickly</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 0f16154

Please sign in to comment.