Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/joeychilson/litemigrate
A simple SQLite migration library for Go.
https://github.com/joeychilson/litemigrate
go golang migrate migrations sql sqlite
Last synced: 1 day ago
JSON representation
A simple SQLite migration library for Go.
- Host: GitHub
- URL: https://github.com/joeychilson/litemigrate
- Owner: joeychilson
- License: mit
- Created: 2023-03-16T11:14:07.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2023-03-16T11:25:21.000Z (over 1 year ago)
- Last Synced: 2024-06-19T21:56:45.211Z (5 months ago)
- Topics: go, golang, migrate, migrations, sql, sqlite
- Language: Go
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# litemigrate
A simple SQLite migration library for Go.
## Installation
```bash
go get github.com/joeychilson/litemigrate
```## Example
```go
package mainimport (
"context"
"database/sql"
"fmt"
"log""github.com/joeychilson/litemigrate"
)func main() {
ctx := context.Background()// Create the migrations slice.
migrations := litemigrate.Migrations{
{
Version: 1,
Description: "create users table",
Up: func(tx *sql.Tx) error {
_, err := tx.Exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
`)
return err
},
Down: func(tx *sql.Tx) error {
_, err := tx.Exec("DROP TABLE IF EXISTS users;")
return err
},
},
{
Version: 2,
Description: "add email column to users table",
Up: func(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE users ADD COLUMN email TEXT;")
return err
},
Down: func(tx *sql.Tx) error {
_, err := tx.Exec("ALTER TABLE users DROP COLUMN email;")
return err
},
},
}// Create a new database instance.
db, err := litemigrate.New("test.db", &migrations)
if err != nil {
log.Fatalf("failed to create database instance: %v", err)
}// Migrate up to the latest version.
err = db.MigrateUp(ctx)
if err != nil {
log.Fatalf("failed to migrate up: %v", err)
}// Migrate down to the previous version.
err = db.MigrateDown(ctx, 1)
if err != nil {
log.Fatalf("failed to migrate down: %v", err)
}// Get the current version of the database.
version, err := db.CurrentVersion(ctx)
if err != nil {
log.Fatalf("failed to get current version: %v", err)
}
fmt.Printf("current database version: %d\n", version)
}
```