2019/07/08

Makefile から PostgreSQL の全テーブルを削除する

Makefile から PostgreSQL の全テーブルを削除する

https://stackoverflow.com/questions/3327312/how-can-i-drop-all-the-tables-in-a-postgresql-database https://stackoverflow.com/questions/649246/is-it-possible-to-create-a-multi-line-string-variable-in-a-makefile

define DROP_ALL_TABLES
DO $$$$ DECLARE
    r RECORD;
BEGIN
    -- if the schema you operate on is not "current", you will want to
    -- replace current_schema() in query with 'schematodeletetablesfrom'
    -- *and* update the generate 'DROP...' accordingly.
    FOR r IN (SELECT tablename FROM pg_tables WHERE schemaname = current_schema()) LOOP
        EXECUTE 'DROP TABLE IF EXISTS ' || quote_ident(r.tablename) || ' CASCADE';
    END LOOP;
END $$$$;
endef
export DROP_ALL_TABLES

drop_all_tables:
	echo "$$DROP_ALL_TABLES" | sudo -u postgres psql target_database

0 件のコメント: