feat: enhance user roles migration with conditional column and index creation
This commit is contained in:
@@ -49,40 +49,29 @@ cp .env.example .env
|
|||||||
docker compose up -d
|
docker compose up -d
|
||||||
docker compose exec web composer install
|
docker compose exec web composer install
|
||||||
|
|
||||||
# Load DB variables from .env for host-side migration commands
|
# Wait until DB is healthy (initial SQL migration files are applied automatically by MySQL entrypoint)
|
||||||
set -a; source .env; set +a
|
|
||||||
|
|
||||||
# Wait until DB is healthy
|
|
||||||
until [ "$(docker inspect -f '{{.State.Health.Status}}' amnezia-panel-db 2>/dev/null)" = "healthy" ]; do
|
until [ "$(docker inspect -f '{{.State.Health.Status}}' amnezia-panel-db 2>/dev/null)" = "healthy" ]; do
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
# Apply migrations (fresh install + updates)
|
|
||||||
# 1) bootstrap base schema
|
|
||||||
docker compose exec -T db mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" < migrations/001_init.sql
|
|
||||||
|
|
||||||
# 2) apply the rest (safe to run repeatedly)
|
|
||||||
for f in migrations/*.sql; do
|
|
||||||
[ "$(basename "$f")" = "001_init.sql" ] && continue
|
|
||||||
docker compose exec -T db mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" < "$f" || true
|
|
||||||
done
|
|
||||||
|
|
||||||
# Or for older Docker Compose V1
|
# Or for older Docker Compose V1
|
||||||
docker-compose up -d
|
docker-compose up -d
|
||||||
docker-compose exec web composer install
|
docker-compose exec web composer install
|
||||||
|
|
||||||
set -a; source .env; set +a
|
|
||||||
|
|
||||||
until [ "$(docker inspect -f '{{.State.Health.Status}}' amnezia-panel-db 2>/dev/null)" = "healthy" ]; do
|
until [ "$(docker inspect -f '{{.State.Health.Status}}' amnezia-panel-db 2>/dev/null)" = "healthy" ]; do
|
||||||
sleep 2
|
sleep 2
|
||||||
done
|
done
|
||||||
|
|
||||||
docker-compose exec -T db mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" < migrations/001_init.sql
|
# Manual migration mode (existing installations / updates only)
|
||||||
|
set -a; source .env; set +a
|
||||||
for f in migrations/*.sql; do
|
for f in migrations/*.sql; do
|
||||||
[ "$(basename "$f")" = "001_init.sql" ] && continue
|
docker compose exec -T db mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" < "$f" || true
|
||||||
docker-compose exec -T db mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" < "$f" || true
|
|
||||||
done
|
done
|
||||||
|
|
||||||
|
# For Docker Compose V1 manual migration mode:
|
||||||
|
# for f in migrations/*.sql; do
|
||||||
|
# docker-compose exec -T db mysql -u"$DB_USERNAME" -p"$DB_PASSWORD" "$DB_DATABASE" < "$f" || true
|
||||||
|
# done
|
||||||
```
|
```
|
||||||
|
|
||||||
Access: http://localhost:8082
|
Access: http://localhost:8082
|
||||||
|
|||||||
@@ -12,9 +12,39 @@ CREATE TABLE IF NOT EXISTS user_roles (
|
|||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
|
||||||
|
|
||||||
-- Add role to users table
|
-- Add role to users table
|
||||||
ALTER TABLE users
|
SET @role_col_exists := (
|
||||||
ADD COLUMN role VARCHAR(50) DEFAULT 'viewer' AFTER ldap_dn,
|
SELECT COUNT(*)
|
||||||
ADD INDEX idx_role (role);
|
FROM information_schema.COLUMNS
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'users'
|
||||||
|
AND COLUMN_NAME = 'role'
|
||||||
|
);
|
||||||
|
|
||||||
|
SET @add_role_col_sql := IF(
|
||||||
|
@role_col_exists = 0,
|
||||||
|
'ALTER TABLE users ADD COLUMN role VARCHAR(50) DEFAULT ''viewer'' AFTER ldap_dn',
|
||||||
|
'SELECT 1'
|
||||||
|
);
|
||||||
|
PREPARE add_role_col_stmt FROM @add_role_col_sql;
|
||||||
|
EXECUTE add_role_col_stmt;
|
||||||
|
DEALLOCATE PREPARE add_role_col_stmt;
|
||||||
|
|
||||||
|
SET @role_idx_exists := (
|
||||||
|
SELECT COUNT(*)
|
||||||
|
FROM information_schema.STATISTICS
|
||||||
|
WHERE TABLE_SCHEMA = DATABASE()
|
||||||
|
AND TABLE_NAME = 'users'
|
||||||
|
AND INDEX_NAME = 'idx_role'
|
||||||
|
);
|
||||||
|
|
||||||
|
SET @add_role_idx_sql := IF(
|
||||||
|
@role_idx_exists = 0,
|
||||||
|
'ALTER TABLE users ADD INDEX idx_role (role)',
|
||||||
|
'SELECT 1'
|
||||||
|
);
|
||||||
|
PREPARE add_role_idx_stmt FROM @add_role_idx_sql;
|
||||||
|
EXECUTE add_role_idx_stmt;
|
||||||
|
DEALLOCATE PREPARE add_role_idx_stmt;
|
||||||
|
|
||||||
-- Insert default roles
|
-- Insert default roles
|
||||||
INSERT IGNORE INTO user_roles (name, display_name, description, permissions) VALUES
|
INSERT IGNORE INTO user_roles (name, display_name, description, permissions) VALUES
|
||||||
|
|||||||
Reference in New Issue
Block a user