Skip to content

Commit

Permalink
Merge branch 'main' into 15932_fix_deleting_phase_in_activity_log
Browse files Browse the repository at this point in the history
  • Loading branch information
chiaberry committed Mar 6, 2024
2 parents dd8c9b2 + f8e082b commit 92e0867
Show file tree
Hide file tree
Showing 33 changed files with 670 additions and 833 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/export_database_views.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env bash

function create_view_file() {
local VIEW_NAME=$1

echo "View: $VIEW_NAME"
MOST_RECENT_MIGRATION=$(grep -rl --include=up.sql -E "CREATE (OR REPLACE )?VIEW (\"?public\"?.)?\"?$VIEW_NAME\"?" moped-database/migrations | sort -V | tail -n 1)
echo "MOST_RECENT_MIGRATION: $MOST_RECENT_MIGRATION"

# Create the view file with header
echo "-- Most recent migration: $MOST_RECENT_MIGRATION" > moped-database/views/$VIEW_NAME.sql
echo "" >> moped-database/views/$VIEW_NAME.sql

# Query the view definition and append to the file
psql -A -t -c "SELECT 'CREATE OR REPLACE VIEW ' || '$VIEW_NAME' || ' AS ' || pg_get_viewdef('$VIEW_NAME'::regclass, true);" >> moped-database/views/$VIEW_NAME.sql
}

# Export the function
export -f create_view_file

function populate_views() {
mkdir -p moped-database/views
psql -A -t -c "SELECT table_name FROM information_schema.views WHERE table_schema = 'public';" | \
grep -v -E '^geo[a-zA-Z]+y_columns$' | \
xargs -I {} bash -c "create_view_file '{}'"
}

populate_views
71 changes: 71 additions & 0 deletions .github/workflows/export_database_views.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Export Moped Views to SQL

on:
push:
paths:
- moped-database/migrations/**
branches-ignore:
- "main"
- "production"

jobs:
publish-views:
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.ref }}

- name: Install SQLFluff
run: |
sudo apt-get update
sudo apt-get install -y python3-pip
pip3 install sqlfluff
- name: Install PostgreSQL client
run: sudo apt-get update && sudo apt-get install -y postgresql-client

- name: Set PostgreSQL environment variables
run: |
echo "PGHOST=localhost" >> $GITHUB_ENV
echo "PGPORT=5432" >> $GITHUB_ENV
echo "PGDATABASE=moped" >> $GITHUB_ENV
echo "PGUSER=moped" >> $GITHUB_ENV
echo "PGPASSWORD=moped" >> $GITHUB_ENV
- name: Install Hasura CLI
run: |
curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash
- name: "Spin-up the DB stack"
run: |
export BRANCH_NAME=${GITHUB_REF##*/}
echo "SHA: ${GITHUB_SHA}"
echo "ACTION/BRANCH_NAME: ${BRANCH_NAME}"
echo "GR: ${GITHUB_REF}"
echo "PWD: $(pwd)"
cd ./moped-database;
./hasura-cluster setenv local;
./hasura-cluster start;
- name: Generate CREATE VIEW statements
run: $(pwd)/.github/workflows/export_database_views.sh

- name: Run SQLFluff
run: |
sqlfluff format --config .sqlfluff moped-database/views/*.sql
- name: Set Git Config
run: |
git config --local user.email "[email protected]"
git config --local user.name "Moped View Bot"
- name: Commit and push changes
run: |
export BRANCH_NAME=${GITHUB_REF##*/}
git add moped-database/views/*
git commit -m "🤖 Export view for $BRANCH_NAME" || echo "No changes to commit"
git push origin HEAD || echo "No changes to push"
continue-on-error: true
5 changes: 4 additions & 1 deletion .sqlfluff
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
[sqlfluff]

# Set loop limit for fixing
runaway_limit = 10

# Supported dialects https://docs.sqlfluff.com/en/stable/dialects.html
# Or run 'sqlfluff dialects'
dialect = postgres
Expand All @@ -14,7 +17,7 @@ templater = jinja
# See https://docs.sqlfluff.com/en/stable/configuration.html#enabling-and-disabling-rules
# AM04 (ambiguous.column_count) and ST06 (structure.column_order) are
# two of the more controversial rules included to illustrate usage.
exclude_rules = ambiguous.column_count, structure.column_order
exclude_rules = ambiguous.column_count, structure.column_order, layout.long_lines

# The standard max_line_length is 80 in line with the convention of
# other tools and several style guides. Many projects however prefer
Expand Down
2 changes: 1 addition & 1 deletion moped-database/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
version: '3.7'
services:
hasura:
image: hasura/graphql-engine:v2.37.0
image: hasura/graphql-engine:v2.37.1
restart: always
depends_on:
- moped-pgsql
Expand Down
56 changes: 33 additions & 23 deletions moped-database/metadata/tables.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2810,95 +2810,107 @@
foreign_key_constraint_on: project_id
- name: moped_user
using:
foreign_key_constraint_on: added_by_user_id
foreign_key_constraint_on: created_by_user_id
insert_permissions:
- role: moped-admin
permission:
check: {}
set:
created_by_user_id: x-hasura-user-db-id
updated_by_user_id: x-hasura-user-db-id
columns:
- added_by_user_id
- date_created
- is_deleted
- phase_id
- project_id
- project_note
- project_note_type
comment: No insert permissions on audit fields
- role: moped-editor
permission:
check: {}
set:
created_by_user_id: x-hasura-user-db-id
updated_by_user_id: x-hasura-user-db-id
columns:
- added_by_user_id
- date_created
- is_deleted
- phase_id
- project_id
- project_note
- project_note_type
comment: No insert permissions on audit fields
select_permissions:
- role: moped-admin
permission:
columns:
- added_by_user_id
- date_created
- created_at
- created_by_user_id
- is_deleted
- phase_id
- project_id
- project_note
- project_note_id
- project_note_type
- updated_at
- updated_by_user_id
filter: {}
allow_aggregations: true
- role: moped-editor
permission:
columns:
- added_by_user_id
- date_created
- created_at
- created_by_user_id
- is_deleted
- phase_id
- project_id
- project_note
- project_note_id
- project_note_type
- updated_at
- updated_by_user_id
filter: {}
allow_aggregations: true
- role: moped-viewer
permission:
columns:
- added_by_user_id
- date_created
- created_at
- created_by_user_id
- is_deleted
- phase_id
- project_id
- project_note
- project_note_id
- project_note_type
- updated_at
- updated_by_user_id
filter: {}
allow_aggregations: true
update_permissions:
- role: moped-admin
permission:
columns:
- added_by_user_id
- date_created
- is_deleted
- phase_id
- project_id
- project_note
- project_note_type
filter: {}
check: null
set:
updated_by_user_id: x-hasura-user-db-id
comment: No update permissions on audit fields
- role: moped-editor
permission:
columns:
- added_by_user_id
- date_created
- is_deleted
- phase_id
- project_id
- project_note
- project_note_type
filter: {}
check: null
set:
updated_by_user_id: x-hasura-user-db-id
comment: No update permissions on audit fields
event_triggers:
- name: activity_log_moped_proj_notes
definition:
Expand All @@ -2920,7 +2932,7 @@
action: transform
template: |-
{
"query": "mutation InsertActivity($object: moped_activity_log_insert_input!, $project_id:Int!, $updated_at:timestamptz ) { insert_moped_activity_log_one(object: $object) { activity_id } update_moped_project_by_pk(pk_columns: {project_id: $project_id}, _set: {updated_at: $updated_at}) { updated_at }}",
"query": "mutation InsertActivity($object: moped_activity_log_insert_input!) { insert_moped_activity_log_one(object: $object) { activity_id } }",
"variables": {
"object": {
"record_id": {{ $body.event.data.new.project_note_id }},
Expand All @@ -2931,9 +2943,7 @@
"description": [{"newSchema": "true"}],
"operation_type": {{ $body.event.op }},
"updated_by_user_id": {{ $session_variables?['x-hasura-user-db-id'] ?? 1}}
},
"updated_at": {{$body.created_at}},
"project_id": {{$body.event.data.new.project_id}}
}
}
}
template_engine: Kriti
Expand Down Expand Up @@ -3430,8 +3440,6 @@
update_permissions:
- role: moped-admin
permission:
set:
updated_by_user_id: x-hasura-user-db-id
columns:
- is_current_phase
- is_deleted
Expand All @@ -3445,10 +3453,10 @@
- is_phase_end_confirmed
filter: {}
check: {}
- role: moped-editor
permission:
set:
updated_by_user_id: x-hasura-user-db-id
- role: moped-editor
permission:
columns:
- is_current_phase
- is_deleted
Expand All @@ -3462,6 +3470,8 @@
- is_phase_end_confirmed
filter: {}
check: {}
set:
updated_by_user_id: x-hasura-user-db-id
event_triggers:
- name: activity_log_moped_proj_phases
definition:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ALTER TABLE public.moped_proj_notes RENAME COLUMN created_at TO date_created;
ALTER TABLE public.moped_proj_notes RENAME COLUMN created_by_user_id TO added_by_user_id;
ALTER TABLE public.moped_proj_notes DROP COLUMN updated_at;
ALTER TABLE public.moped_proj_notes DROP COLUMN updated_by_user_id;

DROP TRIGGER update_moped_proj_notes_and_project_audit_fields ON moped_proj_notes;
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ALTER TABLE moped_proj_notes RENAME COLUMN date_created TO created_at;
ALTER TABLE moped_proj_notes RENAME COLUMN added_by_user_id TO created_by_user_id;
ALTER TABLE moped_proj_notes ADD COLUMN updated_at TIMESTAMPTZ
NULL;
ALTER TABLE moped_proj_notes ADD COLUMN updated_by_user_id INTEGER
NULL;

COMMENT ON COLUMN moped_proj_notes.updated_at IS 'Timestamp when the record was last updated';
COMMENT ON COLUMN moped_proj_notes.updated_by_user_id IS 'ID of the user who last updated the record';

CREATE TRIGGER update_moped_proj_notes_and_project_audit_fields
BEFORE INSERT OR UPDATE ON moped_proj_notes
FOR EACH ROW
EXECUTE FUNCTION public.update_self_and_project_updated_audit_fields();

COMMENT ON TRIGGER update_moped_proj_notes_and_project_audit_fields ON moped_proj_notes IS 'Trigger to execute the update_self_and_project_updated_audit_fields function before each update operation on the moped_proj_notes table.';
4 changes: 2 additions & 2 deletions moped-database/seeds/1602292389297_initial_seed_staging.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ INSERT INTO public.moped_proj_milestones (project_milestone_id, project_id, desc
-- Data for Name: moped_proj_notes; Type: TABLE DATA; Schema: public; Owner: moped
--

INSERT INTO public.moped_proj_notes (project_note_id, project_note, date_created, project_id, added_by_user_id, project_note_type, is_deleted) VALUES (2, '<p>This is a status update</p>', '2022-11-12 18:08:55.845376+00', 227, 1, 2, false);
INSERT INTO public.moped_proj_notes (project_note_id, project_note, date_created, project_id, added_by_user_id, project_note_type, is_deleted) VALUES (3, '<p>This is an internal note</p>', '2022-11-12 18:14:16.640873+00', 227, 1, 1, false);
INSERT INTO public.moped_proj_notes (project_note_id, project_note, created_at, project_id, created_by_user_id, project_note_type, is_deleted) VALUES (2, '<p>This is a status update</p>', '2022-11-12 18:08:55.845376+00', 227, 1, 2, false);
INSERT INTO public.moped_proj_notes (project_note_id, project_note, created_at, project_id, created_by_user_id, project_note_type, is_deleted) VALUES (3, '<p>This is an internal note</p>', '2022-11-12 18:14:16.640873+00', 227, 1, 1, false);


--
Expand Down
8 changes: 2 additions & 6 deletions moped-database/views/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# Updating a SQL view
# Automatically Generated Views

1. Update the view in this directory
2. Paste into the Hasura console through the "Modify" option when exploring the SQL view through the Database tab
3. Run with the "This is a migration" option and provide a meaningful migration name
4. Create a down migration to go back to the previous state of the query
5. At the beginning of each new migration file, add `DROP VIEW <view name>;` because `CREATE OR REPLACE VIEW` has limitations described [here in the PostgreSQL CREATE VIEW documentation](https://www.postgresql.org/docs/9.3/sql-createview.html)
These view files are automatically generated based on the migrations in place. They will be overwritten, so any changes you make won't last too long. To change a view, please write a migration, and these will get updated by GitHub.
Loading

0 comments on commit 92e0867

Please sign in to comment.