From 8a04c4f83b920a7c6b16be73aede3ae4fedeefd4 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 21 Sep 2021 21:06:33 -0400 Subject: [PATCH] Add data migration project --- src/MyPrayerJournal/Api/Data.fs | 6 +-- src/MyPrayerJournal/Api/Program.fs | 7 +-- src/MyPrayerJournal/Migrate/Migrate.fsproj | 21 ++++++++ src/MyPrayerJournal/Migrate/Program.fs | 58 ++++++++++++++++++++++ src/MyPrayerJournal/Migrate/mpj-log.db | 0 5 files changed, 86 insertions(+), 6 deletions(-) create mode 100644 src/MyPrayerJournal/Migrate/Migrate.fsproj create mode 100644 src/MyPrayerJournal/Migrate/Program.fs create mode 100644 src/MyPrayerJournal/Migrate/mpj-log.db diff --git a/src/MyPrayerJournal/Api/Data.fs b/src/MyPrayerJournal/Api/Data.fs index a5e82f1..2e567dd 100644 --- a/src/MyPrayerJournal/Api/Data.fs +++ b/src/MyPrayerJournal/Api/Data.fs @@ -31,14 +31,14 @@ module Mapping = let doc = BsonDocument () doc.["asOf"] <- BsonValue (Ticks.toLong hist.asOf) doc.["status"] <- BsonValue (RequestAction.toString hist.status) - doc.["text"] <- BsonValue (Option.toObj hist.text) + doc.["text"] <- BsonValue (match hist.text with Some t -> t | None -> "") upcast doc /// Map a BSON document to a history entry let historyFromBson (doc : BsonValue) = { asOf = Ticks doc.["asOf"].AsInt64 status = RequestAction.fromString doc.["status"].AsString - text = match doc.["text"].IsNull with true -> None | false -> Some doc.["text"].AsString + text = match doc.["text"].AsString with "" -> None | txt -> Some txt } /// Map a note entry to BSON @@ -65,7 +65,7 @@ module Mapping = doc.["recurType"] <- BsonValue (Recurrence.toString req.recurType) doc.["recurCount"] <- BsonValue req.recurCount doc.["history"] <- BsonArray (req.history |> List.map historyToBson |> Seq.ofList) - doc.["notes"] <- BsonValue (req.notes |> List.map noteToBson |> Seq.ofList) + doc.["notes"] <- BsonArray (req.notes |> List.map noteToBson |> Seq.ofList) upcast doc /// Map a BSON document to a request diff --git a/src/MyPrayerJournal/Api/Program.fs b/src/MyPrayerJournal/Api/Program.fs index 0837b40..5eb3905 100644 --- a/src/MyPrayerJournal/Api/Program.fs +++ b/src/MyPrayerJournal/Api/Program.fs @@ -76,14 +76,15 @@ module Configure = fun opts -> let jwtCfg = bldr.Configuration.GetSection "Auth0" opts.Authority <- sprintf "https://%s/" jwtCfg.["Domain"] - opts.Audience <- jwtCfg.["Id"] - ) + opts.Audience <- jwtCfg.["Id"]) |> ignore let jsonOptions = JsonSerializerOptions () jsonOptions.Converters.Add (JsonFSharpConverter ()) + let db = new LiteDatabase (bldr.Configuration.GetConnectionString "db") + Data.Startup.ensureDb db bldr.Services.AddSingleton(jsonOptions) .AddSingleton() - .AddSingleton(fun _ -> new LiteDatabase (bldr.Configuration.GetConnectionString "db")) + .AddSingleton(db) |> ignore bldr.Build () diff --git a/src/MyPrayerJournal/Migrate/Migrate.fsproj b/src/MyPrayerJournal/Migrate/Migrate.fsproj new file mode 100644 index 0000000..2a374f8 --- /dev/null +++ b/src/MyPrayerJournal/Migrate/Migrate.fsproj @@ -0,0 +1,21 @@ + + + + Exe + net6.0 + + + + + + + + + + + + + + + + diff --git a/src/MyPrayerJournal/Migrate/Program.fs b/src/MyPrayerJournal/Migrate/Program.fs new file mode 100644 index 0000000..2534f7c --- /dev/null +++ b/src/MyPrayerJournal/Migrate/Program.fs @@ -0,0 +1,58 @@ +open FSharp.Data +open FSharp.Data.CsvExtensions +open LiteDB +open MyPrayerJournal.Domain + +module Subdocs = + + open FSharp.Data.JsonExtensions + + let history json = + match JsonValue.Parse json with + | JsonValue.Array hist -> + hist + |> Array.map (fun h -> + { asOf = h?asOf.AsInteger64 () |> Ticks + status = h?status.AsString () |> RequestAction.fromString + text = match h?text.AsString () with "" -> None | txt -> Some txt + }) + |> List.ofArray + | _ -> [] + + let notes json = + match JsonValue.Parse json with + | JsonValue.Array notes -> + notes + |> Array.map (fun n -> + { asOf = n?asOf.AsInteger64 () |> Ticks + notes = n?notes.AsString () + }) + |> List.ofArray + | _ -> [] + +let oldData = CsvFile.Load("data.csv") + +let db = new LiteDatabase("Filename=./mpj.db") + +MyPrayerJournal.Data.Startup.ensureDb db + +let migrated = + oldData.Rows + |> Seq.map (fun r -> + { id = r.["@id"].Replace ("Requests/", "") |> RequestId.ofString + enteredOn = r?enteredOn.AsInteger64 () |> Ticks + userId = UserId r?userId + snoozedUntil = r?snoozedUntil.AsInteger64 () |> Ticks + showAfter = r?showAfter.AsInteger64 () |> Ticks + recurType = r?recurType |> Recurrence.fromString + recurCount = (r?recurCount.AsInteger >> int16) () + history = Subdocs.history r?history + notes = Subdocs.notes r?notes + }) + |> Seq.iter (fun req -> + printfn "%A" req + db.GetCollection("request").Insert req |> ignore) + +db.Checkpoint () + +printfn $"Migrated {migrated} requests" diff --git a/src/MyPrayerJournal/Migrate/mpj-log.db b/src/MyPrayerJournal/Migrate/mpj-log.db new file mode 100644 index 0000000..e69de29