Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/notcoffee418/dbmigrator
Database Migration system for Go.
https://github.com/notcoffee418/dbmigrator
database database-management db go golang migration-tool mssql mysql pgsql sql sqlite
Last synced: 7 days ago
JSON representation
Database Migration system for Go.
- Host: GitHub
- URL: https://github.com/notcoffee418/dbmigrator
- Owner: NotCoffee418
- License: mit
- Created: 2023-10-15T12:36:43.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-06T08:56:13.000Z (11 days ago)
- Last Synced: 2024-11-06T09:36:42.728Z (11 days ago)
- Topics: database, database-management, db, go, golang, migration-tool, mssql, mysql, pgsql, sql, sqlite
- Language: Go
- Homepage:
- Size: 25.4 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Go Database Migrator
Simple database migration system for Go.
## Usage
### Expected migration file structure
- Must contain a `-- +up` comment to indicate the SQL below should run when applying a migration.
- May contain a `-- +down` comment to indicate the SQL below should run when reverting a migration.
- Comments behind `-- +up` and `-- +down` are allowed.```sql
-- +up <- SQL below runs when applying a migration
CREATE TABLE demo_guestbook (
id serial PRIMARY KEY,
name varchar(255) NOT NULL,
message text NOT NULL,
created_at timestamp NOT NULL DEFAULT NOW()
);-- +down <- SQL below runs when reverting a migration
DROP TABLE demo_guestbook;
```### Expected project structure
Your migration files must be named in the format `0001_initial_migration.sql` where `0001` is the migration number and `initial_migration` is the name of the migration.
```md
|-- main.go
|-- migrations
| |-- 0001_initial_migration.sql
| |-- 0002_second_migration.sql
```### Apply and Revert Migrations
```go
import (
"github.com/NotCoffee418/dbmigrator"
)//go:embed all:migrations
var migrationFS embed.FS // `embed.FS` recommended but any `fs.FS` will workfunc main() {
// Define the query set for your database. Defaults to MySQL.
// Must be called before any other dbmigrator functions.
dbmigrator.SetDatabaseType(dbmigrator.PostgreSQL)
// Or
//dbmigrator.SetDatabaseType(dbmigrator.MySQL)
//dbmigrator.SetDatabaseType(dbmigrator.SQLite)
//dbmigrator.SetDatabaseType(dbmigrator.SQLServer)
// Or define your own based on dbmigrator.MigrationQueryDefinition// Directory to migrations inside the FS
migrationsDir := "migrations"// Migrations CLI (optional)
if len(os.Args > 1) {
// help - Display this help message.
// migrate up - Apply all new database migrations.
// migrate down - Rollback a single database migration.`
dbmigrator.HandleMigratorCommand(
db *sql.DB,
migrationFS embed.FS,
migrationsDir string, // Path to migrations dir in your fs
os.Args[1:] ...string)// Manage migrations programatically
} else {
// Apply all new migrations
doneUp := <-dbmigrator.MigrateUpCh(
db *sql.DB,
migrations embed.FS,
migrationsDir string)// Revert Single Migration
doneDown := <-dbmigrator.MigrateDownCh(
db *sql.DB,
migrations embed.FS,
migrationsDir string)
}
}
```