Skip to content

Commit

Permalink
Merge pull request #2727 from fishtown-analytics/fix/2197-long-table-…
Browse files Browse the repository at this point in the history
…names

Check Postgres relation name lengths and throw error when over 63
  • Loading branch information
gshank authored Aug 28, 2020
2 parents 75faceb + 5830f55 commit 2cc2d97
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 0 deletions.
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://github.com/fishtown-analytics/dbt/issues/2492), [#2721](https://github.com/fishtown-analytics/dbt/pull/2721))
- Check for Postgres relation names longer than 63 and throw exception. ([#2197](https://github.com/fishtown-analytics/dbt/issues/2197))


### Fixes
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
19 changes: 19 additions & 0 deletions plugins/postgres/dbt/adapters/postgres/relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,23 @@
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"Relation name '{self.identifier}' "
f"is longer than {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)

0 comments on commit 2cc2d97

Please sign in to comment.