-- Create the faqs table if it doesn't exist CREATE TABLE IF NOT EXISTS faqs ( id SERIAL PRIMARY KEY, question TEXT NOT NULL, answer TEXT NOT NULL, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); -- Add comment to table COMMENT ON TABLE faqs IS 'Stores frequently asked questions and answers'; -- Add RLS (Row Level Security) policy ALTER TABLE faqs ENABLE ROW LEVEL SECURITY; -- Create policy for public read access if it doesn't exist DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'faqs' AND policyname = 'Faqs are viewable by everyone' ) THEN CREATE POLICY "Faqs are viewable by everyone" ON faqs FOR SELECT USING (true); END IF; END $$; -- Create policy for authenticated users to insert/update if it doesn't exist DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'faqs' AND policyname = 'Authenticated users can insert/update faqs' ) THEN CREATE POLICY "Authenticated users can insert/update faqs" ON faqs FOR INSERT TO authenticated WITH CHECK (true); END IF; END $$; -- Create policy for update if it doesn't exist DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM pg_policies WHERE tablename = 'faqs' AND policyname = 'Authenticated users can update faqs' ) THEN CREATE POLICY "Authenticated users can update faqs" ON faqs FOR UPDATE TO authenticated USING (true) WITH CHECK (true); END IF; END $$; -- Install the text search extension if not already installed CREATE EXTENSION IF NOT EXISTS pg_trgm; -- Create index on question for faster searching if it doesn't exist CREATE INDEX IF NOT EXISTS faqs_question_idx ON faqs USING GIN (question gin_trgm_ops); -- Create trigger function for updating the updated_at timestamp if it doesn't exist CREATE OR REPLACE FUNCTION trigger_set_timestamp() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = NOW(); RETURN NEW; END; $$ LANGUAGE plpgsql; -- Create trigger if it doesn't exist DROP TRIGGER IF EXISTS set_timestamp ON faqs; CREATE TRIGGER set_timestamp BEFORE UPDATE ON faqs FOR EACH ROW EXECUTE FUNCTION trigger_set_timestamp(); -- Check if the table has been created successfully SELECT COUNT(*) FROM faqs;