Skip to content

Commit

Permalink
migrate as library. Releasing MigLib
Browse files Browse the repository at this point in the history
  • Loading branch information
lamg committed Jan 11, 2025
1 parent d976a21 commit 7ad151b
Show file tree
Hide file tree
Showing 21 changed files with 152 additions and 41 deletions.
9 changes: 9 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,12 @@ jobs:
working-directory: ./src/mig
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

- name: publish MigLib
run: |
dotnet publish -c Release
dotnet pack
dotnet nuget push nupkg/*.nupkg -k $NUGET_API_KEY -s https://api.nuget.org/v3/index.json
working-directory: ./src/MigLib
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.0.2] - 2025-01-11

Changed:

- migrate as library

## [1.0.1] - 2024-11-26

Added:
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
44 changes: 28 additions & 16 deletions src/mig/Execution/Exec.fs → src/MigLib/Execution/Exec.fs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ let internal dbElements (conn: SqliteConnection) =

let internal getDbFile (dir: DirectoryInfo) = $"{dir.FullName}/{dir.Name}.sqlite"

let internal dbSchema (dir: DirectoryInfo) =
let dbFile = getDbFile dir

let internal dbSchema (dbFile: string) =
result {
use! conn = connect dbFile
let! sql = dbElements conn
Expand All @@ -64,15 +62,11 @@ let internal readFile path =
with e ->
Error(Types.ReadFileFailed e.Message)

let internal readDirSql (dir: DirectoryInfo) =
dir.EnumerateFiles()
|> Seq.toList
|> List.choose (fun f ->
if f.Extension = ".sql" then
let sql = f.OpenText().ReadToEnd()
SqlParser.parse (f.FullName, sql) |> Some
else
None)
type SqlSource = {name:string; content:string}

let internal parseSqlFiles (sources: SqlSource list) =
sources
|> List.map (fun s -> SqlParser.parse(s.name, s.content))
|> Solve.splitResult
|> function
| xs, [] ->
Expand All @@ -89,14 +83,32 @@ let internal readDirSql (dir: DirectoryInfo) =
|> Ok
| _, errs -> errs |> List.map Types.ParsingFailed |> Types.Composed |> Error

let migrationStatements () =
let internal readDirSql (dir: DirectoryInfo) =
dir.EnumerateFiles()
|> Seq.toList
|> List.choose (fun f ->
if f.Extension = ".sql" then
let sql = f.OpenText().ReadToEnd()
{name = f.FullName; content = sql} |> Some
else
None)

let migrationStatementsForDb(dbFile:string, sources: SqlSource list) =
result {
let dir = Environment.CurrentDirectory |> DirectoryInfo
let! expectedSchema = readDirSql dir
let! dbSchema = dbSchema dir
let! expectedSchema = parseSqlFiles sources
let! dbSchema = dbSchema dbFile
return! Migration.migration (dbSchema, expectedSchema)
}

let migrationStatements () =
let dir = Environment.CurrentDirectory
result {
let dir = DirectoryInfo dir
let dbFile = getDbFile dir
let sources = readDirSql dir
return! migrationStatementsForDb(dbFile, sources)
}

let generateMigrationScript (withColors: bool) =
result {
let! statements = migrationStatements ()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module internal migrate.Execution.FormatSql
module migrate.Execution.FormatSql

open System.Text.RegularExpressions
open SqlPrettify
Expand Down
21 changes: 20 additions & 1 deletion src/mig/Execution/README.md → src/MigLib/Execution/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,23 @@ the command
mig gen
```

will output a script that allows to review and migrate the `test_db.sqlite` database, which will be created in case it doesn't exist.
will output a script that allows to review and migrate the `test_db.sqlite` database, which will be created in case it doesn't exist.

## Migration execution using MigLib

MigLib is a library designed for other projects to run migrations relying on internal source code and not in the command line interface `mig`. The usage is as follows:

```fsharp
open migrate.Execution.Exec
open FsToolkit.ErrorHandling
let sources = [
{ name = "schema0.sql"; content="CREATE TABLE table0(id INTEGER NOT NULL)" }
]
result {
let! statements = migrationStatementsForDb ("/path/to/db.sqlite", sources)
let! results = executeMigration statements
return results
}
```
File renamed without changes.
File renamed without changes.
54 changes: 54 additions & 0 deletions src/MigLib/MigLib.fsproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net9.0</TargetFramework>
<RollForward>major</RollForward>
<GenerateDocumentationFile>true</GenerateDocumentationFile>

<PackageId>MigLib</PackageId>
<Version>1.0.2</Version>
<PackageOutputPath>./nupkg</PackageOutputPath>
<InvariantGlobalization>true</InvariantGlobalization>
<RootNamespace>migrate</RootNamespace>
<AssemblyName>MigLib</AssemblyName>

<Authors>Luis Ángel Méndez Gort</Authors>
<PackageProjectUrl>https://github.com/lamg/migrate</PackageProjectUrl>
<RepositoryUrl>https://github.com/lamg/migrate</RepositoryUrl>
<PackageProjectUrl>https://github.com/lamg/migrate</PackageProjectUrl>
<RepositoryUrl>https://github.com/lamg/migrate</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>
<PackageIcon>logo.png</PackageIcon>
</PropertyGroup>


<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="DeclarativeMigrations\Types.fs"/>
<Compile Include="DeclarativeMigrations\SqlParser.fs"/>
<Compile Include="DeclarativeMigrations\GenerateSql.fs"/>
<Compile Include="DeclarativeMigrations\Solve.fs"/>
<Compile Include="DeclarativeMigrations\Migration.fs"/>
<Compile Include="Execution\FormatSql.fs"/>
<Compile Include="Execution\Exec.fs"/>
<Compile Include="MigrationLog\ExecAndLog.fs" />
<Compile Include="ImportGoose\ImportGoose.fs"/>
<Content Include="..\..\README.md" Pack="true" PackagePath="\" />
<Content Include="..\..\images\logo.png" Pack="true" PackagePath="\" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="FSharp.Core"/>
<PackageReference Include="FsToolkit.ErrorHandling"/>
<PackageReference Include="Microsoft.Data.Sqlite"/>
<PackageReference Include="SqlParserCS"/>
<PackageReference Include="SqlPrettify"/>
<PackageReference Include="FSharpPlus"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SqliteParserCs\SqliteParserCs.csproj"/>
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ let internal migrationSteps: Types.CreateTable =
let migrationStatements () =
result {
let dir = Environment.CurrentDirectory |> DirectoryInfo
let! expectedSchema = Exec.readDirSql dir
let! expectedSchema = Exec.readDirSql dir |> Exec.parseSqlFiles

let expectedWithMigTables =
{ expectedSchema with
tables = expectedSchema.tables @ [ migrationLog; migrationSteps ] }

let! dbSchema = Exec.dbSchema dir
let dbFile = Exec.getDbFile dir
let! dbSchema = Exec.dbSchema dbFile
return! Migration.migration (dbSchema, expectedWithMigTables)
}

Expand Down
File renamed without changes.
3 changes: 2 additions & 1 deletion src/Test/Test.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="UseAsLib.fs" />
<Compile Include="CompareObjects.fs" />
<Compile Include="TableMigration.fs" />
<Compile Include="ViewMigration.fs" />
Expand All @@ -24,7 +25,7 @@
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\mig\mig.fsproj" />
<ProjectReference Include="..\MigLib\MigLib.fsproj" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions src/Test/UseAsLib.fs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Test.UseAsLib

open Xunit
open migrate.Execution.Exec
open FsToolkit.ErrorHandling

[<Fact>]
let useAsLib() =
let sources = [
{ name = "schema0.sql"; content="CREATE TABLE table0(id INTEGER NOT NULL)" }
]

result {
let! statements = migrationStatementsForDb ("/path/to/db.sqlite", sources)
let! results = executeMigration statements
return results
}
|> Result.isError // because the path does not exists
|> Assert.True
23 changes: 4 additions & 19 deletions src/mig/mig.fsproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
<PackageId>migtool</PackageId>
<RootNamespace>migrate</RootNamespace>
<AssemblyName>migrate</AssemblyName>
<Version>1.0.1</Version>
<Version>1.0.2</Version>
<PackageOutputPath>./nupkg</PackageOutputPath>
<InvariantGlobalization>true</InvariantGlobalization>


<Authors>Luis Ángel Méndez Gort</Authors>
<PackageProjectUrl>https://github.com/lamg/migrate</PackageProjectUrl>
<RepositoryUrl>https://github.com/lamg/migrate</RepositoryUrl>
<PackageReadmeFile>README.md</PackageReadmeFile>
Expand All @@ -22,16 +23,6 @@
</PropertyGroup>

<ItemGroup>
<Compile Include="AssemblyInfo.fs" />
<Compile Include="DeclarativeMigrations\Types.fs"/>
<Compile Include="DeclarativeMigrations\SqlParser.fs"/>
<Compile Include="DeclarativeMigrations\GenerateSql.fs"/>
<Compile Include="DeclarativeMigrations\Solve.fs"/>
<Compile Include="DeclarativeMigrations\Migration.fs"/>
<Compile Include="Execution\FormatSql.fs"/>
<Compile Include="Execution\Exec.fs"/>
<Compile Include="MigrationLog\ExecAndLog.fs" />
<Compile Include="ImportGoose\ImportGoose.fs"/>
<Compile Include="Program.fs"/>
<Content Include="..\..\README.md" Pack="true" PackagePath="\" />
<Content Include="..\..\images\logo.png" Pack="true" PackagePath="\" />
Expand All @@ -41,16 +32,10 @@

<ItemGroup>
<PackageReference Include="Argu"/>
<PackageReference Include="FSharp.Core"/>
<PackageReference Include="FsToolkit.ErrorHandling"/>
<PackageReference Include="Microsoft.Data.Sqlite"/>
<PackageReference Include="SqlParserCS"/>
<PackageReference Include="SqlPrettify"/>
<PackageReference Include="FSharpPlus"/>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\SqliteParserCs\SqliteParserCs.csproj"/>
<ProjectReference Include="..\MigLib\MigLib.fsproj"/>
</ItemGroup>

</Project>
6 changes: 6 additions & 0 deletions src/migrate.sln
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SqliteParserCs", "SqlitePar
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "Test", "Test\Test.fsproj", "{FED9A465-A5D9-48EB-B9E6-1034974A9DD5}"
EndProject
Project("{F2A71F9B-5D33-465A-A702-920D77279786}") = "MigLib", "MigLib\MigLib.fsproj", "{053AFA3D-80E7-43C0-80E6-BC462120398E}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -30,5 +32,9 @@ Global
{FED9A465-A5D9-48EB-B9E6-1034974A9DD5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{FED9A465-A5D9-48EB-B9E6-1034974A9DD5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{FED9A465-A5D9-48EB-B9E6-1034974A9DD5}.Release|Any CPU.Build.0 = Release|Any CPU
{053AFA3D-80E7-43C0-80E6-BC462120398E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{053AFA3D-80E7-43C0-80E6-BC462120398E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{053AFA3D-80E7-43C0-80E6-BC462120398E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{053AFA3D-80E7-43C0-80E6-BC462120398E}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal

0 comments on commit 7ad151b

Please sign in to comment.