Query goal: | Find cases where in different schemas there are enumerated or range types with the same name. Types are like words that can be used to construct generalized claims about the real world (table predicates). Better not to duplicate the words in the dictionary. Also make sure that this is not a duplication. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | High (Few or no false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Rename a type. In case of duplication drop the duplicates and refactor the database so that the remaining type will be used everywhere it is needed. |
Data source: | INFORMATION_SCHEMA+system catalog |
SQL query: | Click on query to copy it
WITH types AS (SELECT nspname AS type_schema, typname AS type_name, typtype FROM pg_catalog.pg_type t INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace=n.oid WHERE typtype IN ('e','r') AND nspname NOT IN (SELECT schema_name FROM INFORMATION_SCHEMA.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL)) SELECT type_name, string_agg(type_schema || '(' || CASE WHEN typtype='e' THEN 'ENUMERATED' ELSE 'RANGE' END || ')',';' ORDER BY type_schema) AS schemas, Count(*) AS number_of_types FROM types GROUP BY type_name HAVING Count(*)>1 ORDER BY type_name; |
SQL query | Description |
---|---|
WITH types AS (SELECT nspname AS type_schema, typname AS type_name FROM pg_catalog.pg_type t INNER JOIN pg_catalog.pg_namespace n ON t.typnamespace=n.oid WHERE typtype IN ('e','r') AND nspname NOT IN (SELECT schema_name FROM INFORMATION_SCHEMA.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL)), duplicates AS (SELECT type_name FROM types GROUP BY type_name HAVING Count(*)>1) SELECT format('DROP TYPE %1$I.%2$I;', type_schema, type_name) AS statements FROM types WHERE type_name IN (SELECT type_name FROM duplicates) ORDER BY type_schema, type_name; | Drop the duplicates. |
Collection name | Collection description |
---|---|
Find problems automatically | Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not . |
Category name | Category description |
---|---|
Comfortability of database evolution | Queries of this category provide information about the means that influence database evolution. |
Data types | Queries of this category provide information about the data types and their usage. |
Duplication of implementation elements | Queries of this catergory provide information about the duplication of the database objects. |
Naming | Queries of this category provide information about the style of naming. |
Reference |
---|
https://refactoring.guru/smells/alternative-classes-with-different-interfaces |
https://refactoring.guru/smells/duplicate-code |
The corresponding code smell in case of cleaning code is "G5: Duplication". (Robert C. Martin, Clean Code) |