Skip to content

Commit

Permalink
Deploys new database migration script, closes#1
Browse files Browse the repository at this point in the history
  • Loading branch information
TheBoatyMcBoatFace committed Sep 12, 2023
1 parent 4a27ca5 commit 8da4718
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 1 deletion.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.DS_Store
.nova
.env
docks
scratch.json
Binary file added .nova/Artwork
Binary file not shown.
5 changes: 5 additions & 0 deletions .nova/Configuration.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"workspace.art_style" : 1,
"workspace.color" : 10,
"workspace.name" : "GovA11y Postgres"
}
96 changes: 96 additions & 0 deletions migrations/005.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
-- Migration: 005
-- ------------------------------
-- Hasura
-- ------------------------------
-- Create the 'hasura' user with password 'SecretsOfHasura'
CREATE USER hasura WITH PASSWORD 'SecretsOfHasura';

-- Create the 'hasura'database
CREATE DATABASE hasura;

-- Grant all privileges on the 'hasura' and 'gova11y' databases to the 'hasura' user
GRANT ALL PRIVILEGES ON DATABASE hasura TO hasura;
GRANT ALL PRIVILEGES ON DATABASE gova11y TO hasura;

ALTER USER hasura WITH PASSWORD 'SecretsOfHasura';

-- ------------------------------
-- Add URLs & Sitemaps
-- ------------------------------
-- Insert GovA11y Homepage
INSERT INTO targets.urls (url, domain_id)
VALUES
('https://gova11y.io/', 1);

-- ------------------------------
-- Fix Sitemap Table
-- ------------------------------

-- Step 1: Add a new column to hold the foreign key to the targets.urls table
ALTER TABLE targets.sitemaps
ADD COLUMN IF NOT EXISTS url_id bigint;

-- Step 2: Add a foreign key constraint to establish the relationship between the two tables
ALTER TABLE targets.sitemaps
ADD CONSTRAINT sitemaps_fk_url
FOREIGN KEY (url_id) REFERENCES targets.urls(id);

-- Step 3: Creating an index on the new url_id column to speed up queries
CREATE INDEX IF NOT EXISTS idx_sitemaps_url_id ON targets.sitemaps USING btree (url_id);

-- Step 4: Data Migration: Mapping sitemap_url to url_id
WITH url_inserts AS (
INSERT INTO targets.urls (url, domain_id)
SELECT DISTINCT sitemap_url, domain_id FROM targets.sitemaps
ON CONFLICT (url) DO NOTHING
RETURNING id, url
)
UPDATE targets.sitemaps s
SET url_id = u.id
FROM url_inserts u
WHERE s.sitemap_url = u.url;


-- Step 5: Drop the sitemap_url column since we are using url_id
ALTER TABLE targets.sitemaps
DROP COLUMN sitemap_url;


-- ------------------------------
-- Create Sitemap Insertion Function
-- ------------------------------

CREATE OR REPLACE FUNCTION targets.insert_sitemap(
sitemap_url TEXT,
domain_id INT4,
last_modified timestamptz DEFAULT NULL,
change_frequency TEXT DEFAULT NULL,
priority FLOAT4 DEFAULT NULL
)
RETURNS void LANGUAGE plpgsql AS $$
DECLARE
url_id bigint;
BEGIN
-- Step 1: Insert the URL into the targets.urls table and get the ID or do nothing if it already exists
INSERT INTO targets.urls (url, domain_id)
VALUES (sitemap_url, domain_id)
ON CONFLICT (url)
DO UPDATE
SET url = excluded.url
RETURNING id INTO url_id;

-- Step 2: Insert a new record into the targets.sitemaps table with the ID of the URL and the other details
INSERT INTO targets.sitemaps (url_id, domain_id, last_modified, change_frequency, priority)
VALUES (url_id, domain_id, last_modified, change_frequency, priority)
ON CONFLICT (url_id)
DO NOTHING;
END $$;


-- Insert some of the GovA11y Sitemaps
SELECT targets.insert_sitemap('https://gova11y.io/sitemap-pages.xml', 1, '2023-09-12 22:32:00');
SELECT targets.insert_sitemap('https://gova11y.io/sitemap-authors.xml', 1, '2023-09-12 22:03:00');
SELECT targets.insert_sitemap('https://gova11y.io/sitemap-tags.xml', 1, '2023-09-08 04:37:00');



0 comments on commit 8da4718

Please sign in to comment.