Compare commits
87 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b75b492490 | ||
|
|
f9ec3c46ee | ||
|
|
679af1d4a3 | ||
|
|
a9005dc93c | ||
|
|
e81a75b716 | ||
|
|
6d665ab163 | ||
|
|
e5c8015e17 | ||
|
|
f61d07518d | ||
|
|
b5589fdf09 | ||
|
|
de2d3f818e | ||
|
|
9500010a67 | ||
|
|
eff2f8b619 | ||
|
|
f1504fb0b1 | ||
|
|
13140dbc63 | ||
|
|
348b8871f3 | ||
|
|
2bd0b223a6 | ||
|
|
5806ac8f8d | ||
|
|
1c86599f9d | ||
|
|
3c5251e6c6 | ||
|
|
3d595976f6 | ||
|
|
021d922d84 | ||
|
|
82f2e7336b | ||
|
|
1573a199c8 | ||
|
|
558be8b693 | ||
|
|
77182321ff | ||
|
|
b98419aeb8 | ||
|
|
da9b9a640f | ||
|
|
84174c4f71 | ||
|
|
55efde5039 | ||
|
|
8624dd1a00 | ||
|
|
12e90ef6a4 | ||
|
|
625b01ed56 | ||
|
|
5e5471c474 | ||
|
|
d61de906d5 | ||
|
|
91bec69a68 | ||
|
|
50060a2529 | ||
|
|
36dca36927 | ||
|
|
2743b1767b | ||
|
|
b69b2fbdcf | ||
|
|
1815096a37 | ||
|
|
44d32e4db5 | ||
|
|
8dec91de87 | ||
|
|
fb68f225d5 | ||
|
|
9d7de169fb | ||
|
|
9a55b03d6f | ||
|
|
1bb8b85503 | ||
|
|
9afc59da5e | ||
|
|
bc0966c46e | ||
|
|
afcc2498c8 | ||
|
|
81bbfcbda8 | ||
|
|
9dbe854df7 | ||
|
|
5a8b9021c8 | ||
|
|
befa7a10c8 | ||
|
|
f75c256b6e | ||
|
|
d52556c20e | ||
|
|
85afef40d8 | ||
|
|
1692e792a7 | ||
|
|
f8180123eb | ||
|
|
01ae7b8c10 | ||
|
|
edc56e60ee | ||
|
|
f6e0456265 | ||
|
|
1f7b8db532 | ||
|
|
f9f73d2d62 | ||
|
|
fba8d3ab84 | ||
|
|
ca07c81f13 | ||
|
|
9322cecb3d | ||
|
|
e932ec3595 | ||
|
|
3fb1f69eb1 | ||
|
|
e993f83b8d | ||
|
|
651bb8cdb7 | ||
|
|
a92d7d7d37 | ||
|
|
c286727181 | ||
|
|
d762da1ca2 | ||
|
|
a650c951c8 | ||
|
|
9230ffa925 | ||
|
|
ce20009fb2 | ||
|
|
cc9cbdb7a7 | ||
|
|
ac53d6b000 | ||
|
|
f5ad0d0434 | ||
|
|
bfd56f0ea6 | ||
|
|
7483f8d10b | ||
|
|
004c3c5543 | ||
|
|
d1990f1765 | ||
|
|
2601364011 | ||
|
|
85cc74b4f4 | ||
|
|
2ffa13af58 | ||
|
|
09cf368d62 |
@@ -4,8 +4,11 @@ addons:
|
||||
postgresql: 9.3
|
||||
|
||||
before_install:
|
||||
- sudo apt-get update
|
||||
#- sudo apt-get install -q postgresql-9.3-postgis-2.1
|
||||
- sudo apt-get update
|
||||
- sudo apt-get install -q postgresql-server-dev-9.3
|
||||
- sudo apt-get install -q postgresql-plpython-9.3
|
||||
# Install schema_triggers
|
||||
- hg clone https://bitbucket.org/malloclabs/pg_schema_triggers &&
|
||||
cd pg_schema_triggers && make && sudo make install && cd -
|
||||
@@ -18,5 +21,5 @@ before_install:
|
||||
script:
|
||||
- make
|
||||
- sudo make install
|
||||
- PGOPTIONS='-c client_min_messages=NOTICE' make installcheck ||
|
||||
- PGOPTIONS='-c client_min_messages=NOTICE' PGUSER=postgres make installcheck ||
|
||||
{ cat regression.diffs; false; }
|
||||
|
||||
61
CONTRIBUTING
Normal file
61
CONTRIBUTING
Normal file
@@ -0,0 +1,61 @@
|
||||
The development tracker for cartodb-postgresql is on github:
|
||||
http://github.com/cartodb/cartodb-postgresql/
|
||||
|
||||
Bug fixes are best reported as pull requests over there.
|
||||
Features are best discussed on the mailing list:
|
||||
https://groups.google.com/d/forum/cartodb
|
||||
|
||||
Adding features to the extension
|
||||
--------------------------------
|
||||
|
||||
Extension features are coded in scripts found under the
|
||||
"scripts-available" directory. A feature can be a single function
|
||||
or a group of function with a specific scope.
|
||||
|
||||
The "scripts-enabled" directory contains symlinks to the scripts
|
||||
in "scripts-available". Any symlink in that directory is automatically
|
||||
included in the extension. Numbering can be used to enforce the order
|
||||
in which those scripts are loaded.
|
||||
|
||||
Scripts would be best coded in a way to be usable both for creation
|
||||
and upgrade of the objects. This means using CREATE OR REPLACE for
|
||||
the functions, and whatever it takes to check existance of any previous
|
||||
version of objects in other cases.
|
||||
|
||||
When used as an extension (probably always from version 0.2.0 onwards)
|
||||
all the objects will be installed in a "cartodb" schema. Take this into
|
||||
account to fully-qualify internal calls to avoid (possibly dangerous)
|
||||
name clashes.
|
||||
|
||||
Every new feature (as well as bugfixes) should come with a testcase,
|
||||
see next session.
|
||||
|
||||
Writing testcases
|
||||
-----------------
|
||||
|
||||
Tests reside in the test/ directory.
|
||||
You can find information about how to write tests in test/README
|
||||
|
||||
Testing changes live
|
||||
--------------------
|
||||
|
||||
Testing changes made during development requires upgrading
|
||||
the extension into your test database.
|
||||
|
||||
During development the cartodb extension version doesn't change with
|
||||
every commit, so testing latest change requires cheating with PostgreSQL
|
||||
so to enforce re-load of the scripts. To help with cheating, "make install"
|
||||
also installs migration scripts to go from "V" to "V"next and from "V"next
|
||||
to "V". Example to upgrade a 0.2.0dev version:
|
||||
|
||||
```sql
|
||||
ALTER EXTENSION cartodb UPDATE TO '0.2.0devnext';
|
||||
ALTER EXTENSION cartodb UPDATE TO '0.2.0dev';
|
||||
```
|
||||
|
||||
Starting with 0.2.0, the in-place reload can be done with an ad-hoc function:
|
||||
|
||||
```sql
|
||||
SELECT cartodb.cdb_extension_reload();
|
||||
```
|
||||
|
||||
66
Makefile
66
Makefile
@@ -1,19 +1,48 @@
|
||||
# cartodb/Makefile
|
||||
|
||||
EXTENSION = cartodb
|
||||
EXTVERSION = 0.1.0
|
||||
EXTVERSION = 0.3.3
|
||||
|
||||
SED = sed
|
||||
|
||||
CDBSCRIPTS = \
|
||||
scripts-available/CDB_Roles.sql \
|
||||
scripts-enabled/*.sql \
|
||||
scripts-available/CDB_SearchPath.sql \
|
||||
scripts-available/CDB_DDLTriggers.sql \
|
||||
scripts-available/CDB_ExtensionPost.sql \
|
||||
scripts-available/CDB_ExtensionUtils.sql \
|
||||
$(END)
|
||||
|
||||
UPGRADABLE = \
|
||||
unpackaged \
|
||||
0.1.0 \
|
||||
0.1.1 \
|
||||
0.2.0 \
|
||||
0.2.1 \
|
||||
0.3.0 \
|
||||
0.3.0dev \
|
||||
0.3.1 \
|
||||
0.3.2 \
|
||||
$(EXTVERSION)dev \
|
||||
$(EXTVERSION)next \
|
||||
$(END)
|
||||
|
||||
UPGRADES = \
|
||||
$(shell echo $(UPGRADABLE) | \
|
||||
$(SED) 's/^/$(EXTENSION)--/' | \
|
||||
$(SED) 's/$$/--$(EXTVERSION).sql/' | \
|
||||
$(SED) 's/ /--$(EXTVERSION).sql $(EXTENSION)--/g')
|
||||
|
||||
REV=$(shell git describe)
|
||||
GITDIR=$(shell test -d .git && echo '.git' || cat .git | $(SED) 's/^gitdir: //')
|
||||
|
||||
DATA_built = \
|
||||
$(EXTENSION)--$(EXTVERSION).sql \
|
||||
$(EXTENSION)--unpackaged--$(EXTVERSION).sql \
|
||||
$(EXTENSION).control \
|
||||
cartodb_version.sql
|
||||
$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql \
|
||||
$(UPGRADES) \
|
||||
$(EXTENSION).control
|
||||
|
||||
EXTRA_CLEAN = cartodb_version.sql
|
||||
|
||||
DOCS = README.md
|
||||
REGRESS_NEW = test_ddl_triggers
|
||||
@@ -25,24 +54,28 @@ PG_CONFIG = pg_config
|
||||
PGXS := $(shell $(PG_CONFIG) --pgxs)
|
||||
include $(PGXS)
|
||||
|
||||
$(EXTENSION)--$(EXTVERSION).sql: $(CDBSCRIPTS) cartodb_hooks.sql cartodb_version.sql cartodb_config_dump.sql Makefile
|
||||
$(EXTENSION)--$(EXTVERSION).sql: $(CDBSCRIPTS) cartodb_version.sql Makefile
|
||||
echo '\echo Use "CREATE EXTENSION $(EXTENSION)" to load this file. \quit' > $@
|
||||
cat $(CDBSCRIPTS) | \
|
||||
sed -e 's/\<public\./cartodb./g' \
|
||||
$(SED) -e 's/public\./cartodb./g' \
|
||||
-e 's/:DATABASE_USERNAME/cdb_org_admin/g' >> $@
|
||||
echo "GRANT USAGE ON SCHEMA cartodb TO public;" >> $@
|
||||
cat cartodb_hooks.sql >> $@
|
||||
cat cartodb_version.sql >> $@
|
||||
cat cartodb_config_dump.sql >> $@
|
||||
|
||||
$(EXTENSION)--unpackaged--$(EXTVERSION).sql: $(EXTENSION)--$(EXTVERSION).sql util/create_from_unpackaged.sh Makefile
|
||||
./util/create_from_unpackaged.sh $(EXTVERSION)
|
||||
|
||||
$(EXTENSION).control: $(EXTENSION).control.in Makefile
|
||||
sed -e 's/@@VERSION@@/$(EXTVERSION)/' $< > $@
|
||||
$(EXTENSION)--%--$(EXTVERSION).sql: $(EXTENSION)--$(EXTVERSION).sql
|
||||
cp $< $@
|
||||
|
||||
cartodb_version.sql: cartodb_version.sql.in Makefile
|
||||
sed -e 's/@@VERSION@@/$(EXTVERSION)/' $< > $@
|
||||
$(EXTENSION)--$(EXTVERSION)--$(EXTVERSION)next.sql: $(EXTENSION)--$(EXTVERSION).sql
|
||||
cp $< $@
|
||||
|
||||
$(EXTENSION).control: $(EXTENSION).control.in Makefile
|
||||
$(SED) -e 's/@@VERSION@@/$(EXTVERSION)/' $< > $@
|
||||
|
||||
cartodb_version.sql: cartodb_version.sql.in Makefile $(GITDIR)/index
|
||||
$(SED) -e 's/@@VERSION@@/$(EXTVERSION) $(REV)/' $< > $@
|
||||
|
||||
legacy_regress: $(REGRESS_OLD) Makefile
|
||||
mkdir -p sql/test/
|
||||
@@ -56,13 +89,16 @@ legacy_regress: $(REGRESS_OLD) Makefile
|
||||
echo '\\t' >> $${of}; \
|
||||
echo '\\set QUIET off' >> $${of}; \
|
||||
cat $${f} | \
|
||||
sed -e 's/\<public\./cartodb./g' >> $${of}; \
|
||||
$(SED) -e 's/public\./cartodb./g' >> $${of}; \
|
||||
exp=expected/test/$${tn}.out; \
|
||||
echo '\\set ECHO off' > $${exp}; \
|
||||
cat test/$${tn}_expect >> $${exp}; \
|
||||
done
|
||||
|
||||
test_organization:
|
||||
bash test/organization/test.sh
|
||||
|
||||
legacy_tests: legacy_regress
|
||||
|
||||
installcheck: legacy_tests
|
||||
installcheck: legacy_tests test_organization
|
||||
|
||||
|
||||
61
NEWS
61
NEWS
@@ -1,3 +1,64 @@
|
||||
0.3.3 (2014-07-xx)
|
||||
------------------
|
||||
* Splitting of CartodbfyTable method in subfunctions to be able to call in fragments and evade timeouts on hot zones
|
||||
|
||||
0.3.2 (2014-07-28)
|
||||
------------------
|
||||
* Make 0.3.0dev version upgradeable
|
||||
|
||||
0.3.1 (2014-07-22)
|
||||
------------------
|
||||
* Dummy version. We start using semantic versioning
|
||||
|
||||
0.3.0 (2014-07-15)
|
||||
------------------
|
||||
* Permission management functions
|
||||
* Adapt functions to use schemas
|
||||
|
||||
0.2.1 - 2014-06-11
|
||||
------------------
|
||||
|
||||
Enhancements:
|
||||
|
||||
- Do not force re-cartodbfication on CREATE FROM unpackaged
|
||||
- Drop useless DEFAULT specification in plpgsql variable declarations
|
||||
- List plpythonu requirement first, to get pg_catalog scanned before public
|
||||
|
||||
Bug fixes:
|
||||
|
||||
- Do not add unique index on cartodb_id if already a primary key (#38)
|
||||
|
||||
0.2.0 - 2014-06-09
|
||||
------------------
|
||||
|
||||
Important changes:
|
||||
|
||||
- This release adds dependency on "plpythonu" extension
|
||||
- Roles are not created anymore, previously private functions
|
||||
for table information extraction (CDB_UserTables, CDB_TableIndexes,
|
||||
CDB_ColumnNames, CDB_ColumnType) will now be callable by anyone while
|
||||
only returning information about tables over which the calling user
|
||||
has SELECT privilege (#36)
|
||||
|
||||
Bug fixes:
|
||||
|
||||
- Fix recursive trigger on create table (#32)
|
||||
- Ensure cartodb_id uses an associated sequence (#33)
|
||||
- Fully qualify call to cdb_disable_ddl_hooks from cdb_enable_ddl_hooks
|
||||
- Fully qualify call to CDB_UserDataSize from quota trigger
|
||||
- Fully qualify call to CDB_TransformToWebmercator from CDB_CartodbfyTable
|
||||
- Fix potential infinite loop in CDB_CartodbfyTable
|
||||
- Fix potential infinite loop in CDB_QueryStatements
|
||||
|
||||
Enhancements:
|
||||
|
||||
- Include revision info in cdb_version() output (#34)
|
||||
|
||||
New features:
|
||||
|
||||
- Add a cdb_extension_reload() function
|
||||
|
||||
|
||||
0.1.0 - 2014-05-23
|
||||
------------------
|
||||
|
||||
|
||||
38
README.md
38
README.md
@@ -11,9 +11,11 @@ See https://github.com/CartoDB/cartodb/wiki/CartoDB-PostgreSQL-extension
|
||||
Dependencies
|
||||
------------
|
||||
|
||||
* PostgreSQL 9.3+
|
||||
* PostgreSQL 9.3+ (with plpythonu extension)
|
||||
* [PostGIS extension](http://postgis.net)
|
||||
* [Schema triggers extension]
|
||||
(https://bitbucket.org/malloclabs/pg_schema_triggers)
|
||||
(or [fork](https://github.com/CartoDB/pg_schema_triggers))
|
||||
|
||||
Install
|
||||
-------
|
||||
@@ -29,6 +31,9 @@ NOTE: if ``test_ddl_triggers`` fails it's likely due to an incomplete
|
||||
installation of schema_triggers: you need to add ``schema_triggers.so``
|
||||
to the ``shared_preload_libraries`` setting in postgresql.conf !
|
||||
|
||||
NOTE: you need to run the installcheck as a superuser, use PGUSER
|
||||
env variable if needed, like: PGUSER=postgres make installcheck
|
||||
|
||||
Enable database
|
||||
---------------
|
||||
|
||||
@@ -56,3 +61,34 @@ CREATE EXTENSION schema_triggers;
|
||||
CREATE EXTENSION cartodb FROM unpackaged;
|
||||
```
|
||||
|
||||
Update cartodb extension
|
||||
------------------------
|
||||
|
||||
Updating the version of cartodb extension installed in a database
|
||||
is done using ALTER EXTENSION.
|
||||
|
||||
```sql
|
||||
ALTER EXTENSION cartodb UPDATE TO '0.1.1';
|
||||
```
|
||||
|
||||
The target version needs to be installed on the system first
|
||||
(see Install section).
|
||||
|
||||
If the "TO 'x.y.z'" part is omitted, the extension will be updated to the
|
||||
latest installed version, which you can find with the following command:
|
||||
|
||||
```sh
|
||||
grep default_version `pg_config --sharedir`/extension/cartodb.control
|
||||
```
|
||||
|
||||
Updates are performed by PostgreSQL by loading one or more migration scripts
|
||||
as needed to go from the installed version S to the target version T.
|
||||
All migration scripts are in the "extension" directory of PostgreSQL:
|
||||
|
||||
```sh
|
||||
ls `pg_config --sharedir`/extension/cartodb*
|
||||
```
|
||||
|
||||
During development the cartodb extension version doesn't change with
|
||||
every commit, so testing latest change requires special steps documented
|
||||
in the CONTRIBUTING document, under "Testing changes live".
|
||||
|
||||
@@ -3,4 +3,4 @@ comment = 'Turn a database into a cartodb user database.'
|
||||
superuser = true
|
||||
relocatable = false
|
||||
schema = cartodb
|
||||
requires = 'schema_triggers, postgis'
|
||||
requires = 'plpythonu, schema_triggers, postgis'
|
||||
|
||||
@@ -32,6 +32,11 @@ NOTICE: trigger "update_the_geom_webmercator_trigger" for table "c.t3" does not
|
||||
NOTICE: trigger "update_updated_at_trigger" for table "c.t3" does not exist, skipping
|
||||
NOTICE: trigger "test_quota" for table "c.t3" does not exist, skipping
|
||||
NOTICE: trigger "test_quota_per_row" for table "c.t3" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_relation_create" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_relation_drop" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_alter_column" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_drop_column" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_add_column" does not exist, skipping
|
||||
NOTICE: cdb_invalidate_varnish(c.t3) called
|
||||
select
|
||||
cartodb_id, created_at=updated_at as "c=u",
|
||||
@@ -47,12 +52,48 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)) as age
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
tabname | age
|
||||
---------+-----
|
||||
c.t3 | 0
|
||||
(1 row)
|
||||
|
||||
-- Table with cartodb_id field, see
|
||||
-- http://github.com/CartoDB/cartodb-postgresql/issues/32
|
||||
select 1 as cartodb_id INTO c.t4;
|
||||
NOTICE: trigger "track_updates" for table "c.t4" does not exist, skipping
|
||||
NOTICE: trigger "update_the_geom_webmercator_trigger" for table "c.t4" does not exist, skipping
|
||||
NOTICE: trigger "update_updated_at_trigger" for table "c.t4" does not exist, skipping
|
||||
NOTICE: trigger "test_quota" for table "c.t4" does not exist, skipping
|
||||
NOTICE: trigger "test_quota_per_row" for table "c.t4" does not exist, skipping
|
||||
NOTICE: Column cartodb_id already exists
|
||||
NOTICE: Existing cartodb_id field does not have an associated sequence, renaming
|
||||
NOTICE: Trying to recover data from _cartodb_id0 column
|
||||
NOTICE: event trigger "cdb_on_relation_create" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_relation_drop" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_alter_column" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_drop_column" does not exist, skipping
|
||||
NOTICE: event trigger "cdb_on_add_column" does not exist, skipping
|
||||
NOTICE: cdb_invalidate_varnish(c.t4) called
|
||||
select
|
||||
cartodb_id, created_at=updated_at as "c=u",
|
||||
NOW() - updated_at < '1 secs' as "u<1s",
|
||||
the_geom, the_geom_webmercator
|
||||
from c.t4;
|
||||
cartodb_id | c=u | u<1s | the_geom | the_geom_webmercator
|
||||
------------+-----+------+----------+----------------------
|
||||
1 | t | t | |
|
||||
(1 row)
|
||||
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)) as age
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t4'::regclass;
|
||||
tabname | age
|
||||
---------+-----
|
||||
c.t4 | 0
|
||||
(1 row)
|
||||
|
||||
----------------------------
|
||||
-- ALTER TABLE RENAME COLUMN
|
||||
----------------------------
|
||||
@@ -87,7 +128,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
tabname | agecs
|
||||
---------+-------
|
||||
c.t3 | 0
|
||||
@@ -124,7 +165,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
tabname | agecs
|
||||
---------+-------
|
||||
c.t3 | 0
|
||||
@@ -164,7 +205,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
tabname | agecs
|
||||
---------+-------
|
||||
c.t3 | 0
|
||||
@@ -195,7 +236,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
tabname | agecs
|
||||
---------+-------
|
||||
c.t3 | 0
|
||||
@@ -206,8 +247,8 @@ FROM CDB_TableMetadata;
|
||||
----------------------------
|
||||
RESET SESSION AUTHORIZATION;
|
||||
drop schema c cascade;
|
||||
NOTICE: drop cascades to table c.t3
|
||||
select count(*) from CDB_TableMetadata;
|
||||
NOTICE: drop cascades to 2 other objects
|
||||
select count(*) from CDB_TableMetadata;
|
||||
count
|
||||
-------
|
||||
0
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
CREATE EXTENSION postgis;
|
||||
CREATE EXTENSION schema_triggers;
|
||||
CREATE EXTENSION plpythonu;
|
||||
CREATE EXTENSION cartodb;
|
||||
CREATE FUNCTION public.cdb_invalidate_varnish(table_name text)
|
||||
RETURNS void AS $$
|
||||
|
||||
@@ -6,49 +6,35 @@
|
||||
-- (user.rebuild_quota_trigger, called by rake task
|
||||
-- cartodb:db:update_test_quota_trigger)
|
||||
|
||||
-- Update the_geom_webmercator
|
||||
CREATE OR REPLACE FUNCTION _CDB_update_the_geom_webmercator()
|
||||
RETURNS trigger AS $$
|
||||
BEGIN
|
||||
NEW.the_geom_webmercator := CDB_TransformToWebmercator(NEW.the_geom);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
CREATE OR REPLACE FUNCTION _CDB_update_updated_at()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at := now();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
-- Ensure a table is a "cartodb" table
|
||||
-- See https://github.com/CartoDB/cartodb/wiki/CartoDB-user-table
|
||||
CREATE OR REPLACE FUNCTION CDB_CartodbfyTable(reloid REGCLASS)
|
||||
RETURNS void
|
||||
-- 1) Required checks before running cartodbfication
|
||||
-- Either will pass silenty or raise an exception
|
||||
CREATE OR REPLACE FUNCTION _CDB_check_prerequisites(schema_name TEXT, reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
rec RECORD;
|
||||
rec2 RECORD;
|
||||
tabinfo RECORD;
|
||||
had_column BOOLEAN;
|
||||
i INTEGER;
|
||||
new_name TEXT;
|
||||
quota_in_bytes INT8;
|
||||
exists_geom_cols BOOLEAN[];
|
||||
BEGIN
|
||||
IF cartodb.schema_exists(schema_name) = false THEN
|
||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
||||
END IF;
|
||||
|
||||
-- TODO: Check that user quota is set ?
|
||||
BEGIN
|
||||
PERFORM public._CDB_UserQuotaInBytes();
|
||||
EXCEPTION WHEN undefined_function THEN
|
||||
EXECUTE FORMAT('SELECT %I._CDB_UserQuotaInBytes();', schema_name::text) INTO sql;
|
||||
EXCEPTION WHEN undefined_function THEN
|
||||
RAISE EXCEPTION 'Please set user quota before cartodbfying tables.';
|
||||
END;
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
-- Drop cartodb triggers (might prevent changing columns)
|
||||
|
||||
-- 2) Drop cartodb triggers (might prevent changing columns)
|
||||
CREATE OR REPLACE FUNCTION _CDB_drop_triggers(reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
BEGIN
|
||||
-- "track_updates"
|
||||
sql := 'DROP TRIGGER IF EXISTS track_updates ON ' || reloid::text;
|
||||
EXECUTE sql;
|
||||
@@ -66,10 +52,22 @@ BEGIN
|
||||
EXECUTE sql;
|
||||
sql := 'DROP TRIGGER IF EXISTS test_quota_per_row ON ' || reloid::text;
|
||||
EXECUTE sql;
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
-- Ensure required fields exist
|
||||
|
||||
-- We need a cartodb_id column
|
||||
-- 3) Cartodb_id creation & validation or renaming if invalid
|
||||
CREATE OR REPLACE FUNCTION _CDB_create_cartodb_id_column(reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
rec RECORD;
|
||||
rec2 RECORD;
|
||||
had_column BOOLEAN;
|
||||
i INTEGER;
|
||||
new_name TEXT;
|
||||
BEGIN
|
||||
<< cartodb_id_setup >>
|
||||
LOOP --{
|
||||
had_column := FALSE;
|
||||
@@ -78,32 +76,38 @@ BEGIN
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXIT cartodb_id_setup;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
RAISE NOTICE 'Column cartodb_id already exists';
|
||||
had_column := TRUE;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (cartodb_id): % (%)',
|
||||
reloid, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
RAISE NOTICE 'Column cartodb_id already exists';
|
||||
had_column := TRUE;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (cartodb_id): % (%)', reloid, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
|
||||
IF had_column THEN
|
||||
SELECT pg_catalog.pg_get_serial_sequence(reloid::text, 'cartodb_id')
|
||||
AS seq INTO rec2;
|
||||
|
||||
-- Check data type is an integer
|
||||
SELECT t.typname, t.oid, a.attnotnull FROM pg_type t, pg_attribute a
|
||||
WHERE a.atttypid = t.oid AND a.attrelid = reloid AND NOT a.attisdropped
|
||||
AND a.attname = 'cartodb_id'
|
||||
SELECT
|
||||
pg_catalog.pg_get_serial_sequence(reloid::text, 'cartodb_id') as seq,
|
||||
t.typname, t.oid, a.attnotnull FROM pg_type t, pg_attribute a
|
||||
WHERE a.atttypid = t.oid AND a.attrelid = reloid AND NOT a.attisdropped AND a.attname = 'cartodb_id'
|
||||
INTO STRICT rec;
|
||||
IF rec.oid NOT IN (20,21,23) THEN -- int2, int4, int8 {
|
||||
|
||||
-- 20=int2, 21=int4, 23=int8
|
||||
IF rec.oid NOT IN (20,21,23) THEN -- {
|
||||
RAISE NOTICE 'Existing cartodb_id field is of invalid type % (need int2, int4 or int8), renaming', rec.typname;
|
||||
ELSIF rec.seq IS NULL THEN -- }{
|
||||
RAISE NOTICE 'Existing cartodb_id field does not have an associated sequence, renaming';
|
||||
ELSE -- }{
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' ALTER COLUMN cartodb_id SET NOT NULL';
|
||||
IF NOT EXISTS ( SELECT c.conname FROM pg_constraint c, pg_attribute a
|
||||
WHERE c.conkey = ARRAY[a.attnum] AND c.conrelid = reloid
|
||||
AND a.attrelid = reloid
|
||||
AND NOT a.attisdropped
|
||||
AND a.attname = 'cartodb_id'
|
||||
AND c.contype = 'u' ) -- unique
|
||||
WHERE c.conkey = ARRAY[a.attnum] AND c.conrelid = reloid
|
||||
AND a.attrelid = reloid
|
||||
AND NOT a.attisdropped
|
||||
AND a.attname = 'cartodb_id'
|
||||
AND c.contype IN ( 'u', 'p' ) ) -- unique or pkey
|
||||
THEN
|
||||
sql := sql || ', ADD unique(cartodb_id)';
|
||||
END IF;
|
||||
@@ -111,16 +115,14 @@ BEGIN
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXIT cartodb_id_setup;
|
||||
EXCEPTION
|
||||
WHEN unique_violation OR not_null_violation THEN
|
||||
RAISE NOTICE '%, renaming', SQLERRM;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (cartodb_id): % (%)',
|
||||
reloid, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN unique_violation OR not_null_violation THEN
|
||||
RAISE NOTICE '%, renaming', SQLERRM;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (cartodb_id): % (%)', reloid, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
END IF; -- }
|
||||
|
||||
|
||||
-- invalid column, need rename and re-create it
|
||||
i := 0;
|
||||
<< rename_column >>
|
||||
@@ -130,28 +132,29 @@ BEGIN
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' RENAME COLUMN cartodb_id TO ' || new_name;
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
i := i+1;
|
||||
CONTINUE rename_column;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (renaming cartodb_id): % (%)',
|
||||
reloid, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
i := i+1;
|
||||
CONTINUE rename_column;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (renaming cartodb_id): % (%)', reloid, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
EXIT rename_column;
|
||||
END LOOP; --}
|
||||
CONTINUE cartodb_id_setup;
|
||||
|
||||
END IF;
|
||||
END LOOP; -- }
|
||||
|
||||
-- Try to copy data from new name if possible
|
||||
-- Try to copy data from new name if possible
|
||||
IF new_name IS NOT NULL THEN
|
||||
RAISE NOTICE 'Trying to recover data from % coumn', new_name;
|
||||
RAISE NOTICE 'Trying to recover data from % column', new_name;
|
||||
BEGIN
|
||||
-- Copy existing values to new field
|
||||
sql := 'UPDATE ' || reloid::text || ' SET cartodb_id = '
|
||||
|| new_name || '::int4';
|
||||
-- NOTE: using ALTER is a workaround to a PostgreSQL bug and is also known to be faster for tables with many rows
|
||||
-- See http://www.postgresql.org/message-id/20140530143150.GA11051@localhost
|
||||
sql := 'ALTER TABLE ' || reloid::text
|
||||
|| ' ALTER cartodb_id TYPE int USING '
|
||||
|| new_name || '::int4';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
|
||||
@@ -166,72 +169,84 @@ BEGIN
|
||||
|
||||
-- Reset sequence name
|
||||
sql := 'ALTER SEQUENCE ' || rec2.seq::text
|
||||
|| ' RESTART WITH ' || rec.max + 1;
|
||||
|| ' RESTART WITH ' || rec.max + 1;
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
|
||||
-- Drop old column (all went find if we got here)
|
||||
-- Drop old column (all went fine if we got here)
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' DROP ' || new_name;
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE NOTICE 'Could not initialize cartodb_id with existing values: % (%)',
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE NOTICE 'Could not initialize cartodb_id with existing values: % (%)',
|
||||
SQLERRM, SQLSTATE;
|
||||
END;
|
||||
END IF;
|
||||
|
||||
-- We need created_at and updated_at
|
||||
FOR rec IN SELECT * FROM ( VALUES ('created_at'), ('updated_at') ) t(cname) LOOP --{
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
|
||||
-- 4) created_at and updated_at creation & validation or renaming if invalid
|
||||
CREATE OR REPLACE FUNCTION _CDB_create_timestamp_columns(reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
rec RECORD;
|
||||
rec2 RECORD;
|
||||
had_column BOOLEAN;
|
||||
i INTEGER;
|
||||
new_name TEXT;
|
||||
BEGIN
|
||||
|
||||
FOR rec IN SELECT * FROM ( VALUES ('created_at'), ('updated_at') ) t(cname)
|
||||
LOOP --{
|
||||
new_name := null;
|
||||
<< column_setup >>
|
||||
LOOP --{
|
||||
had_column := FALSE;
|
||||
BEGIN
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' ADD ' || rec.cname
|
||||
|| ' TIMESTAMPTZ NOT NULL DEFAULT now()';
|
||||
|| ' TIMESTAMPTZ NOT NULL DEFAULT now()';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXIT column_setup;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
RAISE NOTICE 'Column % already exists', rec.cname;
|
||||
had_column := TRUE;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (%): % (%)',
|
||||
reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
RAISE NOTICE 'Column % already exists', rec.cname;
|
||||
had_column := TRUE;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (%): % (%)', reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
|
||||
IF had_column THEN
|
||||
|
||||
-- Check data type is a TIMESTAMP WITH TIMEZONE
|
||||
SELECT t.typname, t.oid, a.attnotnull FROM pg_type t, pg_attribute a
|
||||
WHERE a.atttypid = t.oid AND a.attrelid = reloid
|
||||
AND NOT a.attisdropped AND a.attname = rec.cname
|
||||
WHERE a.atttypid = t.oid AND a.attrelid = reloid AND NOT a.attisdropped AND a.attname = rec.cname
|
||||
INTO STRICT rec2;
|
||||
IF rec2.oid NOT IN (1184) THEN -- timestamptz {
|
||||
RAISE NOTICE 'Existing % field is of invalid type % (need timestamptz), renaming', rec.
|
||||
cname, rec2.typname;
|
||||
RAISE NOTICE 'Existing % field is of invalid type % (need timestamptz), renaming', rec.cname, rec2.typname;
|
||||
ELSE -- }{
|
||||
-- Ensure data type is a TIMESTAMP WITH TIMEZONE
|
||||
sql := 'ALTER TABLE ' || reloid::text
|
||||
|| ' ALTER ' || rec.cname
|
||||
|| ' SET NOT NULL,'
|
||||
|| ' ALTER ' || rec.cname
|
||||
|| ' SET DEFAULT now()';
|
||||
|| ' ALTER ' || rec.cname
|
||||
|| ' SET NOT NULL,'
|
||||
|| ' ALTER ' || rec.cname
|
||||
|| ' SET DEFAULT now()';
|
||||
BEGIN
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXIT column_setup;
|
||||
EXCEPTION
|
||||
WHEN not_null_violation THEN -- failed not-null
|
||||
RAISE NOTICE '%, renaming', SQLERRM;
|
||||
WHEN cannot_coerce THEN -- failed cast
|
||||
RAISE NOTICE '%, renaming', SQLERRM;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (%): % (%)',
|
||||
reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN not_null_violation THEN -- failed not-null
|
||||
RAISE NOTICE '%, renaming', SQLERRM;
|
||||
WHEN cannot_coerce THEN -- failed cast
|
||||
RAISE NOTICE '%, renaming', SQLERRM;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (%): % (%)', reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
END IF; -- }
|
||||
|
||||
@@ -244,18 +259,17 @@ cname, rec2.typname;
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' RENAME COLUMN ' || rec.cname || ' TO ' || new_name;
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
i := i+1;
|
||||
CONTINUE rename_column;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (renaming %): % (%)',
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
i := i+1;
|
||||
CONTINUE rename_column;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (renaming %): % (%)',
|
||||
reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
EXIT rename_column;
|
||||
END LOOP; --}
|
||||
CONTINUE column_setup;
|
||||
|
||||
END IF;
|
||||
END LOOP; -- }
|
||||
|
||||
@@ -264,8 +278,11 @@ cname, rec2.typname;
|
||||
RAISE NOTICE 'Trying to recover data from % coumn', new_name;
|
||||
BEGIN
|
||||
-- Copy existing values to new field
|
||||
sql := 'UPDATE ' || reloid::text || ' SET ' || rec.cname || ' = '
|
||||
|| new_name || '::timestamptz';
|
||||
-- NOTE: using ALTER is a workaround to a PostgreSQL bug and is also known to be faster for tables with many rows
|
||||
-- See http://www.postgresql.org/message-id/20140530143150.GA11051@localhost
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' ALTER ' || rec.cname
|
||||
|| ' TYPE TIMESTAMPTZ USING '
|
||||
|| new_name || '::timestamptz';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
|
||||
@@ -274,21 +291,36 @@ cname, rec2.typname;
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE NOTICE 'Could not initialize % with existing values: % (%)',
|
||||
rec.cname, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE NOTICE 'Could not initialize % with existing values: % (%)', rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
END IF; -- }
|
||||
|
||||
END LOOP; -- }
|
||||
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
|
||||
-- 5) the_geom and the_geom_webmercator creation & validation or renaming if invalid
|
||||
CREATE OR REPLACE FUNCTION _CDB_create_the_geom_columns(reloid REGCLASS)
|
||||
RETURNS BOOLEAN[]
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
rec RECORD;
|
||||
rec2 RECORD;
|
||||
had_column BOOLEAN;
|
||||
i INTEGER;
|
||||
new_name TEXT;
|
||||
exists_geom_cols BOOLEAN[];
|
||||
BEGIN
|
||||
-- We need the_geom and the_geom_webmercator
|
||||
FOR rec IN SELECT * FROM ( VALUES ('the_geom',4326), ('the_geom_webmercator',3857) ) t(cname,csrid) LOOP --{
|
||||
<< column_setup >> LOOP --{
|
||||
BEGIN
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' ADD ' || rec.cname
|
||||
|| ' GEOMETRY(geometry,' || rec.csrid || ')';
|
||||
|| ' GEOMETRY(geometry,' || rec.csrid || ')';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
sql := 'CREATE INDEX ON ' || reloid::text || ' USING GIST ( ' || rec.cname || ')';
|
||||
@@ -296,25 +328,22 @@ cname, rec2.typname;
|
||||
EXECUTE sql;
|
||||
exists_geom_cols := array_append(exists_geom_cols, false);
|
||||
EXIT column_setup;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
exists_geom_cols := array_append(exists_geom_cols, true);
|
||||
RAISE NOTICE 'Column % already exists', rec.cname;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (%): % (%)',
|
||||
reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
exists_geom_cols := array_append(exists_geom_cols, true);
|
||||
RAISE NOTICE 'Column % already exists', rec.cname;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (%): % (%)', reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
|
||||
<< column_fixup >>
|
||||
LOOP --{
|
||||
|
||||
-- Check data type is a GEOMETRY
|
||||
SELECT t.typname, t.oid, a.attnotnull,
|
||||
postgis_typmod_srid(a.atttypmod) as srid,
|
||||
postgis_typmod_type(a.atttypmod) as gtype
|
||||
FROM pg_type t, pg_attribute a
|
||||
WHERE a.atttypid = t.oid AND a.attrelid = reloid AND NOT a.attisdropped
|
||||
AND a.attname = rec.cname
|
||||
postgis_typmod_srid(a.atttypmod) as srid,
|
||||
postgis_typmod_type(a.atttypmod) as gtype
|
||||
FROM pg_type t, pg_attribute a
|
||||
WHERE a.atttypid = t.oid AND a.attrelid = reloid AND NOT a.attisdropped AND a.attname = rec.cname
|
||||
INTO STRICT rec2;
|
||||
|
||||
IF rec2.typname NOT IN ('geometry') THEN -- {
|
||||
@@ -323,46 +352,44 @@ cname, rec2.typname;
|
||||
END IF; -- }
|
||||
|
||||
IF rec2.srid != rec.csrid THEN -- {
|
||||
BEGIN
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' ALTER ' || rec.cname
|
||||
|| ' TYPE geometry(' || rec2.gtype || ',' || rec.csrid || ') USING ST_Transform('
|
||||
|| rec.cname || ',' || rec.csrid || ')';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
BEGIN
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' ALTER ' || rec.cname
|
||||
|| ' TYPE geometry(' || rec2.gtype || ',' || rec.csrid || ') USING ST_Transform('
|
||||
|| rec.cname || ',' || rec.csrid || ')';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE NOTICE 'Could not enforce SRID % to column %: %, renaming', rec.csrid, rec.cname, SQLERRM;
|
||||
EXIT column_fixup; -- cannot fix, will rename
|
||||
END;
|
||||
END;
|
||||
END IF; -- }
|
||||
|
||||
-- add gist indices if not there already
|
||||
IF NOT EXISTS ( SELECT ir.relname
|
||||
FROM pg_am am, pg_class ir,
|
||||
pg_class c, pg_index i,
|
||||
pg_attribute a
|
||||
pg_class c, pg_index i,
|
||||
pg_attribute a
|
||||
WHERE c.oid = reloid AND i.indrelid = c.oid
|
||||
AND a.attname = rec.cname
|
||||
AND i.indexrelid = ir.oid AND i.indnatts = 1
|
||||
AND i.indkey[0] = a.attnum AND a.attrelid = c.oid
|
||||
AND NOT a.attisdropped AND am.oid = ir.relam
|
||||
AND am.amname = 'gist' )
|
||||
AND a.attname = rec.cname
|
||||
AND i.indexrelid = ir.oid AND i.indnatts = 1
|
||||
AND i.indkey[0] = a.attnum AND a.attrelid = c.oid
|
||||
AND NOT a.attisdropped AND am.oid = ir.relam
|
||||
AND am.amname = 'gist' )
|
||||
THEN -- {
|
||||
BEGIN
|
||||
sql := 'CREATE INDEX ON ' || reloid::text || ' USING GIST ( ' || rec.cname || ')';
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (% index): % (%)',
|
||||
reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (% index): % (%)', reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
END IF; -- }
|
||||
|
||||
-- if we reached this line, all went good
|
||||
EXIT column_setup;
|
||||
|
||||
END LOOP; -- } column_fixup
|
||||
END LOOP; -- } column_fixup
|
||||
|
||||
-- invalid column, need rename and re-create it
|
||||
i := 0;
|
||||
@@ -373,73 +400,155 @@ cname, rec2.typname;
|
||||
sql := 'ALTER TABLE ' || reloid::text || ' RENAME COLUMN ' || rec.cname || ' TO ' || new_name;
|
||||
RAISE DEBUG 'Running %', sql;
|
||||
EXECUTE sql;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
i := i+1;
|
||||
CONTINUE rename_column;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (rename %): % (%)',
|
||||
reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
EXCEPTION
|
||||
WHEN duplicate_column THEN
|
||||
i := i+1;
|
||||
CONTINUE rename_column;
|
||||
WHEN others THEN
|
||||
RAISE EXCEPTION 'Cartodbfying % (rename %): % (%)', reloid, rec.cname, SQLERRM, SQLSTATE;
|
||||
END;
|
||||
EXIT rename_column;
|
||||
END LOOP; --}
|
||||
CONTINUE column_setup;
|
||||
|
||||
END LOOP; -- } column_setup
|
||||
|
||||
END LOOP; -- } column_setup
|
||||
END LOOP; -- } on expected geometry columns
|
||||
|
||||
-- Initialize the_geom with values from the_geom_webmercator
|
||||
-- do this only if the_geom_webmercator was found (not created)
|
||||
-- _and_ the_geom was NOT found.
|
||||
IF exists_geom_cols[2] AND NOT exists_geom_cols[1] THEN
|
||||
RETURN exists_geom_cols;
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
|
||||
-- 6) Initialize the_geom with values from the_geom_webmercator
|
||||
-- do this only if the_geom_webmercator was found (not created) and the_geom was NOT found.
|
||||
CREATE OR REPLACE FUNCTION _CDB_populate_the_geom_from_the_geom_webmercator(reloid REGCLASS, geom_columns_exist BOOLEAN[])
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
BEGIN
|
||||
IF geom_columns_exist[2] AND NOT geom_columns_exist[1] THEN
|
||||
sql := 'UPDATE ' || reloid::text || ' SET the_geom = ST_Transform(the_geom_webmercator, 4326) ';
|
||||
EXECUTE sql;
|
||||
END IF;
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
-- Initialize the_geom_webmercator with values from the_geom
|
||||
-- do this only if the_geom was found (not created)
|
||||
-- _and_ the_geom_webmercator was NOT found.
|
||||
IF exists_geom_cols[1] AND NOT exists_geom_cols[2] THEN
|
||||
sql := 'UPDATE ' || reloid::text || ' SET the_geom_webmercator = CDB_TransformToWebmercator(the_geom) ';
|
||||
|
||||
-- 7) Initialize the_geom_webmercator with values from the_geom
|
||||
-- do this only if the_geom was found (not created) and the_geom_webmercator was NOT found.
|
||||
CREATE OR REPLACE FUNCTION _CDB_populate_the_geom_webmercator_from_the_geom(reloid REGCLASS, geom_columns_exist BOOLEAN[])
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
BEGIN
|
||||
IF geom_columns_exist[1] AND NOT geom_columns_exist[2] THEN
|
||||
sql := 'UPDATE ' || reloid::text || ' SET the_geom_webmercator = public.CDB_TransformToWebmercator(the_geom) ';
|
||||
EXECUTE sql;
|
||||
END IF;
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
-- Re-create all triggers
|
||||
|
||||
-- NOTE: drop/create has the side-effect of re-enabling disabled triggers
|
||||
|
||||
-- "track_updates"
|
||||
-- 8) Create all triggers
|
||||
-- NOTE: drop/create has the side-effect of re-enabling disabled triggers
|
||||
CREATE OR REPLACE FUNCTION _CDB_create_triggers(schema_name TEXT, reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
sql TEXT;
|
||||
BEGIN
|
||||
-- "track_updates"
|
||||
sql := 'CREATE trigger track_updates AFTER INSERT OR UPDATE OR DELETE OR TRUNCATE ON '
|
||||
|| reloid::text
|
||||
|| ' FOR EACH STATEMENT EXECUTE PROCEDURE public.cdb_tablemetadata_trigger()';
|
||||
|| reloid::text
|
||||
|| ' FOR EACH STATEMENT EXECUTE PROCEDURE public.cdb_tablemetadata_trigger()';
|
||||
EXECUTE sql;
|
||||
|
||||
-- "update_the_geom_webmercator"
|
||||
-- TODO: why _before_ and not after ?
|
||||
-- "update_the_geom_webmercator"
|
||||
-- TODO: why _before_ and not after ?
|
||||
sql := 'CREATE trigger update_the_geom_webmercator_trigger BEFORE INSERT OR UPDATE OF the_geom ON '
|
||||
|| reloid::text
|
||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public._CDB_update_the_geom_webmercator()';
|
||||
|| reloid::text
|
||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public._CDB_update_the_geom_webmercator()';
|
||||
EXECUTE sql;
|
||||
|
||||
-- "update_updated_at"
|
||||
-- TODO: why _before_ and not after ?
|
||||
-- "update_updated_at"
|
||||
-- TODO: why _before_ and not after ?
|
||||
sql := 'CREATE trigger update_updated_at_trigger BEFORE UPDATE ON '
|
||||
|| reloid::text
|
||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public._CDB_update_updated_at()';
|
||||
|| reloid::text
|
||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public._CDB_update_updated_at()';
|
||||
EXECUTE sql;
|
||||
|
||||
-- "test_quota" and "test_quota_per_row"
|
||||
-- "test_quota" and "test_quota_per_row"
|
||||
|
||||
sql := 'CREATE TRIGGER test_quota BEFORE UPDATE OR INSERT ON '
|
||||
|| reloid::text
|
||||
|| ' EXECUTE PROCEDURE public.CDB_CheckQuota(1)';
|
||||
|| reloid::text
|
||||
|| ' EXECUTE PROCEDURE public.CDB_CheckQuota(1, ''-1'', '''
|
||||
|| schema_name::text
|
||||
|| ''')';
|
||||
EXECUTE sql;
|
||||
|
||||
sql := 'CREATE TRIGGER test_quota_per_row BEFORE UPDATE OR INSERT ON '
|
||||
|| reloid::text
|
||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public.CDB_CheckQuota(0.001)';
|
||||
|| reloid::text
|
||||
|| ' FOR EACH ROW EXECUTE PROCEDURE public.CDB_CheckQuota(0.001, ''-1'', '''
|
||||
|| schema_name::text
|
||||
|| ''')';
|
||||
EXECUTE sql;
|
||||
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
|
||||
-- Update the_geom_webmercator
|
||||
CREATE OR REPLACE FUNCTION _CDB_update_the_geom_webmercator()
|
||||
RETURNS trigger
|
||||
AS $$
|
||||
BEGIN
|
||||
NEW.the_geom_webmercator := public.CDB_TransformToWebmercator(NEW.the_geom);
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
CREATE OR REPLACE FUNCTION _CDB_update_updated_at()
|
||||
RETURNS TRIGGER AS $$
|
||||
BEGIN
|
||||
NEW.updated_at := now();
|
||||
RETURN NEW;
|
||||
END;
|
||||
$$ LANGUAGE plpgsql VOLATILE;
|
||||
|
||||
|
||||
-- ////////////////////////////////////////////////////
|
||||
|
||||
-- Ensure a table is a "cartodb" table
|
||||
-- See https://github.com/CartoDB/cartodb/wiki/CartoDB-user-table
|
||||
CREATE OR REPLACE FUNCTION CDB_CartodbfyTable(schema_name TEXT, reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
exists_geom_cols BOOLEAN[];
|
||||
BEGIN
|
||||
|
||||
PERFORM cartodb._CDB_check_prerequisites(schema_name, reloid);
|
||||
|
||||
PERFORM cartodb._CDB_drop_triggers(reloid);
|
||||
|
||||
-- Ensure required fields exist
|
||||
PERFORM cartodb._CDB_create_cartodb_id_column(reloid);
|
||||
PERFORM cartodb._CDB_create_timestamp_columns(reloid);
|
||||
SELECT cartodb._CDB_create_the_geom_columns(reloid) INTO exists_geom_cols;
|
||||
|
||||
-- Both only populate if proceeds
|
||||
PERFORM cartodb._CDB_populate_the_geom_from_the_geom_webmercator(reloid, exists_geom_cols);
|
||||
PERFORM cartodb._CDB_populate_the_geom_webmercator_from_the_geom(reloid, exists_geom_cols);
|
||||
|
||||
PERFORM cartodb._CDB_create_triggers(schema_name, reloid);
|
||||
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
CREATE OR REPLACE FUNCTION CDB_CartodbfyTable(reloid REGCLASS)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
PERFORM cartodb.CDB_CartodbfyTable('public', reloid);
|
||||
END;
|
||||
$$ LANGUAGE PLPGSQL;
|
||||
|
||||
@@ -11,6 +11,6 @@ AS $$
|
||||
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
-- This is a private function, so only the db owner need privileges
|
||||
REVOKE ALL ON FUNCTION CDB_ColumnNames(REGCLASS) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION CDB_ColumnNames(REGCLASS) TO ":DATABASE_USERNAME";
|
||||
-- This is to migrate from pre-0.2.0 version
|
||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||
GRANT EXECUTE ON FUNCTION CDB_ColumnNames(REGCLASS) TO PUBLIC;
|
||||
|
||||
@@ -12,6 +12,6 @@ AS $$
|
||||
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
-- This is a private function, so only the db owner need privileges
|
||||
REVOKE ALL ON FUNCTION CDB_ColumnType(REGCLASS, TEXT) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION CDB_ColumnType(REGCLASS, TEXT) TO ":DATABASE_USERNAME";
|
||||
-- This is to migrate from pre-0.2.0 version
|
||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||
GRANT EXECUTE ON FUNCTION CDB_ColumnType(REGCLASS, TEXT) TO public;
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
--LOAD 'schema_triggers.so';
|
||||
--CREATE EXTENSION IF NOT EXISTS schema_triggers;
|
||||
|
||||
--GRANT EXECUTE ON ALL FUNCTIONS IN SCHEMA schema_triggers TO public;
|
||||
|
||||
-- Table creation
|
||||
-- {
|
||||
CREATE OR REPLACE FUNCTION cartodb.cdb_handle_create_table ()
|
||||
@@ -24,9 +19,13 @@ BEGIN
|
||||
RETURN;
|
||||
END IF;
|
||||
|
||||
PERFORM cartodb.cdb_disable_ddl_hooks();
|
||||
|
||||
-- CDB_CartodbfyTable must not create tables, or infinite loop will happen
|
||||
PERFORM cartodb.CDB_CartodbfyTable(event_info.relation);
|
||||
|
||||
PERFORM cartodb.cdb_enable_ddl_hooks();
|
||||
|
||||
RAISE DEBUG 'Inserting into cartodb.CDB_TableMetadata';
|
||||
|
||||
-- Add entry to CDB_TableMetadata (should CartodbfyTable do this?)
|
||||
@@ -171,7 +170,7 @@ CREATE OR REPLACE FUNCTION cartodb.cdb_disable_ddl_hooks() returns void AS $$
|
||||
$$ LANGUAGE sql;
|
||||
|
||||
CREATE OR REPLACE FUNCTION cartodb.cdb_enable_ddl_hooks() returns void AS $$
|
||||
SELECT cdb_disable_ddl_hooks();
|
||||
SELECT cartodb.cdb_disable_ddl_hooks();
|
||||
CREATE EVENT TRIGGER cdb_on_relation_create
|
||||
ON "relation_create" EXECUTE PROCEDURE cartodb.cdb_handle_create_table();
|
||||
CREATE EVENT TRIGGER cdb_on_relation_drop
|
||||
@@ -2,7 +2,7 @@
|
||||
--
|
||||
CREATE OR REPLACE FUNCTION CDB_DateToNumber(input timestamp)
|
||||
RETURNS double precision AS $$
|
||||
DECLARE output double precision DEFAULT NULL;
|
||||
DECLARE output double precision;
|
||||
BEGIN
|
||||
BEGIN
|
||||
SELECT extract (EPOCH FROM input) INTO output;
|
||||
|
||||
20
scripts-available/CDB_ExtensionUtils.sql
Normal file
20
scripts-available/CDB_ExtensionUtils.sql
Normal file
@@ -0,0 +1,20 @@
|
||||
CREATE OR REPLACE FUNCTION cartodb.cdb_extension_reload() RETURNS void
|
||||
AS $$
|
||||
DECLARE
|
||||
ver TEXT;
|
||||
sql TEXT;
|
||||
BEGIN
|
||||
ver := split_part(cartodb.cdb_version(), ' ', 1);
|
||||
sql := 'ALTER EXTENSION cartodb UPDATE TO ''' || ver || 'next''';
|
||||
EXECUTE sql;
|
||||
sql := 'ALTER EXTENSION cartodb UPDATE TO ''' || ver || '''';
|
||||
EXECUTE sql;
|
||||
END;
|
||||
$$ language 'plpgsql' VOLATILE;
|
||||
|
||||
CREATE OR REPLACE FUNCTION cartodb.schema_exists(schema_name text)
|
||||
RETURNS boolean AS
|
||||
$$
|
||||
SELECT EXISTS(SELECT 1 FROM pg_namespace WHERE nspname = schema_name::text);
|
||||
$$
|
||||
language sql VOLATILE;
|
||||
90
scripts-available/CDB_Organizations.sql
Normal file
90
scripts-available/CDB_Organizations.sql
Normal file
@@ -0,0 +1,90 @@
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Member_Group_Role_Member_Name()
|
||||
RETURNS TEXT
|
||||
AS 'SELECT ''cdb_org_member''::text || ''_'' || md5(current_database());'
|
||||
LANGUAGE SQL IMMUTABLE;
|
||||
|
||||
DO LANGUAGE 'plpgsql' $$
|
||||
DECLARE
|
||||
cdb_org_member_role_name TEXT;
|
||||
BEGIN
|
||||
cdb_org_member_role_name := cartodb.CDB_Organization_Member_Group_Role_Member_Name();
|
||||
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= cdb_org_member_role_name )
|
||||
THEN
|
||||
EXECUTE 'CREATE ROLE "' || cdb_org_member_role_name || '" NOLOGIN;';
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Create_Member(role_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'GRANT "' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || '" TO "' || role_name || '"';
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
|
||||
|
||||
-------------------------------------------------------------------------------
|
||||
-- Sharing tables
|
||||
-------------------------------------------------------------------------------
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Add_Table_Read_Permission(from_schema text, table_name text, to_role_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'GRANT USAGE ON SCHEMA "' || from_schema || '" TO "' || to_role_name || '"';
|
||||
EXECUTE 'GRANT SELECT ON "' || from_schema || '".' || table_name || ' TO "' || to_role_name || '"';
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Add_Table_Organization_Read_Permission(from_schema text, table_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'SELECT cartodb.CDB_Organization_Add_Table_Read_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Add_Table_Read_Write_Permission(from_schema text, table_name text, to_role_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'GRANT USAGE ON SCHEMA "' || from_schema || '" TO "' || to_role_name || '"';
|
||||
EXECUTE 'GRANT SELECT, INSERT, UPDATE, DELETE ON "' || from_schema || '".' || table_name || ' TO "' || to_role_name || '"';
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Add_Table_Organization_Read_Write_Permission(from_schema text, table_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'SELECT cartodb.CDB_Organization_Add_Table_Read_Write_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
|
||||
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Remove_Access_Permission(from_schema text, table_name text, to_role_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'REVOKE ALL PRIVILEGES ON TABLE "' || from_schema || '".' || table_name || ' FROM "' || to_role_name || '"';
|
||||
-- EXECUTE 'REVOKE USAGE ON SCHEMA ' || from_schema || ' FROM "' || to_role_name || '"';
|
||||
-- We need to revoke usage on schema only if we are revoking privileges from the last table where to_role_name has
|
||||
-- any permission granted within the schema from_schema
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
|
||||
CREATE OR REPLACE
|
||||
FUNCTION cartodb.CDB_Organization_Remove_Organization_Access_Permission(from_schema text, table_name text)
|
||||
RETURNS void
|
||||
AS $$
|
||||
BEGIN
|
||||
EXECUTE 'SELECT cartodb.CDB_Organization_Remove_Access_Permission(''' || from_schema || ''', ''' || table_name || ''', ''' || cartodb.CDB_Organization_Member_Group_Role_Member_Name() || ''');';
|
||||
END
|
||||
$$ LANGUAGE PLPGSQL VOLATILE;
|
||||
@@ -1,13 +1,14 @@
|
||||
-- Return an array of statements found in the given query text
|
||||
--
|
||||
-- Curtesy of Hubert Lubaczewski (depesz)
|
||||
-- Regexp curtesy of Hubert Lubaczewski (depesz)
|
||||
-- Implemented in plpython for performance reasons
|
||||
--
|
||||
CREATE OR REPLACE FUNCTION CDB_QueryStatements(query text)
|
||||
RETURNS SETOF TEXT AS $$
|
||||
SELECT stmt FROM (
|
||||
SELECT btrim(q[1], E' \n\t\r;') as stmt FROM (
|
||||
SELECT regexp_matches( $1, $REG$((?:[^'"$;]+|"[^"]*"|'(?:[^']*|'')*'|(\$[^$]*\$).*?\2)+)$REG$, 'g' ) as q
|
||||
) i
|
||||
) j
|
||||
WHERE stmt <> '';
|
||||
$$ language sql IMMUTABLE STRICT;
|
||||
import re
|
||||
pat = re.compile( r'''((?:[^'"$;]+|"[^"]*"|'[^']*'|(\$[^$]*\$).*?\2)+)''', re.DOTALL )
|
||||
for match in pat.findall(query):
|
||||
cleaned = match[0].strip()
|
||||
if ( cleaned ):
|
||||
yield cleaned
|
||||
$$ language 'plpythonu' IMMUTABLE STRICT;
|
||||
|
||||
@@ -22,7 +22,7 @@ BEGIN
|
||||
END IF;
|
||||
|
||||
BEGIN
|
||||
EXECUTE 'EXPLAIN (FORMAT XML) ' || rec.q INTO STRICT exp;
|
||||
EXECUTE 'EXPLAIN (FORMAT XML, VERBOSE) ' || rec.q INTO STRICT exp;
|
||||
EXCEPTION WHEN others THEN
|
||||
-- TODO: if error is 'relation "xxxxxx" does not exist', take xxxxxx as
|
||||
-- the affected table ?
|
||||
@@ -33,14 +33,19 @@ BEGIN
|
||||
|
||||
-- Now need to extract all values of <Relation-Name>
|
||||
|
||||
--RAISE DEBUG 'Explain: %', exp;
|
||||
-- RAISE DEBUG 'Explain: %', exp;
|
||||
|
||||
FOR rec2 IN WITH
|
||||
inp AS ( SELECT xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x )
|
||||
SELECT unnest(x)::name as p from inp
|
||||
inp AS (
|
||||
SELECT
|
||||
xpath('//x:Relation-Name/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as x,
|
||||
xpath('//x:Schema/text()', exp, ARRAY[ARRAY['x', 'http://www.postgresql.org/2009/explain']]) as s
|
||||
)
|
||||
SELECT unnest(x)::name as p, unnest(s)::name as sc from inp
|
||||
LOOP
|
||||
--RAISE DEBUG 'tab: %', rec2.p;
|
||||
tables := array_append(tables, rec2.p);
|
||||
-- RAISE DEBUG 'tab: %', rec2.p;
|
||||
-- RAISE DEBUG 'sc: %', rec2.sc;
|
||||
tables := array_append(tables, (rec2.sc || '.' || rec2.p)::name);
|
||||
END LOOP;
|
||||
|
||||
-- RAISE DEBUG 'Tables: %', tables;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
-- Return the estimated size of user data. Used for quota checking.
|
||||
CREATE OR REPLACE FUNCTION CDB_UserDataSize()
|
||||
CREATE OR REPLACE FUNCTION CDB_UserDataSize(schema_name TEXT)
|
||||
RETURNS bigint AS
|
||||
$$
|
||||
-- TODO: double check this query. Maybe use CDB_TableMetadata for lookup ?
|
||||
@@ -7,55 +7,71 @@ $$
|
||||
--
|
||||
-- NOTE: division by 2 is an hack for the_geom_webmercator
|
||||
--
|
||||
SELECT coalesce(int8(sum(pg_total_relation_size(quote_ident(table_name))) / 2), 0)
|
||||
SELECT coalesce(int8(sum(pg_total_relation_size(schema_name || '.' || table_name)) / 2), 0)
|
||||
AS quota
|
||||
FROM information_schema.tables
|
||||
WHERE table_catalog = current_database() AND table_schema = 'public'
|
||||
AND table_name != 'spatial_ref_sys'
|
||||
AND table_name != 'cdb_tablemetadata'
|
||||
AND table_type = 'BASE TABLE';
|
||||
WHERE table_catalog = current_database() AND table_schema = schema_name
|
||||
AND table_name != 'spatial_ref_sys'
|
||||
AND table_name != 'cdb_tablemetadata'
|
||||
AND table_type = 'BASE TABLE';
|
||||
$$
|
||||
LANGUAGE 'sql' VOLATILE;
|
||||
|
||||
|
||||
-- Return the estimated size of user data. Used for quota checking.
|
||||
-- Implicit schema version for backwards compatibility
|
||||
CREATE OR REPLACE FUNCTION CDB_UserDataSize()
|
||||
RETURNS bigint AS
|
||||
$$
|
||||
SELECT public.CDB_UserDataSize('public');
|
||||
$$
|
||||
LANGUAGE 'sql' VOLATILE;
|
||||
|
||||
-- Triggers cannot have declared arguments: pbfact float8, qmax int8, schema_name text
|
||||
CREATE OR REPLACE FUNCTION CDB_CheckQuota()
|
||||
RETURNS trigger AS
|
||||
$$
|
||||
DECLARE
|
||||
|
||||
pbfact float8;
|
||||
qmax int8;
|
||||
schema_name text;
|
||||
dice float8;
|
||||
quota float8;
|
||||
BEGIN
|
||||
|
||||
IF TG_NARGS = 3 THEN
|
||||
schema_name := TG_ARGV[2];
|
||||
IF cartodb.schema_exists(schema_name) = false THEN
|
||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name;
|
||||
END IF;
|
||||
ELSE
|
||||
schema_name := 'public';
|
||||
END IF;
|
||||
-- Hack to support old versions of CDB_CheckQuota with 2 params but without schema_name
|
||||
IF TG_NARGS >= 2 AND TG_ARGV[1] <> '-1' THEN
|
||||
qmax := TG_ARGV[1];
|
||||
ELSE
|
||||
BEGIN
|
||||
EXECUTE FORMAT('SELECT %I._CDB_UserQuotaInBytes();', schema_name) INTO qmax;
|
||||
EXCEPTION WHEN undefined_function THEN
|
||||
RAISE EXCEPTION 'Missing "%"._CDB_UserQuotaInBytes()', schema_name;
|
||||
END;
|
||||
END IF;
|
||||
pbfact := TG_ARGV[0];
|
||||
dice := random();
|
||||
|
||||
-- RAISE DEBUG 'CDB_CheckQuota enter: pbfact=% dice=%', pbfact, dice;
|
||||
dice := random();
|
||||
|
||||
IF dice < pbfact THEN
|
||||
RAISE DEBUG 'Checking quota on table % (dice:%, needed:<%)', TG_RELID::text, dice, pbfact;
|
||||
BEGIN
|
||||
qmax := public._CDB_UserQuotaInBytes();
|
||||
EXCEPTION WHEN undefined_function THEN
|
||||
IF TG_NARGS > 1 THEN
|
||||
RAISE NOTICE 'Using quota specified via trigger parameter';
|
||||
qmax := TG_ARGV[1];
|
||||
ELSE
|
||||
RAISE EXCEPTION 'Missing _CDB_UserQuotaInBytes(), and no quota provided as parameter';
|
||||
END IF;
|
||||
END;
|
||||
|
||||
IF qmax = 0 THEN
|
||||
RETURN NEW;
|
||||
END IF;
|
||||
|
||||
SELECT CDB_UserDataSize() INTO quota;
|
||||
SELECT public.CDB_UserDataSize(schema_name) INTO quota;
|
||||
IF quota > qmax THEN
|
||||
RAISE EXCEPTION 'Quota exceeded by %KB', (quota-qmax)/1024;
|
||||
RAISE EXCEPTION 'Quota exceeded by %KB', (quota-qmax)/1024;
|
||||
ELSE RAISE DEBUG 'User quota in bytes: % < % (max allowed)', quota, qmax;
|
||||
END IF;
|
||||
-- ELSE RAISE DEBUG 'Not checking quota on table % (dice:%, needed:<%)', TG_RELID::text, dice, pbfact;
|
||||
END IF;
|
||||
|
||||
RETURN NEW;
|
||||
@@ -63,26 +79,33 @@ END;
|
||||
$$
|
||||
LANGUAGE 'plpgsql' VOLATILE;
|
||||
|
||||
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(bytes int8)
|
||||
|
||||
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(schema_name text, bytes int8)
|
||||
RETURNS int8 AS
|
||||
$$
|
||||
DECLARE
|
||||
current_quota int8;
|
||||
sql text;
|
||||
BEGIN
|
||||
BEGIN
|
||||
current_quota := public._CDB_UserQuotaInBytes();
|
||||
EXCEPTION WHEN undefined_function THEN
|
||||
current_quota := 0;
|
||||
END;
|
||||
IF cartodb.schema_exists(schema_name::text) = false THEN
|
||||
RAISE EXCEPTION 'Invalid schema name "%"', schema_name::text;
|
||||
END IF;
|
||||
|
||||
sql := 'CREATE OR REPLACE FUNCTION public._CDB_UserQuotaInBytes() '
|
||||
sql := 'CREATE OR REPLACE FUNCTION "' || schema_name::text || '"._CDB_UserQuotaInBytes() '
|
||||
|| 'RETURNS int8 AS $X$ SELECT ' || bytes
|
||||
|| '::int8 $X$ LANGUAGE sql IMMUTABLE';
|
||||
EXECUTE sql;
|
||||
|
||||
return current_quota;
|
||||
|
||||
return bytes;
|
||||
END
|
||||
$$
|
||||
LANGUAGE 'plpgsql' VOLATILE STRICT;
|
||||
|
||||
|
||||
CREATE OR REPLACE FUNCTION CDB_SetUserQuotaInBytes(bytes int8)
|
||||
RETURNS int8 AS
|
||||
$$
|
||||
BEGIN
|
||||
return public.CDB_SetUserQuotaInBytes('public', bytes);
|
||||
END;
|
||||
$$
|
||||
LANGUAGE 'plpgsql' VOLATILE STRICT;
|
||||
|
||||
@@ -1,13 +0,0 @@
|
||||
DO LANGUAGE 'plpgsql' $$
|
||||
BEGIN
|
||||
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= 'cdb_org_admin' )
|
||||
THEN
|
||||
CREATE ROLE cdb_org_admin NOLOGIN;
|
||||
END IF;
|
||||
|
||||
IF NOT EXISTS ( SELECT * FROM pg_roles WHERE rolname= 'cdb_org_user' )
|
||||
THEN
|
||||
CREATE ROLE cdb_org_user NOLOGIN;
|
||||
END IF;
|
||||
END
|
||||
$$;
|
||||
@@ -2,7 +2,7 @@
|
||||
--
|
||||
CREATE OR REPLACE FUNCTION CDB_StringToDate(input character varying)
|
||||
RETURNS date AS $$
|
||||
DECLARE output DATE DEFAULT NULL;
|
||||
DECLARE output DATE;
|
||||
BEGIN
|
||||
BEGIN
|
||||
output := input::date;
|
||||
|
||||
@@ -17,10 +17,11 @@ AS $$
|
||||
ON pg_class.oid = idx.indexrelid
|
||||
WHERE pg_indexes.tablename = '' || $1 || ''
|
||||
AND '' || $1 || '' IN (SELECT CDB_UserTables())
|
||||
AND pg_class.relname=pg_indexes.indexname;
|
||||
AND pg_class.relname=pg_indexes.indexname
|
||||
;
|
||||
|
||||
$$ LANGUAGE SQL;
|
||||
|
||||
-- This is a private function, so only the db owner need privileges
|
||||
REVOKE ALL ON FUNCTION CDB_TableIndexes(REGCLASS) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION CDB_TableIndexes(REGCLASS) TO ":DATABASE_USERNAME";
|
||||
-- This is to migrate from pre-0.2.0 version
|
||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||
GRANT EXECUTE ON FUNCTION CDB_TableIndexes(REGCLASS) TO public;
|
||||
|
||||
@@ -5,7 +5,6 @@
|
||||
--
|
||||
-- Currently accepted permissions are: 'public', 'private' or 'all'
|
||||
--
|
||||
-- DROP FUNCTION IF EXISTS CDB_UserTables(); -- replaced by:
|
||||
CREATE OR REPLACE FUNCTION CDB_UserTables(perm text DEFAULT 'all')
|
||||
RETURNS SETOF information_schema.sql_identifier
|
||||
AS $$
|
||||
@@ -26,14 +25,17 @@ AS $$
|
||||
FROM usertables
|
||||
)
|
||||
SELECT t FROM perms
|
||||
WHERE p = CASE WHEN $1 = 'private' THEN false
|
||||
WHERE (
|
||||
p = CASE WHEN $1 = 'private' THEN false
|
||||
WHEN $1 = 'public' THEN true
|
||||
ELSE not p -- none
|
||||
END
|
||||
OR $1 = 'all'
|
||||
)
|
||||
AND has_table_privilege('public'||'.'||t, 'SELECT')
|
||||
;
|
||||
$$ LANGUAGE 'sql';
|
||||
|
||||
-- This is a private function, so only the db owner need privileges
|
||||
REVOKE ALL ON FUNCTION CDB_UserTables(text) FROM PUBLIC;
|
||||
GRANT EXECUTE ON FUNCTION CDB_UserTables(text) TO ":DATABASE_USERNAME";
|
||||
-- This is to migrate from pre-0.2.0 version
|
||||
-- See http://github.com/CartoDB/cartodb-postgresql/issues/36
|
||||
GRANT EXECUTE ON FUNCTION CDB_UserTables(text) TO public;
|
||||
|
||||
1
scripts-enabled/210-CDB_Organizations.sql
Symbolic link
1
scripts-enabled/210-CDB_Organizations.sql
Symbolic link
@@ -0,0 +1 @@
|
||||
../scripts-available/CDB_Organizations.sql
|
||||
@@ -29,7 +29,20 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)) as age
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
|
||||
-- Table with cartodb_id field, see
|
||||
-- http://github.com/CartoDB/cartodb-postgresql/issues/32
|
||||
select 1 as cartodb_id INTO c.t4;
|
||||
select
|
||||
cartodb_id, created_at=updated_at as "c=u",
|
||||
NOW() - updated_at < '1 secs' as "u<1s",
|
||||
the_geom, the_geom_webmercator
|
||||
from c.t4;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)) as age
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t4'::regclass;
|
||||
|
||||
----------------------------
|
||||
-- ALTER TABLE RENAME COLUMN
|
||||
@@ -48,7 +61,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
|
||||
select pg_sleep(.1);
|
||||
alter table c.t3 rename column the_geom_webmercator to webmerc2;
|
||||
@@ -63,7 +76,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
|
||||
----------------------------
|
||||
-- ALTER TABLE DROP COLUMN
|
||||
@@ -82,7 +95,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
|
||||
----------------------------
|
||||
-- ALTER TABLE ADD COLUMN
|
||||
@@ -101,7 +114,7 @@ from c.t3;
|
||||
select
|
||||
tabname::text,
|
||||
round(extract('secs' from now() - updated_at)*10) as agecs
|
||||
FROM CDB_TableMetadata;
|
||||
FROM CDB_TableMetadata WHERE tabname = 'c.t3'::regclass;
|
||||
|
||||
----------------------------
|
||||
-- DROP TABLE
|
||||
@@ -109,7 +122,7 @@ FROM CDB_TableMetadata;
|
||||
|
||||
RESET SESSION AUTHORIZATION;
|
||||
drop schema c cascade;
|
||||
select count(*) from CDB_TableMetadata;
|
||||
select count(*) from CDB_TableMetadata;
|
||||
|
||||
DROP USER cartodb_postgresql_unpriv_user;
|
||||
DROP FUNCTION _CDB_UserQuotaInBytes();
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
CREATE EXTENSION postgis;
|
||||
CREATE EXTENSION schema_triggers;
|
||||
CREATE EXTENSION plpythonu;
|
||||
CREATE EXTENSION cartodb;
|
||||
CREATE FUNCTION public.cdb_invalidate_varnish(table_name text)
|
||||
RETURNS void AS $$
|
||||
|
||||
@@ -30,7 +30,7 @@ BEGIN
|
||||
END IF;
|
||||
END LOOP;
|
||||
|
||||
PERFORM CDB_CartodbfyTable(tabname);
|
||||
PERFORM CDB_CartodbfyTable('public', tabname);
|
||||
|
||||
sql := 'INSERT INTO ' || tabname::text || '(the_geom) values ( CDB_LatLng(2,1) ) RETURNING cartodb_id';
|
||||
EXECUTE sql INTO STRICT id;
|
||||
@@ -132,7 +132,7 @@ LANGUAGE 'plpgsql';
|
||||
|
||||
-- table with single non-geometrical column
|
||||
CREATE TABLE t AS SELECT 1::int as a;
|
||||
SELECT CDB_CartodbfyTable('t'); -- should fail
|
||||
SELECT CDB_CartodbfyTable('public', 't'); -- should fail
|
||||
SELECT CDB_SetUserQuotaInBytes(0); -- Set user quota to infinite
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'single non-geometrical column');
|
||||
DROP TABLE t;
|
||||
@@ -146,28 +146,28 @@ DROP TABLE t;
|
||||
CREATE TABLE t AS SELECT ST_SetSRID(ST_MakePoint(-1,-1),4326) as the_geom
|
||||
UNION ALL SELECT ST_SetSRID(ST_MakePoint(0,0),3857);
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'mixed-srid the_geom');
|
||||
SELECT 'extent',ST_Extent(the_geom) FROM t;
|
||||
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)) FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with wrong srid-constrained the_geom values
|
||||
CREATE TABLE t AS SELECT 'SRID=3857;LINESTRING(222638.981586547 222684.208505545, 111319.490793274 111325.142866385)'::geometry(geometry,3857) as the_geom;
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'wrong srid-constrained the_geom');
|
||||
SELECT 'extent',ST_Extent(the_geom),ST_Extent(ST_SnapToGrid(the_geom_webmercator,1)) FROM t;
|
||||
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)),ST_Extent(ST_SnapToGrid(the_geom_webmercator,1)) FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with wrong srid-constrained the_geom_webmercator values (and no the_geom!)
|
||||
CREATE TABLE t AS SELECT 'SRID=4326;LINESTRING(1 1,2 2)'::geometry(geometry,4326) as the_geom_webmercator;
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'wrong srid-constrained the_geom_webmercator');
|
||||
-- expect the_geom to be populated from the_geom_webmercator
|
||||
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.1)) FROM t;
|
||||
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)) FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing triggered the_geom
|
||||
CREATE TABLE t AS SELECT 'SRID=4326;LINESTRING(1 1,2 2)'::geometry(geometry) as the_geom;
|
||||
CREATE TRIGGER update_the_geom_webmercator_trigger BEFORE UPDATE OF the_geom ON t
|
||||
FOR EACH ROW EXECUTE PROCEDURE public._CDB_update_the_geom_webmercator();
|
||||
FOR EACH ROW EXECUTE PROCEDURE _CDB_update_the_geom_webmercator();
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'trigger-protected the_geom');
|
||||
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.1)) FROM t;
|
||||
SELECT 'extent',ST_Extent(ST_SnapToGrid(the_geom,0.2)) FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing updated_at and created_at fields ot type text
|
||||
@@ -182,18 +182,48 @@ SELECT CDB_CartodbfyTableCheck('t', 'cartodbfied with view');
|
||||
DROP VIEW v;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing cartodb_id field ot type text
|
||||
-- table with existing cartodb_id field of type text
|
||||
CREATE TABLE t AS SELECT 10::text as cartodb_id;
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'text cartodb_id');
|
||||
select cartodb_id/2 FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing cartodb_id field ot type text not casting
|
||||
-- table with existing cartodb_id field of type text not casting
|
||||
CREATE TABLE t AS SELECT 'nan' as cartodb_id;
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'uncasting text cartodb_id');
|
||||
select cartodb_id,_cartodb_id0 FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing cartodb_id field of type int4 not sequenced
|
||||
CREATE TABLE t AS SELECT 1::int4 as cartodb_id;
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'unsequenced cartodb_id');
|
||||
select cartodb_id FROM t;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing cartodb_id serial primary key
|
||||
CREATE TABLE t ( cartodb_id serial primary key );
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'cartodb_id serial primary key');
|
||||
SELECT c.conname, a.attname FROM pg_constraint c, pg_attribute a
|
||||
WHERE c.conrelid = 't'::regclass and a.attrelid = c.conrelid
|
||||
AND c.conkey[1] = a.attnum AND NOT a.attisdropped;
|
||||
DROP TABLE t;
|
||||
|
||||
-- table with existing the_geom and created_at and containing null values
|
||||
-- Really, a test for surviving an longstanding PostgreSQL bug:
|
||||
-- http://www.postgresql.org/message-id/20140530143150.GA11051@localhost
|
||||
CREATE TABLE t (
|
||||
the_geom geometry(Geometry,4326),
|
||||
created_at timestamptz,
|
||||
updated_at timestamptz
|
||||
);
|
||||
COPY t (the_geom, created_at, updated_at) FROM stdin;
|
||||
0106000020E610000001000000010300000001000000050000009EB8244146435BC017B65E062AD343409EB8244146435BC0F51AF6E2708044400B99891683765AC0F51AF6E2708044400B99891683765AC017B65E062AD343409EB8244146435BC017B65E062AD34340 2012-06-06 21:59:08 2013-06-10 20:17:20
|
||||
0106000020E61000000100000001030000000100000005000000DA7763431A1A5CC0FBCEE869313C3A40DA7763431A1A5CC09C1B8F55BC494440F9F4A9C7993356C09C1B8F55BC494440F9F4A9C7993356C0FBCEE869313C3A40DA7763431A1A5CC0FBCEE869313C3A40 2012-06-06 21:59:08 2013-06-10 20:17:20
|
||||
\N \N \N
|
||||
\.
|
||||
SELECT CDB_CartodbfyTableCheck('t', 'null geom and timestamp values');
|
||||
DROP TABLE t;
|
||||
|
||||
-- TODO: table with existing custom-triggered the_geom
|
||||
|
||||
DROP FUNCTION CDB_CartodbfyTableCheck(regclass, text);
|
||||
|
||||
@@ -2,6 +2,8 @@ SET
|
||||
CREATE FUNCTION
|
||||
SELECT 1
|
||||
ERROR: Please set user quota before cartodbfying tables.
|
||||
CONTEXT: SQL statement "SELECT cartodb._CDB_check_prerequisites(schema_name, reloid)"
|
||||
PL/pgSQL function cdb_cartodbfytable(text,regclass) line 6 at PERFORM
|
||||
0
|
||||
single non-geometrical column cartodbfied fine
|
||||
DROP TABLE
|
||||
@@ -40,5 +42,16 @@ SELECT 1
|
||||
uncasting text cartodb_id cartodbfied fine
|
||||
1|nan
|
||||
DROP TABLE
|
||||
SELECT 1
|
||||
unsequenced cartodb_id cartodbfied fine
|
||||
1
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
cartodb_id serial primary key cartodbfied fine
|
||||
t_pkey|cartodb_id
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
null geom and timestamp values cartodbfied fine
|
||||
DROP TABLE
|
||||
DROP FUNCTION
|
||||
DROP FUNCTION
|
||||
|
||||
@@ -26,3 +26,38 @@ INSER INTO "my''""t" values ('''','""'';;');
|
||||
SELECT $qu;oted$ hi $qu;oted$;
|
||||
$the_param$) as statement )
|
||||
SELECT '5', row_number() over (), statement FROM q;
|
||||
|
||||
WITH q AS ( SELECT CDB_QueryStatements($the_param$
|
||||
SELECT
|
||||
1 ; SELECT
|
||||
2
|
||||
$the_param$) as statement )
|
||||
SELECT '6', row_number() over (), statement FROM q;
|
||||
|
||||
-- This is an insane input, illegal sql
|
||||
-- we are really only testing that it does not
|
||||
-- take forever to process..
|
||||
-- The actual result is not correct, so if the function
|
||||
-- ever gets fixed check if it's better
|
||||
WITH q AS ( SELECT CDB_QueryStatements($the_param$
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/a
|
||||
$b$
|
||||
$c$d
|
||||
;
|
||||
$the_param$) as statement )
|
||||
SELECT '7', row_number() over (), statement FROM q;
|
||||
|
||||
|
||||
WITH q AS ( SELECT CDB_QueryStatements($the_param$
|
||||
SELECT $quoted$ hi
|
||||
$quoted$;
|
||||
$the_param$) as statement )
|
||||
SELECT '8', row_number() over (), statement FROM q;
|
||||
|
||||
@@ -7,3 +7,13 @@
|
||||
4|4|SELECT 5
|
||||
5|1|INSER INTO "my''""t" values ('''','""'';;')
|
||||
5|2|SELECT $qu;oted$ hi $qu;oted$
|
||||
6|1|SELECT
|
||||
1
|
||||
6|2|SELECT
|
||||
2
|
||||
7|1|/a
|
||||
7|2|b
|
||||
7|3|c
|
||||
7|4|d
|
||||
8|1|SELECT $quoted$ hi
|
||||
$quoted$
|
||||
|
||||
@@ -25,3 +25,9 @@ WITH inp AS ( select 'create table test (a int); insert into test values (1); se
|
||||
|
||||
WITH inp AS ( select 'WITH a AS (select * from pg_class) select * from a'::text as q )
|
||||
SELECT q, CDB_QueryTables(q) from inp;
|
||||
|
||||
CREATE SCHEMA sc;
|
||||
create table sc.test (a int);
|
||||
insert into sc.test values (1);
|
||||
WITH inp AS ( select 'select * from sc.test'::text as q )
|
||||
SELECT q, CDB_QueryTables(q) from inp;
|
||||
|
||||
@@ -1,11 +1,15 @@
|
||||
SELECT * FROM geometry_columns|{pg_attribute,pg_class,pg_namespace,pg_type}
|
||||
SELECT a.attname FROM pg_class c JOIN pg_attribute a on (a.attrelid = c.oid)|{pg_attribute,pg_class}
|
||||
SELECT * FROM geometry_columns|{pg_catalog.pg_attribute,pg_catalog.pg_class,pg_catalog.pg_namespace,pg_catalog.pg_type}
|
||||
SELECT a.attname FROM pg_class c JOIN pg_attribute a on (a.attrelid = c.oid)|{pg_catalog.pg_attribute,pg_catalog.pg_class}
|
||||
CREATE table "my'tab;le" as select 1|{}
|
||||
SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_class}
|
||||
SELECT a.oid, b.oid FROM pg_class a, pg_class b|{pg_catalog.pg_class}
|
||||
SELECT 1 as col1; select 2 as col2|{}
|
||||
WARNING: CDB_QueryTables cannot explain query: select 1 from nonexistant (42P01: relation "nonexistant" does not exist)
|
||||
ERROR: relation "nonexistant" does not exist
|
||||
begin; select * from pg_class; commit;|{pg_class}
|
||||
begin; select * from pg_class; commit;|{pg_catalog.pg_class}
|
||||
WARNING: CDB_QueryTables cannot explain query: select * from test (42P01: relation "test" does not exist)
|
||||
ERROR: relation "test" does not exist
|
||||
WITH a AS (select * from pg_class) select * from a|{pg_class}
|
||||
WITH a AS (select * from pg_class) select * from a|{pg_catalog.pg_class}
|
||||
CREATE SCHEMA
|
||||
CREATE TABLE
|
||||
INSERT 0 1
|
||||
select * from sc.test|{sc.test}
|
||||
|
||||
@@ -4,7 +4,7 @@ CREATE TABLE big(a int);
|
||||
-- Try the legacy interface
|
||||
-- See https://github.com/CartoDB/cartodb-postgresql/issues/13
|
||||
CREATE TRIGGER test_quota BEFORE UPDATE OR INSERT ON big
|
||||
EXECUTE PROCEDURE CDB_CheckQuota(1,1);
|
||||
EXECUTE PROCEDURE CDB_CheckQuota(1, 1, 'public');
|
||||
INSERT INTO big VALUES (1); -- allowed, check runs before
|
||||
INSERT INTO big VALUES (1); -- disallowed, quota exceeds before
|
||||
SELECT CDB_SetUserQuotaInBytes(0);
|
||||
|
||||
@@ -6,9 +6,9 @@ ERROR: Quota exceeded by 3.9990234375KB
|
||||
0
|
||||
|
||||
INSERT 0 1024
|
||||
0
|
||||
ERROR: Quota exceeded by 103.9921875KB
|
||||
8
|
||||
ERROR: Quota exceeded by 103.9921875KB
|
||||
0
|
||||
INSERT 0 1
|
||||
DROP TABLE
|
||||
SET
|
||||
|
||||
15
test/README
Normal file
15
test/README
Normal file
@@ -0,0 +1,15 @@
|
||||
Adding tests consists in adding 2 files in this directory: one file
|
||||
containing the sql code and another containing the expected output.
|
||||
|
||||
Example, to add a test for CDB_Something function, you'd add:
|
||||
|
||||
- CDB_SomethingTest.sql
|
||||
- CDB_SomethingTest_expect
|
||||
|
||||
To easy the generation of the expected file you can initially omit it,
|
||||
then run "make -C .. installcheck" from the top-level dir and copy
|
||||
../results/test/CDB_SomethingTest.out to CDB_SomethingTest_expect chopping
|
||||
off the first line:
|
||||
|
||||
make -C .. installcheck
|
||||
tail -n +2 ../results/test/CDB_SomethingTest.out > CDB_SomethingTest_expect
|
||||
334
test/organization/test.sh
Normal file
334
test/organization/test.sh
Normal file
@@ -0,0 +1,334 @@
|
||||
#!/bin/sh
|
||||
|
||||
#
|
||||
# It is expected that you run this script
|
||||
# as a PostgreSQL superuser, for example:
|
||||
#
|
||||
# PGUSER=postgres bash ./test.sh
|
||||
#
|
||||
|
||||
DATABASE=test_organizations
|
||||
CMD='echo psql'
|
||||
CMD=psql
|
||||
|
||||
OK=0
|
||||
PARTIALOK=0
|
||||
|
||||
function set_failed() {
|
||||
OK=1
|
||||
PARTIALOK=1
|
||||
}
|
||||
|
||||
|
||||
function clear_partial_result() {
|
||||
PARTIALOK=0
|
||||
}
|
||||
|
||||
|
||||
function sql() {
|
||||
local ROLE
|
||||
local QUERY
|
||||
if [[ $# -ge 2 ]]
|
||||
then
|
||||
ROLE="$1"
|
||||
QUERY="$2"
|
||||
else
|
||||
QUERY="$1"
|
||||
fi
|
||||
|
||||
if [ -n "${ROLE}" ]; then
|
||||
log_debug "Executing query '${QUERY}' as ${ROLE}"
|
||||
RESULT=`${CMD} -U "${ROLE}" ${DATABASE} -c "${QUERY}" -A -t`
|
||||
else
|
||||
log_debug "Executing query '${QUERY}'"
|
||||
RESULT=`${CMD} ${DATABASE} -c "${QUERY}" -A -t`
|
||||
fi
|
||||
CODERESULT=$?
|
||||
|
||||
echo ${RESULT}
|
||||
echo
|
||||
|
||||
if [[ ${CODERESULT} -ne 0 ]]
|
||||
then
|
||||
echo -n "FAILED TO EXECUTE QUERY: "
|
||||
log_warning "${QUERY}"
|
||||
if [[ "$3" != "fails" ]]
|
||||
then
|
||||
log_error "${QUERY}"
|
||||
set_failed
|
||||
fi
|
||||
else
|
||||
if [[ "$3" == "fails" ]]
|
||||
then
|
||||
log_error "QUERY: '${QUERY}' was expected to fail and it did not fail"
|
||||
set_failed
|
||||
fi
|
||||
fi
|
||||
|
||||
if [[ "$3" == "should" ]]
|
||||
then
|
||||
if [[ "${RESULT}" != "$4" ]]
|
||||
then
|
||||
log_error "QUERY '${QUERY}' expected result '${4}' but got '${RESULT}'"
|
||||
set_failed
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
function log_info()
|
||||
{
|
||||
echo
|
||||
echo
|
||||
echo
|
||||
_log "1;34m" "$1"
|
||||
}
|
||||
|
||||
function log_error() {
|
||||
_log "1;31m" "$1"
|
||||
}
|
||||
|
||||
function log_debug() {
|
||||
_log "1;32m" "> $1"
|
||||
}
|
||||
|
||||
function log_warning() {
|
||||
_log "0;33m" "$1"
|
||||
}
|
||||
|
||||
function _log() {
|
||||
echo -e "\033[$1$2\033[0m"
|
||||
}
|
||||
|
||||
# '############################ HELPERS #############################'
|
||||
function create_role_and_schema() {
|
||||
local ROLE=$1
|
||||
sql "CREATE ROLE ${ROLE} LOGIN;"
|
||||
sql "GRANT CONNECT ON DATABASE \"${DATABASE}\" TO ${ROLE};"
|
||||
sql "CREATE SCHEMA ${ROLE} AUTHORIZATION ${ROLE};"
|
||||
sql "SELECT cartodb.CDB_Organization_Create_Member('${ROLE}')"
|
||||
}
|
||||
|
||||
|
||||
function drop_role_and_schema() {
|
||||
local ROLE=$1
|
||||
sql "DROP SCHEMA \"${ROLE}\";"
|
||||
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM \"${ROLE}\";"
|
||||
sql "DROP ROLE \"${ROLE}\";"
|
||||
}
|
||||
|
||||
|
||||
function create_table() {
|
||||
if [[ $# -ne 2 ]]
|
||||
then
|
||||
log_error "create_table requires two arguments: role and table_name"
|
||||
exit 1
|
||||
fi
|
||||
local ROLE="$1"
|
||||
local TABLENAME="$2"
|
||||
sql ${ROLE} "CREATE TABLE ${ROLE}.${TABLENAME} ( a int );"
|
||||
}
|
||||
|
||||
|
||||
function setup() {
|
||||
${CMD} -c "CREATE DATABASE ${DATABASE}"
|
||||
sql "CREATE SCHEMA cartodb;"
|
||||
sql "GRANT USAGE ON SCHEMA cartodb TO public;"
|
||||
|
||||
log_info "########################### BOOTSTRAP ###########################"
|
||||
${CMD} -d ${DATABASE} -f scripts-available/CDB_Organizations.sql
|
||||
|
||||
|
||||
log_info "############################# SETUP #############################"
|
||||
create_role_and_schema cdb_testmember_1
|
||||
create_role_and_schema cdb_testmember_2
|
||||
|
||||
create_table cdb_testmember_1 foo
|
||||
sql cdb_testmember_1 'INSERT INTO cdb_testmember_1.foo VALUES (1), (2), (3), (4), (5);'
|
||||
sql cdb_testmember_1 'SELECT * FROM cdb_testmember_1.foo;'
|
||||
|
||||
create_table cdb_testmember_2 bar
|
||||
sql cdb_testmember_2 'INSERT INTO bar VALUES (1), (2), (3), (4), (5);'
|
||||
sql cdb_testmember_2 'SELECT * FROM cdb_testmember_2.bar;'
|
||||
}
|
||||
|
||||
function tear_down() {
|
||||
log_info "########################### USER TEAR DOWN ###########################"
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Remove_Access_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2');"
|
||||
sql cdb_testmember_2 "SELECT * FROM cartodb.CDB_Organization_Remove_Access_Permission('cdb_testmember_2', 'bar', 'cdb_testmember_1');"
|
||||
|
||||
sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.foo;'
|
||||
sql cdb_testmember_2 'DROP TABLE cdb_testmember_2.bar;'
|
||||
|
||||
sql "DROP SCHEMA cartodb CASCADE"
|
||||
|
||||
log_info "########################### TEAR DOWN ###########################"
|
||||
sql 'DROP SCHEMA cdb_testmember_1;'
|
||||
sql 'DROP SCHEMA cdb_testmember_2;'
|
||||
|
||||
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_testmember_1;"
|
||||
sql "REVOKE CONNECT ON DATABASE \"${DATABASE}\" FROM cdb_testmember_2;"
|
||||
|
||||
sql 'DROP ROLE cdb_testmember_1;'
|
||||
sql 'DROP ROLE cdb_testmember_2;'
|
||||
|
||||
${CMD} -c "DROP DATABASE ${DATABASE}"
|
||||
}
|
||||
|
||||
function run_tests() {
|
||||
local FAILED_TESTS=()
|
||||
|
||||
local TESTS
|
||||
if [[ $# -ge 1 ]]
|
||||
then
|
||||
TESTS="$@"
|
||||
else
|
||||
TESTS=`cat $0 | perl -n -e'/function (test.*)\(\)/ && print "$1\n"'`
|
||||
fi
|
||||
for t in ${TESTS}
|
||||
do
|
||||
echo "####################################################################"
|
||||
echo "#"
|
||||
echo "# Running: ${t}"
|
||||
echo "#"
|
||||
echo "####################################################################"
|
||||
clear_partial_result
|
||||
setup
|
||||
eval ${t}
|
||||
if [[ ${PARTIALOK} -ne 0 ]]
|
||||
then
|
||||
FAILED_TESTS+=(${t})
|
||||
fi
|
||||
tear_down
|
||||
done
|
||||
if [[ ${OK} -ne 0 ]]
|
||||
then
|
||||
echo
|
||||
log_error "The following tests are failing:"
|
||||
printf -- '\t%s\n' "${FAILED_TESTS[@]}"
|
||||
fi
|
||||
}
|
||||
|
||||
|
||||
|
||||
#################################################### TESTS GO HERE ####################################################
|
||||
|
||||
function test_member_2_cannot_read_without_permission() {
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' fails
|
||||
}
|
||||
|
||||
function test_member_1_cannot_grant_read_permission_to_other_schema_than_its_one() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_2', 'foo', 'cdb_testmember_2')" fails
|
||||
}
|
||||
|
||||
function test_member_1_grants_read_permission_and_member_2_can_read() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_1 'SELECT count(*) FROM cdb_testmember_2.bar;' fails
|
||||
}
|
||||
|
||||
function test_member_2_cannot_add_table_to_member_1_schema_after_table_permission_added() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 "CREATE TABLE cdb_testmember_1.bar ( a int );" fails
|
||||
}
|
||||
|
||||
function test_grant_read_permission_between_two_members() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_2', 'bar', 'cdb_testmember_1')"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_1 'SELECT count(*) FROM cdb_testmember_2.bar;' should 5
|
||||
}
|
||||
|
||||
function test_member_2_cannot_write_to_member_1_table() {
|
||||
sql cdb_testmember_2 'INSERT INTO cdb_testmember_1.foo VALUES (5), (6), (7), (8), (9);' fails
|
||||
}
|
||||
|
||||
function test_member_1_cannot_grant_read_write_permission_to_other_schema_than_its_one() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Write_Permission('cdb_testmember_2', 'foo', 'cdb_testmember_2')" fails
|
||||
}
|
||||
|
||||
function test_member_2_can_write_to_member_1_table_after_write_permission_is_added() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Write_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'INSERT INTO cdb_testmember_1.foo VALUES (5), (6), (7), (8), (9);'
|
||||
sql cdb_testmember_1 'SELECT count(*) FROM cdb_testmember_1.foo;' should 10
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 10
|
||||
sql cdb_testmember_2 'DELETE FROM cdb_testmember_1.foo where a = 9;'
|
||||
sql cdb_testmember_1 'SELECT count(*) FROM cdb_testmember_1.foo;' should 9
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 9
|
||||
}
|
||||
|
||||
function test_member_1_removes_access_and_member_2_can_no_longer_query_the_table() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Remove_Access_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'SELECT * FROM cdb_testmember_1.foo;' fails
|
||||
}
|
||||
|
||||
function test_member_1_removes_access_and_member_2_can_no_longer_write_to_the_table() {
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Write_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'INSERT INTO cdb_testmember_1.foo VALUES (5), (6), (7), (8), (9);'
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Remove_Access_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'INSERT INTO cdb_testmember_1.foo VALUES (5), (6), (7), (8), (9);' fails
|
||||
}
|
||||
|
||||
function test_giving_permissions_to_two_tables_and_removing_from_first_table_should_not_remove_from_second() {
|
||||
#### test setup
|
||||
# create an extra table for cdb_testmember_1
|
||||
create_table cdb_testmember_1 foo_2
|
||||
sql cdb_testmember_1 'INSERT INTO cdb_testmember_1.foo_2 VALUES (1), (2), (3), (4), (5);'
|
||||
sql cdb_testmember_1 'SELECT * FROM cdb_testmember_1.foo_2;'
|
||||
|
||||
# gives read permission to both tables
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo_2', 'cdb_testmember_2')"
|
||||
|
||||
# cdb_testmember_2 has access to both tables
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo_2;' should 5
|
||||
|
||||
# cdb_testmember_1 removes access to foo table
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Remove_Access_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
|
||||
# cdb_testmember_2 should have access to foo_2 table but not to table foo
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' fails
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo_2;' should 5
|
||||
|
||||
|
||||
#### test tear down
|
||||
sql cdb_testmember_1 'DROP TABLE cdb_testmember_1.foo_2;'
|
||||
}
|
||||
|
||||
function test_cdb_org_member_role_allows_reading_to_all_users_without_explicit_permission() {
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' fails
|
||||
sql cdb_testmember_1 "SELECT cartodb.CDB_Organization_Add_Table_Organization_Read_Permission('cdb_testmember_1', 'foo');"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
}
|
||||
|
||||
function test_user_can_read_when_it_has_permission_after_organization_permission_is_removed() {
|
||||
create_role_and_schema cdb_testmember_3
|
||||
|
||||
# shares with cdb_testmember_2 and can read but cdb_testmember_3 cannot
|
||||
sql cdb_testmember_1 "SELECT * FROM cartodb.CDB_Organization_Add_Table_Read_Permission('cdb_testmember_1', 'foo', 'cdb_testmember_2')"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_3 'SELECT count(*) FROM cdb_testmember_1.foo;' fails
|
||||
|
||||
# granting to organization allows to read to both: cdb_testmember_2 and cdb_testmember_3
|
||||
sql cdb_testmember_1 "SELECT cartodb.CDB_Organization_Add_Table_Organization_Read_Permission('cdb_testmember_1', 'foo');"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_3 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
|
||||
# removing access from organization should keep permission on cdb_testmember_2 but drop it to cdb_testmember_3
|
||||
sql cdb_testmember_1 "SELECT cartodb.CDB_Organization_Remove_Organization_Access_Permission('cdb_testmember_1', 'foo');"
|
||||
sql cdb_testmember_2 'SELECT count(*) FROM cdb_testmember_1.foo;' should 5
|
||||
sql cdb_testmember_3 'SELECT count(*) FROM cdb_testmember_1.foo;' fails
|
||||
|
||||
drop_role_and_schema cdb_testmember_3
|
||||
}
|
||||
|
||||
#################################################### TESTS END HERE ####################################################
|
||||
|
||||
|
||||
|
||||
run_tests $@
|
||||
|
||||
exit ${OK}
|
||||
@@ -4,7 +4,22 @@ ver=$1
|
||||
input=cartodb--${ver}.sql
|
||||
output=cartodb--unpackaged--${ver}.sql
|
||||
|
||||
cat ${input} | grep -v 'duplicated extension$' > ${output}
|
||||
echo "-- Script generated by $0 on `date`" > ${output}
|
||||
|
||||
# Migrate CDB functions from public schema to cartodb 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 /ALTER FUNCTION public./' |
|
||||
sed 's/$/ SET SCHEMA cartodb;/' |
|
||||
sed 's/^/DO LANGUAGE plpgsql \$\$ BEGIN /' |
|
||||
sed "s/$/ EXCEPTION WHEN OTHERS THEN RAISE NOTICE 'Got % (%)', SQLERRM, SQLSTATE; END; \$\$;/" |
|
||||
cat >> ${output}
|
||||
|
||||
# Upgrade all functions
|
||||
cat ${input} | grep -v 'duplicated extension$' >> ${output}
|
||||
|
||||
# Migrate CDB_TableMetadata
|
||||
cat >> ${output} <<'EOF'
|
||||
@@ -29,32 +44,34 @@ BEGIN
|
||||
DROP FUNCTION public._CDB_UserQuotaInBytes();
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
-- 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}
|
||||
## Cartodbfy tables with a trigger using 'CDB_CheckQuota' or
|
||||
## 'CDB_TableMetadata_Trigger' from the 'public' schema
|
||||
#cat >> ${output} <<'EOF'
|
||||
#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 any leftover function from public schema (there should be none)
|
||||
#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 IF EXISTS public./' |
|
||||
# cat >> ${output}
|
||||
|
||||
9
util/create_upgrade.sh
Executable file
9
util/create_upgrade.sh
Executable file
@@ -0,0 +1,9 @@
|
||||
#!/bin/sh
|
||||
|
||||
fromver=$1
|
||||
ver=$2
|
||||
input=cartodb--${ver}.sql
|
||||
output=cartodb--${fromver}--${ver}.sql
|
||||
|
||||
cat ${input} | grep -v 'duplicated extension$' > ${output}
|
||||
|
||||
Reference in New Issue
Block a user