Make schema.sql idempotent for CI/CD migrations
Some checks failed
Deploy / detect (push) Successful in 4s
Deploy / deploy-api (push) Failing after 22s
Deploy / deploy-frontend (push) Successful in 2m20s
Deploy / tag-deploy (push) Has been skipped

Add IF NOT EXISTS to CREATE TABLE and CREATE INDEX statements.
Drop and recreate triggers to avoid errors on existing databases.
This commit is contained in:
DaX
2026-03-05 02:09:45 +01:00
parent c5a7666ce6
commit 629b9c1756

View File

@@ -4,7 +4,7 @@
CREATE EXTENSION IF NOT EXISTS "uuid-ossp"; CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
-- Colors table -- Colors table
CREATE TABLE colors ( CREATE TABLE IF NOT EXISTS colors (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
name VARCHAR(100) NOT NULL UNIQUE, name VARCHAR(100) NOT NULL UNIQUE,
hex VARCHAR(7) NOT NULL, hex VARCHAR(7) NOT NULL,
@@ -15,7 +15,7 @@ CREATE TABLE colors (
); );
-- Filaments table -- Filaments table
CREATE TABLE filaments ( CREATE TABLE IF NOT EXISTS filaments (
id UUID PRIMARY KEY DEFAULT uuid_generate_v4(), id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
tip VARCHAR(50) NOT NULL, tip VARCHAR(50) NOT NULL,
finish VARCHAR(50) NOT NULL, finish VARCHAR(50) NOT NULL,
@@ -32,9 +32,9 @@ CREATE TABLE filaments (
); );
-- Create indexes for better performance -- Create indexes for better performance
CREATE INDEX idx_filaments_tip ON filaments(tip); CREATE INDEX IF NOT EXISTS idx_filaments_tip ON filaments(tip);
CREATE INDEX idx_filaments_boja ON filaments(boja); CREATE INDEX IF NOT EXISTS idx_filaments_boja ON filaments(boja);
CREATE INDEX idx_filaments_created_at ON filaments(created_at); CREATE INDEX IF NOT EXISTS idx_filaments_created_at ON filaments(created_at);
-- Create updated_at trigger function -- Create updated_at trigger function
CREATE OR REPLACE FUNCTION update_updated_at_column() CREATE OR REPLACE FUNCTION update_updated_at_column()
@@ -46,10 +46,12 @@ END;
$$ language 'plpgsql'; $$ language 'plpgsql';
-- Apply trigger to filaments table -- Apply trigger to filaments table
DROP TRIGGER IF EXISTS update_filaments_updated_at ON filaments;
CREATE TRIGGER update_filaments_updated_at BEFORE UPDATE CREATE TRIGGER update_filaments_updated_at BEFORE UPDATE
ON filaments FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); ON filaments FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
-- Apply trigger to colors table -- Apply trigger to colors table
DROP TRIGGER IF EXISTS update_colors_updated_at ON colors;
CREATE TRIGGER update_colors_updated_at BEFORE UPDATE CREATE TRIGGER update_colors_updated_at BEFORE UPDATE
ON colors FOR EACH ROW EXECUTE FUNCTION update_updated_at_column(); ON colors FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();