fsprojects / Rezoom.SQL

Statically typechecks a common SQL dialect and translates it to various RDBMS backends
MIT License
669 stars 25 forks source link

Less horrible way to run migrations with dynamic connection string? #49

Open mydogisbox opened 5 years ago

mydogisbox commented 5 years ago
let loadUserModel () =
    let assemblyFolder = Path.GetDirectoryName(Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath)
    let resolutionFolder = Path.Combine(assemblyFolder, "../../../TempDBImpl")
    UserModel.Load(resolutionFolder, ".")

let migrate connectionString =
    let backend : IBackend = unbox (Rezoom.SQL.Compiler.Postgres.PostgresBackend())
    let connection = Configuration.ConnectionStringSettings("Test", connectionString)
    connection.ProviderName <- "Npgsql"
    let result = <@ (%backend.MigrationBackend) connection @>
    let migrationBackend = result.Evaluate()
    let model = loadUserModel()
    MigrationExtensions.Run ((model.Migrations.ToArray()), MigrationConfig.Default, fun () -> migrationBackend)

Note that this requires pulling in a library just to execute the expressions ( result.Evaluate() ).

It would be great if SQLModel<".">.Migrate() had an overload to take a connection string instead of just the name of a connectionName from a config.

mydogisbox commented 5 years ago

From looking at the code, it looks like it would be pretty easy to add a MigrateUsingConnectionString. If that's acceptable I might find some time to create a PR for it.