5 Commits

Author SHA1 Message Date
2eacf6fbc4 Improve migration handling and error forwarding in deferred statements
All checks were successful
Build / build (push) Successful in 42s
2025-03-16 11:38:40 +01:00
2ff3477812 Name step
All checks were successful
Build / build (push) Successful in 28s
2025-03-16 11:06:41 +01:00
68f8dc50e0 Use go action
All checks were successful
Build / build (push) Successful in 1m56s
2025-03-16 11:06:12 +01:00
187ed5987d Add update test
All checks were successful
Build / build (push) Successful in 1m13s
2025-03-16 11:04:21 +01:00
a377448de3 Add readme
All checks were successful
Build / build (push) Successful in 1m22s
2025-03-16 09:31:00 +01:00
7 changed files with 148 additions and 2 deletions

View File

@@ -7,5 +7,10 @@ jobs:
- name: Checkout
uses: actions/checkout@v4
- name: Setup Go
uses: actions/setup-go@v5
with:
go-version: '>=1.24'
- name: Test
run: go test . -v

92
README.md Normal file
View File

@@ -0,0 +1,92 @@
# MySQLite
A Go library that provides a convenient wrapper around SQLite with additional functionality for database management, migrations, and transactions.
## Features
- Simple and intuitive SQLite database connection management
- Thread-safe database operations with built-in locking mechanism
- Support for database migrations
- Transaction management
- Built on top of [zombiezen.com/go/sqlite](https://pkg.go.dev/zombiezen.com/go/sqlite)
## Installation
```bash
go get gitea.seeseepuff.be/seeseemelk/mysqlite
```
## Usage
### Opening a Database Connection
```go
import "gitea.seeseepuff.be/seeseemelk/mysqlite"
// Open an in-memory database
db, err := mysqlite.OpenDb(":memory:")
if err != nil {
// Handle error
}
defer db.Close()
// Open a file-based database
db, err := mysqlite.OpenDb("path/to/database.db")
if err != nil {
// Handle error
}
defer db.Close()
```
### Executing Queries
The library provides methods for executing SQL queries and managing transactions:
```go
// Execute a simple query
err := db.Query("CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT)").Exec()
// Use transactions
tx, err := db.BeginTransaction()
if err != nil {
// Handle error
}
// Perform operations within transaction
// ...
// Commit or rollback
err = tx.Commit() // or tx.Rollback()
```
### Database Migrations
The library includes support for SQL-based migrations. Migrations are SQL files stored in a directory and are executed in order based on their filename prefix:
1. Create a directory for your migrations (e.g., `migrations/`)
2. Add numbered SQL migration files:
```
migrations/
├── 1_initial.sql
├── 2_add_users.sql
├── 3_add_posts.sql
```
3. Embed the migrations in your Go code:
```go
import "embed"
//go:embed migrations/*.sql
var migrations embed.FS
// Apply migrations
err := db.MigrateDb(migrations, "migrations")
if err != nil {
// Handle error
}
```
Each migration file should contain valid SQL statements. The migrations are executed in order and are tracked internally to ensure they only run once.
## Requirements
- Go 1.24 or higher

View File

@@ -74,6 +74,9 @@ func performSingleMigration(d *Db, migrationScript []byte, targetVersion int) er
for _, statement := range statements {
statement = strings.TrimSpace(statement)
if statement == "" {
continue
}
err = tx.Query(statement).Exec()
if err != nil {
return fmt.Errorf("error performing migration: %v", err)

View File

@@ -58,7 +58,7 @@ func (q *Query) Exec() (rerr error) {
defer q.unlock()
if q.stmt != nil {
defer func() { rerr = q.stmt.Finalize() }()
defer func() { forwardError(q.stmt.Finalize(), &rerr) }()
}
if q.err != nil {
return q.err
@@ -84,7 +84,7 @@ func (q *Query) ScanSingle(results ...any) (rerr error) {
defer q.unlock()
// Scan rows
if q.stmt != nil {
defer func() { rerr = q.stmt.Finalize() }()
defer func() { forwardError(q.stmt.Finalize(), &rerr) }()
}
if q.err != nil {
return q.err

View File

@@ -66,3 +66,27 @@ func TestQueryWithRange(t *testing.T) {
}
require.NoError(t, err)
}
func TestUpdateQuery(t *testing.T) {
db := openTestDb(t)
func() {
tx := db.MustBegin()
defer tx.MustRollback()
tx.Query("insert into mytable(key, value) values ('lorem', 'bar')").MustExec()
value := "ipsum"
key := "lorem"
tx.Query("update mytable set value = ? where key = ?").Bind(value, key).MustExec()
tx.MustCommit()
}()
var value string
db.Query("select value from mytable where key = 'lorem'").MustScanSingle(&value)
require.Equal(t, "ipsum", value)
}
func TestUpdateQueryWithWrongArguments(t *testing.T) {
db := openTestDb(t)
value := "ipsum"
err := db.Query("insert into mytable(key, value) values ('lorem', ?)").Bind(&value).Exec()
require.Error(t, err)
}

View File

@@ -15,11 +15,26 @@ func (d *Db) Begin() (*Tx, error) {
return &Tx{db: d}, nil
}
func (d *Db) MustBegin() *Tx {
tx, err := d.Begin()
if err != nil {
panic(err)
}
return tx
}
func (tx *Tx) Commit() error {
defer tx.unlock()
return tx.Query("COMMIT").Exec()
}
func (tx *Tx) MustCommit() {
err := tx.Commit()
if err != nil {
panic(err)
}
}
func (tx *Tx) Rollback() error {
if tx.db == nil {
// The transaction was already commited

7
util.go Normal file
View File

@@ -0,0 +1,7 @@
package mysqlite
func forwardError(from error, to *error) {
if from != nil {
*to = from
}
}