Skip to content
This repository was archived by the owner on Jul 20, 2026. It is now read-only.

Commit ff871db

Browse files
Merge pull request #25 from TheRefraction/config-migration
Migration Configuration and Scripts
2 parents 129517f + 4c6591e commit ff871db

17 files changed

Lines changed: 374 additions & 12 deletions

File tree

.env.example

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
# MySQL .env variables
2+
# Credentials to establish a connection to a given DB
13
MYSQL_HOST=mysql
4+
MYSQL_PORT=3306
25
MYSQL_DATABASE=efes_db
36
MYSQL_USER=
47
MYSQL_PASSWORD=
5-
MYSQL_ROOT_PASSWORD=
8+
MYSQL_ROOT_PASSWORD=
9+
10+
# Docker ports
11+
DOCKER_PORT_MYSQL=3306
12+
DOCKER_PORT_PHP=8080
13+
DOCKER_PORT_PMA=8081

db/init/01_schema.sql

Lines changed: 0 additions & 2 deletions
This file was deleted.

db/init/02_triggers.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

db/init/03_seed.sql

Lines changed: 0 additions & 1 deletion
This file was deleted.

docker-compose.yml

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,55 @@ services:
55
env_file:
66
- .env
77
ports:
8-
- "3306:3306"
8+
- "${DOCKER_PORT_MYSQL:-3306}:3306"
99
volumes:
1010
- mysql_data:/var/lib/mysql
11-
- ./db/init:/docker-entrypoint-initdb.d
11+
#- ./db/init:/docker-entrypoint-initdb.d
12+
healthcheck:
13+
test: [ "CMD", "mysqladmin", "ping", "-h", "localhost", "-u", "root", "-p${MYSQL_ROOT_PASSWORD}" ]
14+
timeout: 20s
15+
retries: 10
16+
interval: 5s
17+
start_period: 30s
1218

1319
php:
1420
build: ./php
1521
restart: always
1622
ports:
17-
- "8080:80"
23+
- "${DOCKER_PORT_PHP:-8080}:80"
1824
volumes:
1925
- ./src:/var/www/html
2026
env_file:
2127
- .env
2228
depends_on:
23-
- mysql
29+
mysql:
30+
condition: service_healthy
2431

2532
phpmyadmin:
2633
image: phpmyadmin/phpmyadmin
2734
restart: always
2835
ports:
29-
- "8081:80"
36+
- "${DOCKER_PORT_PMA:-8081}:80"
3037
environment:
3138
PMA_HOST: mysql
3239
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
40+
depends_on:
41+
mysql:
42+
condition: service_healthy
43+
44+
flyway:
45+
image: flyway/flyway:latest
46+
restart: no
47+
environment:
48+
FLYWAY_URL: jdbc:mysql://${MYSQL_HOST}:${MYSQL_PORT}/${MYSQL_DATABASE}?allowPublicKeyRetrieval=true&useSSL=false
49+
FLYWAY_USER: ${MYSQL_USER}
50+
FLYWAY_PASSWORD: ${MYSQL_PASSWORD}
51+
command: migrate
52+
volumes:
53+
- ./db/migrations:/flyway/sql
54+
depends_on:
55+
mysql:
56+
condition: service_healthy
3357

3458
volumes:
35-
mysql_data:
59+
mysql_data:

manage.ps1

Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
# manage.ps1
2+
3+
[CmdletBinding(DefaultParameterSetName='Action')]
4+
param(
5+
[Parameter(Mandatory=$false, Position=0, ParameterSetName='Action')]
6+
[ValidateSet('start','stop','destroy','build','force-build','backup','migrate')]
7+
[string]$Action,
8+
9+
[Parameter(Mandatory=$false, ParameterSetName='Help')]
10+
[switch]$Help
11+
)
12+
13+
function Write-Color($color, $message) {
14+
Write-Host $message -ForegroundColor $color
15+
}
16+
17+
function Show-Help {
18+
$helpText = @"
19+
Use: .\manage.ps1 [-Action] <action> [-Help]
20+
21+
Available actions:
22+
start Start all services
23+
stop Stop all services
24+
destroy Delete services and volumes (data loss!)
25+
build Build services images
26+
force-build Build services images without using the cache
27+
backup Back the database up in a .sql file
28+
migrate Migrate the current database to a newer schema version
29+
30+
Options:
31+
-Help Displays some help
32+
33+
Examples:
34+
.\manage.ps1 start
35+
.\manage.ps1 backup
36+
.\manage.ps1 -Action migrate
37+
"@
38+
Write-Host $helpText
39+
}
40+
41+
# Help or no action specified
42+
if ($Help -or [string]::IsNullOrEmpty($Action)) {
43+
Show-Help
44+
exit 0
45+
}
46+
47+
function Test-DockerRunning {
48+
try {
49+
$output = docker info 2>&1
50+
if ($LASTEXITCODE -eq 0) {
51+
return $true
52+
} else {
53+
Write-Color Red "Docker cannot be accessed. Please ensure Docker Desktop has been started beforehand."
54+
return $false
55+
}
56+
}
57+
catch {
58+
Write-Color Red "Could not run 'docker info' : $_"
59+
return $false
60+
}
61+
}
62+
63+
# Check if Docker is running
64+
if (-not (Test-DockerRunning)) {
65+
exit 1
66+
}
67+
68+
# Load environment variables from .env
69+
if (Test-Path .env) {
70+
Get-Content .env | ForEach-Object {
71+
if ($_ -match '^([^#][^=]+)=(.*)$') {
72+
[Environment]::SetEnvironmentVariable($matches[1], $matches[2], 'Process')
73+
}
74+
}
75+
} else {
76+
Write-Color Red "Environment file .env has not been found!"
77+
exit 1
78+
}
79+
80+
switch ($Action) {
81+
'start' {
82+
Write-Color Green "Starting containers..."
83+
docker-compose up -d
84+
}
85+
'stop' {
86+
Write-Color Green "Stopping containers..."
87+
docker-compose down
88+
}
89+
'destroy' {
90+
Write-Color Red "Deletion of containers and volumes (all data will be lost!)"
91+
$confirmation = Read-Host "Proceed? (y/N)"
92+
if ($confirmation -eq 'y' -or $confirmation -eq 'Y') {
93+
docker-compose down -v
94+
} else {
95+
Write-Host "Operation cancelled."
96+
}
97+
}
98+
'build' {
99+
Write-Color Green "Building services..."
100+
docker-compose build
101+
}
102+
'force-build' {
103+
Write-Color Green "Building services (no cache)..."
104+
docker-compose build --no-cache
105+
}
106+
'backup' {
107+
Write-Color Green "Database backup in progress..."
108+
109+
$backupDir = "db\backups"
110+
if (-not (Test-Path $backupDir)) {
111+
New-Item -ItemType Directory -Path $backupDir -Force | Out-Null
112+
}
113+
114+
$backupFile = "$backupDir\backup_$(Get-Date -Format 'yyyyMMdd_HHmmss').sql"
115+
$mysqlContainer = docker-compose ps -q mysql
116+
if (-not $mysqlContainer) {
117+
Write-Color Red "MySQL service has not been found. Please ensure it has been started beforehand."
118+
exit 1
119+
}
120+
docker exec $mysqlContainer mysqldump -u root -p"${env:MYSQL_ROOT_PASSWORD}" efes_db > $backupFile
121+
if ($LASTEXITCODE -eq 0) {
122+
Write-Color Green "Backup has been successful: $backupFile"
123+
} else {
124+
Write-Color Red "An error occurred!"
125+
}
126+
}
127+
'migrate' {
128+
Write-Color Green "Migrations are being applied..."
129+
docker-compose run --rm flyway
130+
}
131+
default {
132+
Write-Color Red "Unknown argument. Please use -Help for more info."
133+
exit 1
134+
}
135+
}

0 commit comments

Comments
 (0)