Skip to content

Commit ae09b64

Browse files
committed
Embed migrations and static files.
1 parent a889156 commit ae09b64

File tree

8 files changed

+656
-35
lines changed

8 files changed

+656
-35
lines changed

Makefile

+1-2
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,12 @@ test:
1717
golint $$pkg ; \
1818
done
1919
@go vet $(PKGS)
20-
@go test -cover -v $(PKGS)
20+
@go test -p 1 -cover -v $(PKGS)
2121

2222
package: clean build
2323
@echo "Creating package"
2424
@mkdir -p builds/$(VERSION)
2525
@cp -R bin/ builds/$(VERSION)/bin
26-
@cp -R migrations/ builds/$(VERSION)/migrations
2726
@cd builds/$(VERSION)/ && tar -pczf ../loraserver_$(VERSION)_linux_amd64.tar.gz .
2827
@rm -rf builds/$(VERSION)
2928

README.md

+5-4
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,12 @@ Note: This project is under development. Please test and give feedback but know
4141
* Install Redis (used to store node sessions).
4242

4343
* Start the ``loraserver`` service. The ``--help`` argument will show you all the available
44-
config options.
44+
config options. With ``--db-automigrate`` the database schema will be created / updated
45+
automatically.
4546

46-
* Use the web-interface to create an application and node. You should now be able to
47-
use OTAA to activate your node. Alternatively, use the web-interface to activate your
48-
node (Session / ABP button).
47+
* Use the web-interface (by default http://localhost:8000/) to create an application and
48+
node. You should now be able to use OTAA to activate your node. Alternatively, use the
49+
web-interface to activate your node (Session / ABP button).
4950

5051
* See https://github.com/brocaar/loratestapp for an example application implementation.
5152

base_test.go

+8-6
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
golog "log"
66
"os"
77

8-
"github.com/DavidHuie/gomigrate"
98
log "github.com/Sirupsen/logrus"
9+
"github.com/brocaar/loraserver/migrations"
1010
"github.com/brocaar/lorawan"
1111
"github.com/garyburd/redigo/redis"
1212
"github.com/jmoiron/sqlx"
13+
"github.com/rubenv/sql-migrate"
1314
)
1415

1516
func init() {
@@ -47,14 +48,15 @@ func mustFlushRedis(p *redis.Pool) {
4748
}
4849

4950
func mustResetDB(db *sqlx.DB) {
50-
m, err := gomigrate.NewMigrator(db.DB, gomigrate.Postgres{}, "./migrations")
51-
if err != nil {
52-
log.Fatal(err)
51+
m := &migrate.AssetMigrationSource{
52+
Asset: migrations.Asset,
53+
AssetDir: migrations.AssetDir,
54+
Dir: "",
5355
}
54-
if err := m.RollbackAll(); err != nil {
56+
if _, err := migrate.Exec(db.DB, "postgres", m, migrate.Down); err != nil {
5557
log.Fatal(err)
5658
}
57-
if err := m.Migrate(); err != nil {
59+
if _, err := migrate.Exec(db.DB, "postgres", m, migrate.Up); err != nil {
5860
log.Fatal(err)
5961
}
6062
}

cmd/loraserver/main.go

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
1+
//go:generate go-bindata -prefix ../../migrations/ -pkg migrations -o ../../migrations/migrations.go ../../migrations/
2+
//go:generate go-bindata -prefix ../../static/ -pkg static -o ../../static/static.go ../../static/...
3+
14
package main
25

36
import (
4-
"io/ioutil"
5-
golog "log"
67
"net/http"
78
"os"
89
"os/signal"
910
"syscall"
1011

11-
"github.com/DavidHuie/gomigrate"
1212
log "github.com/Sirupsen/logrus"
1313
"github.com/brocaar/loraserver"
1414
application "github.com/brocaar/loraserver/application/mqttpubsub"
1515
gateway "github.com/brocaar/loraserver/gateway/mqttpubsub"
16+
"github.com/brocaar/loraserver/migrations"
17+
"github.com/brocaar/loraserver/static"
1618
"github.com/brocaar/lorawan"
1719
"github.com/codegangsta/cli"
20+
"github.com/elazarl/go-bindata-assetfs"
1821
_ "github.com/lib/pq"
22+
"github.com/rubenv/sql-migrate"
1923
)
2024

2125
var version string // set by the compiler
2226

23-
func init() {
24-
golog.SetOutput(ioutil.Discard)
25-
}
26-
2727
func run(c *cli.Context) {
2828
// parse the NetID
2929
var netID lorawan.NetID
@@ -57,13 +57,16 @@ func run(c *cli.Context) {
5757
// auto-migrate the database
5858
if c.Bool("db-automigrate") {
5959
log.Info("applying database migrations")
60-
migrator, err := gomigrate.NewMigrator(db.DB, gomigrate.Postgres{}, c.String("db-migrations-path"))
61-
if err != nil {
62-
log.Fatalf("could not create the migrator: %s", err)
60+
m := &migrate.AssetMigrationSource{
61+
Asset: migrations.Asset,
62+
AssetDir: migrations.AssetDir,
63+
Dir: "",
6364
}
64-
if err = migrator.Migrate(); err != nil {
65-
log.Fatalf("could run the migrations: %s", err)
65+
n, err := migrate.Exec(db.DB, "postgres", m, migrate.Up)
66+
if err != nil {
67+
log.Fatalf("migrations failed: %s", err)
6668
}
69+
log.WithField("count", n).Info("migrations applied")
6770
}
6871

6972
ctx := loraserver.Context{
@@ -91,7 +94,12 @@ func run(c *cli.Context) {
9194

9295
// setup static file server (for the gui)
9396
log.WithField("path", "/").Info("registering gui handler")
94-
http.Handle("/", http.FileServer(http.Dir("static")))
97+
http.Handle("/", http.FileServer(&assetfs.AssetFS{
98+
Asset: static.Asset,
99+
AssetDir: static.AssetDir,
100+
AssetInfo: static.AssetInfo,
101+
Prefix: "",
102+
}))
95103

96104
// start the http server
97105
go func() {
@@ -119,11 +127,6 @@ func main() {
119127
EnvVar: "DB_AUTOMIGRATE",
120128
},
121129
cli.StringFlag{
122-
Name: "db-migrations-path",
123-
Value: "./migrations",
124-
Usage: "path to the directory containing the database migrations",
125-
EnvVar: "DB_MIGRATIONS_PATH",
126-
}, cli.StringFlag{
127130
Name: "net-id",
128131
Usage: "network identifier (NetID, 3 bytes) encoded as HEX (e.g. 010203)",
129132
EnvVar: "NET_ID",

migrations/0001_initial_up.sql migrations/0001_initial.sql

+9
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
-- +migrate Up
12
create table application (
23
app_eui bytea primary key,
34
name character varying (100) not null
@@ -11,3 +12,11 @@ create table node (
1112
);
1213

1314
create index node_app_eui on node (app_eui);
15+
16+
17+
-- +migrate Down
18+
drop index node_app_eui;
19+
20+
drop table node;
21+
22+
drop table application;

migrations/0001_initial_down.sql

-5
This file was deleted.

0 commit comments

Comments
 (0)