-
Notifications
You must be signed in to change notification settings - Fork 0
/
zap.go
51 lines (41 loc) · 1.24 KB
/
zap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package main
import (
"database/sql"
"fmt"
"strings"
"github.com/stimtech/go-migration/v2"
_ "github.com/mattn/go-sqlite3"
"go.uber.org/zap"
)
// Simple example app with a zap logger
// An sqlite database will be created, and a table added.
// Run with 'go run zap.go'.
// Two migrations are present in db/migration and will be loaded into the db in alphabetical order.
func main() {
logger, _ := zap.NewProduction()
// connect to a new or existing sqlite datasource
db, err := sql.Open("sqlite3", "db.sqlite")
if err != nil {
logger.Fatal("failed to create datasource", zap.Error(err))
}
// run migration
m := migration.New(db, migration.ZapOption{Logger: logger})
err = m.Migrate()
if err != nil {
logger.Fatal("failed to run database migration", zap.Error(err))
}
// list all tables in the database
res, err := db.Query("select name from sqlite_master where type='table' order by name")
if err != nil {
logger.Fatal("failed to query database", zap.Error(err))
}
var tables []string
for res.Next() {
var tn string
if err := res.Scan(&tn); err != nil {
logger.Fatal("failed to scan row", zap.Error(err))
}
tables = append(tables, tn)
}
logger.Info(fmt.Sprintf("Tables in database: %s", strings.Join(tables, ", ")))
}