Skip to content

Commit e6de8b6

Browse files
committed
Convert from MySQL to sqlite3
1 parent 3c47d44 commit e6de8b6

File tree

16 files changed

+66
-75
lines changed

16 files changed

+66
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
_vendor
22
/ping
33
/ping-initialize-db
4+
*.sqlite3

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ WORKDIR /go/src/github.com/parkr/ping
33
EXPOSE 3306
44
COPY . .
55
RUN go version
6-
RUN CGO_ENABLED=0 go install github.com/parkr/ping/...
6+
RUN go install github.com/parkr/ping/...
77

88
FROM scratch
99
HEALTHCHECK --start-period=1s --interval=30s --timeout=5s --retries=1 \

Makefile

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@ build:
1111

1212
testdeps:
1313
go build $(PKG)/cmd/ping-initialize-db
14-
script/setup-test-database
1514

1615
test: testdeps
17-
. script/test-env && go test ./...
16+
. script/test-env && go test -v ./...
1817

1918
docker-build:
2019
docker build -t parkr/ping:$(REV) .

README.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ Want to run ping? No problem.
3939

4040
```bash
4141
$ go get github.com/parkr/ping
42-
$ mysql -e 'create database ping;'
43-
$ PING_DB=user:passwd@localhost/ping ping -http=:8972
42+
$ PING_DB=./ping_production.sqlite3 ping -http=:8972
4443
```
4544

4645
Specify a port (defaults to `8000`) and a database URL and you're off to
@@ -52,13 +51,13 @@ invoking `ping` and you're good to go.
5251
Prefer Docker? We got that too!
5352

5453
```bash
55-
$ mysql -e 'create database ping;'
54+
$ mkdir data
5655
$ docker run --rm \
57-
-e PING_DB=user:passwd@host-ip/ping \
56+
-e PING_DB=/srv/data/ping_production.sqlite3 \
57+
-v $(pwd)/data:/srv/data:rw \
5858
parkr/ping \
5959
ping -http=:8972
6060
```
6161

62-
Ensure that your Docker container is given access to the server MySQL is
63-
running on and that MySQL's `bind-address` host is `0.0.0.0` so it can be
64-
accessed by others. Or, use a MySQL proxy.
62+
This will save all data to the specified sqlite3 database,
63+
mounted to the container and written back to the host.

analytics/analytics_test.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
package analytics
22

33
import (
4+
"log"
45
"os"
56
"testing"
67

7-
_ "github.com/go-sql-driver/mysql"
88
"github.com/jmoiron/sqlx"
9+
_ "github.com/mattn/go-sqlite3"
10+
"github.com/parkr/ping/database"
911
)
1012

11-
var db = sqlx.MustConnect("mysql", os.Getenv("PING_DB"))
13+
var db *sqlx.DB
1214

1315
func init() {
14-
db.MustExec(`INSERT INTO visits (ip, host, path, user_agent, created_at) VALUES ('127.0.0.1', 'example.org', '/root', 'go test client', NOW())`)
16+
var err error
17+
db, err = database.Initialize()
18+
if err != nil {
19+
log.Printf("Error connecting to db '%s'", os.Getenv("PING_DB"))
20+
panic(err)
21+
}
22+
db.MustExec(`INSERT INTO visits (ip, host, path, user_agent, created_at) VALUES ('127.0.0.1', 'example.org', '/root', 'go test client', datetime('now'))`)
1523
}
1624

1725
func TestVisitorsForPath(t *testing.T) {
Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,20 @@
11
package main
22

33
import (
4+
"log"
45
"os"
56

6-
_ "github.com/go-sql-driver/mysql"
7-
"github.com/jmoiron/sqlx"
7+
"github.com/parkr/ping/database"
88
)
99

10-
const (
11-
schema = `CREATE TABLE visits (
12-
id int(11) NOT NULL AUTO_INCREMENT,
13-
ip varchar(255) NOT NULL,
14-
host text NOT NULL,
15-
user_agent text NOT NULL,
16-
path text NOT NULL,
17-
created_at datetime NOT NULL,
18-
PRIMARY KEY (id)
19-
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;`
20-
checkIfSchemaExists = `SELECT COUNT(*) as does_exist FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'visits'`
21-
)
22-
23-
type TableCheck struct {
24-
DoesExist int `db:"does_exist"`
25-
}
26-
27-
func VerifySchema() {
28-
db := sqlx.MustConnect("mysql", os.Getenv("PING_DB"))
29-
db.Ping()
30-
var check TableCheck
31-
err := db.Get(&check, checkIfSchemaExists)
32-
if err != nil {
33-
panic(err)
34-
}
35-
if check.DoesExist < 1 {
36-
db.MustExec(schema)
37-
}
10+
func VerifySchema() error {
11+
_, err := database.Initialize()
12+
return err
3813
}
3914

4015
func main() {
41-
VerifySchema()
16+
if err := VerifySchema(); err != nil {
17+
log.Fatalf("error setting up database: %+v", err)
18+
}
19+
log.Printf("database setup at %s", os.Getenv("PING_DB"))
4220
}

database/database.go

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,23 @@ import (
44
"fmt"
55
"os"
66

7-
_ "github.com/go-sql-driver/mysql"
87
"github.com/jmoiron/sqlx"
8+
_ "github.com/mattn/go-sqlite3"
99
)
1010

1111
const (
12-
// This is the format for a MySQL Datetime Literal.
13-
MySQLDateTimeFormat = "2006-01-02 15:04:05"
12+
// This is the format for a SQL Datetime Literal.
13+
SQLDateTimeFormat = "2006-01-02 15:04:05"
1414

1515
schema = `CREATE TABLE visits (
16-
id int(11) NOT NULL AUTO_INCREMENT,
16+
id integer NOT NULL PRIMARY KEY AUTOINCREMENT,
1717
ip varchar(255) NOT NULL,
1818
host text NOT NULL,
19-
user_agent text NOT NULL,
19+
user_agent text NOT NULL,
2020
path text NOT NULL,
21-
created_at datetime NOT NULL,
22-
PRIMARY KEY (id)
23-
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;`
24-
checkIfSchemaExists = `SELECT COUNT(*) as does_exist FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'visits'`
21+
created_at datetime NOT NULL
22+
);`
23+
checkIfSchemaExists = `SELECT COUNT(*) as does_exist FROM sqlite_master WHERE type='table' AND name='visits';`
2524
insertVisit = `INSERT INTO visits (ip, host, path, user_agent, created_at) VALUES (:ip, :host, :path, :user_agent, :created_at)`
2625
)
2726

@@ -30,7 +29,7 @@ type TableCheck struct {
3029
}
3130

3231
func Initialize() (*sqlx.DB, error) {
33-
db, err := sqlx.Connect("mysql", os.Getenv("PING_DB"))
32+
db, err := sqlx.Connect("sqlite3", os.Getenv("PING_DB"))
3433
if err != nil {
3534
return nil, err
3635
}

docs/index.md

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,18 @@ A single tiny JavaScript file that the user requests sends down these three
3131
things and that's all there is to it. As unintrusive as possible, while
3232
still providing insight into the site's strenghts.
3333

34+
It also respects the [Do Not Track header](http://donottrack.us/), which
35+
many browsers now allow users to set for all requests. Complying with this
36+
header is not mandatory, but aligns nicely with our motivation for
37+
respecting users' privacy when they ask for it.
38+
3439
## Installation
3540

3641
Want to run ping? No problem.
3742

3843
```bash
3944
$ go get github.com/parkr/ping
40-
$ mysql -e 'create database ping;'
41-
$ PING_DB=user:passwd@localhost/ping ping -http=:8972
45+
$ PING_DB=./ping_production.sqlite3 ping -http=:8972
4246
```
4347

4448
Specify a port (defaults to `8000`) and a database URL and you're off to
@@ -50,13 +54,13 @@ invoking `ping` and you're good to go.
5054
Prefer Docker? We got that too!
5155

5256
```bash
53-
$ mysql -e 'create database ping;'
57+
$ mkdir data
5458
$ docker run --rm \
55-
-e PING_DB=user:passwd@host-ip/ping \
59+
-e PING_DB=/srv/data/ping_production.sqlite3 \
60+
-v $(pwd)/data:/srv/data:rw \
5661
parkr/ping \
5762
ping -http=:8972
5863
```
5964

60-
Ensure that your Docker container is given access to the server MySQL is
61-
running on and that MySQL's `bind-address` host is `0.0.0.0` so it can be
62-
accessed by others. Or, use a MySQL proxy.
65+
This will save all data to the specified sqlite3 database,
66+
mounted to the container and written back to the host.

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ module github.com/parkr/ping
33
go 1.14
44

55
require (
6-
github.com/go-sql-driver/mysql v1.5.0
76
github.com/jmoiron/sqlx v0.0.0-20170121103519-f980a91bdc37
87
github.com/lib/pq v1.4.0 // indirect
9-
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
8+
github.com/mattn/go-sqlite3 v2.0.3+incompatible
109
github.com/parkr/gossip v0.0.0-20170207041538-a9ea9f924daf
1110
)

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ github.com/jmoiron/sqlx v0.0.0-20170121103519-f980a91bdc37 h1:FZc3adQBtm3UfBaJmQ
44
github.com/jmoiron/sqlx v0.0.0-20170121103519-f980a91bdc37/go.mod h1:IiEW3SEiiErVyFdH8NTuWjSifiEQKUoyK3LNqr2kCHU=
55
github.com/lib/pq v1.4.0 h1:TmtCFbH+Aw0AixwyttznSMQDgbR5Yed/Gg6S8Funrhc=
66
github.com/lib/pq v1.4.0/go.mod h1:5WUZQaWbwv1U+lTReE5YruASi9Al49XbQIvNi/34Woo=
7+
github.com/mattn/go-sqlite3 v1.14.6/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
78
github.com/mattn/go-sqlite3 v2.0.3+incompatible h1:gXHsfypPkaMZrKbD5209QV9jbUTJKjyR5WD3HYQSd+U=
89
github.com/mattn/go-sqlite3 v2.0.3+incompatible/go.mod h1:FPy6KqzDD04eiIsT53CuJW3U88zkxoIYsOqkbpncsNc=
910
github.com/parkr/gossip v0.0.0-20170207041538-a9ea9f924daf h1:rUZlwm2L4Px9+c1V7V2Cdddj55NhfOw8kcILdFHt1eQ=

0 commit comments

Comments
 (0)