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.
35 lines
1.1 KiB
Bash
35 lines
1.1 KiB
Bash
#!/bin/bash
|
|
set -e
|
|
|
|
MIGRATION_NAME=""
|
|
MIGRATION_PATH="database/migrations"
|
|
|
|
# Parse command-line arguments
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case $1 in
|
|
--path) MIGRATION_PATH="$2"; shift ;;
|
|
*) MIGRATION_NAME="$1" ;;
|
|
esac
|
|
shift
|
|
done
|
|
|
|
if [ -z "$MIGRATION_NAME" ]; then
|
|
echo "Usage: /core:migrate create <migration_name> [--path <path>]" >&2
|
|
exit 1
|
|
fi
|
|
|
|
# Let artisan create the file in the specified path
|
|
core php artisan make:migration "$MIGRATION_NAME" --path="$MIGRATION_PATH" > /dev/null
|
|
|
|
# Find the newest file in the target directory that matches the name.
|
|
FILE_PATH=$(find "$MIGRATION_PATH" -name "*_$MIGRATION_NAME.php" -print -quit)
|
|
|
|
if [ -f "$FILE_PATH" ]; then
|
|
# Add the workspace_id column and a placeholder for model generation
|
|
awk '1; /->id\(\);/ { print " \$table->foreignId(\"workspace_id\")->constrained();\n // --- AUTO-GENERATED COLUMNS GO HERE ---" }' "$FILE_PATH" > "$FILE_PATH.tmp" && mv "$FILE_PATH.tmp" "$FILE_PATH"
|
|
# Output just the path for other scripts
|
|
echo "$FILE_PATH"
|
|
else
|
|
echo "ERROR: Could not find created migration file for '$MIGRATION_NAME' in '$MIGRATION_PATH'." >&2
|
|
exit 1
|
|
fi
|