Add data migration project
This commit is contained in:
parent
bbe7294ba6
commit
8a04c4f83b
|
@ -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
|
||||
|
|
|
@ -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<Json.ISerializer, SystemTextJson.Serializer>()
|
||||
.AddSingleton<LiteDatabase>(fun _ -> new LiteDatabase (bldr.Configuration.GetConnectionString "db"))
|
||||
.AddSingleton<LiteDatabase>(db)
|
||||
|> ignore
|
||||
bldr.Build ()
|
||||
|
||||
|
|
21
src/MyPrayerJournal/Migrate/Migrate.fsproj
Normal file
21
src/MyPrayerJournal/Migrate/Migrate.fsproj
Normal file
|
@ -0,0 +1,21 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<OutputType>Exe</OutputType>
|
||||
<TargetFramework>net6.0</TargetFramework>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="Program.fs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="FSharp.Data" Version="4.2.3" />
|
||||
<PackageReference Include="LiteDB" Version="5.0.11" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Api\MyPrayerJournal.Api.fsproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
58
src/MyPrayerJournal/Migrate/Program.fs
Normal file
58
src/MyPrayerJournal/Migrate/Program.fs
Normal file
|
@ -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>("request").Insert req |> ignore)
|
||||
|
||||
db.Checkpoint ()
|
||||
|
||||
printfn $"Migrated {migrated} requests"
|
0
src/MyPrayerJournal/Migrate/mpj-log.db
Normal file
0
src/MyPrayerJournal/Migrate/mpj-log.db
Normal file
Loading…
Reference in New Issue
Block a user