mirror of
https://github.com/shadoll/just-commons.git
synced 2025-12-20 02:25:47 +00:00
Add database management recipes for PostgreSQL and MySQL
This commit is contained in:
56
README.md
56
README.md
@@ -8,6 +8,7 @@ This repository contains reusable Just recipes for:
|
||||
- **Container operations**: start, stop, restart, logs, shell, exec, status
|
||||
- **Registry authentication**: login, logout, status checks for GitHub Container Registry
|
||||
- **Image operations**: build, push, pull, tag, test, info, clean
|
||||
- **Database operations**: PostgreSQL and MySQL database management
|
||||
|
||||
## Usage
|
||||
|
||||
@@ -21,29 +22,56 @@ Import in your `justfile`:
|
||||
|
||||
```bash
|
||||
# Import universal commands
|
||||
import 'just-commons/container.just'
|
||||
import 'just-commons/registry.just'
|
||||
import 'just-commons/images.just'
|
||||
import 'just-commons/core.just' # Core utilities
|
||||
import 'just-commons/container.just' # Container operations
|
||||
import 'just-commons/registry.just' # Registry authentication
|
||||
import 'just-commons/images.just' # Image operations
|
||||
|
||||
# Your project-specific commands
|
||||
import 'just/postgres.just'
|
||||
import 'just/servapp.just'
|
||||
# Import database commands (optional)
|
||||
import 'just-commons/postgres.just' # PostgreSQL operations
|
||||
import 'just-commons/mysql.just' # MySQL operations
|
||||
```
|
||||
|
||||
## Files
|
||||
|
||||
### Core Files
|
||||
- `core.just` - Core utilities (_detect_runtime, _detect_compose, env-check)
|
||||
- `container.just` - Universal container operations (start, stop, logs, shell, exec, status)
|
||||
- `registry.just` - GitHub Container Registry authentication
|
||||
- `images.just` - Universal image build/push/pull operations
|
||||
- `registry.just` - GitHub Container Registry authentication (registry-login, registry-logout, registry-check)
|
||||
- `images.just` - Universal image operations (image-build, image-push, image-pull, image-tag, etc.)
|
||||
|
||||
### Database Files (Optional)
|
||||
- `postgres.just` - PostgreSQL operations (postgres-sql, postgres-check, postgres-list-databases, etc.)
|
||||
- `mysql.just` - MySQL operations (mysql-sql, mysql-check, mysql-list-databases, etc.)
|
||||
|
||||
### Command Structure
|
||||
- **Container commands**: No prefix (start, stop, logs, shell, exec, status, restart)
|
||||
- **All other commands**: Prefixed by file type (image-*, registry-*, postgres-*, mysql-*)
|
||||
|
||||
### Usage Examples
|
||||
|
||||
```bash
|
||||
# Container operations (no prefix)
|
||||
just start postgres
|
||||
just logs postgres
|
||||
just shell postgres
|
||||
|
||||
# Image operations (image- prefix)
|
||||
just image-build postgres
|
||||
just image-push postgres v1.0.0
|
||||
|
||||
# Registry operations (registry- prefix)
|
||||
just registry-login
|
||||
just registry-check
|
||||
|
||||
# Database operations (database- prefix)
|
||||
just postgres-sql "SELECT version();"
|
||||
just postgres-check
|
||||
just mysql-sql "SELECT VERSION();"
|
||||
```
|
||||
|
||||
## Requirements
|
||||
|
||||
- Just command runner
|
||||
- Docker or Podman
|
||||
- Git
|
||||
|
||||
## Projects Using This
|
||||
|
||||
- [servass](https://github.com/shadoll/servass) - Universal container image management
|
||||
- [servass_sh](https://github.com/shadoll/servass_sh) - Production deployment (SH)
|
||||
- [servass_ri](https://github.com/shadoll/servass_ri) - Production deployment (RI)
|
||||
107
container.just
107
container.just
@@ -1,127 +1,158 @@
|
||||
# Universal container management operations
|
||||
|
||||
# Start service (or all services if no service specified)
|
||||
start service="":
|
||||
start service="" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
if [ -n "$service" ]; then
|
||||
echo -e "{{BLUE}}Starting service: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml up -d "$service"
|
||||
$compose_cmd $file_arg up -d "$service"
|
||||
echo -e "{{GREEN}}✓ Service $service started{{NC}}"
|
||||
else
|
||||
echo -e "{{BLUE}}Starting all services...{{NC}}"
|
||||
$compose_cmd -f compose.yml up -d
|
||||
$compose_cmd $file_arg up -d
|
||||
echo -e "{{GREEN}}✓ All services started{{NC}}"
|
||||
fi
|
||||
|
||||
# Stop service (or all services if no service specified)
|
||||
stop service="":
|
||||
stop service="" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
if [ -n "$service" ]; then
|
||||
echo -e "{{BLUE}}Stopping service: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml stop "$service"
|
||||
$compose_cmd $file_arg stop "$service"
|
||||
echo -e "{{GREEN}}✓ Service $service stopped{{NC}}"
|
||||
else
|
||||
echo -e "{{BLUE}}Stopping all services...{{NC}}"
|
||||
$compose_cmd -f compose.yml down
|
||||
$compose_cmd $file_arg down
|
||||
echo -e "{{GREEN}}✓ All services stopped{{NC}}"
|
||||
fi
|
||||
|
||||
# Restart service (or all services if no service specified)
|
||||
restart service="":
|
||||
restart service="" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
if [ -n "$service" ]; then
|
||||
echo -e "{{BLUE}}Restarting service: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml restart "$service"
|
||||
$compose_cmd $file_arg restart "$service"
|
||||
echo -e "{{GREEN}}✓ Service $service restarted{{NC}}"
|
||||
else
|
||||
just stop
|
||||
just start
|
||||
just stop "$service" "$compose_file"
|
||||
just start "$service" "$compose_file"
|
||||
fi
|
||||
|
||||
# Show service status (specific service or all)
|
||||
status service="":
|
||||
status service="" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
if [ -n "$service" ]; then
|
||||
echo -e "{{BLUE}}Status for: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml ps "$service"
|
||||
$compose_cmd $file_arg ps "$service"
|
||||
else
|
||||
echo -e "{{BLUE}}Service Status:{{NC}}"
|
||||
$compose_cmd -f compose.yml ps
|
||||
$compose_cmd $file_arg ps
|
||||
fi
|
||||
|
||||
# View logs for specific service or all services
|
||||
logs service="":
|
||||
logs service="" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
if [ -n "$service" ]; then
|
||||
echo -e "{{BLUE}}Showing logs for: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml logs -f "$service"
|
||||
$compose_cmd $file_arg logs -f "$service"
|
||||
else
|
||||
echo -e "{{BLUE}}Showing logs for all services{{NC}}"
|
||||
$compose_cmd -f compose.yml logs -f
|
||||
$compose_cmd $file_arg logs -f
|
||||
fi
|
||||
|
||||
# Open shell in specific container
|
||||
shell service:
|
||||
shell service compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Opening shell in: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml exec "$service" /bin/bash
|
||||
$compose_cmd $file_arg exec "$service" /bin/bash
|
||||
|
||||
# Execute command in specific service container
|
||||
exec service cmd:
|
||||
exec service cmd compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
cmd="{{cmd}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="-f $compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Executing in $service: $cmd{{NC}}"
|
||||
$compose_cmd -f compose.yml exec "$service" bash -c "$cmd"
|
||||
|
||||
# Pull latest images for specific service or all services
|
||||
pull service="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
compose_cmd=$(just _detect_compose)
|
||||
service="{{service}}"
|
||||
|
||||
if [ -n "$service" ]; then
|
||||
echo -e "{{BLUE}}Pulling image for: $service{{NC}}"
|
||||
$compose_cmd -f compose.yml pull "$service"
|
||||
echo -e "{{GREEN}}✓ Image updated for $service{{NC}}"
|
||||
else
|
||||
echo -e "{{BLUE}}Pulling all images...{{NC}}"
|
||||
$compose_cmd -f compose.yml pull
|
||||
echo -e "{{GREEN}}✓ All images updated{{NC}}"
|
||||
fi
|
||||
$compose_cmd $file_arg exec "$service" bash -c "$cmd"
|
||||
32
images.just
32
images.just
@@ -1,7 +1,7 @@
|
||||
# Universal image operations - works for any project
|
||||
|
||||
# Build any project's container image
|
||||
build project *args="":
|
||||
image-build project *args="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -57,7 +57,7 @@ build project *args="":
|
||||
echo -e "${GREEN}✓ Local tags: $project:$tag, $project:latest${NC}"
|
||||
|
||||
# Push any project's image to registry
|
||||
push project tag=DEFAULT_TAG:
|
||||
image-push project tag=DEFAULT_TAG:
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -131,7 +131,7 @@ push project tag=DEFAULT_TAG:
|
||||
fi
|
||||
|
||||
# Pull any project's image from registry
|
||||
pull project tag=DEFAULT_TAG:
|
||||
image-pull project tag=DEFAULT_TAG:
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -176,7 +176,7 @@ pull project tag=DEFAULT_TAG:
|
||||
echo -e "${GREEN}✓ Tagged locally as: $project:$tag${NC}"
|
||||
|
||||
# Tag any project's existing image
|
||||
tag project new_tag:
|
||||
image-tag project new_tag:
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -206,7 +206,7 @@ tag project new_tag:
|
||||
fi
|
||||
|
||||
# Test any project's image
|
||||
test project tag="latest":
|
||||
image-test project tag="latest":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -226,7 +226,7 @@ test project tag="latest":
|
||||
# Check if image exists
|
||||
if ! $runtime images "$project:$tag" | grep -q "$project"; then
|
||||
echo -e "${RED}Error: Image $project:$tag not found${NC}" >&2
|
||||
echo "Build it first with: just build $project $tag" >&2
|
||||
echo "Build it first with: just image-build $project $tag" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
@@ -254,7 +254,7 @@ test project tag="latest":
|
||||
echo -e "${GREEN}✓ $project image test completed${NC}"
|
||||
|
||||
# Show any project's image information
|
||||
info project tag="latest":
|
||||
image-info project tag="latest":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -279,11 +279,11 @@ info project tag="latest":
|
||||
$runtime history "$project:$tag" | head -10
|
||||
else
|
||||
echo "Image $project:$tag not found locally"
|
||||
echo "Build it with: just build $project $tag"
|
||||
echo "Build it with: just image-build $project $tag"
|
||||
fi
|
||||
|
||||
# List all project images
|
||||
list:
|
||||
images-list:
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -312,7 +312,7 @@ list:
|
||||
fi
|
||||
|
||||
# Remove any project's local images
|
||||
clean project:
|
||||
image-clean project:
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
@@ -357,15 +357,15 @@ clean project:
|
||||
echo -e "${GREEN}✓ Cleaned local images for: $project${NC}"
|
||||
|
||||
# Quick build all known projects
|
||||
build-all:
|
||||
images-build-all:
|
||||
@echo "{{BLUE}}Building all known projects...{{NC}}"
|
||||
@just build postgres
|
||||
@just build servapp
|
||||
@just image-build postgres
|
||||
@just image-build servapp
|
||||
@echo "{{GREEN}}✓ All projects built successfully{{NC}}"
|
||||
|
||||
# Quick push all known projects
|
||||
push-all tag=DEFAULT_TAG:
|
||||
images-push-all tag=DEFAULT_TAG:
|
||||
@echo "{{BLUE}}Pushing all known projects with tag: {{tag}}{{NC}}"
|
||||
@just push postgres {{tag}}
|
||||
@just push servapp {{tag}}
|
||||
@just image-push postgres {{tag}}
|
||||
@just image-push servapp {{tag}}
|
||||
@echo "{{GREEN}}✓ All projects pushed successfully{{NC}}"
|
||||
246
mysql.just
Normal file
246
mysql.just
Normal file
@@ -0,0 +1,246 @@
|
||||
# Universal MySQL database operations
|
||||
|
||||
# Execute MySQL SQL query
|
||||
mysql-sql query service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
query="{{query}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$query" ]; then
|
||||
echo "Error: SQL query is required" >&2
|
||||
echo "Usage: just mysql-sql \"SELECT VERSION();\"" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Executing MySQL query in service: $service{{NC}}"
|
||||
echo -e "{{YELLOW}}Query: $query{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e \"$query\"" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e \"$query\""
|
||||
fi
|
||||
|
||||
# Check MySQL connection and status
|
||||
mysql-check service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Checking MySQL status in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysqladmin -u root -p\${MYSQL_ROOT_PASSWORD} ping" "$file_arg"
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'SELECT VERSION();'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysqladmin -u root -p\${MYSQL_ROOT_PASSWORD} ping"
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'SELECT VERSION();'"
|
||||
fi
|
||||
|
||||
# List MySQL databases
|
||||
mysql-list-databases service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Listing MySQL databases in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'SHOW DATABASES;'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'SHOW DATABASES;'"
|
||||
fi
|
||||
|
||||
# List MySQL users
|
||||
mysql-list-users service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Listing MySQL users in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'SELECT User, Host FROM mysql.user;'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'SELECT User, Host FROM mysql.user;'"
|
||||
fi
|
||||
|
||||
# Create MySQL database
|
||||
mysql-create-database database service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
database="{{database}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$database" ]; then
|
||||
echo "Error: Database name is required" >&2
|
||||
echo "Usage: just mysql-create-database mydb" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Creating MySQL database: $database{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'CREATE DATABASE \`$database\`;'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'CREATE DATABASE \`$database\`;'"
|
||||
fi
|
||||
|
||||
# Drop MySQL database
|
||||
mysql-drop-database database service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
database="{{database}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$database" ]; then
|
||||
echo "Error: Database name is required" >&2
|
||||
echo "Usage: just mysql-drop-database mydb" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}"
|
||||
read -p "Are you sure? (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo -e "{{BLUE}}Dropping MySQL database: $database{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e 'DROP DATABASE \`$database\`;'"
|
||||
fi
|
||||
else
|
||||
echo "Operation cancelled"
|
||||
fi
|
||||
|
||||
# Create MySQL user
|
||||
mysql-create-user username password service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
username="{{username}}"
|
||||
password="{{password}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$username" ] || [ -z "$password" ]; then
|
||||
echo "Error: Username and password are required" >&2
|
||||
echo "Usage: just mysql-create-user myuser mypassword" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Creating MySQL user: $username{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e \"CREATE USER '$username'@'%' IDENTIFIED BY '$password';\"" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e \"CREATE USER '$username'@'%' IDENTIFIED BY '$password';\""
|
||||
fi
|
||||
|
||||
# Grant MySQL privileges
|
||||
mysql-grant-privileges database username service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
database="{{database}}"
|
||||
username="{{username}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$database" ] || [ -z "$username" ]; then
|
||||
echo "Error: Database and username are required" >&2
|
||||
echo "Usage: just mysql-grant-privileges mydb myuser" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Granting privileges on $database to $username{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e \"GRANT ALL PRIVILEGES ON \\\`$database\\\`.* TO '$username'@'%'; FLUSH PRIVILEGES;\"" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD} -e \"GRANT ALL PRIVILEGES ON \\\`$database\\\`.* TO '$username'@'%'; FLUSH PRIVILEGES;\""
|
||||
fi
|
||||
|
||||
# MySQL interactive shell
|
||||
mysql-shell service="mysql" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Opening MySQL interactive shell in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD}" "$file_arg"
|
||||
else
|
||||
just exec "$service" "mysql -u root -p\${MYSQL_ROOT_PASSWORD}"
|
||||
fi
|
||||
186
postgres.just
Normal file
186
postgres.just
Normal file
@@ -0,0 +1,186 @@
|
||||
# Universal PostgreSQL database operations
|
||||
|
||||
# Execute PostgreSQL SQL query
|
||||
postgres-sql query service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
query="{{query}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$query" ]; then
|
||||
echo "Error: SQL query is required" >&2
|
||||
echo "Usage: just postgres-sql \"SELECT version();\"" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Executing PostgreSQL query in service: $service{{NC}}"
|
||||
echo -e "{{YELLOW}}Query: $query{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "psql -U postgres -c \"$query\"" "$file_arg"
|
||||
else
|
||||
just exec "$service" "psql -U postgres -c \"$query\""
|
||||
fi
|
||||
|
||||
# Check PostgreSQL connection and status
|
||||
postgres-check service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Checking PostgreSQL status in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "pg_isready -U postgres" "$file_arg"
|
||||
just exec "$service" "psql -U postgres -c 'SELECT version();'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "pg_isready -U postgres"
|
||||
just exec "$service" "psql -U postgres -c 'SELECT version();'"
|
||||
fi
|
||||
|
||||
# List PostgreSQL databases
|
||||
postgres-list-databases service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Listing PostgreSQL databases in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "psql -U postgres -c '\\l'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "psql -U postgres -c '\\l'"
|
||||
fi
|
||||
|
||||
# List PostgreSQL users
|
||||
postgres-list-users service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Listing PostgreSQL users in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "psql -U postgres -c '\\du'" "$file_arg"
|
||||
else
|
||||
just exec "$service" "psql -U postgres -c '\\du'"
|
||||
fi
|
||||
|
||||
# Create PostgreSQL database
|
||||
postgres-create-database database service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
database="{{database}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$database" ]; then
|
||||
echo "Error: Database name is required" >&2
|
||||
echo "Usage: just postgres-create-database mydb" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Creating PostgreSQL database: $database{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "createdb -U postgres \"$database\"" "$file_arg"
|
||||
else
|
||||
just exec "$service" "createdb -U postgres \"$database\""
|
||||
fi
|
||||
|
||||
# Drop PostgreSQL database
|
||||
postgres-drop-database database service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
database="{{database}}"
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
if [ -z "$database" ]; then
|
||||
echo "Error: Database name is required" >&2
|
||||
echo "Usage: just postgres-drop-database mydb" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{YELLOW}}WARNING: This will permanently delete database: $database{{NC}}"
|
||||
read -p "Are you sure? (y/N): " -n 1 -r
|
||||
echo
|
||||
if [[ $REPLY =~ ^[Yy]$ ]]; then
|
||||
echo -e "{{BLUE}}Dropping PostgreSQL database: $database{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "dropdb -U postgres \"$database\"" "$file_arg"
|
||||
else
|
||||
just exec "$service" "dropdb -U postgres \"$database\""
|
||||
fi
|
||||
else
|
||||
echo "Operation cancelled"
|
||||
fi
|
||||
|
||||
# PostgreSQL interactive shell
|
||||
postgres-shell service="postgres" compose-file="":
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
|
||||
service="{{service}}"
|
||||
compose_file="{{compose-file}}"
|
||||
|
||||
# Build compose file argument
|
||||
file_arg=""
|
||||
if [ -n "$compose_file" ]; then
|
||||
file_arg="$compose_file"
|
||||
fi
|
||||
|
||||
echo -e "{{BLUE}}Opening PostgreSQL interactive shell in service: $service{{NC}}"
|
||||
|
||||
if [ -n "$file_arg" ]; then
|
||||
just exec "$service" "psql -U postgres" "$file_arg"
|
||||
else
|
||||
just exec "$service" "psql -U postgres"
|
||||
fi
|
||||
Reference in New Issue
Block a user