ddns/db/util.go

41 lines
889 B
Go

package db
import (
"context"
"database/sql"
)
// InitSchema initializes the database schema
func InitSchema(db *sql.DB) error {
// Create a new Queries instance
q := New(db)
// Execute the config table creation
if err := q.InitSchema(context.Background()); err != nil {
return err
}
// Create static_records table separately
_, err := db.ExecContext(context.Background(), `
CREATE TABLE IF NOT EXISTS static_records (
record_id TEXT PRIMARY KEY,
record_name TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
`)
if err != nil {
return err
}
// Insert default config if none exists
_, err = db.ExecContext(context.Background(), `
INSERT OR IGNORE INTO config (api_token, zone_id, domain, update_period)
SELECT '', '', 'mz.uy', '0 */6 * * *'
WHERE NOT EXISTS (SELECT 1 FROM config);
`)
if err != nil {
return err
}
return nil
}