Skip to content

Commit

Permalink
Merge pull request #54 from keith-hall/data-folder
Browse files Browse the repository at this point in the history
fix creating data folder when no data is exported
  • Loading branch information
sethreno committed Dec 17, 2015
2 parents fae7722 + d59fbdf commit 2e789ec
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
7 changes: 5 additions & 2 deletions model/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,8 @@ private static string MakeFileName(string schema, string name) {
}

public void ExportData(string tableHint = null, Action<TraceLevel, string> log = null) {
if (!DataTables.Any())
return;
var dataDir = Dir + "/data";
if (!Directory.Exists(dataDir)) {
Directory.CreateDirectory(dataDir);
Expand Down Expand Up @@ -1100,13 +1102,14 @@ public static string ScriptPropList(IList<DbProp> props) {
#region Create

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

var dataDir = Dir + "\\data";
if (!Directory.Exists(dataDir)) {
log(TraceLevel.Verbose, "No data to import.");
return;
}

if (log == null) log = (tl, s) => { };

log(TraceLevel.Verbose, "Loading database schema...");
Load(); // load the schema first so we can import data
log(TraceLevel.Verbose, "Database schema loaded.");
Expand Down
48 changes: 48 additions & 0 deletions test/DatabaseTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,5 +471,53 @@ public void TestScriptToDir() {
copy.Load();
TestCompare(db, copy);
}

[Test]
public void TestScriptToDirOnlyCreatesNecessaryFolders()
{
var db = new Database("TestEmptyDB");

db.Connection = ConfigHelper.TestDB.Replace("database=TESTDB", "database=" + db.Name);

db.ExecCreate(true);

db.Dir = db.Name;
db.Load();

if (Directory.Exists(db.Dir)) // if the directory exists, delete it to make it a fair test
{
Directory.Delete(db.Dir, true);
}

db.ScriptToDir();

Assert.AreEqual(0, db.Assemblies.Count);
Assert.AreEqual(0, db.DataTables.Count);
Assert.AreEqual(0, db.ForeignKeys.Count);
Assert.AreEqual(0, db.Routines.Count);
Assert.AreEqual(0, db.Schemas.Count);
Assert.AreEqual(0, db.Synonyms.Count);
Assert.AreEqual(0, db.Tables.Count);
Assert.AreEqual(0, db.TableTypes.Count);
Assert.AreEqual(0, db.Users.Count);
Assert.AreEqual(0, db.ViewIndexes.Count);

Assert.IsTrue(Directory.Exists(db.Name));
Assert.IsTrue(File.Exists(db.Name + "\\props.sql"));
//Assert.IsFalse(File.Exists(db.Name + "\\schemas.sql"));

Assert.IsFalse(Directory.Exists(db.Name + "\\assemblies"));
Assert.IsFalse(Directory.Exists(db.Name + "\\data"));
Assert.IsFalse(Directory.Exists(db.Name + "\\foreign_keys"));
foreach (var routineType in Enum.GetNames(typeof(Routine.RoutineKind)))
{
var dir = routineType.ToLower() + "s";
Assert.IsFalse(Directory.Exists(db.Name + "\\" + dir));
}
Assert.IsFalse(Directory.Exists(db.Name + "\\synonyms"));
Assert.IsFalse(Directory.Exists(db.Name + "\\tables"));
Assert.IsFalse(Directory.Exists(db.Name + "\\table_types"));
Assert.IsFalse(Directory.Exists(db.Name + "\\users"));
}
}
}

0 comments on commit 2e789ec

Please sign in to comment.