Skip to content

Commit 8534dba

Browse files
committed
feat(be): sqlite support, limit max connections
1 parent 60de522 commit 8534dba

File tree

7 files changed

+24
-224
lines changed

7 files changed

+24
-224
lines changed

.dredd/hooks/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func truncateAll() {
7070
case *bolt.BoltDb:
7171
// Do nothing
7272
case *sql.SqlDb:
73-
switch store.(*sql.SqlDb).Sql().Dialect().(type) {
73+
switch store.(*sql.SqlDb).Sql().Dialect.(type) {
7474
case gorp.PostgresDialect:
7575
// Do nothing
7676
case gorp.MySQLDialect:

db/sql/SqlDb.go

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import (
2222
)
2323

2424
type SqlDb struct {
25-
sql Sql
25+
sql *gorp.DbMap
2626
dialect string
2727
}
2828

@@ -124,7 +124,7 @@ func (d *SqlDb) prepareQueryWithDialect(query string, dialect gorp.Dialect) stri
124124
}
125125

126126
func (d *SqlDb) PrepareQuery(query string) string {
127-
return d.prepareQueryWithDialect(query, d.sql.Dialect())
127+
return d.prepareQueryWithDialect(query, d.sql.Dialect)
128128
}
129129

130130
func (d *SqlDb) insert(primaryKeyColumnName string, query string, args ...any) (int, error) {
@@ -134,7 +134,7 @@ func (d *SqlDb) insert(primaryKeyColumnName string, query string, args ...any) (
134134
func (d *SqlDb) insertBy(executor gorp.SqlExecutor, primaryKeyColumnName string, query string, args ...any) (int, error) {
135135
var insertId int64
136136

137-
switch d.sql.Dialect().(type) {
137+
switch d.sql.Dialect.(type) {
138138
case gorp.PostgresDialect:
139139
var err error
140140
if primaryKeyColumnName != "" {
@@ -337,7 +337,7 @@ func (d *SqlDb) deleteObject(projectID int, props db.ObjectProps, objectID any)
337337
}
338338

339339
func (d *SqlDb) Close(token string) {
340-
err := d.sql.Close()
340+
err := d.sql.Db.Close()
341341
if err != nil {
342342
panic(err)
343343
}
@@ -378,11 +378,22 @@ func (d *SqlDb) Connect(_ string) {
378378
panic(err)
379379
}
380380

381-
d.sql = Create(cfg.Dialect, sqlDb) //&gorp.DbMap{Db: sqlDb, Dialect: dialect}
381+
var dialect gorp.Dialect
382382

383-
//if d.GetDialect() == util.DbDriverSQLite {
384-
// sqlDb.SetMaxOpenConns(1)
385-
//}
383+
switch cfg.Dialect {
384+
case util.DbDriverMySQL:
385+
dialect = gorp.MySQLDialect{Engine: "InnoDB", Encoding: "UTF8"}
386+
case util.DbDriverPostgres:
387+
dialect = gorp.PostgresDialect{}
388+
case util.DbDriverSQLite:
389+
dialect = gorp.SqliteDialect{}
390+
}
391+
392+
d.sql = &gorp.DbMap{Db: sqlDb, Dialect: dialect}
393+
394+
if d.GetDialect() == util.DbDriverSQLite {
395+
sqlDb.SetMaxOpenConns(1)
396+
}
386397

387398
d.sql.AddTableWithName(db.APIToken{}, "user__token").SetKeys(false, "id")
388399
d.sql.AddTableWithName(db.AccessKey{}, "access_key").SetKeys(true, "id")
@@ -495,7 +506,7 @@ func (d *SqlDb) getObjectRefsFrom(
495506
return
496507
}
497508

498-
func (d *SqlDb) Sql() Sql {
509+
func (d *SqlDb) Sql() *gorp.DbMap {
499510
return d.sql
500511
}
501512

db/sql/common_sql.go

Lines changed: 0 additions & 190 deletions
This file was deleted.

db/sql/migration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ func getVersionSQL(name string, ignoreErrors bool) (queries []string) {
5555
// prepareMigration converts migration SQLite-query to current dialect.
5656
// Supported MySQL and Postgres dialects.
5757
func (d *SqlDb) prepareMigration(query string) string {
58-
switch d.sql.Dialect().(type) {
58+
switch d.sql.Dialect.(type) {
5959
case gorp.MySQLDialect:
6060
query = autoIncrementRE.ReplaceAllString(query, "auto_increment")
6161
query = ifExistsRE.ReplaceAllString(query, "")

db/sql/migration_2_10_24.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type migration_2_10_24 struct {
77
}
88

99
func (m migration_2_10_24) PreApply(tx *gorp.Transaction) error {
10-
switch m.db.sql.Dialect().(type) {
10+
switch m.db.sql.Dialect.(type) {
1111
case gorp.MySQLDialect:
1212
_, _ = tx.Exec(m.db.PrepareQuery("alter table `project__template` drop foreign key `project__template_ibfk_6`"))
1313
case gorp.PostgresDialect:

db/sql/migration_2_8_42.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ type migration_2_8_42 struct {
77
}
88

99
func (m migration_2_8_42) PostApply(tx *gorp.Transaction) error {
10-
switch m.db.sql.Dialect().(type) {
10+
switch m.db.sql.Dialect.(type) {
1111
case gorp.MySQLDialect:
1212
_, _ = tx.Exec(m.db.PrepareQuery("alter table `task` drop foreign key `task_ibfk_3`"))
1313
case gorp.PostgresDialect:

db/sql/sql.go

Lines changed: 0 additions & 21 deletions
This file was deleted.

0 commit comments

Comments
 (0)