Files
ddns-updater/internal/persistence/sqlite/database.go
Quentin McGaw bdb0c2bf2e Refactor entire Go codebase (#32)
- Small UI adjustments
- Only show last 2 previous IP addresses in notifications and UI
- Database uses interfaces to be modular/pluggable in order to move away from sqlite
- Less dependencies, it even uses a switch statement instead of httprouter
- Updated golibs
- Changed default logging format to `console` (zap)
- Better code overall, modular updater and trigger system
- Refactored readme
- CI script improved
2020-02-22 17:21:32 -05:00

35 lines
694 B
Go

package sqlite
import (
"database/sql"
"sync"
)
type database struct {
sqlite *sql.DB
sync.Mutex
}
func (db *database) Close() error {
return db.sqlite.Close()
}
// NewDatabase opens or creates the database if necessary.
func NewDatabase(dataDir string) (*database, error) {
sqlite, err := sql.Open("sqlite3", dataDir+"/updates.db")
if err != nil {
return nil, err
}
_, err = sqlite.Exec(
`CREATE TABLE IF NOT EXISTS updates_ips (
domain TEXT NOT NULL,
host TEXT NOT NULL,
ip TEXT NOT NULL,
t_new DATETIME NOT NULL,
t_last DATETIME NOT NULL,
current INTEGER DEFAULT 1 NOT NULL,
PRIMARY KEY(domain, host, ip, t_new)
);`)
return &database{sqlite: sqlite}, err
}