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 ¶
- func FindCommonColumns(oldColumns, newColumns []ColumnInfo) []string
- func GetColumns(db *sql.DB, tableName string) ([]string, error)
- func GetTables(db *sql.DB) ([]string, error)
- func Migrate(schema, dbPath string) (*sql.DB, error)
- func MigrateTable(oldDB, newDB *sql.DB, tableName string) error
- func MigrateToNewFile(schema, oldDbPath string, newDbPath string) (*sql.DB, error)
- func Open(schema, dbPath string) (*sql.DB, error)
- func SchemasEqual(schema, dbPath string) bool
- type ColumnInfo
- type SchemaVersion
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 ¶
GetColumns returns a list of column names for a table.
func GetTables ¶
GetTables returns a list of user table names in the database (ignores _autosqlite_version).
func Migrate ¶
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 ¶
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 ¶
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 ¶
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 ¶
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