Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into mvs-schemazen-creat…
Browse files Browse the repository at this point in the history
…e-tables

# Conflicts:
#	model/Models/Database.cs
#	model/Schemazen.Library.nuspec
  • Loading branch information
marcio-santos-zocdoc committed Dec 12, 2016
2 parents 0f16154 + fe679c1 commit b5de691
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 19 deletions.
45 changes: 27 additions & 18 deletions model/Models/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,16 @@ public Database(string name, IList<string> filteredTypes = null)
public const string SqlRegularIdentifierRegex = @"(?!\d)[\w@$#]+";
// see rules for regular identifiers here https://msdn.microsoft.com/en-us/library/ms175874.aspx

public static readonly string AutoGenerateComment = String.Format(
@"/*----------------------------------------------------------------------------
public const string AutoGenerateComment = @"/*----------------------------------------------------------------------------
<auto-generated>
This file was generated by schemazen.
Date: {0}
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
</auto-generated>
----------------------------------------------------------------------------*/
", DateTime.Today.ToString("MM-dd-yyyy"));
";

#region " Properties "

Expand All @@ -93,7 +91,18 @@ the code is regenerated.
public List<SqlUser> Users = new List<SqlUser>();
public List<Constraint> ViewIndexes = new List<Constraint>();

public DbProp FindProp(string name) {
private string _databaseName ;

private string DatabaseName {
get {
if (_databaseName == null) {
_databaseName = new SqlConnectionStringBuilder(Connection).InitialCatalog;
}
return _databaseName;
}
}

public DbProp FindProp(string name) {
return Props.FirstOrDefault(p => string.Equals(p.Name, name, StringComparison.CurrentCultureIgnoreCase));
}

Expand Down Expand Up @@ -1381,10 +1390,10 @@ public void ImportData(Action<TraceLevel, string> log = null) {
return;
}

log(TraceLevel.Verbose, "Loading database schema...");
log(TraceLevel.Verbose, string.Format("Loading database schema for {0}", DatabaseName));
Load(); // load the schema first so we can import data
log(TraceLevel.Verbose, "Database schema loaded.");
log(TraceLevel.Info, "Importing data...");
log(TraceLevel.Verbose, string.Format("Database schema loaded for {0}", DatabaseName));
log(TraceLevel.Info, string.Format("Importing data for {0}", DatabaseName));

foreach (var f in Directory.GetFiles(dataDir)) {
var fi = new FileInfo(f);
Expand All @@ -1408,14 +1417,14 @@ public void ImportData(Action<TraceLevel, string> log = null) {
throw new DataFileException(ex.Message, fi.FullName, -1);
}
}
log(TraceLevel.Info, "Data imported successfully.");
log(TraceLevel.Info, string.Format("Data imported successfully for {0}", DatabaseName));
}

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


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

// resolve dependencies by trying over and over
Expand All @@ -1427,13 +1436,13 @@ public void CreateDbObjectsFromDir(string databaseFilesPath = null, Action<Trace
if (errors.Count > 0) {
prevCount = errors.Count;
log(TraceLevel.Info, string.Format(
"{0} errors occurred, retrying...", errors.Count));
"{0} errors occurred, retrying database {1}...", errors.Count, DatabaseName));
}
errors.Clear();
var index = 0;
var total = scripts.Count;
foreach (var f in scripts.ToArray()) {
log(TraceLevel.Verbose, string.Format("Executing script {0} of {1}...{2}", ++index, total, index < total ? "\r" : string.Empty));

log(TraceLevel.Verbose, string.Format("Executing {0} scripts against {1}", scripts.Count, DatabaseName));

foreach (var f in scripts.ToArray()) {
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
scripts.Remove(f);
Expand All @@ -1444,13 +1453,13 @@ public void CreateDbObjectsFromDir(string databaseFilesPath = null, Action<Trace
}
}
if (prevCount > 0)
log(TraceLevel.Info, errors.Any() ? string.Format("{0} errors unresolved. Details will follow later.", prevCount) : "All errors resolved, were probably dependency issues...");
log(TraceLevel.Info, errors.Any() ? string.Format("{0} errors unresolved for {1}. Details will follow later.", prevCount, DatabaseName) : "All errors resolved, were probably dependency issues...");
log(TraceLevel.Info, string.Empty);

ImportData(log); // load data

if (Directory.Exists(Dir + "/after_data")) {
log(TraceLevel.Verbose, "Executing after-data scripts...");
log(TraceLevel.Verbose, string.Format("Executing after-data scripts {0}", DatabaseName));
foreach (var f in Directory.GetFiles(Dir + "/after_data", "*.sql")) {
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
Expand All @@ -1462,7 +1471,7 @@ public void CreateDbObjectsFromDir(string databaseFilesPath = null, Action<Trace

// foreign keys
if (Directory.Exists(Dir + "/foreign_keys")) {
log(TraceLevel.Info, "Adding foreign key constraints...");
log(TraceLevel.Info, string.Format("Adding foreign key constraints {0}", DatabaseName));
foreach (var f in Directory.GetFiles(Dir + "/foreign_keys", "*.sql")) {
try {
DBHelper.ExecBatchSql(Connection, File.ReadAllText(f));
Expand Down
2 changes: 1 addition & 1 deletion model/Schemazen.Library.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<metadata>
<id>Schemazen.Library</id>
<version>1.0.69</version>
<title>Script and create SQL Server objects quickly</title>
<title>Schemazen.Library</title>
<authors>sethreno</authors>
<owners>sethreno</owners>
<licenseUrl>https://opensource.org/licenses/MIT</licenseUrl>
Expand Down

0 comments on commit b5de691

Please sign in to comment.