From 0c97f22069d77cf3cac92d68173e38f83b258a9a Mon Sep 17 00:00:00 2001 From: Jonas Knobloch Date: Sat, 11 Apr 2026 01:09:16 +0200 Subject: Add lesci module --- research/lesci/database.go | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 research/lesci/database.go (limited to 'research/lesci/database.go') diff --git a/research/lesci/database.go b/research/lesci/database.go new file mode 100644 index 0000000..69c8d49 --- /dev/null +++ b/research/lesci/database.go @@ -0,0 +1,38 @@ +package lesci + +import ( + "context" + "database/sql" + "errors" + "fmt" +) + +func EnsureTable(db *sql.DB, table string, query string) (bool, error) { + ctx := context.Background() + + var exists bool + + queryTableExists := `SELECT COUNT(*) > 0 FROM information_schema.tables WHERE table_name = ?` + + if err := db.QueryRowContext(ctx, queryTableExists, table).Scan(&exists); err != nil { + return false, err + } + + if !exists { + if _, err := db.ExecContext(ctx, query); err != nil { + return false, err + } + + return true, nil + } + + var count int64 + + queryNumRows := fmt.Sprintf(`SELECT COUNT(*) FROM "%s"`, table) + + if err := db.QueryRowContext(ctx, queryNumRows).Scan(&count); err != nil { + return false, errors.New("table not empty") + } + + return count == 0, nil +} -- cgit v1.3.1