1+ package  sqlite
2+ 
3+ import  (
4+ 	"fmt" 
5+ 	"sync" 
6+ 	"time" 
7+ 
8+ 	"github.com/golang/glog" 
9+ 	_tls "github.com/kubeflow/model-registry/internal/tls" 
10+ 	"gorm.io/driver/sqlite" 
11+ 	"gorm.io/gorm" 
12+ 	"gorm.io/gorm/logger" 
13+ )
14+ 
15+ const  (
16+ 	// sqliteMaxRetriesDefault is the maximum number of attempts to retry SQLite connection. 
17+ 	sqliteMaxRetriesDefault  =  5  // SQLite is file-based, so fewer retries needed 
18+ )
19+ 
20+ type  SQLiteDBConnector  struct  {
21+ 	DSN           string 
22+ 	TLSConfig     * _tls.TLSConfig  // Not used for SQLite but kept for interface consistency 
23+ 	db            * gorm.DB 
24+ 	connectMutex  sync.Mutex 
25+ 	maxRetries    int 
26+ }
27+ 
28+ func  NewSQLiteDBConnector (
29+ 	dsn  string ,
30+ 	tlsConfig  * _tls.TLSConfig ,
31+ ) * SQLiteDBConnector  {
32+ 	return  & SQLiteDBConnector {
33+ 		DSN :        dsn ,
34+ 		TLSConfig :  tlsConfig ,
35+ 		maxRetries : sqliteMaxRetriesDefault ,
36+ 	}
37+ }
38+ 
39+ func  (c  * SQLiteDBConnector ) WithMaxRetries (maxRetries  int ) * SQLiteDBConnector  {
40+ 	c .maxRetries  =  maxRetries 
41+ 
42+ 	return  c 
43+ }
44+ 
45+ func  (c  * SQLiteDBConnector ) Connect () (* gorm.DB , error ) {
46+ 	// Use mutex to ensure only one connection attempt at a time 
47+ 	c .connectMutex .Lock ()
48+ 	defer  c .connectMutex .Unlock ()
49+ 
50+ 	// If we already have a working connection, return it 
51+ 	if  c .db  !=  nil  {
52+ 		return  c .db , nil 
53+ 	}
54+ 
55+ 	var  db  * gorm.DB 
56+ 	var  err  error 
57+ 
58+ 	// Log warning if TLS configuration is specified (not supported by SQLite) 
59+ 	if  c .TLSConfig  !=  nil  &&  c .needsTLSConfig () {
60+ 		glog .Warningf ("TLS configuration is not supported for SQLite connections, ignoring TLS settings" )
61+ 	}
62+ 
63+ 	for  i  :=  range  c .maxRetries  {
64+ 		glog .V (2 ).Infof ("Attempting to connect to SQLite database: %q (attempt %d/%d)" , c .DSN , i + 1 , c .maxRetries )
65+ 		db , err  =  gorm .Open (sqlite .Open (c .DSN ), & gorm.Config {
66+ 			Logger :         logger .Default .LogMode (logger .Silent ),
67+ 			TranslateError : true ,
68+ 		})
69+ 		if  err  ==  nil  {
70+ 			break 
71+ 		}
72+ 
73+ 		glog .Warningf ("Retrying connection to SQLite (attempt %d/%d): %v" , i + 1 , c .maxRetries , err )
74+ 
75+ 		time .Sleep (time .Duration (i + 1 ) *  time .Second )
76+ 	}
77+ 
78+ 	if  err  !=  nil  {
79+ 		return  nil , fmt .Errorf ("failed to connect to SQLite: %w" , err )
80+ 	}
81+ 
82+ 	glog .Info ("Successfully connected to SQLite database" )
83+ 
84+ 	c .db  =  db 
85+ 
86+ 	return  db , nil 
87+ }
88+ 
89+ func  (c  * SQLiteDBConnector ) DB () * gorm.DB  {
90+ 	return  c .db 
91+ }
92+ 
93+ func  (c  * SQLiteDBConnector ) needsTLSConfig () bool  {
94+ 	if  c .TLSConfig  ==  nil  {
95+ 		return  false 
96+ 	}
97+ 
98+ 	return  c .TLSConfig .CertPath  !=  ""  ||  c .TLSConfig .KeyPath  !=  ""  ||  c .TLSConfig .RootCertPath  !=  ""  ||  c .TLSConfig .CAPath  !=  ""  ||  c .TLSConfig .Cipher  !=  ""  ||  c .TLSConfig .VerifyServerCert 
99+ }
0 commit comments