From df45a1d1621bb561089fae77ef749114ee20f866 Mon Sep 17 00:00:00 2001 From: glebarez Date: Mon, 6 Dec 2021 18:02:26 +0300 Subject: [PATCH] update readme + workflows --- .github/workflows/badge-gorm-tests.yml | 52 ++++++++++++++++++++++ .github/workflows/badge-sqlite-version.yml | 41 +++++++++++++++++ .github/workflows/gorm-tests.yml | 2 +- README.md | 40 +++++++++++------ sqlite_version_test.go | 23 ++++++++++ 5 files changed, 144 insertions(+), 14 deletions(-) create mode 100644 .github/workflows/badge-gorm-tests.yml create mode 100644 .github/workflows/badge-sqlite-version.yml create mode 100644 sqlite_version_test.go diff --git a/.github/workflows/badge-gorm-tests.yml b/.github/workflows/badge-gorm-tests.yml new file mode 100644 index 0000000..a3daeeb --- /dev/null +++ b/.github/workflows/badge-gorm-tests.yml @@ -0,0 +1,52 @@ +name: Badge GORM tests + +on: + workflow_dispatch: + workflow_run: + workflows: ["GORM-tests"] + branches: [master] + types: [completed] + +jobs: + create-gorm-tests-badge: + runs-on: ubuntu-latest + steps: + - name: Download artifact + uses: dawidd6/action-download-artifact@v2 + with: + workflow: gorm-tests.yml + workflow_conclusion: completed + path: . + + - name: Prepare badge data + working-directory: Linux-1.17-tests.out + run: | + echo "tests_passed=$(cat ./*.out | grep PASS | wc -l)" >> $GITHUB_ENV + echo "tests_skipped=$(cat ./*.out | grep SKIP | wc -l)" >> $GITHUB_ENV + echo "tests_failed=$(cat ./*.out | grep FAIL | wc -l)" >> $GITHUB_ENV + + - name: Make success badge + if: ${{ env.tests_failed == '0' }} + uses: schneegans/dynamic-badges-action@v1.1.0 + with: + auth: ${{ secrets.GIST_SECRET }} + gistID: fb4d23f63d866b3e1e58b26d2f5ed01f + filename: badge-gorm-tests.json + label: GORM tests + message: "Passed: ${{ env.tests_passed }} | Failed: ${{ env.tests_failed }}" + color: 54a158 + style: for-the-badge + labelColor: 25292d + + - name: Make fail badge + if: ${{ env.tests_failed != '0' }} + uses: schneegans/dynamic-badges-action@v1.1.0 + with: + auth: ${{ secrets.GIST_SECRET }} + gistID: fb4d23f63d866b3e1e58b26d2f5ed01f + filename: badge-gorm-tests.json + label: GORM tests + message: "Passed: ${{ env.tests_passed }} | Failed: ${{ env.tests_failed }}" + color: 96232b + style: for-the-badge + labelColor: 25292d diff --git a/.github/workflows/badge-sqlite-version.yml b/.github/workflows/badge-sqlite-version.yml new file mode 100644 index 0000000..99505af --- /dev/null +++ b/.github/workflows/badge-sqlite-version.yml @@ -0,0 +1,41 @@ +name: Badge Sqlite version + +on: + workflow_dispatch: + workflow_run: + workflows: ["GORM-tests"] + branches: [master] + types: [completed] + +jobs: + create-sqlite-version-badge: + runs-on: ubuntu-latest + steps: + - name: Set up Go + uses: actions/setup-go@v2 + with: + go-version: 1.17 + + - name: Check out code into the Go module directory + uses: actions/checkout@v2 + + - name: go mod package cache + uses: actions/cache@v2 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-${{ hashFiles('go.mod') }} + + - name: request sqlite_version() + run: echo "sqlite_version=$(go test . -run '^TestSQLiteVersion$' -v | grep sqlite_version | tr -s ' ' | cut -d' ' -f3)" >> $GITHUB_ENV + + - name: Make version badge + uses: schneegans/dynamic-badges-action@v1.1.0 + with: + auth: ${{ secrets.GIST_SECRET }} + gistID: fb4d23f63d866b3e1e58b26d2f5ed01f + filename: badge-sqlite-version.json + label: SQLite + message: "${{ env.sqlite_version }}" + color: 2269d3 + style: for-the-badge + labelColor: 25292d diff --git a/.github/workflows/gorm-tests.yml b/.github/workflows/gorm-tests.yml index 05256f1..6751ef0 100644 --- a/.github/workflows/gorm-tests.yml +++ b/.github/workflows/gorm-tests.yml @@ -14,7 +14,7 @@ jobs: strategy: matrix: go: ['1.17'] - platform: [ubuntu-latest] + platform: [ubuntu-latest, macos-latest, windows-latest] runs-on: ${{ matrix.platform }} steps: diff --git a/README.md b/README.md index 91ce166..321d520 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,28 @@ +![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/glebarez/fb4d23f63d866b3e1e58b26d2f5ed01f/raw/badge-gorm-tests.json) +![badge](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/glebarez/fb4d23f63d866b3e1e58b26d2f5ed01f/raw/badge-sqlite-version.json) + # Pure-Go GORM Sqlite driver -Pure-go (without cgo) implementation of SQLite driver for [GORM](https://gorm.io) +Pure-go (without cgo) implementation of SQLite driver for [GORM](https://gorm.io)
+ +## How is this better than standard GORM SQLite driver? +The [standard GORM driver for SQLite](gorm.io/driver/sqlite) has one major drawback: it is based on a [Go-bindings of SQLite C-source](https://github.com/mattn/go-sqlite3) (this is called [cgo](https://go.dev/blog/cgo])). That fact imposes following restrictions on Go developers: +- to build and run your code, you will need a C compiler installed on a machine +- SQLite has many features that need to be enabled at compile time (e.g. [json support](https://www.sqlite.org/json1.html)). If you are using those features, you will need to include proper go build tags for every go command to work properly (go run, go test, etc.). Such tweaks may be easy to forget / hard to achieve (e.g. in automated environments like universal CI pipelines for Go) +- Because of C-compiler requirement, you can't build your Go code inside tiny stripped containers like (golang-alpine) + + +# FAQ +### Is this tested good ? +Yes, The CI pipeline of this driver employs [whole test base](https://github.com/go-gorm/gorm/tree/master/tests) of GORM, which includes for than **12k** tests (see badge on the page-top) + +### SQLite is written in C, why don't we need a cgo ? +This driver is based on pure-Go implementation of SQLite (https://gitlab.com/cznic/sqlite), which is basically an original SQLite C-source AST, translated into Go! So, you may be sure you're using the original SQLite implementation under the hood. -## Usage +### Is JSON feature of SQLite enabled? +Yes! + + +# Usage ```go import ( @@ -9,25 +30,18 @@ import ( "gorm.io/gorm" ) -db, err := gorm.Open(sqlite.Open("file:sqlite.db"), &gorm.Config{}) +db, err := gorm.Open(sqlite.Open("sqlite.db"), &gorm.Config{}) ``` ### In-memory DB example ```go -db, err := gorm.Open(sqlite.Open("file::memory:"), &gorm.Config{}) +db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{}) ``` ### Foreign-key constraint activation -Foreign-key constraint is disabled by default in SQLite. To activate it, use connection parameter: +Foreign-key constraint is disabled by default in SQLite. To activate it, use connection URL parameter: ```go -db, err := gorm.Open(sqlite.Open("file::memory:?_pragma=foreign_keys(1)"), &gorm.Config{}) +db, err := gorm.Open(sqlite.Open(":memory:?_pragma=foreign_keys(1)"), &gorm.Config{}) ``` More info: [https://www.sqlite.org/foreignkeys.html](https://www.sqlite.org/foreignkeys.html) -### Shared cache -You also might want to enable shared cache: -```go -db, err := gorm.Open(sqlite.Open("file::memory:?cache=shared"), &gorm.Config{}) -``` -More info: [https://www.sqlite.org/sharedcache.html](https://www.sqlite.org/sharedcache.html) - diff --git a/sqlite_version_test.go b/sqlite_version_test.go new file mode 100644 index 0000000..663230e --- /dev/null +++ b/sqlite_version_test.go @@ -0,0 +1,23 @@ +package sqlite + +import ( + "database/sql" + "log" + "testing" +) + +func TestSQLiteVersion(t *testing.T) { + var version string + + db, err := sql.Open(DriverName, ":memory:") + if err != nil { + log.Fatal(err) + } + + row := db.QueryRow("select sqlite_version()") + if row.Scan(&version) != nil { + log.Fatal(err) + } + + t.Log(version) +}