Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check Postgres relation name lengths and throw error when over 63 #2727

Merged
merged 4 commits into from
Aug 28, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugins/postgres/dbt/adapters/postgres/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from dbt.adapters.postgres.connections import PostgresConnectionManager # noqa
from dbt.adapters.postgres.connections import PostgresCredentials
from dbt.adapters.postgres.relation import PostgresColumn # noqa
from dbt.adapters.postgres.relation import PostgresRelation
gshank marked this conversation as resolved.
Show resolved Hide resolved
from dbt.adapters.postgres.impl import PostgresAdapter

from dbt.adapters.base import AdapterPlugin
Expand Down
2 changes: 2 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.postgres import PostgresConnectionManager
from dbt.adapters.postgres import PostgresColumn
from dbt.adapters.postgres import PostgresRelation
import dbt.exceptions


Expand All @@ -18,6 +19,7 @@ class PostgresConfig(AdapterConfig):


class PostgresAdapter(SQLAdapter):
Relation = PostgresRelation
ConnectionManager = PostgresConnectionManager
Column = PostgresColumn

Expand Down
25 changes: 25 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,29 @@
from dbt.adapters.base import Column
from dataclasses import dataclass
from dbt.adapters.base.relation import BaseRelation
from dbt.exceptions import RuntimeException


@dataclass(frozen=True, eq=False, repr=False)
class PostgresRelation(BaseRelation):
def __post_init__(self):
# Check for length of Postgres table/view names.
# Check self.type to exclude test relation identifiers
if (
self.identifier is not None
and self.type is not None
and len(self.identifier) > self.relation_max_name_length()
):
raise RuntimeException(
"Postgres relation name '"
+ self.identifier
+ "' is longer than "
+ str(self.relation_max_name_length())
+ " characters"
)
gshank marked this conversation as resolved.
Show resolved Hide resolved

def relation_max_name_length(self):
return 63


class PostgresColumn(Column):
Expand Down
1 change: 1 addition & 0 deletions plugins/redshift/dbt/adapters/redshift/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dbt.adapters.redshift.connections import RedshiftConnectionManager # noqa
from dbt.adapters.redshift.connections import RedshiftCredentials
from dbt.adapters.redshift.relation import RedshiftColumn # noqa
from dbt.adapters.redshift.relation import RedshiftRelation
gshank marked this conversation as resolved.
Show resolved Hide resolved
from dbt.adapters.redshift.impl import RedshiftAdapter


Expand Down
2 changes: 2 additions & 0 deletions plugins/redshift/dbt/adapters/redshift/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dbt.adapters.postgres import PostgresAdapter
from dbt.adapters.redshift import RedshiftConnectionManager
from dbt.adapters.redshift import RedshiftColumn
from dbt.adapters.redshift import RedshiftRelation
from dbt.logger import GLOBAL_LOGGER as logger # noqa


Expand All @@ -16,6 +17,7 @@ class RedshiftConfig(AdapterConfig):


class RedshiftAdapter(PostgresAdapter):
Relation = RedshiftRelation
ConnectionManager = RedshiftConnectionManager
Column = RedshiftColumn

Expand Down
10 changes: 10 additions & 0 deletions plugins/redshift/dbt/adapters/redshift/relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
from dbt.adapters.base import Column
from dataclasses import dataclass
from dbt.adapters.base.relation import BaseRelation


@dataclass(frozen=True, eq=False, repr=False)
class RedshiftRelation(BaseRelation):
# Override the method in the Postgres Relation
# because Redshift allows longer names
def relation_max_name_length(self):
return 127
gshank marked this conversation as resolved.
Show resolved Hide resolved


class RedshiftColumn(Column):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

select * from {{ this.schema }}.seed
111 changes: 111 additions & 0 deletions test/integration/063_relation_name_tests/seed.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
create table {schema}.seed (
id INTEGER,
first_name TEXT,
email TEXT,
ip_address TEXT,
birthday TIMESTAMP WITHOUT TIME ZONE
);


INSERT INTO {schema}.seed
("id","first_name","email","ip_address","birthday")
VALUES
(1,'Larry','[email protected]','69.135.206.194','2008-09-12 19:08:31'),
(2,'Larry','[email protected]','64.210.133.162','1978-05-09 04:15:14'),
(3,'Anna','[email protected]','168.104.64.114','2011-10-16 04:07:57'),
(4,'Sandra','[email protected]','229.235.252.98','1973-07-19 10:52:43'),
(5,'Fred','[email protected]','78.229.170.124','2012-09-30 16:38:29'),
(6,'Stephen','[email protected]','182.227.157.105','1995-11-07 21:40:50'),
(7,'William','[email protected]','135.139.249.50','1982-09-05 03:11:59'),
(8,'Jessica','[email protected]','203.62.178.210','1991-10-16 11:03:15'),
(9,'Douglas','[email protected]','178.187.247.1','1979-10-01 09:49:48'),
(10,'Lisa','[email protected]','168.234.128.249','2011-05-26 07:45:49'),
(11,'Ralph','[email protected]','55.152.163.149','1972-11-18 19:06:11'),
(12,'Louise','[email protected]','141.116.153.154','2014-11-25 20:56:14'),
(13,'Clarence','[email protected]','81.171.31.133','2011-11-17 07:02:36'),
(14,'Daniel','[email protected]','8.204.211.37','1980-09-13 00:09:04'),
(15,'Katherine','[email protected]','176.96.134.59','1997-08-22 19:36:56'),
(16,'Billy','[email protected]','214.108.78.85','2003-10-19 02:14:47'),
(17,'Annie','[email protected]','190.108.42.70','1988-10-28 15:12:35'),
(18,'Shirley','[email protected]','109.251.164.84','1988-08-24 10:50:57'),
(19,'Roger','[email protected]','38.145.218.108','1985-12-31 15:17:15'),
(20,'Lillian','[email protected]','47.57.236.17','1970-06-08 02:09:05'),
(21,'Aaron','[email protected]','205.245.118.221','1985-10-11 23:07:49'),
(22,'Patrick','[email protected]','19.8.100.182','2006-03-29 12:53:56'),
(23,'Phillip','[email protected]','41.38.254.103','2011-11-07 15:35:43'),
(24,'Henry','[email protected]','1.191.216.252','2008-08-28 08:30:44'),
(25,'Irene','[email protected]','50.17.60.190','1994-04-01 07:15:02'),
(26,'Andrew','[email protected]','123.52.253.176','2000-11-01 06:03:25'),
(27,'David','[email protected]','238.23.203.42','1988-01-25 07:29:18'),
(28,'Henry','[email protected]','248.102.2.185','1983-01-01 13:36:37'),
(29,'Evelyn','[email protected]','32.80.46.119','1979-07-16 17:24:12'),
(30,'Tammy','[email protected]','249.246.167.88','2001-04-03 10:00:23'),
(31,'Jacqueline','[email protected]','127.181.97.47','1986-02-11 21:35:50'),
(32,'Earl','[email protected]','166.47.248.240','1996-07-06 08:16:27'),
(33,'Juan','[email protected]','71.77.2.200','1987-01-31 03:46:44'),
(34,'Diane','[email protected]','140.94.133.12','1994-06-11 02:30:05'),
(35,'Randy','[email protected]','73.255.34.196','2005-05-26 20:28:39'),
(36,'Janice','[email protected]','22.214.227.32','1990-02-09 04:16:52'),
(37,'Laura','[email protected]','159.148.145.73','2015-03-17 05:59:25'),
(38,'Gary','[email protected]','40.193.124.56','1970-01-27 10:04:51'),
(39,'Jesse','[email protected]','31.7.86.103','2009-03-14 08:14:29'),
(40,'Sandra','[email protected]','223.80.168.239','1993-05-21 14:08:54'),
(41,'Scott','[email protected]','38.238.46.83','1980-08-30 11:16:56'),
(42,'Phillip','[email protected]','158.234.59.34','2011-12-15 23:26:31'),
(43,'Steven','[email protected]','90.247.57.68','2011-10-29 19:03:28'),
(44,'Deborah','[email protected]','179.125.143.240','1995-04-10 14:36:07'),
(45,'Lori','[email protected]','64.80.162.180','1980-12-27 16:49:15'),
(46,'Sean','[email protected]','240.116.183.69','1988-06-12 21:24:45'),
(47,'Terry','[email protected]','118.38.213.137','1997-09-22 16:43:19'),
(48,'Dorothy','[email protected]','116.81.76.49','2005-02-28 13:33:24'),
(49,'Samuel','[email protected]','38.191.253.40','1989-01-19 21:15:48'),
(50,'Ralph','[email protected]','104.84.60.174','2007-08-11 10:21:49'),
(51,'Wayne','[email protected]','90.61.24.102','1983-07-03 16:58:12'),
(52,'Rose','[email protected]','240.83.81.10','1995-06-08 11:46:23'),
(53,'Louise','[email protected]','105.11.82.145','2016-09-19 14:45:51'),
(54,'Kenneth','[email protected]','139.5.45.94','1976-08-17 11:26:19'),
(55,'Donna','[email protected]','19.45.169.45','2006-05-27 16:51:40'),
(56,'Johnny','[email protected]','118.202.238.23','1975-11-17 08:42:32'),
(57,'Patrick','[email protected]','231.25.153.198','1997-08-06 11:51:09'),
(58,'Helen','[email protected]','8.40.21.39','1993-08-04 19:53:40'),
(59,'Patricia','[email protected]','212.198.40.15','1977-08-03 16:37:27'),
(60,'Joseph','[email protected]','13.15.63.238','2005-07-23 20:22:06'),
(61,'Phillip','[email protected]','177.98.201.190','1976-05-19 21:47:44'),
(62,'Joan','[email protected]','105.229.170.71','1972-09-07 17:53:47'),
(63,'Phyllis','[email protected]','35.145.8.244','2000-01-01 22:33:37'),
(64,'Katherine','[email protected]','248.168.205.32','1991-01-09 06:40:24'),
(65,'Laura','[email protected]','128.129.115.152','1997-10-23 12:04:56'),
(66,'Juan','[email protected]','44.228.124.51','2004-11-10 05:07:35'),
(67,'Judith','[email protected]','40.227.179.115','1977-08-02 17:01:45'),
(68,'Beverly','[email protected]','208.34.84.59','2016-03-06 20:07:23'),
(69,'Lawrence','[email protected]','59.158.212.223','1988-09-13 06:07:21'),
(70,'Gloria','[email protected]','245.231.88.33','1995-03-18 22:32:46'),
(71,'Steven','[email protected]','104.50.58.255','2001-08-05 21:26:20'),
(72,'Betty','[email protected]','103.177.214.220','1981-12-14 21:26:54'),
(73,'Mildred','[email protected]','151.158.8.130','2000-04-19 10:13:55'),
(74,'Donald','[email protected]','9.178.102.255','1972-12-03 00:58:24'),
(75,'Eric','[email protected]','85.2.241.227','1992-11-01 05:59:30'),
(76,'Joyce','[email protected]','169.224.20.36','1985-10-24 06:50:01'),
(77,'Maria','[email protected]','143.189.167.135','2005-10-05 05:17:42'),
(78,'Harry','[email protected]','156.47.176.237','1978-03-26 05:53:33'),
(79,'Kevin','[email protected]','79.136.183.83','1994-10-12 04:38:52'),
(80,'David','[email protected]','133.149.172.153','1976-12-15 16:24:24'),
(81,'Kathy','[email protected]','229.242.72.228','1979-03-04 02:58:56'),
(82,'Adam','[email protected]','13.145.21.10','1982-11-07 11:46:59'),
(83,'Brandon','[email protected]','73.249.128.212','2013-10-30 05:30:36'),
(84,'Henry','[email protected]','211.36.214.242','1985-01-09 06:37:27'),
(85,'Eric','[email protected]','191.166.188.251','2004-05-01 23:21:42'),
(86,'Jason','[email protected]','193.92.16.182','1973-01-08 09:05:39'),
(87,'Diana','[email protected]','19.130.175.245','1994-10-05 22:50:49'),
(88,'Andrea','[email protected]','94.155.233.96','2002-04-26 08:41:44'),
(89,'Louis','[email protected]','26.217.34.111','2003-08-25 07:56:39'),
(90,'Jane','[email protected]','43.4.220.135','1987-03-20 20:39:04'),
(91,'Larry','[email protected]','97.126.79.34','2000-09-07 20:26:19'),
(92,'Louis','[email protected]','37.148.40.127','2011-09-16 20:12:14'),
(93,'Jennifer','[email protected]','38.106.254.142','1988-07-15 05:06:49'),
(94,'Wayne','[email protected]','223.28.26.187','2009-12-15 06:16:54'),
(95,'Lori','[email protected]','181.250.181.58','1984-10-28 03:29:19'),
(96,'Judy','[email protected]','180.121.239.219','1986-02-07 15:18:10'),
(97,'Phillip','[email protected]','255.247.0.175','2002-12-26 08:44:45'),
(98,'Gloria','[email protected]','156.140.7.128','1997-10-04 07:58:58'),
(99,'Paul','[email protected]','183.59.198.197','1991-11-14 12:33:55');
26 changes: 26 additions & 0 deletions test/integration/063_relation_name_tests/test_relation_name.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from test.integration.base import DBTIntegrationTest, use_profile
import dbt.exceptions

class TestAdapterDDL(DBTIntegrationTest):

def setUp(self):
DBTIntegrationTest.setUp(self)

self.run_sql_file("seed.sql")

@property
def schema(self):
return "adapter_ddl_063"

@property
def models(self):
return "models"

@use_profile('postgres')
def test_postgres_long_name_fails(self):
self.run_dbt(['run'],expect_pass=False)

@use_profile('redshift')
def test_redshift_long_name_succeeds(self):
self.run_dbt(['run'],expect_pass=True)