-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust db example: apply migrations in INIT template; add 'manual' mi…
…grations
- Loading branch information
Showing
7 changed files
with
43 additions
and
10 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
CREATE TABLE manual(id); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE manual; |
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
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}} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<!DOCTYPE html> | ||
<script src="https://unpkg.com/[email protected]"></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}} |
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
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
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." |