Skip to content

Instantly share code, notes, and snippets.

@djeikyb
Last active June 21, 2022 19:42
Show Gist options
  • Save djeikyb/6c9ab5debcad396335b9169984dfb704 to your computer and use it in GitHub Desktop.
Save djeikyb/6c9ab5debcad396335b9169984dfb704 to your computer and use it in GitHub Desktop.
Wrapper around dotnet-ef cli
#!/bin/sh
SELF=${0##*/}
die() {
log "$@"
exit 1
}
log() {
printf "$SELF %s: %s\n" "$(date +%T)" "$@" >&2
}
usage() {
cat <<EOF
usage: $SELF <command> [<args>]
Commands:
create a migration
<name> The migration name
discard uncommitted migration
oops
Examples:
$SELF InitialCreate
$SELF oops
EOF
}
requireCommand() {
command -v $1 >/dev/null 2>&1 || die "'$1' is required, but not found"
}
dieIfNonZeroStatus() {
estatus=$1 # some shells reserve $status
if [ $estatus -ne 0 ]; then
log "$*"
exit $estatus
fi
}
launchenv() {
launchSettings="$(find . \( -path '*/bin/*' -o -path '*/obj/*' \) -prune -o -iname 'launchSettings.json' -print | head -1 2>/dev/null)"
log "launchSettings.json: ${launchSettings:?not found}"
properties=$(cat "$launchSettings" | egrep -v '^ *//' | jq --raw-output \
'.profiles."Local Development".environmentVariables
| to_entries[]
| "\"\(.key)\"=\"\(.value)\""')
env -S "$properties" "$@"
# s/:/__/g but just for the key names
# | .key |= gsub(":";"__")
}
add() {
migdir="$(find . -name 'Migrations' -type d 2>/dev/null | head -1)"
log "migration dir: ${migdir:?not found, see comments about initial migration}"
projdir="$(dirname $migdir)"
log "project dir: ${projdir:?not found}"
launchenv dotnet ef migrations add --project "$projdir" "$*"
git add "$projdir"/Migrations
### flip to this version for first migration
# launchenv dotnet ef migrations add "$*"
# git add Migrations
}
remove() {
migdir="$(find . -name 'Migrations' -type d 2>/dev/null | head -1)"
log "migration dir: ${migdir:?not found, see comments about initial migration}"
projdir="$(dirname $migdir)"
log "project dir: ${projdir:?not found}"
set -x
lastMigration=$(launchenv dotnet ef migrations --project "$projdir" list | tail -2 | head -1)
dieIfNonZeroStatus $?
launchenv dotnet ef database --project "$projdir" update "$lastMigration"
dieIfNonZeroStatus $?
launchenv dotnet ef migrations --project "$projdir" remove
dieIfNonZeroStatus $?
set +x
}
oops() {
migdir="$(find . -name 'Migrations' -type d 2>/dev/null | head -1)"
log "migration dir: ${migdir:?not found}"
git reset HEAD "$migdir"
rm -r "$migdir"
git checkout -- "$migdir"
}
migrate() {
launchSettings=$(find . \( -path '*/bin/*' -o -path '*/obj/*' \) -prune -o -iname 'launchSettings.json' -print | head -1 2>/dev/null)
projdir="$(dirname $(dirname $launchSettings))"
echo "$projdir"
log "project dir: ${projdir:?not found, don\'t know how to launch}"
launchenv dotnet run --project "$projdir" -- migrate-database "$*"
}
tool() {
migdir="$(find . -name 'Migrations' -type d 2>/dev/null | head -1)"
log "migration dir: ${migdir:?not found, see comments about initial migration}"
projdir="$(dirname $migdir)"
log "project dir: ${projdir:?not found}"
# set -x
launchenv dotnet ef --project "$projdir" "$@"
dieIfNonZeroStatus $?
# set +x
}
while [ $# -gt 0 ]; do
case "$1" in
"")
usage
die "unrecognized argument: $1"
exit 1
;;
--help|help|-help)
usage
exit 0
;;
reset|oops)
oops
exit 0
;;
remove)
remove
exit 0
;;
migrate|mig)
migrate
exit 0
;;
env)
launchenv "$@"
exit 0
;;
tool)
shift
tool "$@"
exit 0
;;
*)
add "$*"
exit 0
;;
esac
done
@djeikyb
Copy link
Author

djeikyb commented Feb 4, 2020

A couple useful things..

  • My context factory requires some environment variables. They're already stored in launchSettings.json, but dotnet-ef doesn't read this file (yet). This wrapper makes it happen. I get to define local env vars in one place!

  • Sometimes I want to experiment. What sort of migration happens if I tweak a bit of code? Maybe I want to keep it, maybe I'm just playing. I want to quickly generate, inspect, and discard migrations. The oops command helps discard, and my custom add command stages the migration files so it's easy to inspect whether I'm in a terminal, IDE, or git tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment