MigLib.Codegen 12.1.0

dotnet add package MigLib.Codegen --version 12.1.0
                    
NuGet\Install-Package MigLib.Codegen -Version 12.1.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="MigLib.Codegen" Version="12.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MigLib.Codegen" Version="12.1.0" />
                    
Directory.Packages.props
<PackageReference Include="MigLib.Codegen" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MigLib.Codegen --version 12.1.0
                    
#r "nuget: MigLib.Codegen, 12.1.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package MigLib.Codegen@12.1.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MigLib.Codegen&version=12.1.0
                    
Install as a Cake Addin
#tool nuget:?package=MigLib.Codegen&version=12.1.0
                    
Install as a Cake Tool

logo

.NET F# License: Apache2 NuGet Version NuGet Downloads Tests

Migrate is a SQLite-first toolkit: a schema directory is the desired catalog, _migration.sql is the hop from the previous catalog, and mig codegen emits typed F# query modules plus Migration.migrate.

What it does

  1. You author snapshot *.sql files under schema/ (nested dirs allowed) and optional schema/_migration.sql.
  2. You annotate tables/views with -- mig: comments for the ops you want.
  3. mig codegen applies the snapshot to a temp DB, introspects, and emits one .fs module per annotated relation plus Migration.fs (let migrate dbPath).
  4. At runtime you call Migration.migrate dbPath and use dbTxn with generated helpers.

SQL travels inside the binary as compiled string data. Empty databases get the snapshot; existing databases get the hop and must then match the snapshot catalog.

See specs/schema_dir_and_migration.md.

Annotation example

-- mig:rel User
-- mig:ops insert, select_by(email), select_by_id, count
-- mig:ops upsert
-- mig:bool active
-- mig:datetime created_at
CREATE TABLE app_user (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  email TEXT NOT NULL,
  active INTEGER NOT NULL,
  created_at TEXT NOT NULL
) STRICT;

-- mig:ops select_all, select_one_by(email), count
CREATE VIEW active_user AS
  SELECT id, email, created_at FROM app_user WHERE active = 1;

-- Optional AND filters (filter catalog):
-- mig:ops filter_search(created_at desc, id), filter_count
-- mig:filter status eq status
-- mig:filter label_prefix like_prefix label
-- mig:filter text_any eq_any label, notes
CREATE TABLE item (
  id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
  status TEXT NOT NULL,
  label TEXT NOT NULL,
  notes TEXT NOT NULL,
  created_at TEXT NOT NULL
) STRICT;

Rules:

  • Unannotated relations generate no F#.
  • Ops are explicit; no ops means no type.
  • Multiple -- mig:ops lines on one relation are merged in order (handy for long lists).
  • Views allow read ops (select_*, count, filter_search, filter_count) plus optional delete_matching(table, key); other write ops fail at codegen.
  • count emits unfiltered SELECT COUNT(*) FROM [relation] as let count : TxnStep<int64> (tables and views).
  • Filter catalog (-- mig:filter name kind column[, column…] plus filter_search / filter_count):
    • Emits {Rel}Filter (each field T option), emptyFilter, public applyFilter (WHERE + params for hand SQL), search filter skip take, and/or countByFilter filter.
    • Kinds: eq, neq, gt, gte, lt, lte, like_prefix (binds v + "%"), eq_any (OR across 2+ columns, one bind), in (T list; empty list is 1=0).
    • Present Some values become AND clauses; all-None uses WHERE 1=1.
    • Requires at least one -- mig:filter with filter_search and/or filter_count (and vice versa). At most one of each filter op per relation.
  • -- mig:rel Name is optional; otherwise the F# name is derived from the SQL identifier.

See specs/schema_dir_and_migration.md and specs/sql_first_rewrite.md.

Installation

dotnet tool install --global migtool

Library:

dotnet add package MigLib

Codegen

mig codegen \
  --migrations ./schema \
  --output ./Stores \
  --namespace MyApp.Db

This writes Stores/User.fs, Stores/ActiveUser.fs, etc., plus Stores/Migration.fs (module MyApp.Db.Migration with let migrate). Hand-written companions can live alongside generated files; only files marked // <auto-generated /> are deleted when a relation disappears (Migration.fs is always kept).

From build.fsx / F#:

open MigLib.Codegen

match generate "./schema" "./Stores" "MyApp.Db" with
| Ok r ->
    Console.WriteLine(
      "generated "
      + string r.relationCount
      + " relations in "
      + r.outputDir)
    r.generatedFiles |> List.iter (fun p -> Console.WriteLine("  " + p))
| Error e -> failwith e

Runtime

open System
open MigLib
open MyApp.Db

// apply compiled snapshot / hop (module MyApp.Db.Migration)
let! db = Migration.migrate dbPath

// use transactions + generated queries (module MyApp.Db.User)
let! user =
  db {
    match! User.selectOneByEmail email with
    | Some u -> return u
    | None ->
      let! id =
        User.insert
          { Email = email
            Active = true
            CreatedAt = DateTimeOffset.UtcNow }
      return! User.selectById id |> TxnStep.map Option.get
  }

Packages

Package Role
migtool CLI (mig codegen, mig version)
MigLib Runtime: dbTxn / TxnStep, Query, migrate (AOT-friendly; no reflection)
MigLib.Codegen Dev-time generate API (used by CLI and build.fsx)

Local build

dotnet fsi build.fsx -- --target build
dotnet fsi build.fsx -- --target install
cd src && dotnet test

Version

Major version 10 is a greenfield rewrite (SQL-first). Older MigSchema / attribute-based workflows are not supported.

Further reading

See documentation/interesting-links.md for SQLite concurrency, migrations, and related theory links.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
12.1.0 42 8/13/2026
12.0.0 40 8/13/2026
11.2.0 97 8/4/2026
11.1.0 105 8/3/2026
11.0.0 105 7/31/2026
10.0.0 102 7/30/2026