update readme + workflows

This commit is contained in:
glebarez
2021-12-06 18:02:26 +03:00
parent ff083a003b
commit df45a1d162
5 changed files with 145 additions and 15 deletions

52
.github/workflows/badge-gorm-tests.yml vendored Normal file
View File

@@ -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

View File

@@ -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

View File

@@ -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:

View File

@@ -1,7 +1,28 @@
# Pure-Go GORM Sqlite driver
Pure-go (without cgo) implementation of SQLite driver for [GORM](https://gorm.io)
![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)
## Usage
# Pure-Go GORM Sqlite driver
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.
### 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)

23
sqlite_version_test.go Normal file
View File

@@ -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)
}