From edadb42c998123d12ba0c2e7906e21e6c885f5a8 Mon Sep 17 00:00:00 2001 From: Michael Crosby Date: Fri, 30 May 2014 17:44:00 -0700 Subject: [PATCH] Init database if empty file Docker-DCO-1.1-Signed-off-by: Michael Crosby (github: crosbymichael) --- graphdb/conn_sqlite3.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/graphdb/conn_sqlite3.go b/graphdb/conn_sqlite3.go index 33355ae..b6a8027 100644 --- a/graphdb/conn_sqlite3.go +++ b/graphdb/conn_sqlite3.go @@ -3,23 +3,32 @@ package graphdb import ( - _ "code.google.com/p/gosqlite/sqlite3" // registers sqlite "database/sql" "os" + + _ "code.google.com/p/gosqlite/sqlite3" // registers sqlite ) func NewSqliteConn(root string) (*Database, error) { initDatabase := false - if _, err := os.Stat(root); err != nil { + + stat, err := os.Stat(root) + if err != nil { if os.IsNotExist(err) { initDatabase = true } else { return nil, err } } + + if stat != nil && stat.Size() == 0 { + initDatabase = true + } + conn, err := sql.Open("sqlite3", root) if err != nil { return nil, err } + return NewDatabase(conn, initDatabase) }