Giter Club home page Giter Club logo

Comments (10)

brettrowberry avatar brettrowberry commented on August 27, 2024 1

This is interesting because we have a lot of code snippets for applying and reverting changes in an idempotent fashion for SQL Server. Being able to bring that to Migrondi as an F# script or something would be awesome, especially with interpolated strings now.

from migrondi.

brettrowberry avatar brettrowberry commented on August 27, 2024 1

Almost every change script I write modifies at least 2 objects that have a dependency relationship, say a table and a stored procedure. So, I think the whole script must be run as a transaction.

from migrondi.

AngelMunoz avatar AngelMunoz commented on August 27, 2024

NOTE: This would be an alternative way to do migrations, the SQL migrations would still be the default unless specified otherwise

from migrondi.

AngelMunoz avatar AngelMunoz commented on August 27, 2024

cool! thanks for the input @brettrowberry I'll try to come up with something over the next week

from migrondi.

brettrowberry avatar brettrowberry commented on August 27, 2024

Of course, we could just have an F# script whose job is to generate SQL and Migrondi just runs the SQL.

from migrondi.

AngelMunoz avatar AngelMunoz commented on August 27, 2024

there are a couple of interesting approaches here, one could also have a program that sends a json serializable array with the SQL migrations to stdout read it from something like https://github.com/Tyrrrz/CliWrap

but I think the approach above is fairly simpler since you only need to have an F# list with up/down commands at the end of your script, scripts can be versioned with a timestamp as well

one thing that comes to my mind is that if there are multiple SQL statements that come from a script should each be in its own transaction? or should all of the statements be part of a single transaction

from migrondi.

AngelMunoz avatar AngelMunoz commented on August 27, 2024

I've been playing with this but it seems to work once then it stops working
image

it might be a bug within the F# compiler services or something that I'm doing wrong but I'll keep experimenting on this

namespace Migrondi

open System
open System.IO
open FSharp.Compiler.SourceCodeServices
open FSharp.Compiler.Interactive.Shell
open Types

module ScriptedContent =

    let getContentFromScripts (file: FileInfo): string * string =
        let defConfig =
            FsiEvaluationSession.GetDefaultConfiguration()

        let argv =
            [| "dotnet"
               "fsi"
               "--noninteractive"
               "--nologo"
               "--gui-" |]

        use stdin = new StreamReader(Stream.Null)

        use stdout = new StringWriter()
        use stderr = new StringWriter()
        use script = file.OpenText()

        let session =
            FsiEvaluationSession.Create(defConfig, argv, stdin, stdout, stderr)


        let interaction =
            session.EvalInteractionNonThrowing(script.ReadToEnd())

        match interaction with
        | Choice2Of2 ex, error -> raise (ex)
        | Choice1Of2 None, error -> failwith $"Unsuported type %A{error}"
        | Choice1Of2 (Some value), error ->
            match value.ReflectionValue with
            | :? (list<list<string>>) as migrations ->
                let up =
                    (List.tryHead migrations |> Option.defaultValue [])
                    |> List.reduce (fun next curr -> $"{next}\n{curr}")

                let down =
                    (List.tryLast migrations |> Option.defaultValue [])
                    |> List.reduce (fun next curr -> $"{next}\n{curr}")

                $"\n{up}", $"\n{down}"
            | _ -> failwith $"Unsuported type %A{value.ReflectionType}"

from migrondi.

AngelMunoz avatar AngelMunoz commented on August 27, 2024

ok ok fixed that I was doing some concurrent stuff without knowing it

dir.EnumerateFiles()
        |> Seq.filter (fun f -> f.Extension = ".sql" || f.Extension = ".fsx")
        |> Seq.toArray
        |> Array.parallel.map fileMapping // <- this calls the fsi stuff fails if it's doing it parallel
        |> Array.sortBy (fun m -> m.timestamp)
dir.EnumerateFiles()
        |> Seq.filter (fun f -> f.Extension = ".sql" || f.Extension = ".fsx")
        |> Seq.toArray
        |> Array.map fileMapping // <- fix is to remove the parallel map
        |> Array.sortBy (fun m -> m.timestamp)

from migrondi.

VitorRigoni avatar VitorRigoni commented on August 27, 2024

Sorry I'm so late to this discussion! This looks pretty cool! If I understand correctly, it'd give us a kind of an EF-like way to writing the up/down scripts rather than plain scripts. It'd also give us full fsharp compiler support so I wouldn't struggle with forgotten semicolons again 😂
I like what you did with mondocks there, it looks great! I also agree with @brettrowberry that the whole thing should be run as a transaction.
This does look like the next big thing for Migrondi!

from migrondi.

AngelMunoz avatar AngelMunoz commented on August 27, 2024

after months of thinking it, I don't think this is something we'd like to pursue I think it would be easier to expose the F# mechanisms as a library and use it from F# rather than evaluate F# scripts ourselves I might still look into alternatives

from migrondi.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.