Update db-migration job
This commit is contained in:
parent
b45a2e86ae
commit
7b8a065c0d
@ -1,68 +1,110 @@
|
|||||||
apiVersion: batch/v1
|
apiVersion: v1
|
||||||
kind: Job
|
kind: ConfigMap
|
||||||
metadata:
|
metadata:
|
||||||
name: {{ .Release.Name }}-db-migration-{{ .Release.Revision }}
|
name: {{ .Release.Name }}-db-migration
|
||||||
namespace: {{ .Values.global.namespace }}
|
namespace: {{ .Values.global.namespace }}
|
||||||
labels:
|
data:
|
||||||
app: {{ .Release.Name }}-db-migration
|
migrate.sql: |
|
||||||
component: migration
|
-- Create users table if it doesn't exist
|
||||||
annotations:
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
"helm.sh/hook": pre-install,pre-upgrade
|
id SERIAL PRIMARY KEY,
|
||||||
"helm.sh/hook-weight": "-5"
|
username TEXT UNIQUE NOT NULL,
|
||||||
"helm.sh/hook-delete-policy": before-hook-creation
|
email TEXT UNIQUE NOT NULL,
|
||||||
spec:
|
password_hash TEXT NOT NULL,
|
||||||
ttlSecondsAfterFinished: 300
|
first_name TEXT,
|
||||||
template:
|
last_name TEXT,
|
||||||
metadata:
|
display_name TEXT NOT NULL,
|
||||||
labels:
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
app: {{ .Release.Name }}-db-migration
|
);
|
||||||
spec:
|
|
||||||
restartPolicy: Never
|
-- Create recipes table if it doesn't exist (matching backend schema)
|
||||||
containers:
|
CREATE TABLE IF NOT EXISTS recipes (
|
||||||
- name: migrate
|
id SERIAL PRIMARY KEY,
|
||||||
image: postgres:16-alpine
|
name TEXT NOT NULL,
|
||||||
command:
|
meal_type TEXT NOT NULL,
|
||||||
- /bin/sh
|
time_minutes INTEGER NOT NULL,
|
||||||
- -c
|
tags TEXT[] NOT NULL DEFAULT '{}',
|
||||||
- |
|
ingredients TEXT[] NOT NULL DEFAULT '{}',
|
||||||
echo "Waiting for database to be ready..."
|
steps TEXT[] NOT NULL DEFAULT '{}',
|
||||||
until pg_isready -h $DB_HOST -U $DB_USER; do
|
image TEXT,
|
||||||
echo "Database not ready, waiting..."
|
made_by TEXT,
|
||||||
sleep 2
|
user_id INTEGER REFERENCES users(id) ON DELETE SET NULL,
|
||||||
done
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
echo "Database is ready, running migration..."
|
);
|
||||||
PGPASSWORD=$DB_PASSWORD psql -h $DB_HOST -U $DB_USER -d $DB_NAME -f /migration/migrate.sql
|
|
||||||
echo "Migration completed successfully"
|
-- Add new user columns if they don't exist
|
||||||
env:
|
DO $$
|
||||||
- name: DB_HOST
|
BEGIN
|
||||||
valueFrom:
|
IF NOT EXISTS (
|
||||||
secretKeyRef:
|
SELECT 1 FROM information_schema.columns
|
||||||
name: {{ .Release.Name }}-db-credentials
|
WHERE table_name = 'users' AND column_name = 'first_name'
|
||||||
key: DB_HOST
|
) THEN
|
||||||
- name: DB_PORT
|
ALTER TABLE users ADD COLUMN first_name TEXT;
|
||||||
valueFrom:
|
END IF;
|
||||||
secretKeyRef:
|
END $$;
|
||||||
name: {{ .Release.Name }}-db-credentials
|
|
||||||
key: DB_PORT
|
DO $$
|
||||||
- name: DB_NAME
|
BEGIN
|
||||||
valueFrom:
|
IF NOT EXISTS (
|
||||||
secretKeyRef:
|
SELECT 1 FROM information_schema.columns
|
||||||
name: {{ .Release.Name }}-db-credentials
|
WHERE table_name = 'users' AND column_name = 'last_name'
|
||||||
key: DB_NAME
|
) THEN
|
||||||
- name: DB_USER
|
ALTER TABLE users ADD COLUMN last_name TEXT;
|
||||||
valueFrom:
|
END IF;
|
||||||
secretKeyRef:
|
END $$;
|
||||||
name: {{ .Release.Name }}-db-credentials
|
|
||||||
key: DB_USER
|
DO $$
|
||||||
- name: DB_PASSWORD
|
BEGIN
|
||||||
valueFrom:
|
IF NOT EXISTS (
|
||||||
secretKeyRef:
|
SELECT 1 FROM information_schema.columns
|
||||||
name: {{ .Release.Name }}-db-credentials
|
WHERE table_name = 'users' AND column_name = 'display_name'
|
||||||
key: DB_PASSWORD
|
) THEN
|
||||||
volumeMounts:
|
ALTER TABLE users ADD COLUMN display_name TEXT;
|
||||||
- name: migration-script
|
-- Set display_name to username for existing users
|
||||||
mountPath: /migration
|
UPDATE users SET display_name = username WHERE display_name IS NULL;
|
||||||
volumes:
|
ALTER TABLE users ALTER COLUMN display_name SET NOT NULL;
|
||||||
- name: migration-script
|
END IF;
|
||||||
configMap:
|
END $$;
|
||||||
name: {{ .Release.Name }}-db-migration
|
|
||||||
|
-- Add user_id column if it doesn't exist (for existing recipes tables)
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recipes' AND column_name = 'user_id'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recipes ADD COLUMN user_id INTEGER REFERENCES users(id) ON DELETE SET NULL;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Add created_at column to recipes if it doesn't exist
|
||||||
|
DO $$
|
||||||
|
BEGIN
|
||||||
|
IF NOT EXISTS (
|
||||||
|
SELECT 1 FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recipes' AND column_name = 'created_at'
|
||||||
|
) THEN
|
||||||
|
ALTER TABLE recipes ADD COLUMN created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP;
|
||||||
|
END IF;
|
||||||
|
END $$;
|
||||||
|
|
||||||
|
-- Create indexes
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_recipes_meal_type ON recipes (meal_type);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_recipes_time_minutes ON recipes (time_minutes);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_recipes_made_by ON recipes (made_by);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_recipes_user_id ON recipes (user_id);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_username ON users (username);
|
||||||
|
CREATE INDEX IF NOT EXISTS idx_users_email ON users (email);
|
||||||
|
|
||||||
|
-- Verify schema
|
||||||
|
SELECT 'Users table:' as info;
|
||||||
|
SELECT column_name, data_type
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'users'
|
||||||
|
ORDER BY ordinal_position;
|
||||||
|
|
||||||
|
SELECT 'Recipes table:' as info;
|
||||||
|
SELECT column_name, data_type
|
||||||
|
FROM information_schema.columns
|
||||||
|
WHERE table_name = 'recipes'
|
||||||
|
ORDER BY ordinal_position;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user