#!/usr/bin/env bash # Creates a new numbered migration for the system or company schema (SPEC §5.3). # scripts/new-migration.sh [cartella] [modulo] # Example: scripts/new-migration.sh system create_sys_users # The version is today's date plus the next free number of the day across the repository (yyyymmdd_nnn); the # class is M__ in the given folder (default: the system or company migrations folder). set -euo pipefail root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" cd "$root" usage() { echo "Uso: scripts/new-migration.sh [cartella] [modulo]" >&2 exit 2 } [ $# -ge 2 ] || usage target="$1" name="$2" module="${4:-core}" case "$target" in system) dir="${3:-src/Khaemhat.Data/SystemDatabase/Migrations}"; attribute_target="MigrationTarget.System" ;; company) dir="${3:-src/Khaemhat.Application/Migrations}"; attribute_target="MigrationTarget.Company" ;; *) usage ;; esac if ! [[ "$name" =~ ^[a-z][a-z0-9_]{0,62}$ ]]; then echo "Il nome deve essere snake_case (lettere minuscole, cifre, _): $name" >&2 exit 2 fi if ! [[ "$module" =~ ^[a-z][a-z0-9_]{0,39}$ ]]; then echo "Il modulo deve essere snake_case: $module" >&2 exit 2 fi if [[ "$dir" != src/* ]]; then echo "La cartella deve stare sotto src/: $dir" >&2 exit 2 fi day="$(date +%Y%m%d)" last="$(grep -rhoE "\[Migration\(${day}_[0-9]{3}" src --include='*.cs' 2>/dev/null | sed -E 's/.*_([0-9]{3})$/\1/' | sort -n | tail -1 || true)" next=$(printf '%03d' $((10#${last:-0} + 1))) pascal="$(echo "$name" | awk -F_ '{ for (i = 1; i <= NF; i++) printf "%s%s", toupper(substr($i, 1, 1)), substr($i, 2) }')" class="M${day}_${next}_${pascal}" namespace="$(echo "${dir#src/}" | tr '/' '.')" file="$dir/$class.cs" mkdir -p "$dir" if [ -e "$file" ]; then echo "Esiste già: $file" >&2 exit 1 fi if [ "$attribute_target" = "MigrationTarget.Company" ]; then target_argument="" else target_argument=", Target = $attribute_target" fi if [ "$module" = "core" ]; then module_argument="" else module_argument=", Module = \"$module\"" fi cat > "$file" <TODO: what this migration changes and why (SPEC section). [Migration(${day}_${next}, "$name"$target_argument$module_argument)] public sealed class $class : IMigration { /// public void Up(ISchemaBuilder schema) { ArgumentNullException.ThrowIfNull(schema); // schema.CreateTable("table_name", t => { t.Id(); t.AuditColumns(); }); } } CS echo "Creata $file (versione ${day}_${next}, modulo $module)." echo "Dopo averla scritta: aggiornare la mappa dell'entità, eseguire scripts/gen-datamodel.sh e i test."