Implements a new `/core:migrate` command to provide a set of helpers for working with Laravel migrations in a monorepo environment. The new command includes the following subcommands: - `/core:migrate create <name>`: Creates a new migration file. - `/core:migrate run`: Runs all pending migrations. - `/core:migrate rollback`: Rolls back the last migration. - `/core:migrate fresh`: Drops all tables and re-runs all migrations. - `/core:migrate status`: Shows the status of all migrations. - `/core:migrate from-model <ModelName>`: Generates a new migration by analyzing an existing Laravel model. Key Features: - **Smart Migration Generation**: The `from-model` command uses a robust PHP script with Reflection to accurately parse model properties and relationships, generating a complete schema definition. - **Multi-Tenant Awareness**: New migrations automatically include a `workspace_id` foreign key to support multi-tenant architectures. - **Module Support**: The `create` and `from-model` commands accept `--path` and `--model-path` arguments, allowing them to be used with different modules in a monorepo. - **Automatic Indexing**: The `from-model` command automatically adds database indexes to foreign key columns.
57 lines
1.8 KiB
Bash
57 lines
1.8 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
MODEL_NAME=""
|
|
MODEL_PATH_PREFIX="app/Models"
|
|
MIGRATION_PATH="database/migrations"
|
|
|
|
# Parse command-line arguments
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case $1 in
|
|
--model-path) MODEL_PATH_PREFIX="$2"; shift ;;
|
|
--path) MIGRATION_PATH="$2"; shift ;;
|
|
*) MODEL_NAME="$1" ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [ -z "$MODEL_NAME" ]; then
|
|
echo "Usage: /core:migrate from-model <ModelName> [--model-path <path>] [--path <path>]"
|
|
exit 1
|
|
fi
|
|
|
|
MODEL_PATH="${MODEL_PATH_PREFIX}/${MODEL_NAME}.php"
|
|
TABLE_NAME=$(echo "$MODEL_NAME" | sed 's/\([A-Z]\)/_\L\1/g' | cut -c 2- | sed 's/$/s/')
|
|
MIGRATION_NAME="create_${TABLE_NAME}_table"
|
|
|
|
if [ ! -f "$MODEL_PATH" ]; then
|
|
echo "Model not found at: $MODEL_PATH"
|
|
exit 1
|
|
fi
|
|
|
|
# Generate the migration file
|
|
MIGRATION_FILE=$("${CLAUDE_PLUGIN_ROOT}/scripts/create.sh" "$MIGRATION_NAME" --path "$MIGRATION_PATH")
|
|
|
|
if [ ! -f "$MIGRATION_FILE" ]; then
|
|
echo "Failed to create migration file."
|
|
exit 1
|
|
fi
|
|
|
|
# Parse the model using the PHP script
|
|
SCHEMA_JSON=$(core php "${CLAUDE_PLUGIN_ROOT}/scripts/parse-model.php" "$MODEL_PATH")
|
|
|
|
if echo "$SCHEMA_JSON" | jq -e '.error' > /dev/null; then
|
|
echo "Error parsing model: $(echo "$SCHEMA_JSON" | jq -r '.error')"
|
|
exit 1
|
|
fi
|
|
|
|
# Generate schema definitions from the JSON output
|
|
SCHEMA=$(echo "$SCHEMA_JSON" | jq -r '.columns[] |
|
|
" $table->" + .type + "(\"" + .name + "\")" +
|
|
(if .type == "foreignId" then "->constrained()->onDelete(\"cascade\")" else "" end) + ";" +
|
|
(if .index then "\n $table->index(\"" + .name + "\");" else "" end)')
|
|
|
|
# Insert the generated schema into the migration file
|
|
awk -v schema="$SCHEMA" '{ sub("// --- AUTO-GENERATED COLUMNS GO HERE ---", schema); print }' "$MIGRATION_FILE" > "$MIGRATION_FILE.tmp" && mv "$MIGRATION_FILE.tmp" "$MIGRATION_FILE"
|
|
|
|
echo "Generated migration for $MODEL_NAME in $MIGRATION_FILE"
|