Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

admindb: add migrations based on rubenv/sql-migrate #3

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# the binaries
cmd/server/server
cmd/insert-user/insert-user

# testrun contains repos that were use for tests
test/go/testrun
test/nodejs/testrun
web/handlers/testrun
admindb/sqlite/testrun
26 changes: 14 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,42 +42,44 @@ Usage of ./server:
print version number and build date
```

If you are working on the html templates or assets for them, build the server with `go build -tags dev`.
If you are working on the sqlite migrations, html templates or website assets, build the server with `go build -tags dev`.
This way it won't use the assets that are embedded in the binary but read them directly from the local filesystem.

Once you are done with your changes run `go generate` in package web to update them.
Once you are done with your changes run `go generate` in the changed packages to update them.

## Tooling

### mocks

[counterfeiter](https://github.com/maxbrunsfeld/counterfeiter) enables generating mocks for defined interfaces. To update them run `go generate` in package admindb.

TODO: automate setup of tool
TODO: setup tool as dependency (no manual install)


### Database schema

This project uses [sql-migrate](https://github.com/rubenv/sql-migrate) to upgrate the sqlite database when necessary.

Just create a new file in `admindb/sqlite/migrations` with your changes but be reminded: Similar to the web assets, you need to use `go test -tags dev` to test them. Afterwards run `go generate` to embedd them in the code and thus the resulting server binary.

### No ORM

We use [sqlboiler](github.com/volatiletech/sqlboiler) to generate type-safe Go code code directly from SQL statements and table definitions. This approach suits the programming language much more then classical ORM approaches, which usually rely havily on reflection for (un)packing structs.

To generate them run the following commands. This will populate `admindb/sqlite/models`:

(TODO: automate this with `go generate`)

```bash
# also included as generate_models.sh
cd admindb/sqlite
rm generate.db
sqlite3 generate.db < schema-v1.sql
go test
sqlboiler sqlite3 --wipe
```

The generated package `admindb/sqlite/models` is then used to implemente the custom logic of the different services in `admindb/sqlite`

TODO: automate this with `go generate`

TODO: we still need to incorporate automatic migrations. Until then use this workaround before starting the server: `mkdir $HOME/.ssb-go-room; sqlite3 $HOME/.ssb-go-room/roomdb < $src/admindb/sqlite/schema-v1.sql`.
The generated package `admindb/sqlite/models` is then used to implemente the custom logic of the different services in `admindb/sqlite`.

Aside: I would have used `sqlc` since it's a bit more minimal and uses hand written SQL queries instead of generic query builders but it [currently doesn't support sqlite](https://github.com/kyleconroy/sqlc/issues/161).


### Development user creation

`cmd/insert-user` contains code to create a fallback user. Build it and point it too your database with a username, like so:
Expand Down
8 changes: 4 additions & 4 deletions admindb/sqlite/generate_models.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/sh

dbName=generated.db
set -e

test -f $dbName && rm $dbName
sqlite3 $dbName < schema-v1.sql
sqlboiler sqlite3 --wipe
go test
sqlboiler sqlite3 --wipe
echo "all done!"
9 changes: 9 additions & 0 deletions admindb/sqlite/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package sqlite

import migrate "github.com/rubenv/sql-migrate"

//go:generate go run -tags=dev migrations_generate.go

var migrationSource = &migrate.HttpFileSystemMigrationSource{
FileSystem: Migrations,
}
9 changes: 9 additions & 0 deletions admindb/sqlite/migrations/create-auth-fallback.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
-- +migrate Up
CREATE TABLE auth_fallback (
id int PRIMARY KEY NOT NULL,
name text NOT NULL UNIQUE,
password_hash blob not null
);

-- +migrate Down
DROP TABLE auth_fallback;
23 changes: 23 additions & 0 deletions admindb/sqlite/migrations_dev.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// SPDX-License-Identifier: MIT

// +build dev

/*
This is the development version of the migrations folder, where they are read directly from the local filesystem.

to use this pass '-tags dev' to your go build or test commands.
*/

package sqlite

import (
"net/http"
"path/filepath"

"go.mindeco.de/goutils"
)

// absolute path of where this package is located
var pkgDir = goutils.MustLocatePackage("github.com/ssb-ngi-pointer/gossb-rooms/admindb/sqlite")

var Migrations http.FileSystem = http.Dir(filepath.Join(pkgDir, "migrations"))
25 changes: 25 additions & 0 deletions admindb/sqlite/migrations_generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT

// +build ignore

package main

import (
"log"

"github.com/shurcooL/vfsgen"

"github.com/ssb-ngi-pointer/gossb-rooms/admindb/sqlite"
)

func main() {
err := vfsgen.Generate(sqlite.Migrations, vfsgen.Options{
PackageName: "sqlite",
BuildTags: "!dev",
VariableName: "Migrations",
})
if err != nil {
log.Fatalln(err)
}

}
186 changes: 186 additions & 0 deletions admindb/sqlite/migrations_vfsdata.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading