From 598642a0db9f282e859bd8d7aec64467b7987147 Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Wed, 22 Jan 2025 21:24:19 +0100 Subject: [PATCH 1/5] Use a pghero docker image in the local environment --- README.md | 9 +++++---- docker-compose.yml | 10 +++++++++- nginx.conf | 9 +++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index e1cc204..5905fd0 100644 --- a/README.md +++ b/README.md @@ -57,16 +57,17 @@ The backend service will seed the database with fake data. It's worth noting tha The backend, frontend and workers have hot-reloading enabled, so changes made to the codebase should be reflected in the application on the next request. -Once all containers have started, visit http://libre-wedding-planner.app.localhost/default/dashboard to load the application. +Once all containers have started, visit: + +- http://libre-wedding-planner.app.localhost/default to load the application. +- http://libre-wedding-planner.app.localhost/letter_opener/ to get a list of emails generated by the application. +- http://pghero.libre-wedding-planner.app.localhost to load the [pghero](https://github.com/ankane/pghero) tool. ## Multitenancy LibreWeddingPlanner is designed to manage multiple weddings in a single host. All URLs (in the API and the frontend) are scoped under a slug that is unique per wedding. The slug is made of lowercase letters, numbers, and dashes (-). The development environment is seeded with a wedding whose slug is `default`. -## Email delivery - -In the development environment, real emails will not be sent. You can visit http://libre-wedding-planner.app.localhost/letter_opener/ to get a list of emails generated by the application. ## Testing diff --git a/docker-compose.yml b/docker-compose.yml index 93cff59..7593984 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -79,5 +79,13 @@ services: interval: 10s timeout: 5s retries: 5 - + pghero: + image: ankane/pghero + ports: + - 8080 + environment: + DATABASE_URL: postgres://postgres:postgres@db:5432/postgres + depends_on: + db: + condition: service_healthy \ No newline at end of file diff --git a/nginx.conf b/nginx.conf index 58c2bb9..2d4c4c2 100644 --- a/nginx.conf +++ b/nginx.conf @@ -23,3 +23,12 @@ server { } } +server { + listen 80; + server_name pghero.libre-wedding-planner.app.localhost; + + location / { + proxy_pass http://pghero:8080; + proxy_set_header Host $http_host; + } +} \ No newline at end of file -- 2.47.1 From 0c5162b326870d0379e46b8e1e82a4f74df48add Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Wed, 22 Jan 2025 21:29:13 +0100 Subject: [PATCH 2/5] Install pg_stat_statements extension --- db/migrate/20250122202836_enable_pg_stat_statements.rb | 5 +++++ db/schema.rb | 7 ++----- 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 db/migrate/20250122202836_enable_pg_stat_statements.rb diff --git a/db/migrate/20250122202836_enable_pg_stat_statements.rb b/db/migrate/20250122202836_enable_pg_stat_statements.rb new file mode 100644 index 0000000..0651eec --- /dev/null +++ b/db/migrate/20250122202836_enable_pg_stat_statements.rb @@ -0,0 +1,5 @@ +class EnablePgStatStatements < ActiveRecord::Migration[8.0] + def change + enable_extension 'pg_stat_statements' + end +end diff --git a/db/schema.rb b/db/schema.rb index cfc1383..197a8b1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,7 +1,3 @@ -# Copyright (C) 2024 Manuel Bustillo - -# Copyright (C) 2024-2025 LibreWeddingPlanner contributors - # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -14,9 +10,10 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2024_12_16_231415) do +ActiveRecord::Schema[8.0].define(version: 2025_01_22_202836) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" + enable_extension "pg_stat_statements" # Custom types defined in this database. # Note that some types may not work with other database engines. Be careful if changing database. -- 2.47.1 From 259d559b71f088d3df6ef0457328adfd3fed11cd Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Wed, 22 Jan 2025 21:40:08 +0100 Subject: [PATCH 3/5] Enable pgstatements in Postgres --- docker-compose.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/docker-compose.yml b/docker-compose.yml index 7593984..4c71d59 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -74,6 +74,7 @@ services: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: postgres + command: postgres -c shared_preload_libraries='pg_stat_statements' -c pg_stat_statements.track=all healthcheck: test: ['CMD-SHELL', 'pg_isready -U postgres'] interval: 10s -- 2.47.1 From 356e799c6803c3c4072b2127ce7a4edc1382d31a Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 23 Jan 2025 21:57:18 +0100 Subject: [PATCH 4/5] WIP store stats history --- ...250122204932_create_pghero_stats_tables.rb | 21 +++++++++++++++++++ db/schema.rb | 13 +++++++++++- 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20250122204932_create_pghero_stats_tables.rb diff --git a/db/migrate/20250122204932_create_pghero_stats_tables.rb b/db/migrate/20250122204932_create_pghero_stats_tables.rb new file mode 100644 index 0000000..e3a541c --- /dev/null +++ b/db/migrate/20250122204932_create_pghero_stats_tables.rb @@ -0,0 +1,21 @@ +class CreatePgheroStatsTables < ActiveRecord::Migration[8.0] + def up + execute <<~SQL + CREATE TABLE "pghero_query_stats" ( + "id" bigserial primary key, + "database" text, + "user" text, + "query" text, + "query_hash" bigint, + "total_time" float, + "calls" bigint, + "captured_at" timestamp + ); + CREATE INDEX ON "pghero_query_stats" ("database", "captured_at"); + SQL + end + + def down + drop_table :pghero_query_stats, if_exists: true, force: :cascade + end +end diff --git a/db/schema.rb b/db/schema.rb index 197a8b1..03f31cb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_01_22_202836) do +ActiveRecord::Schema[8.0].define(version: 2025_01_22_204932) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" enable_extension "pg_stat_statements" @@ -68,6 +68,17 @@ ActiveRecord::Schema[8.0].define(version: 2025_01_22_202836) do t.index ["wedding_id"], name: "index_guests_on_wedding_id" end + create_table "pghero_query_stats", force: :cascade do |t| + t.text "database" + t.text "user" + t.text "query" + t.bigint "query_hash" + t.float "total_time" + t.bigint "calls" + t.datetime "captured_at", precision: nil + t.index ["database", "captured_at"], name: "pghero_query_stats_database_captured_at_idx" + end + create_table "seats", id: :uuid, default: -> { "gen_random_uuid()" }, force: :cascade do |t| t.uuid "guest_id", null: false t.uuid "tables_arrangement_id", null: false -- 2.47.1 From 7aaa671bbf4fc419b73b2200a8b5a77d3d5fb10f Mon Sep 17 00:00:00 2001 From: Manuel Bustillo Date: Thu, 23 Jan 2025 20:59:31 +0000 Subject: [PATCH 5/5] Add copyright notice --- db/migrate/20250122202836_enable_pg_stat_statements.rb | 2 ++ db/migrate/20250122204932_create_pghero_stats_tables.rb | 2 ++ db/schema.rb | 2 ++ 3 files changed, 6 insertions(+) diff --git a/db/migrate/20250122202836_enable_pg_stat_statements.rb b/db/migrate/20250122202836_enable_pg_stat_statements.rb index 0651eec..33dbf1a 100644 --- a/db/migrate/20250122202836_enable_pg_stat_statements.rb +++ b/db/migrate/20250122202836_enable_pg_stat_statements.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + class EnablePgStatStatements < ActiveRecord::Migration[8.0] def change enable_extension 'pg_stat_statements' diff --git a/db/migrate/20250122204932_create_pghero_stats_tables.rb b/db/migrate/20250122204932_create_pghero_stats_tables.rb index e3a541c..d813a38 100644 --- a/db/migrate/20250122204932_create_pghero_stats_tables.rb +++ b/db/migrate/20250122204932_create_pghero_stats_tables.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + class CreatePgheroStatsTables < ActiveRecord::Migration[8.0] def up execute <<~SQL diff --git a/db/schema.rb b/db/schema.rb index 03f31cb..4de4cf6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,3 +1,5 @@ +# Copyright (C) 2024 Manuel Bustillo + # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. -- 2.47.1