Skip to content
This repository has been archived by the owner on May 2, 2022. It is now read-only.

Commit

Permalink
[Core] (Improvement) Add better exception handling and atomic operati…
Browse files Browse the repository at this point in the history
…on for `mvt` command in case of failure
  • Loading branch information
chaojian-zhang committed Sep 14, 2019
1 parent d1ecdf5 commit 4a1f3f7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
31 changes: 22 additions & 9 deletions SomewhereStandard/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -843,10 +843,10 @@ public IEnumerable<string> MVT(params string[] args)
List<string> results = new List<string>();
if (!IsTagInDatabase(sourceTag))
throw new InvalidOperationException($"Specified tag `{sourceTag}` does not exist in database.");
// Commit (already in a transaction)
TryRecordCommit(JournalEvent.CommitOperation.RenameTag, sourceTag, args[1]);
// Perform actions in a transaction for efficiency
// Perform actions in a transaction for efficiency and atomity
SQLiteTransaction transaction = Connection.BeginTransaction(); // Create and dispose manually to avoid changing too many lines in git
// Commit
TryRecordCommit(JournalEvent.CommitOperation.RenameTag, sourceTag, args[1], transaction);
// Get files with old tag
List<int> sourceFileIDs = FilterByTags(new string[] { sourceTag }, transaction).Select(f => f.ID).ToList();
List<TagRow> tagIDs = GetTagRows(targetTags.Union(new string[] { sourceTag })); // Including all mentioned tags that exist
Expand Down Expand Up @@ -876,16 +876,18 @@ void AppendNewTag(string targetTag)
AddFileTags(sourceFileIDs, targetTagID, transaction);
}
// Rename source tag and add new multiple new tags
try
{
for (int i = 0; i < targetTags.Length; i++)
{
var targetTag = targetTags[i];
// Handle same
if(sourceTag == targetTag)
if (sourceTag == targetTag)
{
results.Add($"Tag `{targetTag}` is the same as source tag, skipped.");
continue;
}
// For first new tag, old one still exists, so just rename or merge it
// For first new tag, old one (source tag) still exists, so just rename or merge it (source tag) (into new tag, if new tag already exist)
if (i == 0)
{
if (!IsTagInDatabase(targetTag, transaction)) // If target doesn't exist yet just rename source
Expand Down Expand Up @@ -917,8 +919,12 @@ void AppendNewTag(string targetTag)
}
}
transaction.Commit();
}
catch (Exception e) { return new string[] { $"An error happended during renaming tags: {e.Message}; " +
$"Rest assured - all changes are reverted, nothing is affected. Notice it's known that currently we are " +
$"having issue renaming a tag for items that already contains target tag as their tag." }; }
transaction.Dispose();

return results;
}
[Command("Create a new Somewhere home at current home directory.")]
Expand Down Expand Up @@ -2202,10 +2208,13 @@ void OutputDependingOnWwitch(string line)
/// <summary>
/// Try to make a commit journal entry if commiting is enabled
/// </summary>
private void TryRecordCommit(JournalEvent.CommitOperation operation, string itemname, string value)
private void TryRecordCommit(JournalEvent.CommitOperation operation, string itemname, string value, SQLiteTransaction transaction = null)
{
// Whether the transaction object is created within this function of passed from outside
bool shouldManageTransaction = (transaction == null);
// Check whether we have enabled committing and record new journal entry in one single line instead of calling ShouldRecordCommits and AddJournal()
using(SQLiteTransaction transaction = Connection.BeginTransaction())
if (shouldManageTransaction)
transaction = Connection.BeginTransaction();
{
bool shouldRecordCommit = Connection.ExecuteQuery(transaction,
@"select Value from Configuration where Key=@key", new { key = "RegisterCommits" })
Expand All @@ -2224,7 +2233,11 @@ private void TryRecordCommit(JournalEvent.CommitOperation operation, string item
}),
type = JournalType.Commit.ToString()
});
transaction.Commit();
if(shouldManageTransaction)
{
transaction.Commit();
transaction.Dispose();
}
}
}
}
Expand Down
1 change: 1 addition & 0 deletions TODO List.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ GetPhysicalName(string itemName) has very serious issue - as when called by Add(
Allow foreign reference (absolute path) implementation.
(Bug) If we are at Notebook tab tags field and press F2, tags edit will not be saved
(Bug) Currently for `mvt` command if the target tag is already one of the tags in the items that are tagged with source tag there will be an UNIQUE constraint failture during `mvt` process because we are trying to add the target tag again to the item. This will be fixed inside the logic for updating item tags.
Import/Export implementation
Filename Tests
Expand Down

0 comments on commit 4a1f3f7

Please sign in to comment.