Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/frioux/dh

Go based database migration tool: deployment handler
https://github.com/frioux/dh

Last synced: 19 days ago
JSON representation

Go based database migration tool: deployment handler

Awesome Lists containing this project

README

        

# dh - A simple Database Deployment Handler in Go

`dh` is a reaction to
[DBIx::Class::DeploymentHandler](https://metacpan.org/pod/DBIx::Class::DeploymentHandler),
a database migration system I built in in 2010. While the original DBICDH was built on
top of an existing ORM and schema transformation tooling, this version is intentionally
simpler, and allows users to drop in ORM support if needed.

## Getting Started

The primary users of this program are expected to be Go programmers, using `dh`
as a library. First, create your deployment directory:

```
mkdir -p dh/001
cat < dh/001/dh.sql
CREATE TABLE users (
"id",
"name",
"email"
);
EOF

cat < dh/001/shortlinks.sql
CREATE TABLE shortlinks (
"from",
"to",
"deleted"
PRIMARY KEY ("from")
);
EOF

cat < dh/001/history.sql
CREATE TABLE IF NOT EXISTS history (
"from",
"to",
"when",
"who"
);
EOF
```

Add that directory to your migration plan:

```
echo 000-sqlite > dh/plan.txt
echo 001 >> dh/plan.txt
```

Now let's create a migration to add another column:

```
mkdir dh/002

cat <dh/002/shortlinks.sql
ALTER TABLE shortlinks ADD COLUMN "description" NOT NULL DEFAULT ''
SQL

cat < dh/002/history.sql
ALTER TABLE history ADD COLUMN "description" NOT NULL DEFAULT ''
SQL
```

And add it to your plan:

```
echo 002 >> dh/plan.txt
```

And here's how you'd use it:

```golang
dbh := sql.Connect(...)
m := dh.NewMigrator()
if err := m.MigrateOne(dbh, dh.DHMigrations, "000-sqlite"); err != nil {
panic(err)
}
if err := m.MigrateAll(dbh, os.DirFS("dh")); err != nil {
return err
}
```

---

Out of the box `dh` can apply SQL or lists of SQL from JSON files, but the
migration interface is extensible so you could wire up
[gopher-lua](https://github.com/yuin/gopher-lua) to do more advanced
migrations.