138 lines
No EOL
2.9 KiB
Bash
138 lines
No EOL
2.9 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
# ============================================
|
|
# Yii2 — Commands
|
|
# ============================================
|
|
#
|
|
# All functions run inside the app container
|
|
# via runtime::exec, so they work regardless
|
|
# of whether the stack is Docker or native.
|
|
|
|
# ============================================
|
|
# Helpers
|
|
# ============================================
|
|
|
|
# Generates a snake_case migration class name with timestamp.
|
|
# e.g. m260430_151441_create_invoice_table
|
|
function yii2::migrate::_class_name() {
|
|
local name="$1"
|
|
local timestamp
|
|
timestamp="$(date '+%y%m%d_%H%M%S')"
|
|
echo "m${timestamp}_${name}"
|
|
}
|
|
|
|
# Resolves the migrations directory on the host.
|
|
function yii2::migrate::_dir() {
|
|
echo "$(ctx::root)/console/migrations"
|
|
}
|
|
|
|
# ============================================
|
|
# Yii
|
|
# ============================================
|
|
|
|
# Runs any yii console command inside the container.
|
|
#
|
|
# Usage:
|
|
# dx yii <command> [args...]
|
|
# yii <command> [args...] (inside dx workspace)
|
|
#
|
|
# Examples:
|
|
# dx yii migrate
|
|
# dx yii rbac/init
|
|
# yii cache/flush-all
|
|
function yii2::exec() {
|
|
runtime::exec php yii "$@"
|
|
}
|
|
|
|
# ============================================
|
|
# Migrations
|
|
# ============================================
|
|
|
|
# Creates a new migration file with the correct namespace,
|
|
# class name, and stub — bypassing Yii's generator entirely.
|
|
#
|
|
# Usage:
|
|
# dx migrate-create <name>
|
|
# e.g. dx migrate-create create_invoice_table
|
|
function yii2::migrate::create() {
|
|
local name="${1:?Usage: dx migrate-create <migration_name>}"
|
|
local namespace="console\\migrations"
|
|
local class
|
|
class="$(yii2::migrate::_class_name "$name")"
|
|
local dir
|
|
dir="$(yii2::migrate::_dir)"
|
|
local file="${dir}/${class}.php"
|
|
|
|
local title
|
|
title="$(echo "$name" | sed 's/_/ /g' | awk '{for(i=1;i<=NF;i++) $i=toupper(substr($i,1,1)) substr($i,2)}1')"
|
|
|
|
mkdir -p "$dir"
|
|
|
|
cat > "$file" <<PHP
|
|
<?php
|
|
|
|
namespace ${namespace};
|
|
|
|
use console\models\Migration;
|
|
|
|
/**
|
|
* ${title}
|
|
*/
|
|
class ${class} extends Migration
|
|
{
|
|
public \$tableName = '{{%TODO}}';
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function safeUp()
|
|
{
|
|
|
|
}
|
|
|
|
/**
|
|
* {@inheritdoc}
|
|
*/
|
|
public function safeDown()
|
|
{
|
|
\$this->dropTable(\$this->tableName);
|
|
}
|
|
}
|
|
PHP
|
|
|
|
log::success "Created: ${file}"
|
|
}
|
|
|
|
# Runs all pending migrations.
|
|
#
|
|
# Usage:
|
|
# dx migrate
|
|
function yii2::migrate::run() {
|
|
yii2::exec migrate "$@"
|
|
}
|
|
|
|
# Reverts the last N migrations (default: 1).
|
|
#
|
|
# Usage:
|
|
# dx migrate-down
|
|
# dx migrate-down 3
|
|
function yii2::migrate::down() {
|
|
local n="${1:-1}"
|
|
yii2::exec migrate/down "$n" "${@:2}"
|
|
}
|
|
|
|
# Shows new (pending) migrations.
|
|
#
|
|
# Usage:
|
|
# dx migrate-new
|
|
function yii2::migrate::new() {
|
|
yii2::exec migrate/new "$@"
|
|
}
|
|
|
|
# Shows migration history.
|
|
#
|
|
# Usage:
|
|
# dx migrate-history
|
|
function yii2::migrate::history() {
|
|
yii2::exec migrate/history "$@"
|
|
} |