Skip to content

Commit

Permalink
If not specified, the Name, Email, and Comment fields should always b…
Browse files Browse the repository at this point in the history
…e NULL instead of an empty string

+ updated UpdateEntry method to set values as null if empty or whitespace
+ added migration to fix existing values in the database
  • Loading branch information
abjerner committed Mar 9, 2021
1 parent acf717e commit 193f78b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ public void Initialize() {
var plan = new MigrationPlan("Skybrud.Umbraco.Feedback");

plan.From(string.Empty)
.To<CreateTableMigration>("3.0.0-alpha001");
.To<CreateTableMigration>("3.0.0-alpha001")
.To<FixEmptyStringValuesMigration>("3.0.0-alpha004");

var upgrader = new Upgrader(plan);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using Skybrud.Umbraco.Feedback.Constants;
using Umbraco.Core.Logging;
using Umbraco.Core.Migrations;

namespace Skybrud.Umbraco.Feedback.Migrations {

public class FixEmptyStringValuesMigration : MigrationBase {

public FixEmptyStringValuesMigration(IMigrationContext context) : base(context) { }

public override void Migrate() {

if (!TableExists(FeedbackConstants.TableName)) return;

int affected1 = Context.Database.Execute("UPDATE [SkybrudFeedback] SET [Name] = null WHERE [Name] LIKE '';");
int affected2 = Context.Database.Execute("UPDATE [SkybrudFeedback] SET [Email] = null WHERE [Email] LIKE '';");
int affected3 = Context.Database.Execute("UPDATE [SkybrudFeedback] SET [Comment] = null WHERE [Comment] LIKE '';");

Logger.Info<FixEmptyStringValuesMigration>($"Fixed empty string values for name column in {affected1} rows.");
Logger.Info<FixEmptyStringValuesMigration>($"Fixed empty string values for email column in {affected2} rows.");
Logger.Info<FixEmptyStringValuesMigration>($"Fixed empty string values for comment column in {affected3} rows.");

}

}

}
7 changes: 6 additions & 1 deletion src/Skybrud.Umbraco.Feedback/Services/FeedbackService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ public FeedbackEntry GetEntryByKey(Guid key) {
public void Archive(FeedbackEntry entry) {

if (entry == null) throw new ArgumentNullException(nameof(entry));

entry.IsArchived = true;

_databaseService.Update(entry._entry);
Expand Down Expand Up @@ -313,6 +313,11 @@ public AddRatingResult AddRating(FeedbackSiteSettings site, IPublishedContent pa
}

public UpdateEntryResult UpdateEntry(FeedbackEntry entry) {

// Ensure the string values are NULL (opposed to empty or white space)
entry.Name = FeedbackUtils.TrimToNull(entry.Name);
entry.Email = FeedbackUtils.TrimToNull(entry.Email);
entry.Comment = FeedbackUtils.TrimToNull(entry.Comment);

// Attempt to add the entry to the database
try {
Expand Down

0 comments on commit 193f78b

Please sign in to comment.