Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/frioux/dh
- Owner: frioux
- License: apache-2.0
- Created: 2022-06-10T16:11:31.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2022-06-15T05:36:46.000Z (over 2 years ago)
- Last Synced: 2024-10-22T12:03:19.920Z (2 months ago)
- Language: Go
- Size: 18.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
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"
);
EOFcat < dh/001/shortlinks.sql
CREATE TABLE shortlinks (
"from",
"to",
"deleted"
PRIMARY KEY ("from")
);
EOFcat < 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/002cat <dh/002/shortlinks.sql
ALTER TABLE shortlinks ADD COLUMN "description" NOT NULL DEFAULT ''
SQLcat < 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.