autosqlite

package module
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2025 License: Unlicense Imports: 10 Imported by: 0

README

Autosqlite

A Go module for creating SQLite databases from schema files and automatically handling migrations when the schema changes.

  • Creates SQLite databases from SQL schema strings
  • Automatic schema migration
  • Returns a *sql.DB handle for immediate use
  • Skips migration if schema is unchanged
  • Avoids backwards migrations
  • Creates backups before migration
  • Uses the mattn/go-sqlite3 driver

When a schema change is found, Autosqlite creates a database from the new schema, and copies all of the data from the old database where table and column names are equal, and then renames the new database on top of the old one.

Autosqlite creates a table called _autosqlite_version, listing schemas that have been applied by Autosqlite. If Autosqlite finds itself trying to apply a schema that is older than the newest version that has been applied (for example because you launched an old version of your program) it bails out instead of reverting to the old schema.

Caveats

Schema migration might not do the right thing in some circumstances:

  • Renaming a column or table is indistinguishable from deleting the old one and adding a new one, so data loss is guaranteed if you rename columns or tables
  • If another program has the old database file open while you try to migrate it, you might lose data
  • If you use foreign key constraints, Autosqlite won't necessarily re-populate the tables in the right order, leading to migration failures
  • If you introduce a NOT NULL constraint on a column that previously had NULL values, migration will fail unless the column also has a DEFAULT value (in which case NULL values will be replaced with the default)
  • You can't revert to an old schema, because of the backwards migration prevention; you'd need to make some other trivial change to the schema

Use Autosqlite to save yourself time while developing a new application. DON'T attempt to use Autosqlite to handle important data. For handling important data you should use a more robust method to deploy database schema migrations.

If you do want to use Autosqlite to handle schema migrations automatically, the safest way is to use the CLI tool, and manually run it every time you need to update the schema.

The more dangerous, but more convenient, way is to embed the schema into your program using go:embed and use autosqlite.Open() to open the database. It will automatically migrate the schema whenever a new version is found.

CLI Tool

Autosqlite includes a command-line tool for database management.

Installing the CLI Tool

Option 1: Install globally (recommended)

go install github.com/jes/autosqlite/cmd/autosqlite@latest

Option 2: Add as a project tool

go get -tool github.com/jes/autosqlite/cmd/autosqlite

Then run with: go tool autosqlite

Option 3: Build locally

git clone https://github.com/jes/autosqlite.git
cd autosqlite
go build -o autosqlite cmd/autosqlite/main.go
CLI Usage
# Validate a schema file
autosqlite -validate -schema schema.sql

# Test migration without applying changes
autosqlite -dry-run -schema schema.sql -db app.db

# Migrate database in place (creates backup)
autosqlite -schema schema.sql -db app.db -in-place

# Create new database with migrated schema
autosqlite -schema schema.sql -db app.db -new-db app_v2.db

# Add verbose output to any command
autosqlite -schema schema.sql -db app.db -in-place -verbose
CLI Commands
  • -validate -schema <file> - Validate schema syntax
  • -dry-run -schema <file> -db <file> - Test migration without applying
  • -schema <file> -db <file> -in-place - Migrate database in place
  • -schema <file> -db <file> -new-db <file> - Create new database with migrated schema
  • -verbose - Show detailed tool mation

Package Usage

go get github.com/jes/autosqlite

Put your schema in schema.sql and embed it in a string in your program:

package main

import (
    "log"
    "github.com/jes/autosqlite"
    _ "github.com/mattn/go-sqlite3"
    "embed"
)

//go:embed schema.sql
var schemaSQL string

func main() {
    db, err := autosqlite.Open(schemaSQL, "myapp.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    
    // Use the database...
}

Function Signatures

Open
func Open(schema string, dbPath string) (*sql.DB, error)

Creates or migrates a SQLite database at dbPath using the provided schema SQL. If the database does not exist, it is created. If it exists and the schema is unchanged, the database is opened as-is. If the schema has changed, a migration is performedand the previous database file is backed up with a ".backup" extension.

Returns a *sql.DB handle or an error.

Migrate
func Migrate(schema string, dbPath string) (*sql.DB, error)

Migrates an existing SQLite database at dbPath to the provided schema. It creates a backup with a ".backup" extension, migrates data for common columns, and atomically replaces the old database.

Returns a *sql.DB handle or an error.

MigrateToNewFile
func MigrateToNewFile(schema string, oldDbPath string, newDbPath string) (*sql.DB, error)

Migrates an existing SQLite database at oldDbPath to the provided schema, writing the result to newDbPath. It migrates data for common columns and tables.

Returns a *sql.DB handle to the new database or an error.

Example

See the cmd/autosqlite/ directory for a complete working example.

Dependencies

  • github.com/mattn/go-sqlite3 - SQLite driver for Go
  • github.com/gofrs/flock - Cross-platform file locking

Documentation

Overview

Package autosqlite provides automatic SQLite database creation and migration from a schema string. It can create a new database, migrate an existing one to a new schema, and ensures data is preserved for common columns and tables.

Features:

  • Automatic schema migration
  • Data preservation for common columns
  • Backup and atomic replacement
  • Skips migration if schema is unchanged
  • Prevents backward migrations by tracking hashes of schemas already applied
  • Handles NOT NULL constraints with DEFAULT values by replacing NULL values during migration

Usage:

package main

import (
    "log"
    "github.com/jes/autosqlite"
    _ "github.com/mattn/go-sqlite3"
    "embed"
)

//go:embed schema.sql
var schemaSQL string

func main() {
    db, err := autosqlite.Open(schemaSQL, "myapp.db")
    if err != nil {
        log.Fatal(err)
    }
    defer db.Close()
    // ...
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FindCommonColumns

func FindCommonColumns(oldColumns, newColumns []ColumnInfo) []string

FindCommonColumns returns columns that exist in both old and new tables.

func GetColumns

func GetColumns(db *sql.DB, tableName string) ([]string, error)

GetColumns returns a list of column names for a table.

func GetTables

func GetTables(db *sql.DB) ([]string, error)

GetTables returns a list of user table names in the database (ignores _autosqlite_version).

func Migrate

func Migrate(schema, dbPath string) (*sql.DB, error)

Migrate migrates an existing SQLite database at dbPath to the provided schema. It creates a backup with a ".backup" extension, migrates data for common columns, and atomically replaces the old database.

The dbPath parameter can include SQLite query parameters (e.g., "foo.db?_busy_timeout=1000"). File operations will use only the filename part, while database connections will use the full string.

Returns a *sql.DB handle or an error.

func MigrateTable

func MigrateTable(oldDB, newDB *sql.DB, tableName string) error

MigrateTable migrates data from old table to new table, copying only common columns. When migrating to a NOT NULL column with a DEFAULT value, NULL values from the old table are automatically replaced with the DEFAULT value using SQL's COALESCE function. Returns an error if migration fails.

func MigrateToNewFile

func MigrateToNewFile(schema, oldDbPath string, newDbPath string) (*sql.DB, error)

MigrateToNewFile migrates an existing SQLite database at oldDbPath to the provided schema, writing the result to newDbPath. It migrates data for common columns and tables.

Returns a *sql.DB handle to the new database or an error.

func Open

func Open(schema, dbPath string) (*sql.DB, error)

Open creates or migrates a SQLite database at dbPath using the provided schema SQL. If the database does not exist, it is created. If it exists and the schema is unchanged, the database is opened as-is. If the schema has changed, a migration is performed and the previous database file is backed up with a ".backup" extension.

The dbPath parameter can include SQLite query parameters (e.g., "foo.db?_busy_timeout=1000"). File operations will use only the filename part, while database connections will use the full string.

Returns a *sql.DB handle or an error.

func SchemasEqual

func SchemasEqual(schema, dbPath string) bool

SchemasEqual compares the provided schema with the existing database schema at dbPath. Returns true if the schemas are equivalent (same tables, columns, triggers, indexes, and views).

Types

type ColumnInfo added in v0.0.3

type ColumnInfo struct {
	Name         string         // Column name
	Type         string         // SQLite data type (TEXT, INTEGER, etc.)
	NotNull      bool           // Whether the column has a NOT NULL constraint
	DefaultValue sql.NullString // Default value for the column (if any)
	PrimaryKey   bool           // Whether the column is part of the primary key
}

ColumnInfo represents detailed information about a database column

func GetColumnInfo added in v0.0.3

func GetColumnInfo(db *sql.DB, tableName string) ([]ColumnInfo, error)

GetColumnInfo returns detailed information about columns in a table. This includes column names, types, constraints, and default values. Returns an error if the table does not exist or if there's a database error.

type SchemaVersion

type SchemaVersion struct {
	Version   int    // Numeric version (optional, for explicit versioning)
	Hash      string // SHA256 hash of the schema
	Timestamp string // When this version was applied
}

SchemaVersion represents the version information for a schema

Directories

Path Synopsis
cmd
autosqlite command

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL