Skip to content

Commit 9dbb232

Browse files
committed
support for multiple column PRIMARY KEY and UNIQUE constraints
1 parent 15562dd commit 9dbb232

File tree

6 files changed

+60
-26
lines changed

6 files changed

+60
-26
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [1.0.3] - 2025-03-03
9+
10+
Added:
11+
12+
- Support for multiple column UNIQUE and PRIMARY KEY constraints
13+
- Environment variable `migrate_db` sets the default database path, falling back to `<CURRENT_DIR_NAME>.sqlite` if not defined
14+
815
## [1.0.2] - 2025-01-11
916

1017
Changed:

src/Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<PackageVersion Include="Antlr4.Runtime.Standard" Version="4.13.1" />
77
<PackageVersion Include="Antlr4BuildTasks" Version="12.8.0" />
88
<PackageVersion Include="Argu" Version="6.2.4" />
9+
<PackageVersion Include="dotenv.net" Version="3.2.1" />
910
<PackageVersion Include="FSharp.Core" Version="9.0.100" />
1011
<PackageVersion Include="FsToolkit.ErrorHandling" Version="4.18.0" />
1112
<PackageVersion Include="Microsoft.Data.Sqlite" Version="9.0.0" />

src/MigLib/DeclarativeMigrations/SqlParser.fs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,21 @@ type SqlVisitor() =
9393
let constraints =
9494
context.table_constraint ()
9595
|> Array.choose (fun c ->
96-
if c.FOREIGN_() <> null then
96+
match c with
97+
| _ when c.PRIMARY_() <> null ->
98+
{ constraintName = None
99+
columns = c.indexed_column () |> Array.map _.column_name().GetText() |> Array.toList
100+
isAutoincrement = false }
101+
|> PrimaryKey
102+
|> Some
103+
104+
| _ when c.UNIQUE_() <> null ->
105+
c.indexed_column ()
106+
|> Array.map _.column_name().GetText()
107+
|> Array.toList
108+
|> Unique
109+
|> Some
110+
| _ when c.FOREIGN_() <> null ->
97111
let columns = c.column_name () |> Array.map _.GetText() |> Array.toList
98112
let refTable = c.foreign_key_clause().foreign_table().GetText()
99113

@@ -108,8 +122,7 @@ type SqlVisitor() =
108122
refTable = refTable
109123
refColumns = refColumns }
110124
)
111-
else
112-
None)
125+
| _ -> None)
113126
|> Array.toList
114127

115128
Table

src/MigLib/Execution/Exec.fs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,18 @@ let internal dbElements (conn: SqliteConnection) =
4040
with e ->
4141
Error(Types.ReadSchemaFailed $"{e.Message}")
4242

43-
let internal getDbFile (dir: DirectoryInfo) = $"{dir.FullName}/{dir.Name}.sqlite"
43+
[<Literal>]
44+
let migrateDbEnvVar = "migrate_db"
45+
46+
let internal getDbFile (dir: DirectoryInfo) =
47+
dotenv.net.DotEnv.Load()
48+
49+
let getEnv s =
50+
Environment.GetEnvironmentVariable s |> Option.ofObj
51+
52+
match getEnv migrateDbEnvVar with
53+
| Some x -> x
54+
| None -> $"{dir.FullName}/{dir.Name}.sqlite"
4455

4556
let internal dbSchema (dbFile: string) =
4657
result {
@@ -62,11 +73,11 @@ let internal readFile path =
6273
with e ->
6374
Error(Types.ReadFileFailed e.Message)
6475

65-
type SqlSource = {name:string; content:string}
76+
type SqlSource = { name: string; content: string }
6677

6778
let internal parseSqlFiles (sources: SqlSource list) =
6879
sources
69-
|> List.map (fun s -> SqlParser.parse(s.name, s.content))
80+
|> List.map (fun s -> SqlParser.parse (s.name, s.content))
7081
|> Solve.splitResult
7182
|> function
7283
| xs, [] ->
@@ -89,11 +100,11 @@ let internal readDirSql (dir: DirectoryInfo) =
89100
|> List.choose (fun f ->
90101
if f.Extension = ".sql" then
91102
let sql = f.OpenText().ReadToEnd()
92-
{name = f.FullName; content = sql} |> Some
103+
{ name = f.FullName; content = sql } |> Some
93104
else
94105
None)
95106

96-
let migrationStatementsForDb(dbFile:string, sources: SqlSource list) =
107+
let migrationStatementsForDb (dbFile: string, sources: SqlSource list) =
97108
result {
98109
let! expectedSchema = parseSqlFiles sources
99110
let! dbSchema = dbSchema dbFile
@@ -102,11 +113,12 @@ let migrationStatementsForDb(dbFile:string, sources: SqlSource list) =
102113

103114
let migrationStatements () =
104115
let dir = Environment.CurrentDirectory
116+
105117
result {
106118
let dir = DirectoryInfo dir
107119
let dbFile = getDbFile dir
108120
let sources = readDirSql dir
109-
return! migrationStatementsForDb(dbFile, sources)
121+
return! migrationStatementsForDb (dbFile, sources)
110122
}
111123

112124
let generateMigrationScript (withColors: bool) =

src/MigLib/MigLib.fsproj

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77

88
<PackageId>MigLib</PackageId>
9-
<Version>1.0.2</Version>
9+
<Version>1.0.3</Version>
1010
<PackageOutputPath>./nupkg</PackageOutputPath>
1111
<InvariantGlobalization>true</InvariantGlobalization>
1212
<RootNamespace>migrate</RootNamespace>
@@ -25,30 +25,31 @@
2525

2626
<ItemGroup>
2727
<Compile Include="AssemblyInfo.fs" />
28-
<Compile Include="DeclarativeMigrations\Types.fs"/>
29-
<Compile Include="DeclarativeMigrations\SqlParser.fs"/>
30-
<Compile Include="DeclarativeMigrations\GenerateSql.fs"/>
31-
<Compile Include="DeclarativeMigrations\Solve.fs"/>
32-
<Compile Include="DeclarativeMigrations\Migration.fs"/>
33-
<Compile Include="Execution\FormatSql.fs"/>
34-
<Compile Include="Execution\Exec.fs"/>
28+
<Compile Include="DeclarativeMigrations\Types.fs" />
29+
<Compile Include="DeclarativeMigrations\SqlParser.fs" />
30+
<Compile Include="DeclarativeMigrations\GenerateSql.fs" />
31+
<Compile Include="DeclarativeMigrations\Solve.fs" />
32+
<Compile Include="DeclarativeMigrations\Migration.fs" />
33+
<Compile Include="Execution\FormatSql.fs" />
34+
<Compile Include="Execution\Exec.fs" />
3535
<Compile Include="MigrationLog\ExecAndLog.fs" />
36-
<Compile Include="ImportGoose\ImportGoose.fs"/>
36+
<Compile Include="ImportGoose\ImportGoose.fs" />
3737
<Content Include="..\..\README.md" Pack="true" PackagePath="\" />
3838
<Content Include="..\..\images\logo.png" Pack="true" PackagePath="\" />
3939
</ItemGroup>
4040

4141
<ItemGroup>
42-
<PackageReference Include="FSharp.Core"/>
43-
<PackageReference Include="FsToolkit.ErrorHandling"/>
44-
<PackageReference Include="Microsoft.Data.Sqlite"/>
45-
<PackageReference Include="SqlParserCS"/>
46-
<PackageReference Include="SqlPrettify"/>
47-
<PackageReference Include="FSharpPlus"/>
42+
<PackageReference Include="dotenv.net" />
43+
<PackageReference Include="FSharp.Core" />
44+
<PackageReference Include="FsToolkit.ErrorHandling" />
45+
<PackageReference Include="Microsoft.Data.Sqlite" />
46+
<PackageReference Include="SqlParserCS" />
47+
<PackageReference Include="SqlPrettify" />
48+
<PackageReference Include="FSharpPlus" />
4849
</ItemGroup>
4950

5051
<ItemGroup>
51-
<ProjectReference Include="..\SqliteParserCs\SqliteParserCs.csproj"/>
52+
<ProjectReference Include="..\SqliteParserCs\SqliteParserCs.csproj" />
5253
</ItemGroup>
5354

5455
</Project>

src/mig/mig.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageId>migtool</PackageId>
1111
<RootNamespace>migrate</RootNamespace>
1212
<AssemblyName>migrate</AssemblyName>
13-
<Version>1.0.2</Version>
13+
<Version>1.0.3</Version>
1414
<PackageOutputPath>./nupkg</PackageOutputPath>
1515
<InvariantGlobalization>true</InvariantGlobalization>
1616

0 commit comments

Comments
 (0)