Skip to content

Commit

Permalink
Adjust db example: apply migrations in INIT template; add 'manual' mi…
Browse files Browse the repository at this point in the history
…grations
  • Loading branch information
infogulch committed May 26, 2024
1 parent d7a5e25 commit 71c1e29
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
2 changes: 1 addition & 1 deletion providers/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *DotDB) rollback() error {
// - includeIds: A list of specific migration IDs to apply. If provided other ids are skipped.
//
// Returns the number of migrations applied, and returns an error on these conditions:
// - Rexex expr does not have a submatch
// - Regex expr does not have a submatch
// - Fails to list files in the dir
// - The submatch expression is not convertible to a uint64
// - The migration file could not be read
Expand Down
1 change: 1 addition & 0 deletions test/migrations/manual.1.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE TABLE manual(id);
1 change: 1 addition & 0 deletions test/migrations/manual.2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE manual;
6 changes: 3 additions & 3 deletions test/templates/db/index.html → test/templates/db/.init.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<!DOCTYPE html>

<!-- this runs at startup -->
{{define "INIT db"}}
<p>{{.DB.Migrate .Migrations `^schema\.(\d+)\.sql$` (.DB.QueryVal `PRAGMA user_version;`)}} migrations applied.</p>

<p>Current user_version: {{.DB.QueryVal `PRAGMA user_version`}}</p>
{{end}}
29 changes: 29 additions & 0 deletions test/templates/db/manual.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<script src="https://unpkg.com/htmx.org@1.9.12"></script>

{{$RE := `^manual\.(\d+)\.sql$`}}
<p>Run a manual migration:</p>
{{range .Migrations.List "."}}
{{$id := atoi (regexReplaceAll $RE .Name "$1")}}
{{if eq $id 0}}{{continue}}{{end}}
<form><button hx-post="/db/run" hx-target="#results" hx-swap="beforeend">{{.Name}} ({{$id}})</button><input type="hidden" name="id" value="{{$id}}"></form>
{{end}}

<p>Results:</p>
<ul id="results">
</ul>

{{define "POST /db/run"}}
{{$RE := `^manual\.(\d+)\.sql$`}}
{{.Req.ParseForm}}
{{$id := int64 (atoi (.Req.FormValue "id"))}}
{{if eq $id 0}}<li>Invalid id: {{$id}}</li>{{return}}{{end}}
{{$result := try .DB `Migrate` .Migrations $RE (int64 0) $id}}
<li>{{now | date "2006-01-02 15:04:05"}}:
{{if $result.OK}}
Applied migration {{$id}}.
{{else}}
Migration error: <code>{{$result.Error}}</code>
{{end}}
</li>
{{end}}
1 change: 1 addition & 0 deletions test/templates/index{$}.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<div>
Navigate to different tests:
<ul>
<li><a href="/db/migrate-manual">DB</a></li>
<li><a href="/fs/">FS</a></li>
<li><a href="/kv/">KV</a></li>
<li><a href="/nats/">Nats</a></li>
Expand Down
13 changes: 7 additions & 6 deletions test/tests/db.hurl
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
GET http://localhost:8080/db
GET http://localhost:8080/db/manual

HTTP 200
[Asserts]
body contains "3 migrations applied"
body contains "Current user_version: 10"
body contains "Run a manual migration"
body contains "manual.1.sql (1)"


GET http://localhost:8080/db
POST http://localhost:8080/db/run
[FormParams]
id: 1

HTTP 200
[Asserts]
body contains "0 migrations applied"
body contains "Current user_version: 10"
body contains "Applied migration 1."

0 comments on commit 71c1e29

Please sign in to comment.