Does GORM support working with encrypted SQLite DB?

Viewed 920

I have a requirement for using an ORM for my Go application which can connect to a postgreSQL db or SQLite db.

The docs of gorm don't mention anything about connecting to an encrypted sqlite db.

Is it possible to use it or any way of providing a custom driver for sqlite which will work with and encrypted SQlite DB?

UPDATE

As per Ezequiel Muns comment, I updated the gorm SQLite driver code to use github.com/mutecomm/go-sqlcipher.

This new GORM driver can be found here.

Cheers !!

1 Answers

Gorm uses a system of drivers that adapt the individual database clients for its use.

You'd have to create a new module that copies the source of go-gorm/driver/sqlite but updating https://github.com/go-gorm/sqlite/blob/master/sqlite.go#L8 to import the alternate library of your choice. Since these are forks and they comply with the database/sql interface, it should just work. You'll then be able to use this new driver as per normal in gorm:

import (
    "gorm.io/gorm"
    sqliteEncrypt "path.to/your/gorm-sqlite-encrypt"
)

db, err := gorm.Open(sqliteEncrypt.Open("file:dbFileName?_crypto_key=auxten"), gorm.Options{})
if err != nil {
    panic("failed to connect database")
}
// ...

Note: You could try to to avoid having to update any source in go-gorm/driver/sqlite by making use of the go.mod replace directive, though I don't know how that plays with CGO modules that don't themselves have a go.mod file as is the case with the two libraries you linked.

Related