Skip to content

v1.0.0-beta-008

Compare
Choose a tag to compare
@AngelMunoz AngelMunoz released this 08 Feb 17:48
· 22 commits to vnext since this release

Woops... I also missed a method on the public API on the last release, here's the good one!

As I was saying before:
This Version adds a small convenience API that was missing on the IMigrondi interface
Now it should be very simple to create and run migrations programmatically, here's an example how you can create and run migrations from an F# script!

#r "nuget: Migrondi.Core, 1.0.0-beta-008"
#r "nuget: Microsoft.Extensions.Logging"
#r "nuget: Microsoft.Extensions.Logging.Console"

open System
open System.IO
open Migrondi.Core

open Microsoft.Extensions.Logging

module Config =
  let getLogger loggerName =
    let loggerFactory =
      LoggerFactory.Create(fun builder ->
        builder.SetMinimumLevel(LogLevel.Debug).AddSimpleConsole() |> ignore
      )

    loggerFactory.CreateLogger loggerName

  let ensureWellFormed (migrationsDir: string) =
    // when you're using URIs "./path" is not the same as "./path/"
    // the first one is a file and the second one is a directory
    // so ensure you always end your paths with a directory separator

    if Path.EndsInDirectorySeparator(migrationsDir) then
      migrationsDir
    else
      $"{migrationsDir}{Path.DirectorySeparatorChar}"


let logger = Config.getLogger("sample-app")
let factory = Migrondi.MigrondiFactory(logger)

let config = MigrondiConfig.Default

let rootDir =
  Uri(__SOURCE_DIRECTORY__ + $"{Path.DirectorySeparatorChar}", UriKind.Absolute)

let migrationsDir =
  Uri(Config.ensureWellFormed config.migrations, UriKind.Relative)

let migrondi = factory.Invoke(config, rootDir, migrationsDir)


// Let's create a new Migration, since this is just an I/O operation
// there's no need to initialize the database yet, but ideally
// you would want to do that anyways for safety
migrondi.RunNew(
  "add-test-table",
  "create table if not exists test (id int not null primary key);",
  "drop table if exists test;"
)

// Before we can talk to the database we need to initialize the migrondi object
// this will ensure that the migrations directory exists, and the required pieces
// of information are available in the database to see if it is ready to accept migrations.
migrondi.Initialize()

// once that the migrondi service is initialized we can try to commmunicate to the
// database and in this case go for a dry run
let applied = migrondi.DryRunUp()

logger.LogInformation(
  $"List of the migrations that would have been ran:\n\n%A{applied}"
)

Full Changelog: v1.0.0-beta-006...v1.0.0-beta-008