Skip to content

Commit

Permalink
update readme + workflows
Browse files Browse the repository at this point in the history
  • Loading branch information
glebarez committed Dec 6, 2021
1 parent ff083a0 commit df45a1d
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 14 deletions.
52 changes: 52 additions & 0 deletions .github/workflows/badge-gorm-tests.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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/[email protected]
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
41 changes: 41 additions & 0 deletions .github/workflows/badge-sqlite-version.yml
Original file line number Diff line number Diff line change
@@ -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/[email protected]
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
2 changes: 1 addition & 1 deletion .github/workflows/gorm-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
40 changes: 27 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,47 @@
![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)<br>

## 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 (
"github.com/glebarez/sqlite"
"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)

23 changes: 23 additions & 0 deletions sqlite_version_test.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit df45a1d

Please sign in to comment.