fix: add provider column migration script for existing db
This commit is contained in:
67
backend/scripts/migrate_provider_columns.sql
Normal file
67
backend/scripts/migrate_provider_columns.sql
Normal file
@@ -0,0 +1,67 @@
|
||||
-- Rename legacy IdP column names to provider_* naming.
|
||||
-- Safe to run multiple times.
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'companies' AND column_name = 'idp_group_id'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'companies' AND column_name = 'provider_group_id'
|
||||
) THEN
|
||||
ALTER TABLE public.companies RENAME COLUMN idp_group_id TO provider_group_id;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'sites' AND column_name = 'idp_group_id'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'sites' AND column_name = 'provider_group_id'
|
||||
) THEN
|
||||
ALTER TABLE public.sites RENAME COLUMN idp_group_id TO provider_group_id;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'systems' AND column_name = 'idp_client_id'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'systems' AND column_name = 'provider_client_id'
|
||||
) THEN
|
||||
ALTER TABLE public.systems RENAME COLUMN idp_client_id TO provider_client_id;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'roles' AND column_name = 'idp_role_name'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'roles' AND column_name = 'provider_role_name'
|
||||
) THEN
|
||||
ALTER TABLE public.roles RENAME COLUMN idp_role_name TO provider_role_name;
|
||||
END IF;
|
||||
END $$;
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'idp_user_id'
|
||||
) AND NOT EXISTS (
|
||||
SELECT 1 FROM information_schema.columns
|
||||
WHERE table_schema = 'public' AND table_name = 'users' AND column_name = 'provider_user_id'
|
||||
) THEN
|
||||
ALTER TABLE public.users RENAME COLUMN idp_user_id TO provider_user_id;
|
||||
END IF;
|
||||
END $$;
|
||||
@@ -7,6 +7,27 @@ psql "$DATABASE_URL" -f scripts/init_schema.sql
|
||||
```
|
||||
- DB schema 檔案:[backend/scripts/init_schema.sql](../backend/scripts/init_schema.sql)
|
||||
|
||||
如果你是 macOS 本機沒裝 `psql`,改用:
|
||||
```bash
|
||||
cd backend
|
||||
./.venv/bin/python - <<'PY'
|
||||
import psycopg
|
||||
from pathlib import Path
|
||||
sql = Path('scripts/migrate_provider_columns.sql').read_text()
|
||||
with psycopg.connect(
|
||||
host='127.0.0.1',
|
||||
port=54321,
|
||||
dbname='member.ose.tw',
|
||||
user='member_ose',
|
||||
password='你的DB密碼'
|
||||
) as conn:
|
||||
with conn.cursor() as cur:
|
||||
cur.execute(sql)
|
||||
print('provider column migration done')
|
||||
PY
|
||||
```
|
||||
- 欄位改名 migration:[backend/scripts/migrate_provider_columns.sql](../backend/scripts/migrate_provider_columns.sql)
|
||||
|
||||
## 2) 啟動後端
|
||||
```bash
|
||||
cd backend
|
||||
|
||||
Reference in New Issue
Block a user