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 3 commits
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 CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

### Under the hood
- Added 3 more adapter methods that the new dbt-adapter-test suite can use for testing. ([#2492](https:/fishtown-analytics/dbt/issues/2492), [#2721](https:/fishtown-analytics/dbt/pull/2721))
- Check for Postgres relation names longer than 63 and throw exception. ([#2197](https:/fishtown-analytics/dbt/issues/2197))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should also add a link to this PR!



## dbt 0.18.0rc1 (August 19, 2020)
Expand Down
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 # noqa: F401
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
22 changes: 22 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
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(
f"Postgres relation name '{self.identifier}' is longer than "
gshank marked this conversation as resolved.
Show resolved Hide resolved
f"{self.relation_max_name_length()} characters"
)

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 # noqa: F401
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.postgres.relation import PostgresRelation


@dataclass(frozen=True, eq=False, repr=False)
class RedshiftRelation(PostgresRelation):
# Override the method in the Postgres Relation
# because Redshift allows longer names
def relation_max_name_length(self):
return 127


class RedshiftColumn(Column):
Expand Down
4 changes: 4 additions & 0 deletions test/integration/063_relation_name_tests/data/seed.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
col_A,col_B
1,2
3,4
5,6
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

select * from {{ this.schema }}.seed
34 changes: 34 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,34 @@
from test.integration.base import DBTIntegrationTest, use_profile
import dbt.exceptions

class TestAdapterDDL(DBTIntegrationTest):

def setUp(self):
DBTIntegrationTest.setUp(self)
self.run_dbt(['seed'])

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

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

@property
def project_config(self):
return {
'config-version': 2,
'seeds': {
'quote_columns': False,
},
}

@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)