2014-02-19 04:15:51 +03:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
|
|
|
|
REV=$(date -u +%Y%m%d%H%M%S)
|
|
|
|
filename=$1
|
|
|
|
|
|
|
|
TAB="$(printf '\t')"
|
|
|
|
|
|
|
|
titleize() {
|
|
|
|
echo "$1" | sed -r -e "s/-|_/ /g" -e 's/\b(.)/\U\1/g' -e 's/ //g'
|
|
|
|
}
|
|
|
|
|
2014-03-12 04:33:04 +03:00
|
|
|
howto() {
|
|
|
|
echo "Usage:"
|
|
|
|
echo " ./migration create_sample_table"
|
|
|
|
echo ""
|
|
|
|
echo "Above invocation will create a migration script called:"
|
|
|
|
echo " ${REV}_create_sample_table.go"
|
|
|
|
echo "You can add your migration step at the Up and Down function"
|
|
|
|
echo "definition inside the file."
|
|
|
|
echo ""
|
|
|
|
echo "Database transaction available through MigrationDriver,"
|
|
|
|
echo "so you can access mg.Tx (sql.Tx instance) directly,"
|
|
|
|
echo "there are also some migration helpers available, see api.go"
|
|
|
|
echo "for the list of available helpers (Operation interface)."
|
|
|
|
echo ""
|
|
|
|
}
|
|
|
|
|
|
|
|
[[ $# -eq 0 ]] && howto && exit 0
|
|
|
|
|
2014-02-19 04:15:51 +03:00
|
|
|
cat > ${REV}_$filename.go << EOF
|
|
|
|
package migrate
|
|
|
|
|
|
|
|
type rev${REV} struct{}
|
|
|
|
|
|
|
|
var $(titleize $filename) = &rev${REV}{}
|
|
|
|
|
|
|
|
func (r *rev$REV) Revision() int64 {
|
|
|
|
${TAB}return $REV
|
|
|
|
}
|
|
|
|
|
2014-03-12 04:33:04 +03:00
|
|
|
func (r *rev$REV) Up(mg *MigrationDriver) error {
|
2014-02-19 04:15:51 +03:00
|
|
|
${TAB}// Migration steps here
|
|
|
|
}
|
|
|
|
|
2014-03-12 04:33:04 +03:00
|
|
|
func (r *rev$REV) Down(mg *MigrationDriver) error {
|
2014-02-19 04:15:51 +03:00
|
|
|
${TAB}// Revert migration steps here
|
|
|
|
}
|
|
|
|
EOF
|