Skip to content
Snippets Groups Projects
Select Git revision
  • main default
  • update_test_data
  • fix_hopping_eip-service_parsing
  • gateway_selector
  • maxb/update-agent-api-swag
  • maxb/agent-auth-empty-config-fix
  • fix_certs_in_proxy_mode
  • build-img
  • feat/bucket-tagged-locations
  • fix/gitlab-ci
  • fix_ca_cert_handling2
  • set_encryption_algorithm
  • fix_private_key_encoding
  • fix_serviceProvider2
  • maxb/fix-lb-mod
  • maxb/testing-removing-replace
  • feat/migrate-ci-to-podman-buildah
  • maxb/agent-endpoints
  • maxb/agent-endpoints-ci-debugging
  • ca_crt
  • v1.8.1
  • 1.8.0
  • v1.8.0
  • 1.7.2
  • 1.7.1
  • 1.7.0
  • 1.6.0
  • 1.5.2
  • 1.5.1
  • 1.5.0
  • 1.4.1
  • 1.4.0
  • 1.3.0
  • 1.2.0
  • 1.1.0
  • 1.0
36 results

db.go

Blame
  • db.go 863 B
    package storage
    
    import (
    	"errors"
    	"fmt"
    	"strings"
    
    	sqlx "github.com/jmoiron/sqlx"
    
    	_ "github.com/mattn/go-sqlite3"
    )
    
    func OpenDatabase(dburi string) (*sqlx.DB, error) {
    
    	if len(dburi) == 0 {
    		return nil, errors.New("Could not open database (dburi is empty)")
    	}
    
    	// https://www.sqlite.org/wal.html
    	// https://www.sqlite.org/foreignkeys.html
    	if !strings.Contains(dburi, "?") {
    		dburi += "?_foreign_keys=on&_journal=WAL"
    	}
    
    	db, err := sqlx.Open("sqlite3", dburi)
    	if err != nil {
    		return nil, fmt.Errorf("Error opening sqlite db: %w", err)
    	}
    
    	// https://github.com/mattn/go-sqlite3/issues/209
    	db.SetMaxOpenConns(1)
    
    	_, err = db.Exec(`CREATE TABLE IF NOT EXISTS tokens (
    					  key TEXT PRIMARY KEY NOT NULL,
    					  buckets TEXT NOT NULL)`)
    
    	if err != nil {
    		return nil, fmt.Errorf("Error creating tokens table: %w", err)
    	}
    
    	return db, nil
    }