Skip to content

Instantly share code, notes, and snippets.

@CallumVass
Created July 29, 2021 14:36
Show Gist options
  • Save CallumVass/420b175e60f1d20f167ebd6ac542d6c4 to your computer and use it in GitHub Desktop.
Save CallumVass/420b175e60f1d20f167ebd6ac542d6c4 to your computer and use it in GitHub Desktop.
Migration POC
#r "nuget: System.Data.SqlClient"
open System.Data
open System.Data.SqlClient
type Types =
| String
| Int
type Action =
| Add
| Update
| Delete
type Column =
{ Name: string
Type: Types
Relationship: string option }
type Table =
{ Name: string
Columns: Column list
Action: Action }
type Index =
{ Table: string
Columns: string list
Unique: bool
Action: Action }
type MigratorState =
{ Tables: Table list
Indexes: Index list }
type MigrationBuilder(dbConnection: IDbConnection) =
[<CustomOperation("create_table")>]
member __.CreateTable(state, tableName, columns) =
{ state with
Tables =
state.Tables
@ [ { Name = tableName
Columns = columns
Action = Add } ] }
[<CustomOperation("create_unique_index")>]
member __.CreateUniqueIndex(state, tableName, columns) =
{ state with
Indexes =
state.Indexes
@ [ { Table = tableName
Columns = columns
Unique = true
Action = Add } ] }
member _.Yield(_) = { Tables = []; Indexes = [] }
member _.Run(state) =
printfn "Running migration"
()
let migration = MigrationBuilder(new SqlConnection())
let column name type' =
{ Name = name
Type = type'
Relationship = None }
let relationshipColumn name type' rel =
{ Name = name
Type = type'
Relationship = Some rel }
migration {
create_table
"Users"
[ column "Username" String
column "Password" String ]
create_table "Links" [ relationshipColumn "userId" Int "Users" ]
create_unique_index "Users" [ "Username" ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment