-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into 15932_fix_deleting_phase_in_activity_log
- Loading branch information
Showing
33 changed files
with
670 additions
and
833 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...ions/1708036428508_alter_table_public_moped_proj_notes_alter_column_date_created/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
16 changes: 16 additions & 0 deletions
16
...ations/1708036428508_alter_table_public_moped_proj_notes_alter_column_date_created/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
Oops, something went wrong.