woodpecker/pkg/database/migrate/migration

49 lines
1.1 KiB
Text
Raw Normal View History

#!/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'
}
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
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
}
func (r *rev$REV) Up(mg *MigrationDriver) error {
${TAB}// Migration steps here
}
func (r *rev$REV) Down(mg *MigrationDriver) error {
${TAB}// Revert migration steps here
}
EOF