A lightweight database migration tool written in Zig, inspired by goose. Currently supports SQLite databases.
- SQLite database support
- Command-line interface for managing migrations
- Database initialization
- Migration file generation
- Programmatic migration API
git clone https://github.com/yourusername/zigmigrate
cd zigmigrate
zig buildzigmigrate init path/to/database.sqliteThis will create a new SQLite database with the migrations table.
zigmigrate create add_users_tableThis will create a new migration file in the migrations directory with the format:
timestamp_add_users_table.sql
zigmigrate upThis will run all pending migrations to bring your database to the latest version.
const std = @import("std");
const zigmigrate = @import("zigmigrate");
const sqlite = @import("sqlite);
pub fn main() !void {
// Initialize the db
var db = try sqlite.Db.init(.{
.open_flags = .{
.write = true,
.create = true,
},
.threading_mode = .MultiThread,
});
defer db.deinit();
var sqlite_driver = zigmigrate.SqliteDriver{ .db = &db }
var migration = zigmigrate.Driver{ .sqlite_driver = &sqlite_driver}
try init(&db);
}Migration files should be named using the following format:
timestamp_description.sql
Each migration file should contain the SQL statements for both up and down migrations:
-- +zigmigrate Up start
CREATE TABLE users (
id INTEGER PRIMARY KEY,
username TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- +zigmigrate Up stop
-- +zigmigrate Down start
DROP TABLE users;
-- +zigmigrate Down stopzigmigrate keeps track of applied migrations in a migrations table with the following schema:
CREATE TABLE migrations (
id INTEGER PRIMARY KEY,
maname TEXT NOT NULL,
applied_at DATETIME DEFAULT CURRENT_TIMESTAMP
);Requirements:
- Zig 0.11.0 or later
git clone https://github.com/yourusername/zigmigrate
cd zigmigrate
zig buildzig build testThis project is licensed under the MIT License - see the LICENSE file for details.