chore: adds useful SQL scripts for devops (#14068)
This commit is contained in:
@@ -0,0 +1,34 @@
|
||||
-- Create or replace the function to automatically grant permissions to a user when a table is created.
|
||||
CREATE OR REPLACE
|
||||
FUNCTION auto_grant_func()
|
||||
RETURNS event_trigger AS $$
|
||||
|
||||
BEGIN
|
||||
GRANT ALL ON ALL TABLES IN SCHEMA public TO REPLACE_ME_WITH_USERNAME;
|
||||
GRANT ALL ON ALL SEQUENCES IN SCHEMA public TO REPLACE_ME_WITH_USERNAME;
|
||||
GRANT SELECT ON ALL TABLES IN SCHEMA public TO REPLACE_ME_WITH_USERNAME;
|
||||
GRANT SELECT ON ALL SEQUENCES IN SCHEMA public TO REPLACE_ME_WITH_USERNAME;
|
||||
END;
|
||||
|
||||
$$ LANGUAGE plpgsql;
|
||||
|
||||
-- Create event trigger for auto_grant_func
|
||||
CREATE EVENT TRIGGER auto_grant_trigger
|
||||
ON
|
||||
ddl_command_end
|
||||
WHEN TAG IN ('CREATE TABLE', 'CREATE TABLE AS')
|
||||
EXECUTE PROCEDURE auto_grant_func();
|
||||
|
||||
-- Check if auto_grant_func exists
|
||||
SELECT
|
||||
prosrc
|
||||
FROM
|
||||
pg_proc
|
||||
WHERE
|
||||
proname = 'auto_grant_func';
|
||||
|
||||
-- List event triggers
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
pg_event_trigger;
|
||||
@@ -0,0 +1,24 @@
|
||||
-- Check all connection activity
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
pg_stat_activity;
|
||||
|
||||
-- Clear idle connections older than 15 minutes
|
||||
SELECT
|
||||
pg_terminate_backend(pid)
|
||||
FROM
|
||||
pg_stat_activity
|
||||
WHERE
|
||||
state = 'idle'
|
||||
AND
|
||||
state_change < now() - '15min'::INTERVAL;
|
||||
|
||||
-- Check connections by user
|
||||
SELECT
|
||||
usename,
|
||||
count(*)
|
||||
FROM
|
||||
pg_stat_activity
|
||||
GROUP BY
|
||||
usename;
|
||||
@@ -0,0 +1,18 @@
|
||||
-- Count how many empty google credentials are in the database
|
||||
SELECT
|
||||
count(*)
|
||||
FROM
|
||||
"Credential" c
|
||||
WHERE
|
||||
"key" = '""'
|
||||
AND "type" = 'google_calendar'
|
||||
|
||||
-- Delete empty google credentials
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
"Credential" c
|
||||
DELETE FROM "Credential" c
|
||||
WHERE
|
||||
"key" = '""'
|
||||
AND "type" = 'google_calendar' RETURNING *
|
||||
@@ -0,0 +1,13 @@
|
||||
-- Find users with uppercase characters in email
|
||||
SELECT * FROM users u WHERE u.email ~ '[[:upper:]]';
|
||||
|
||||
-- Find emails with case insensitive duplicates
|
||||
SELECT lower(email) FROM users GROUP BY lower(email) HAVING count(*) > 1;
|
||||
|
||||
-- List emails with their case insensitive duplicates
|
||||
SELECT * FROM users u WHERE LOWER(u.email) IN (
|
||||
SELECT lower(email) FROM users GROUP BY lower(email) HAVING count(*) > 1
|
||||
);
|
||||
|
||||
-- Lowercase all user emails (This will fail if there are case insensitive duplicates)
|
||||
UPDATE users SET email = lower(email) WHERE email ~ '[[:upper:]]';
|
||||
Reference in New Issue
Block a user