Merge pull request #173 from CartoDB/better-unique-identifiers
Better unique identifiers
This commit is contained in:
commit
e99231252e
4
Makefile
4
Makefile
@ -1,7 +1,7 @@
|
||||
# cartodb/Makefile
|
||||
|
||||
EXTENSION = cartodb
|
||||
EXTVERSION = 0.11.2
|
||||
EXTVERSION = 0.11.3
|
||||
|
||||
SED = sed
|
||||
|
||||
@ -11,6 +11,7 @@ CDBSCRIPTS = \
|
||||
scripts-available/CDB_DDLTriggers.sql \
|
||||
scripts-available/CDB_ExtensionPost.sql \
|
||||
scripts-available/CDB_ExtensionUtils.sql \
|
||||
scripts-available/CDB_Helper.sql \
|
||||
$(END)
|
||||
|
||||
UPGRADABLE = \
|
||||
@ -53,6 +54,7 @@ UPGRADABLE = \
|
||||
0.11.0 \
|
||||
0.11.1 \
|
||||
0.11.2 \
|
||||
0.11.3 \
|
||||
$(EXTVERSION)dev \
|
||||
$(EXTVERSION)next \
|
||||
$(END)
|
||||
|
7
NEWS.md
7
NEWS.md
@ -1,6 +1,13 @@
|
||||
next (2015-mm-dd)
|
||||
-----------------
|
||||
|
||||
0.11.3 (2015-10-27)
|
||||
-------------------
|
||||
* Added CDB_Helper.sql [#173](https://github.com/CartoDB/cartodb-postgresql/pull/173)
|
||||
* Added _CDB_Unique_Identifier for creating UTF8 aware unique identifiers
|
||||
* Added _CDB_Unique_Column_Identifier for creating UTF8 aware unique identifiers for columns
|
||||
* Added _CDB_Octet_Truncate that truncates text to a certain amount of octets.
|
||||
|
||||
0.11.2 (2015-10-19)
|
||||
-------------------
|
||||
* Fix schema not being specified on pg_get_serial_sequence [#170](https://github.com/CartoDB/cartodb-postgresql/pull/170)
|
||||
|
@ -1,4 +1,5 @@
|
||||
-- Depends on:
|
||||
-- * CDB_Helper.sql
|
||||
-- * CDB_ExtensionUtils.sql
|
||||
-- * CDB_TransformToWebmercator.sql
|
||||
-- * CDB_TableMetadata.sql
|
||||
@ -427,8 +428,9 @@ END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
|
||||
-- Find a unique relation name in the given schema, starting from the
|
||||
-- template given. If the template is already unique, just return it;
|
||||
-- DEPRECATED: Use _CDB_Unique_Identifier since it's UTF8 Safe and length
|
||||
-- aware. Find a unique relation name in the given schema, starting from the
|
||||
-- template given. If the template is already unique, just return it;
|
||||
-- otherwise, append an increasing integer until you find a unique variant.
|
||||
CREATE OR REPLACE FUNCTION _CDB_Unique_Relation_Name(schemaname TEXT, relationname TEXT)
|
||||
RETURNS TEXT
|
||||
@ -439,36 +441,15 @@ DECLARE
|
||||
newrelname TEXT;
|
||||
BEGIN
|
||||
|
||||
i := 0;
|
||||
newrelname := relationname;
|
||||
LOOP
|
||||
RAISE EXCEPTION '_CDB_Unique_Relation_Name is DEPRECATED. Use _CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)';
|
||||
|
||||
SELECT c.relname, n.nspname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON c.relnamespace = n.oid
|
||||
WHERE c.relname = newrelname
|
||||
AND n.nspname = schemaname;
|
||||
|
||||
IF NOT FOUND THEN
|
||||
RETURN newrelname;
|
||||
END IF;
|
||||
|
||||
i := i + 1;
|
||||
newrelname := relationname || '_' || i;
|
||||
|
||||
IF i > 100 THEN
|
||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Relation_Name');
|
||||
END IF;
|
||||
|
||||
END LOOP;
|
||||
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
|
||||
-- Find a unique column name in the given relation, starting from the
|
||||
-- column name given. If the column name is already unique, just return it;
|
||||
-- DEPRECATED: Use _CDB_Unique_Column_Identifier since it's UTF8 Safe and length
|
||||
-- aware. Find a unique column name in the given relation, starting from the
|
||||
-- column name given. If the column name is already unique, just return it;
|
||||
-- otherwise, append an increasing integer until you find a unique variant.
|
||||
CREATE OR REPLACE FUNCTION _CDB_Unique_Column_Name(reloid REGCLASS, columnname TEXT)
|
||||
RETURNS TEXT
|
||||
@ -479,32 +460,8 @@ DECLARE
|
||||
newcolname TEXT;
|
||||
BEGIN
|
||||
|
||||
i := 0;
|
||||
newcolname := columnname;
|
||||
LOOP
|
||||
RAISE EXCEPTION '_CDB_Unique_Column_Name is DEPRECATED. Use _CDB_Unique_Column_Identifier(prefix TEXT, relname TEXT, suffix TEXT, reloid REGCLASS DEFAULT NULL)';
|
||||
|
||||
SELECT a.attname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||
WHERE NOT a.attisdropped
|
||||
AND a.attnum > 0
|
||||
AND c.oid = reloid
|
||||
AND a.attname = newcolname;
|
||||
|
||||
IF NOT FOUND THEN
|
||||
RETURN newcolname;
|
||||
END IF;
|
||||
|
||||
i := i + 1;
|
||||
newcolname := columnname || '_' || i;
|
||||
|
||||
IF i > 100 THEN
|
||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Column_Name');
|
||||
END IF;
|
||||
|
||||
END LOOP;
|
||||
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
@ -589,7 +546,7 @@ BEGIN
|
||||
PERFORM _CDB_SQL(
|
||||
Format('ALTER TABLE %s RENAME COLUMN %s TO %I',
|
||||
reloid::text, rec.attname,
|
||||
_CDB_Unique_Column_Name(reloid, const.pkey)),
|
||||
cartodb._CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
|
||||
'_CDB_Has_Usable_Primary_ID');
|
||||
|
||||
END IF;
|
||||
@ -606,7 +563,7 @@ BEGIN
|
||||
|
||||
PERFORM _CDB_SQL(
|
||||
Format('ALTER TABLE %s RENAME COLUMN %s TO %I',
|
||||
reloid::text, rec.attname, _CDB_Unique_Column_Name(reloid, const.pkey)),
|
||||
reloid::text, rec.attname, cartodb._CDB_Unique_Column_Identifier(NULL, const.pkey, NULL, reloid)),
|
||||
'_CDB_Has_Usable_Primary_ID');
|
||||
|
||||
END IF;
|
||||
@ -772,7 +729,7 @@ BEGIN
|
||||
WHEN others THEN
|
||||
IF SQLERRM = 'parse error - invalid geometry' THEN
|
||||
text_geom_column := false;
|
||||
str := _CDB_Unique_Column_Name(reloid, r1.attname);
|
||||
str := cartodb._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
|
||||
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
|
||||
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
||||
@ -784,7 +741,7 @@ BEGIN
|
||||
|
||||
-- Just change its name so we can write a new column into that name.
|
||||
ELSE
|
||||
str := _CDB_Unique_Column_Name(reloid, r1.attname);
|
||||
str := cartodb._CDB_Unique_Column_Identifier(NULL, r1.attname, NULL, reloid);
|
||||
sql := Format('ALTER TABLE %s RENAME COLUMN %s TO %I', reloid::text, r1.attname, str);
|
||||
PERFORM _CDB_SQL(sql,'_CDB_Has_Usable_Geom');
|
||||
RAISE DEBUG 'CDB(_CDB_Has_Usable_Geom): %',
|
||||
@ -854,8 +811,7 @@ DECLARE
|
||||
destname TEXT;
|
||||
destseq TEXT;
|
||||
destseqmax INTEGER;
|
||||
|
||||
salt TEXT := md5(random()::text || now());
|
||||
|
||||
copyname TEXT;
|
||||
|
||||
column_name_sql TEXT;
|
||||
@ -961,19 +917,18 @@ BEGIN
|
||||
-- Put the primary key sequence in the right schema
|
||||
-- If the new table is not moving, better ensure the sequence name
|
||||
-- is unique
|
||||
destseq := relname || '_' || const.pkey || '_seq';
|
||||
destseq := _CDB_Unique_Relation_Name(destschema, destseq);
|
||||
destseq := cartodb._CDB_Unique_Identifier(NULL, relname, '_' || const.pkey || '_seq', destschema);
|
||||
destseq := Format('%I.%I', destschema, destseq);
|
||||
PERFORM _CDB_SQL(Format('CREATE SEQUENCE %s', destseq), '_CDB_Rewrite_Table');
|
||||
|
||||
-- Salt a temporary table name if we are re-writing in place
|
||||
-- Temporary table name if we are re-writing in place
|
||||
-- Note copyname is already escaped and safe to use as identifier
|
||||
IF destschema = relschema THEN
|
||||
copyname := Format('%I.%I', destschema, Format('%s_%s', destname, salt));
|
||||
copyname := Format('%I.%I', destschema, cartodb._CDB_Unique_Identifier(NULL, destname, NULL), destschema);
|
||||
ELSE
|
||||
copyname := Format('%I.%I', destschema, destname);
|
||||
END IF;
|
||||
|
||||
|
||||
-- Start building the SQL!
|
||||
sql := Format('CREATE TABLE %s AS SELECT ', copyname);
|
||||
|
||||
|
156
scripts-available/CDB_Helper.sql
Normal file
156
scripts-available/CDB_Helper.sql
Normal file
@ -0,0 +1,156 @@
|
||||
-- UTF8 safe and length aware. Find a unique identifier with a given prefix
|
||||
-- and/or suffix and withing a schema. If a schema is not specified, the identifier
|
||||
-- is guaranteed to be unique for all schemas.
|
||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Identifier(prefix TEXT, relname TEXT, suffix TEXT, schema TEXT DEFAULT NULL)
|
||||
RETURNS TEXT
|
||||
AS $$
|
||||
DECLARE
|
||||
maxlen CONSTANT INTEGER := 63;
|
||||
|
||||
rec RECORD;
|
||||
usedspace INTEGER;
|
||||
ident TEXT;
|
||||
origident TEXT;
|
||||
candrelname TEXT;
|
||||
|
||||
i INTEGER;
|
||||
BEGIN
|
||||
-- Accounts for the _XX incremental suffix in case the identifier is taken
|
||||
usedspace := 3;
|
||||
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
||||
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
||||
|
||||
candrelname := _CDB_Octet_Truncate(relname, maxlen - usedspace);
|
||||
|
||||
IF candrelname = '' THEN
|
||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Identifier');
|
||||
END IF;
|
||||
|
||||
ident := coalesce(prefix, '') || candrelname || coalesce(suffix, '');
|
||||
|
||||
i := 0;
|
||||
origident := ident;
|
||||
|
||||
WHILE i < 100 LOOP
|
||||
IF schema IS NOT NULL THEN
|
||||
SELECT c.relname, n.nspname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON c.relnamespace = n.oid
|
||||
WHERE c.relname = ident
|
||||
AND n.nspname = schema;
|
||||
ELSE
|
||||
SELECT c.relname, n.nspname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON c.relnamespace = n.oid
|
||||
WHERE c.relname = ident;
|
||||
END IF;
|
||||
|
||||
IF NOT FOUND THEN
|
||||
RETURN ident;
|
||||
END IF;
|
||||
|
||||
ident := origident || '_' || i;
|
||||
i := i + 1;
|
||||
END LOOP;
|
||||
|
||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Identifier');
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
|
||||
-- UTF8 safe and length aware. Find a unique identifier for a column with a given prefix
|
||||
-- and/or suffix based on colname and within a relation specified via reloid.
|
||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Unique_Column_Identifier(prefix TEXT, colname TEXT, suffix TEXT, reloid REGCLASS)
|
||||
RETURNS TEXT
|
||||
AS $$
|
||||
DECLARE
|
||||
maxlen CONSTANT INTEGER := 63;
|
||||
|
||||
rec RECORD;
|
||||
candcolname TEXT;
|
||||
usedspace INTEGER;
|
||||
ident TEXT;
|
||||
origident TEXT;
|
||||
|
||||
i INTEGER;
|
||||
BEGIN
|
||||
-- Accounts for the _XX incremental suffix in case the identifier is taken
|
||||
usedspace := 3;
|
||||
usedspace := usedspace + coalesce(octet_length(prefix), 0);
|
||||
usedspace := usedspace + coalesce(octet_length(suffix), 0);
|
||||
|
||||
candcolname := _CDB_Octet_Truncate(colname, maxlen - usedspace);
|
||||
|
||||
IF candcolname = '' THEN
|
||||
PERFORM _CDB_Error('prefixes are to long to generate a valid identifier', '_CDB_Unique_Column_Identifier');
|
||||
END IF;
|
||||
|
||||
ident := coalesce(prefix, '') || candcolname || coalesce(suffix, '');
|
||||
|
||||
i := 0;
|
||||
origident := ident;
|
||||
|
||||
WHILE i < 100 LOOP
|
||||
SELECT a.attname
|
||||
INTO rec
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute a ON a.attrelid = c.oid
|
||||
WHERE NOT a.attisdropped
|
||||
AND a.attnum > 0
|
||||
AND c.oid = reloid
|
||||
AND a.attname = ident;
|
||||
|
||||
IF NOT FOUND THEN
|
||||
RETURN ident;
|
||||
END IF;
|
||||
|
||||
ident := origident || '_' || i;
|
||||
i := i + 1;
|
||||
END LOOP;
|
||||
|
||||
PERFORM _CDB_Error('looping too far', '_CDB_Unique_Column_Identifier');
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
||||
|
||||
|
||||
-- Truncates a given string to a max_octets octets taking care
|
||||
-- not to leave characters in half. UTF8 safe.
|
||||
CREATE OR REPLACE FUNCTION cartodb._CDB_Octet_Truncate(string TEXT, max_octets INTEGER)
|
||||
RETURNS TEXT
|
||||
AS $$
|
||||
DECLARE
|
||||
extcharlen CONSTANT INTEGER := octet_length('ñ');
|
||||
|
||||
expected INTEGER;
|
||||
examined INTEGER;
|
||||
strlen INTEGER;
|
||||
|
||||
i INTEGER;
|
||||
BEGIN
|
||||
|
||||
IF max_octets <= 0 THEN
|
||||
RETURN '';
|
||||
ELSIF max_octets >= octet_length(string) THEN
|
||||
RETURN string;
|
||||
END IF;
|
||||
|
||||
strlen := char_length(string);
|
||||
|
||||
expected := char_length(string);
|
||||
examined := octet_length(string);
|
||||
|
||||
IF expected = examined THEN
|
||||
RETURN left(string, max_octets);
|
||||
END IF;
|
||||
|
||||
i := max_octets / extcharlen;
|
||||
|
||||
WHILE octet_length(left(string, i)) <= max_octets LOOP
|
||||
i := i + 1;
|
||||
END LOOP;
|
||||
|
||||
RETURN left(string, (i - 1));
|
||||
END;
|
||||
$$ LANGUAGE 'plpgsql';
|
@ -291,7 +291,7 @@ INSERT INTO test VALUES
|
||||
(NULL),
|
||||
(3);
|
||||
SELECT CDB_CartodbfyTableCheck('test', 'Table with null cartodb_id #148');
|
||||
SELECT cartodb_id, cartodb_id_1 from test;
|
||||
SELECT cartodb_id, cartodb_id_0 from test;
|
||||
DROP TABLE test;
|
||||
|
||||
-- Table with non unique cartodb_id
|
||||
@ -303,7 +303,7 @@ INSERT INTO test VALUES
|
||||
(2),
|
||||
(2);
|
||||
SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique cartodb_id #148');
|
||||
SELECT cartodb_id, cartodb_id_1 from test;
|
||||
SELECT cartodb_id, cartodb_id_0 from test;
|
||||
DROP TABLE test;
|
||||
|
||||
-- Table with non unique and null cartodb_id
|
||||
@ -316,7 +316,7 @@ INSERT INTO test VALUES
|
||||
(NULL),
|
||||
(2);
|
||||
SELECT CDB_CartodbfyTableCheck('test', 'Table with non unique and null cartodb_id #148');
|
||||
SELECT cartodb_id, cartodb_id_1 from test;
|
||||
SELECT cartodb_id, cartodb_id_0 from test;
|
||||
DROP TABLE test;
|
||||
|
||||
|
||||
|
128
test/CDB_HelperTest.sql
Normal file
128
test/CDB_HelperTest.sql
Normal file
@ -0,0 +1,128 @@
|
||||
-- Test unique identifier creation with normal length normal relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'relname', NULL);
|
||||
|
||||
-- Test unique identifier creation with prefix with normal length normal relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier('prefix_', 'relname', NULL);
|
||||
|
||||
-- Test unique identifier creation with suffix with normal length normal relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'relname', '_suffix');
|
||||
|
||||
-- Test unique identifier creation with long length normal relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'largolargolargolargolargolargolargolargolargolargolargolargolar', NULL);
|
||||
|
||||
-- Test unique identifier creation with prefix with long length normal relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier('prefix_', 'largolargolargolargolargolargolargolargolargolargolargolargolar', NULL);
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE prefix_largolargolargolargolargolargolargolargolargolargolar (name text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier('prefix_', 'largolargolargolargolargolargolargolargolargolargolargolargolar', NULL);
|
||||
DROP TABLE prefix_largolargolargolargolargolargolargolargolargolargolar;
|
||||
|
||||
-- Test unique identifier creation with suffix with long length normal relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'largolargolargolargolargolargolargolargolargolargolargolargolar', '_suffix');
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE largolargolargolargolargolargolargolargolargolargolar_suffix (name text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'largolargolargolargolargolargolargolargolargolargolargolargolar', '_suffix');
|
||||
DROP TABLE largolargolargolargolargolargolargolargolargolargolar_suffix;
|
||||
|
||||
-- Test unique identifier creation with normal length UTF8 relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'piraña', NULL);
|
||||
|
||||
-- Test unique identifier creation with prefix with normal length UTF8 relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier('prefix_', 'piraña', NULL);
|
||||
|
||||
-- Test unique identifier creation with suffix with normal length UTF8 relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'piraña', '_suffix');
|
||||
|
||||
-- Test unique identifier creation with long length UTF8 relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', NULL);
|
||||
|
||||
-- Test unique identifier creation with prefix with long length UTF8 relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier('prefix_', 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', NULL);
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi (name text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier('prefix_', 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', NULL);
|
||||
DROP TABLE prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi;
|
||||
|
||||
-- Test unique identifier creation with suffix with long length UTF8 relname
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', '_suffix');
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix (name text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Identifier(NULL, 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', '_suffix');
|
||||
DROP TABLE piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix;
|
||||
|
||||
CREATE TABLE test (name text);
|
||||
-- Test unique identifier creation with normal length normal colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'colname', NULL, 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with prefix with normal length normal colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier('prefix_', 'colname', NULL, 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with suffix with normal length normal colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'colname', '_suffix', 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with long length normal colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'largolargolargolargolargolargolargolargolargolargolargolargolar', NULL, 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with prefix with long length normal colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier('prefix_', 'largolargolargolargolargolargolargolargolargolargolargolargolar', NULL, 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE test (prefix_largolargolargolargolargolargolargolargolargolargolar text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier('prefix_', 'largolargolargolargolargolargolargolargolargolargolargolargolar', NULL, 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test unique identifier creation with suffix with long length normal colname
|
||||
CREATE TABLE test (name text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'largolargolargolargolargolargolargolargolargolargolargolargolar', '_suffix', 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE test (largolargolargolargolargolargolargolargolargolargolar_suffix text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'largolargolargolargolargolargolargolargolargolargolargolargolar', '_suffix', 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
CREATE TABLE test (name text);
|
||||
-- Test unique identifier creation with normal length UTF8 colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'piraña', NULL, 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with prefix with normal length UTF8 colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier('prefix_', 'piraña', NULL, 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with suffix with normal length UTF8 colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'piraña', '_suffix', 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with long length UTF8 colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', NULL, 'test'::regclass);
|
||||
|
||||
-- Test unique identifier creation with prefix with long length UTF8 colname
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier('prefix_', 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', NULL, 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE test (prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier('prefix_', 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', NULL, 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test unique identifier creation with suffix with long length UTF8 colname
|
||||
CREATE TABLE test (name text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', '_suffix', 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test new identifier is found when name is taken from previous case
|
||||
CREATE TABLE test (piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix text);
|
||||
SELECT * FROM cartodb._CDB_Unique_Column_Identifier(NULL, 'piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácidpin', '_suffix', 'test'::regclass);
|
||||
DROP TABLE test;
|
||||
|
||||
-- Test _CDB_Octet_Truncate simple case
|
||||
SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 5);
|
||||
|
||||
-- Test _CDB_Octet_Truncate UTF8 case
|
||||
SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 6);
|
||||
|
||||
-- Test _CDB_Octet_Truncate UTF8 case
|
||||
SELECT * FROM cartodb._CDB_Octet_Truncate('piraña', 7);
|
59
test/CDB_HelperTest_expect
Normal file
59
test/CDB_HelperTest_expect
Normal file
@ -0,0 +1,59 @@
|
||||
relname
|
||||
prefix_relname
|
||||
relname_suffix
|
||||
largolargolargolargolargolargolargolargolargolargolargolargo
|
||||
prefix_largolargolargolargolargolargolargolargolargolargolar
|
||||
CREATE TABLE
|
||||
prefix_largolargolargolargolargolargolargolargolargolargolar_0
|
||||
DROP TABLE
|
||||
largolargolargolargolargolargolargolargolargolargolar_suffix
|
||||
CREATE TABLE
|
||||
largolargolargolargolargolargolargolargolargolargolar_suffix_0
|
||||
DROP TABLE
|
||||
piraña
|
||||
prefix_piraña
|
||||
piraña_suffix
|
||||
piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácid
|
||||
prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi
|
||||
CREATE TABLE
|
||||
prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_0
|
||||
DROP TABLE
|
||||
piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix
|
||||
CREATE TABLE
|
||||
piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix_0
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
colname
|
||||
prefix_colname
|
||||
colname_suffix
|
||||
largolargolargolargolargolargolargolargolargolargolargolargo
|
||||
prefix_largolargolargolargolargolargolargolargolargolargolar
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
prefix_largolargolargolargolargolargolargolargolargolargolar_0
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
largolargolargolargolargolargolargolargolargolargolar_suffix
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
largolargolargolargolargolargolargolargolargolargolar_suffix_0
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
piraña
|
||||
prefix_piraña
|
||||
piraña_suffix
|
||||
piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpiñaácid
|
||||
prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
prefix_piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_0
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix
|
||||
DROP TABLE
|
||||
CREATE TABLE
|
||||
piñaácidpiñaácidpiñaácidpiñaácidpiñaácidpi_suffix_0
|
||||
DROP TABLE
|
||||
pira
|
||||
pirañ
|
||||
piraña
|
Loading…
Reference in New Issue
Block a user