2019-07-28 20:47:11 -05:00
|
|
|
|
|
|
|
|
|
open Microsoft.FSharpLu.Json
|
|
|
|
|
open MyPrayerJournal
|
|
|
|
|
open Npgsql
|
|
|
|
|
open Raven.Client.Documents
|
|
|
|
|
|
|
|
|
|
type NpgsqlDataReader with
|
|
|
|
|
member this.getShort = this.GetOrdinal >> this.GetInt16
|
|
|
|
|
member this.getString = this.GetOrdinal >> this.GetString
|
|
|
|
|
member this.getTicks = this.GetOrdinal >> this.GetInt64 >> Ticks
|
|
|
|
|
member this.isNull = this.GetOrdinal >> this.IsDBNull
|
|
|
|
|
|
2019-09-02 13:38:50 -05:00
|
|
|
|
let pgConn connStr =
|
|
|
|
|
let c = new NpgsqlConnection (connStr)
|
2019-07-28 20:47:11 -05:00
|
|
|
|
c.Open ()
|
|
|
|
|
c
|
|
|
|
|
|
|
|
|
|
let isValidStatus stat =
|
|
|
|
|
try
|
|
|
|
|
(RequestAction.fromString >> ignore) stat
|
|
|
|
|
true
|
|
|
|
|
with _ -> false
|
|
|
|
|
|
2019-09-02 13:38:50 -05:00
|
|
|
|
let getHistory reqId connStr =
|
|
|
|
|
use conn = pgConn connStr
|
2019-07-28 20:47:11 -05:00
|
|
|
|
use cmd = conn.CreateCommand ()
|
|
|
|
|
cmd.CommandText <- """SELECT "asOf", status, text FROM mpj.history WHERE "requestId" = @reqId ORDER BY "asOf" """
|
|
|
|
|
(cmd.Parameters.Add >> ignore) (NpgsqlParameter ("@reqId", reqId :> obj))
|
|
|
|
|
use rdr = cmd.ExecuteReader ()
|
|
|
|
|
seq {
|
|
|
|
|
while rdr.Read () do
|
|
|
|
|
match (rdr.getString >> isValidStatus) "status" with
|
|
|
|
|
| true ->
|
|
|
|
|
yield
|
|
|
|
|
{ asOf = rdr.getTicks "asOf"
|
|
|
|
|
status = (rdr.getString >> RequestAction.fromString) "status"
|
|
|
|
|
text = match rdr.isNull "text" with true -> None | false -> (rdr.getString >> Some) "text"
|
|
|
|
|
}
|
|
|
|
|
| false ->
|
|
|
|
|
printf "Invalid status %s; skipped history entry %s/%i\n" (rdr.getString "status") reqId
|
|
|
|
|
((rdr.getTicks >> Ticks.toLong) "asOf")
|
|
|
|
|
}
|
|
|
|
|
|> List.ofSeq
|
|
|
|
|
|
2019-09-02 13:38:50 -05:00
|
|
|
|
let getNotes reqId connStr =
|
|
|
|
|
use conn = pgConn connStr
|
2019-07-28 20:47:11 -05:00
|
|
|
|
use cmd = conn.CreateCommand ()
|
|
|
|
|
cmd.CommandText <- """SELECT "asOf", notes FROM mpj.note WHERE "requestId" = @reqId"""
|
|
|
|
|
(cmd.Parameters.Add >> ignore) (NpgsqlParameter ("@reqId", reqId :> obj))
|
|
|
|
|
use rdr = cmd.ExecuteReader ()
|
|
|
|
|
seq {
|
|
|
|
|
while rdr.Read () do
|
|
|
|
|
yield
|
|
|
|
|
{ asOf = rdr.getTicks "asOf"
|
|
|
|
|
notes = rdr.getString "notes"
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|> List.ofSeq
|
|
|
|
|
|
2019-09-02 13:38:50 -05:00
|
|
|
|
let migrateRequests (store : IDocumentStore) connStr =
|
2019-07-28 20:47:11 -05:00
|
|
|
|
use sess = store.OpenSession ()
|
2019-09-02 13:38:50 -05:00
|
|
|
|
use conn = pgConn connStr
|
2019-07-28 20:47:11 -05:00
|
|
|
|
use cmd = conn.CreateCommand ()
|
|
|
|
|
cmd.CommandText <-
|
|
|
|
|
"""SELECT "requestId", "enteredOn", "userId", "snoozedUntil", "showAfter", "recurType", "recurCount" FROM mpj.request"""
|
|
|
|
|
use rdr = cmd.ExecuteReader ()
|
|
|
|
|
while rdr.Read () do
|
2019-09-02 09:20:41 -05:00
|
|
|
|
let reqId = rdr.getString "requestId"
|
2019-09-02 17:37:18 -05:00
|
|
|
|
let recurrence =
|
|
|
|
|
match rdr.getString "recurType" with
|
|
|
|
|
| "immediate" -> Immediate
|
|
|
|
|
| "hours" -> Hours
|
|
|
|
|
| "days" -> Days
|
|
|
|
|
| "weeks" -> Weeks
|
|
|
|
|
| x -> invalidOp (sprintf "%s is not a valid recurrence" x)
|
2019-07-28 20:47:11 -05:00
|
|
|
|
sess.Store (
|
|
|
|
|
{ Id = (RequestId.fromIdString >> RequestId.toString) reqId
|
|
|
|
|
enteredOn = rdr.getTicks "enteredOn"
|
|
|
|
|
userId = (rdr.getString >> UserId) "userId"
|
|
|
|
|
snoozedUntil = rdr.getTicks "snoozedUntil"
|
2019-09-02 09:20:41 -05:00
|
|
|
|
showAfter = match recurrence with Immediate -> Ticks 0L | _ -> rdr.getTicks "showAfter"
|
|
|
|
|
recurType = recurrence
|
2019-07-28 20:47:11 -05:00
|
|
|
|
recurCount = rdr.getShort "recurCount"
|
2019-09-02 13:38:50 -05:00
|
|
|
|
history = getHistory reqId connStr
|
|
|
|
|
notes = getNotes reqId connStr
|
2019-07-28 20:47:11 -05:00
|
|
|
|
})
|
|
|
|
|
sess.SaveChanges ()
|
|
|
|
|
|
2019-09-02 09:20:41 -05:00
|
|
|
|
open Converters
|
2019-09-02 17:37:18 -05:00
|
|
|
|
open System
|
|
|
|
|
open System.Security.Cryptography.X509Certificates
|
2019-09-02 09:20:41 -05:00
|
|
|
|
|
2019-07-28 20:47:11 -05:00
|
|
|
|
[<EntryPoint>]
|
|
|
|
|
let main argv =
|
2019-09-02 17:37:18 -05:00
|
|
|
|
match argv.Length with
|
|
|
|
|
| 4 ->
|
|
|
|
|
let clientCert = new X509Certificate2 (argv.[1], argv.[2])
|
|
|
|
|
let raven = new DocumentStore (Urls = [| argv.[0] |], Database = "myPrayerJournal", Certificate = clientCert)
|
|
|
|
|
raven.Conventions.CustomizeJsonSerializer <-
|
|
|
|
|
fun x ->
|
|
|
|
|
x.Converters.Add (RequestIdJsonConverter ())
|
|
|
|
|
x.Converters.Add (TicksJsonConverter ())
|
|
|
|
|
x.Converters.Add (UserIdJsonConverter ())
|
|
|
|
|
x.Converters.Add (CompactUnionJsonConverter ())
|
|
|
|
|
let store = raven.Initialize ()
|
|
|
|
|
migrateRequests store argv.[3]
|
|
|
|
|
printfn "fin"
|
|
|
|
|
| _ ->
|
|
|
|
|
Console.WriteLine "Usage: dotnet migrate.dll [raven-url] [raven-cert-file] [raven-cert-pw] [postgres-conn-str]"
|
|
|
|
|
0
|