[DB] Fix migration 013: MySQL 8.0 compat + script deploy idempotente

- Rimosso ADD COLUMN IF NOT EXISTS (non supportato MySQL 8.0 standard)
- Aggiunto deploy_013.sh: script bash idempotente con check information_schema

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
DevEnv nis2-agile 2026-03-07 16:48:53 +01:00
parent f7347ccd8c
commit c906a6eff3
2 changed files with 54 additions and 8 deletions

View File

@ -7,24 +7,23 @@
USE nis2_agile_db; USE nis2_agile_db;
-- ── invites: campi commerciali aggiuntivi ────────────────────────────────── -- ── invites: campi commerciali aggiuntivi ──────────────────────────────────
-- NOTA: MySQL 8.0 non supporta ADD COLUMN IF NOT EXISTS (solo NDB).
-- Eseguire questo file con: mysql -e "source /path/013_license_ext.sql"
-- oppure usare lo script shell deploy_013.sh nella stessa cartella.
-- Numero massimo di utenti per organizzazione (NULL = illimitato)
ALTER TABLE invites ALTER TABLE invites
ADD COLUMN IF NOT EXISTS max_users_per_org SMALLINT UNSIGNED NULL DEFAULT NULL AFTER max_uses; ADD COLUMN max_users_per_org SMALLINT UNSIGNED NULL DEFAULT NULL AFTER max_uses;
-- Prezzo di listino (solo riferimento, non enforced da NIS2)
ALTER TABLE invites ALTER TABLE invites
ADD COLUMN IF NOT EXISTS price_eur DECIMAL(8,2) NULL DEFAULT NULL AFTER max_users_per_org; ADD COLUMN price_eur DECIMAL(8,2) NULL DEFAULT NULL AFTER max_users_per_org;
-- Nome reseller / partner che ha acquistato la licenza
ALTER TABLE invites ALTER TABLE invites
ADD COLUMN IF NOT EXISTS reseller_name VARCHAR(128) NULL DEFAULT NULL AFTER price_eur; ADD COLUMN reseller_name VARCHAR(128) NULL DEFAULT NULL AFTER price_eur;
-- ── organizations: limite utenti dalla licenza ───────────────────────────── -- ── organizations: limite utenti dalla licenza ─────────────────────────────
-- Numero massimo utenti per org (valorizzato al provisioning)
ALTER TABLE organizations ALTER TABLE organizations
ADD COLUMN IF NOT EXISTS license_max_users SMALLINT UNSIGNED NULL DEFAULT NULL AFTER license_expires_at; ADD COLUMN license_max_users SMALLINT UNSIGNED NULL DEFAULT NULL AFTER license_expires_at;
-- ── Verifica ────────────────────────────────────────────────────────────── -- ── Verifica ──────────────────────────────────────────────────────────────
SELECT SELECT

47
docs/sql/deploy_013.sh Normal file
View File

@ -0,0 +1,47 @@
#!/bin/bash
# Deploy migration 013 su MySQL 8.0 (idempotente)
# Uso: bash deploy_013.sh (da /var/www/nis2-agile)
# Richiede: DB_NAME, DB_USER, DB_PASS dal .env o da variabili d'ambiente
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ENV_FILE="$(dirname "$SCRIPT_DIR")/.env"
# Carica .env se presente
if [ -f "$ENV_FILE" ]; then
export $(grep -v '^#' "$ENV_FILE" | grep -E '^DB_' | xargs)
fi
DB_NAME="${DB_NAME:-nis2_agile_db}"
DB_USER="${DB_USER:-nis2_user}"
DB_PASS="${DB_PASS:-}"
DB_HOST="${DB_HOST:-localhost}"
MYSQL="mysql -h$DB_HOST -u$DB_USER -p$DB_PASS $DB_NAME"
add_column_if_missing() {
local table=$1 column=$2 definition=$3 after=$4
EXISTS=$($MYSQL -sN -e "
SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA='$DB_NAME' AND TABLE_NAME='$table' AND COLUMN_NAME='$column';
")
if [ "$EXISTS" -eq 0 ]; then
echo " + ADD COLUMN $table.$column"
$MYSQL -e "ALTER TABLE $table ADD COLUMN $column $definition AFTER $after;"
else
echo " . SKIP $table.$column (già presente)"
fi
}
echo "=== Migration 013: License Ext ==="
# invites
add_column_if_missing invites max_users_per_org "SMALLINT UNSIGNED NULL DEFAULT NULL" max_uses
add_column_if_missing invites price_eur "DECIMAL(8,2) NULL DEFAULT NULL" max_users_per_org
add_column_if_missing invites reseller_name "VARCHAR(128) NULL DEFAULT NULL" price_eur
# organizations
add_column_if_missing organizations license_max_users "SMALLINT UNSIGNED NULL DEFAULT NULL" license_expires_at
echo "=== Migration 013 completata. ==="