Generate script to create migrate from unpackaged

Usage: CREATE EXTENSION cartodb FROM unpackaged;
This commit is contained in:
Sandro Santilli 2014-05-07 18:06:20 +02:00
parent f56221ca25
commit b3bade740d
2 changed files with 47 additions and 1 deletions

View File

@ -7,7 +7,8 @@ CDBSCRIPTS = \
scripts-enabled/*.sql \
$(END)
DATA_built = $(EXTENSION)--$(EXTVERSION).sql
DATA_built = $(EXTENSION)--$(EXTVERSION).sql \
$(EXTENSION)--unpackaged--$(EXTVERSION).sql
DOCS = README.md
REGRESS_NEW = test_ddl_triggers
REGRESS_OLD = $(wildcard test/*.sql)
@ -26,6 +27,9 @@ $(EXTENSION)--$(EXTVERSION).sql: $(CDBSCRIPTS) cartodb_hooks.sql Makefile
echo "GRANT USAGE ON SCHEMA cartodb TO public;" >> $@
cat cartodb_hooks.sql >> $@
$(EXTENSION)--unpackaged--0.1.sql: $(EXTENSION)--$(EXTVERSION).sql util/create_from_unpackaged.sh Makefile
./util/create_from_unpackaged.sh cartodb--unpackaged--0.1.sql
legacy_regress: $(REGRESS_OLD) Makefile
mkdir -p sql/test/
mkdir -p expected/test/

42
util/create_from_unpackaged.sh Executable file
View File

@ -0,0 +1,42 @@
#!/bin/sh
input=cartodb--0.1.sql
output=cartodb--unpackaged--0.1.sql
cat ${input} > ${output}
# Migrate CDB_TableMetadata
cat >> ${output} <<EOF
ALTER TABLE cartodb.CDB_TableMetadata DISABLE TRIGGER ALL;
INSERT INTO cartodb.CDB_TableMetadata SELECT * FROM public.CDB_TableMetadata;
ALTER TABLE cartodb.CDB_TableMetadata ENABLE TRIGGER ALL;
DROP TABLE public.CDB_TableMetadata;
-- Cartodbfy tables with a trigger using 'CDB_CheckQuota' or
-- 'CDB_TableMetadata_Trigger' from the 'public' schema
select cartodb.CDB_CartodbfyTable(relname::regclass) from (
-- names of tables using public.CDB_CheckQuota or
-- public.CDB_TableMetadata_Trigger in their triggers
SELECT distinct c.relname
FROM
pg_trigger t,
pg_class c,
pg_proc p,
pg_namespace n
WHERE
n.nspname = 'public' AND
p.pronamespace = n.oid AND
p.proname IN ( 'cdb_checkquota', 'cdb_tablemetadata_trigger' ) AND
t.tgrelid = c.oid AND
p.oid = t.tgfoid
) as foo;
EOF
# Drop functions from public schema
cat ${input} |
grep '^ *CREATE OR REPLACE FUNCTION' |
grep -v ' cartodb\.' | # should only match DDL hooks
sed 's/).*$/);/' |
sed 's/DEFAULT [^ ,)]*//g' |
sed 's/CREATE OR REPLACE FUNCTION /DROP FUNCTION public./' |
cat >> ${output}