Skip to content

Instantly share code, notes, and snippets.

@derekforeman
Created January 18, 2021 21:51
Show Gist options
  • Save derekforeman/8942df0166e0305915e75e021ca6abb3 to your computer and use it in GitHub Desktop.
Save derekforeman/8942df0166e0305915e75e021ca6abb3 to your computer and use it in GitHub Desktop.
Add migrations for two providers at one time.
#!/usr/bin/env bash
set -o errexit # Exit on most errors (see the manual)
set -o errtrace # Make sure any error trap is inherited
set -o nounset # Disallow expansion of unset variables
set -Eeuo pipefail # Use last non-zero exit code in a pipelin
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
#DESC: Usage Help
#Args: None
#OUTS: None
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] -m param_value arg1 [arg2...]
Script description here.
Available options:
-h, --help Print this help and exit
-v, --verbose Print script debug info
-m, --msg Migration Description
EOF
exit
}
cleanup() {
trap - SIGINT SIGTERM ERR EXIT
# script cleanup here
}
setup_colors() {
if [[ -t 2 ]] && [[ -z "${NO_COLOR-}" ]] && [[ "${TERM-}" != "dumb" ]]; then
NOFORMAT='\033[0m' RED='\033[0;31m' GREEN='\033[0;32m' ORANGE='\033[0;33m' BLUE='\033[0;34m' PURPLE='\033[0;35m' CYAN='\033[0;36m' YELLOW='\033[1;33m'
else
NOFORMAT='' RED='' GREEN='' ORANGE='' BLUE='' PURPLE='' CYAN='' YELLOW=''
fi
}
# DESC: Generic script initialisation
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: $orig_cwd: The current working directory when the script was run
# $script_path: The full path to the script
# $script_dir: The directory path of the script
# $script_name: The file name of the script
# $script_params: The original parameters provided to the script
# $ta_none: The ANSI control code to reset all text attributes
# NOTE: $script_path only contains the path that was used to call the script
# and will not resolve any symlinks which may be present in the path.
# You can use a tool like realpath to obtain the "true" path. The same
# caveat applies to both the $script_dir and $script_name variables.
# shellcheck disable=SC2034
function script_init() {
# Useful paths
readonly orig_cwd="$PWD"
readonly script_path="${BASH_SOURCE[1]}"
readonly script_dir="$(dirname "$script_path")"
readonly script_name="$(basename "$script_path")"
readonly script_params="$*"
}
# DESC: Ouput to commandline
# ARGS: $1 (required): Message to display
# OUTS: None
msg() {
echo >&2 -e "${1-}"
}
# DESC: Ouput to commandline
# ARGS: $1 (required): Message to display
# : $2 (optional): Defaults to 1 for general errors, 2 should be script errors.
# OUTS: None
die() {
local msg=$1
local code=${2-1} # default exit status 1
msg "$msg"
exit "$code"
}
# DESC: Parameter parser
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: Variables indicating command-line parameters and options
parse_params() {
# default values of variables set from params
flag=0
msg=''
while :; do
case "${1-}" in
-h | --help)
usage
;;
-v | --verbose)
set -x
;;
--no-color)
NO_COLOR=1
;;
-f | --flag)
flag=1
;; # example flag
-m | --msg) # named parameter
msg="${2-}"
shift
;;
-?*) die "Unknown option: $1" ;;
*) break ;;
esac
shift
done
args=("$@")
# check required params and arguments
[[ -z "${msg-}" ]] && die "Missing required parameter: param"
#[[ ${#args[@]} -eq 0 ]] && die "Missing script arguments"
return 0
}
# DESC: Main control flow
# ARGS: $@ (optional): Arguments provided to the script
# OUTS: None
function main() {
cleanup
parse_params "$@"
setup_colors
#https://docs.microsoft.com/en-us/ef/core/managing-schemas/migrations/providers?tabs=dotnet-core-cli
dotnet ef migrations add "${msg}" --project SqlMigrations/SqlMigrations.csproj --context AppDbContext --startup-project Api/Api.csproj -- --provider SqlServer
dotnet ef migrations add "${msg}" --project SqliteMigrations/SqliteMigrations.csproj --context AppDbContext --startup-project Api/Api.csproj -- --provider Sqlite
msg "${RED}Read parameters:${NOFORMAT}"
#msg "- flag: ${flag}"
msg "- msg: ${msg}"
#msg "- arguments: ${args[*]-}"
}
#Drop the mic
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment