Skip to content

Instantly share code, notes, and snippets.

@banagale
Created March 21, 2023 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save banagale/e80a92e0842265bfa18b0d12f9baa368 to your computer and use it in GitHub Desktop.
Save banagale/e80a92e0842265bfa18b0d12f9baa368 to your computer and use it in GitHub Desktop.
Footgun blast wagtail out of a django installation
--This script removes all tables that start with 'wagtail' and 'taggit' database.
--Use with great caution, slightly modified https://stackoverflow.com/a/4202280
--If you have an app to support wagtail, such as 'blog', uncomment that line at end.
--You will also need to manually remove all related entries from django_migrations (consider extending this to also remove those) to run migrations and get back to clean install
CREATE OR REPLACE FUNCTION footgun(IN _schema TEXT, IN _parttionbase TEXT)
RETURNS void
LANGUAGE plpgsql
AS
$$
DECLARE
row record;
BEGIN
FOR row IN
SELECT
table_schema,
table_name
FROM
information_schema.tables
WHERE
table_type = 'BASE TABLE'
AND
table_schema = _schema
AND
table_name ILIKE (_parttionbase || '%')
LOOP
EXECUTE 'DROP TABLE ' || quote_ident(row.table_schema) || '.' || quote_ident(row.table_name) || ' CASCADE ';
RAISE INFO 'Dropped table: %', quote_ident(row.table_schema) || '.' || quote_ident(row.table_name);
END LOOP;
END;
$$;
SELECT footgun('public', 'wagtail');
SELECT footgun('public', 'taggit');
--SELECT footgun('public', 'blog');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment