Created
July 29, 2021 14:36
-
-
Save CallumVass/420b175e60f1d20f167ebd6ac542d6c4 to your computer and use it in GitHub Desktop.
Migration POC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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