Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add database driver for SQLCipher #436

Merged
merged 5 commits into from
Sep 3, 2020

Conversation

klingtnet
Copy link
Contributor

@klingtnet klingtnet commented Sep 2, 2020

This is a copy of the existing sqlite3 driver, except that it imports _ "github.com/mutecomm/go-sqlcipher/v4" as database driver. This allows to use migrate with encrypted sqlite databases.

@klingtnet
Copy link
Contributor Author

Here's a small demo application that I used against my local fork:

package main

import (
        "database/sql"
        "errors"
        "fmt"
        "log"
        "net/url"

        "github.com/golang-migrate/migrate/database/sqlcipher"
        "github.com/golang-migrate/migrate/v4"
        _ "github.com/golang-migrate/migrate/v4/source/file"
        _ "github.com/mutecomm/go-sqlcipher/v4"
)

func main() {
        key := url.QueryEscape("secret")
        dbname := fmt.Sprintf("file:example.db?_pragma_key=%s&_pragma_cipher_page_size=4096", key)
        db, err := sql.Open("sqlite3", dbname)
        if err != nil {
                log.Fatal("sql.Open: ", err)
        }
        driver, err := sqlcipher.WithInstance(db, &sqlcipher.Config{})
        if err != nil {
                log.Fatal("sqlcipher.WithInstance: ", err)
        }

        m, err := migrate.NewWithDatabaseInstance(
                "file://migrations",
                "sqlcipher", driver)
        if err != nil {
                log.Fatal("migrate.NewWithDatabaseInstance: ", err)
        }
        err = m.Up()
        if err != nil {
                if errors.Is(err, migrate.ErrNoChange) {
                        log.Println("already migrated, nothing to do")
                } else {
                        log.Fatal(err)
                }
        }
}

@klingtnet
Copy link
Contributor Author

klingtnet commented Sep 2, 2020

/cc @frankbraun

This is a copy of the existing sqlite3 driver, except that it imports `_
"github.com/mutecomm/go-sqlcipher/v4"` as database driver.  This allows
to use migrate with encrypted sqlite databases.
@coveralls
Copy link

coveralls commented Sep 2, 2020

Pull Request Test Coverage Report for Build 853

  • 118 of 176 (67.05%) changed or added relevant lines in 1 file are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 53.733%

Changes Missing Coverage Covered Lines Changed/Added Lines %
database/sqlcipher/sqlcipher.go 118 176 67.05%
Totals Coverage Status
Change from base Build 846: 0.5%
Covered Lines: 2764
Relevant Lines: 5144

💛 - Coveralls

@klingtnet
Copy link
Contributor Author

klingtnet commented Sep 2, 2020

Pull Request Test Coverage Report for Build 851

* **118** of **176**   **(67.05%)**  changed or added relevant lines in **1** file are covered.

* No unchanged relevant lines lost coverage.

* Overall coverage increased (+**0.5%**) to **53.733%**

Changes Missing Coverage Covered Lines Changed/Added Lines %
database/sqlcipher/sqlcipher.go 118 176 67.05%
Totals Coverage Status
Change from base Build 846: 0.5%
Covered Lines: 2764
Relevant Lines: 5144

yellow_heart - Coveralls

I could not find a failing even though the CI run with Go 1.14 exited with 1.

Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

Add sqlcipher to DATABASE_TEST in the Makefile.

If you want sqlcipher in the CLI, you'll also need to add the proper build_sqlcipher.go file in internal/cli.

@@ -0,0 +1,3 @@
# sqlcipher

This is just a copy of the sqlite3 driver except that it imports `github.com/mutecomm/go-sqlcipher`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

link to the sqlite3 db driver

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything done.

@klingtnet klingtnet changed the title Add database driver for sqlcipher Add database driver for SQLCipher Sep 2, 2020
Copy link
Member

@dhui dhui left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Waiting for tests to pass

@dhui dhui merged commit cb28f6b into golang-migrate:master Sep 3, 2020
@klingtnet klingtnet deleted the add-sqlcipher-database-driver branch September 10, 2020 10:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants