From 20dcaf6e1b716c50c6d522e384899035e727cf30 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Mon, 9 Oct 2023 22:19:34 -0400 Subject: [PATCH] Remove LiteDB/PostgreSQL migration util --- src/MyPrayerJournal.ToPostgres/LiteData.fs | 106 ------------------ .../MyPrayerJournal.ToPostgres.fsproj | 23 ---- src/MyPrayerJournal.ToPostgres/Program.fs | 33 ------ 3 files changed, 162 deletions(-) delete mode 100644 src/MyPrayerJournal.ToPostgres/LiteData.fs delete mode 100644 src/MyPrayerJournal.ToPostgres/MyPrayerJournal.ToPostgres.fsproj delete mode 100644 src/MyPrayerJournal.ToPostgres/Program.fs diff --git a/src/MyPrayerJournal.ToPostgres/LiteData.fs b/src/MyPrayerJournal.ToPostgres/LiteData.fs deleted file mode 100644 index c7277b2..0000000 --- a/src/MyPrayerJournal.ToPostgres/LiteData.fs +++ /dev/null @@ -1,106 +0,0 @@ -module MyPrayerJournal.LiteData - -open LiteDB -open MyPrayerJournal -open NodaTime - -/// Request is the identifying record for a prayer request -[] -type OldRequest = - { /// The ID of the request - Id : RequestId - - /// The time this request was initially entered - EnteredOn : Instant - - /// The ID of the user to whom this request belongs ("sub" from the JWT) - UserId : UserId - - /// The time at which this request should reappear in the user's journal by manual user choice - SnoozedUntil : Instant option - - /// The time at which this request should reappear in the user's journal by recurrence - ShowAfter : Instant option - - /// The recurrence for this request - Recurrence : Recurrence - - /// The history entries for this request - History : History[] - - /// The notes for this request - Notes : Note[] - } - - -/// LiteDB extensions -[] -module Extensions = - - /// Extensions on the LiteDatabase class - type LiteDatabase with - - /// The Request collection - member this.Requests = this.GetCollection "request" - - -/// Map domain to LiteDB -// It does mapping, but since we're so DU-heavy, this gives us control over the JSON representation -[] -module Mapping = - - open NodaTime.Text - - /// A NodaTime instant pattern to use for parsing instants from the database - let instantPattern = InstantPattern.CreateWithInvariantCulture "g" - - /// Mapping for NodaTime's Instant type - module Instant = - let fromBson (value : BsonValue) = (instantPattern.Parse value.AsString).Value - let toBson (value : Instant) : BsonValue = value.ToString ("g", null) - - /// Mapping for option types - module Option = - let instantFromBson (value : BsonValue) = if value.IsNull then None else Some (Instant.fromBson value) - let instantToBson (value : Instant option) = match value with Some it -> Instant.toBson it | None -> null - - let stringFromBson (value : BsonValue) = match value.AsString with "" -> None | x -> Some x - let stringToBson (value : string option) : BsonValue = match value with Some txt -> txt | None -> "" - - /// Mapping for Recurrence - module Recurrence = - let fromBson (value : BsonValue) = Recurrence.ofString value - let toBson (value : Recurrence) : BsonValue = Recurrence.toString value - - /// Mapping for RequestAction - module RequestAction = - let fromBson (value : BsonValue) = RequestAction.ofString value.AsString - let toBson (value : RequestAction) : BsonValue = RequestAction.toString value - - /// Mapping for RequestId - module RequestId = - let fromBson (value : BsonValue) = RequestId.ofString value.AsString - let toBson (value : RequestId) : BsonValue = RequestId.toString value - - /// Mapping for UserId - module UserId = - let fromBson (value : BsonValue) = UserId value.AsString - let toBson (value : UserId) : BsonValue = UserId.toString value - - /// Set up the mapping - let register () = - BsonMapper.Global.RegisterType(Instant.toBson, Instant.fromBson) - BsonMapper.Global.RegisterType(Option.instantToBson, Option.instantFromBson) - BsonMapper.Global.RegisterType(Recurrence.toBson, Recurrence.fromBson) - BsonMapper.Global.RegisterType(RequestAction.toBson, RequestAction.fromBson) - BsonMapper.Global.RegisterType(RequestId.toBson, RequestId.fromBson) - BsonMapper.Global.RegisterType(Option.stringToBson, Option.stringFromBson) - BsonMapper.Global.RegisterType(UserId.toBson, UserId.fromBson) - -/// Code to be run at startup -module Startup = - - /// Ensure the database is set up - let ensureDb (db : LiteDatabase) = - db.Requests.EnsureIndex (fun it -> it.UserId) |> ignore - Mapping.register () diff --git a/src/MyPrayerJournal.ToPostgres/MyPrayerJournal.ToPostgres.fsproj b/src/MyPrayerJournal.ToPostgres/MyPrayerJournal.ToPostgres.fsproj deleted file mode 100644 index 018a50c..0000000 --- a/src/MyPrayerJournal.ToPostgres/MyPrayerJournal.ToPostgres.fsproj +++ /dev/null @@ -1,23 +0,0 @@ - - - - Exe - net7.0 - 3391 - - - - - - - - - - - - - - - - - diff --git a/src/MyPrayerJournal.ToPostgres/Program.fs b/src/MyPrayerJournal.ToPostgres/Program.fs deleted file mode 100644 index fff4a16..0000000 --- a/src/MyPrayerJournal.ToPostgres/Program.fs +++ /dev/null @@ -1,33 +0,0 @@ -open LiteDB -open MyPrayerJournal.Data -open MyPrayerJournal.Domain -open MyPrayerJournal.LiteData -open Microsoft.Extensions.Configuration - - -let lite = new LiteDatabase "Filename=./mpj.db" -Startup.ensureDb lite - -let cfg = (ConfigurationBuilder().AddJsonFile "appsettings.json").Build () -Connection.setUp cfg |> Async.AwaitTask |> Async.RunSynchronously - -let reqs = lite.Requests.FindAll () - -reqs -|> Seq.map (fun old -> - { Request.empty with - Id = old.Id - EnteredOn = old.EnteredOn - UserId = old.UserId - SnoozedUntil = old.SnoozedUntil - ShowAfter = old.ShowAfter - Recurrence = old.Recurrence - History = old.History |> Array.sortByDescending (fun it -> it.AsOf) |> List.ofArray - Notes = old.Notes |> Array.sortByDescending (fun it -> it.AsOf) |> List.ofArray - }) -|> Seq.map Request.add -|> List.ofSeq -|> List.iter (Async.AwaitTask >> Async.RunSynchronously) - -System.Console.WriteLine $"Migration complete - {Seq.length reqs} requests migrated" -