Skip to content

Commit 5ea91b0

Browse files
author
Jacob Beck
committedApr 26, 2019
Fix postgres text handling
Fix incorrect maximum text field length on redshift On postgres, pass through text without size information to fix archival of long text fields Add a test that makes sure postgres archives work with very long text entries
1 parent 32f39f3 commit 5ea91b0

File tree

8 files changed

+73
-1
lines changed

8 files changed

+73
-1
lines changed
 

‎core/dbt/adapters/base/relation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ def string_size(self):
361361

362362
if self.dtype == 'text' or self.char_size is None:
363363
# char_size should never be None. Handle it reasonably just in case
364-
return 255
364+
return 256
365365
else:
366366
return int(self.char_size)
367367

‎plugins/postgres/dbt/adapters/postgres/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# these are mostly just exports, #noqa them so flake8 will be happy
22
from dbt.adapters.postgres.connections import PostgresConnectionManager # noqa
33
from dbt.adapters.postgres.connections import PostgresCredentials
4+
from dbt.adapters.postgres.relation import PostgresColumn # noqa
45
from dbt.adapters.postgres.impl import PostgresAdapter
56

67
from dbt.adapters.base import AdapterPlugin

‎plugins/postgres/dbt/adapters/postgres/impl.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from dbt.adapters.base.meta import available
22
from dbt.adapters.sql import SQLAdapter
33
from dbt.adapters.postgres import PostgresConnectionManager
4+
from dbt.adapters.postgres import PostgresColumn
45
import dbt.compat
56
import dbt.exceptions
67

@@ -11,6 +12,7 @@
1112

1213
class PostgresAdapter(SQLAdapter):
1314
ConnectionManager = PostgresConnectionManager
15+
Column = PostgresColumn
1416

1517
@classmethod
1618
def date_function(cls):
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
from dbt.adapters.base import Column
2+
3+
4+
class PostgresColumn(Column):
5+
@property
6+
def data_type(self):
7+
# on postgres, do not convert 'text' to 'varchar()'
8+
if self.dtype.lower() == 'text':
9+
return self.dtype
10+
return super(PostgresColumn, self).data_type

‎plugins/redshift/dbt/adapters/redshift/impl.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
from dbt.adapters.postgres import PostgresAdapter
22
from dbt.adapters.redshift import RedshiftConnectionManager
3+
from dbt.adapters.base import Column
34
from dbt.logger import GLOBAL_LOGGER as logger # noqa
45

56

67
class RedshiftAdapter(PostgresAdapter):
78
ConnectionManager = RedshiftConnectionManager
9+
# postgres overrides this, so override the override.
10+
Column = Column
811

912
AdapterSpecificConfigs = frozenset({"sort_type", "dist", "sort", "bind"})
1013

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
create table {database}.{schema}.super_long (
2+
id INTEGER,
3+
longstring TEXT,
4+
updated_at TIMESTAMP WITHOUT TIME ZONE
5+
);
6+
7+
insert into {database}.{schema}.super_long (id, longstring, updated_at) VALUES
8+
(1, 'short', current_timestamp),
9+
(2, repeat('a', 500), current_timestamp);
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% archive archive_actual %}
2+
{{
3+
config(
4+
target_database=var('target_database', database),
5+
target_schema=schema,
6+
unique_key='id',
7+
strategy='timestamp',
8+
updated_at='updated_at',
9+
)
10+
}}
11+
select * from {{database}}.{{schema}}.super_long
12+
{% endarchive %}

‎test/integration/004_simple_archive_test/test_simple_archive.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,38 @@ def test__bigquery__archive_with_new_field(self):
498498

499499
self.assertEqual(expected_name, actual_name, "names are different")
500500
self.assertEqual(expected_type, actual_type, "data types are different")
501+
502+
503+
class TestLongText(DBTIntegrationTest):
504+
505+
@property
506+
def schema(self):
507+
return "simple_archive_004"
508+
509+
@property
510+
def models(self):
511+
return "test/integration/004_simple_archive_test/models"
512+
513+
def run_archive(self):
514+
return self.run_dbt(['archive'])
515+
516+
@property
517+
def project_config(self):
518+
return {
519+
"archive-paths": ['test/integration/004_simple_archive_test/test-archives-longtext'],
520+
}
521+
522+
@use_profile('postgres')
523+
def test__postgres__long_text(self):
524+
self.run_sql_file('test/integration/004_simple_archive_test/seed_longtext.sql')
525+
results = self.run_dbt(['archive'])
526+
self.assertEqual(len(results), 1)
527+
528+
with self.adapter.connection_named('test'):
529+
status, results = self.adapter.execute(
530+
'select * from {}.{}.archive_actual'.format(self.default_database, self.unique_schema()),
531+
fetch=True
532+
)
533+
self.assertEqual(len(results), 2)
534+
got_names = set(r.get('longstring') for r in results)
535+
self.assertEqual(got_names, {'a' * 500, 'short'})

0 commit comments

Comments
 (0)