Skip to content

Commit

Permalink
Make run_started_at timezone aware (#553) (#556)
Browse files Browse the repository at this point in the history
* Make run_started_at timezone aware

Set run_started_at timezone to UTC
Enable timezone change in models
Extend requirements
Extend tests

* Address comments from code review

Create modules namespace to context
Move pytz to modules
Add new dependencies to setup.py
  • Loading branch information
drewbanin authored Dec 2, 2017
1 parent f7f78c0 commit 2afc202
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 1 deletion.
4 changes: 4 additions & 0 deletions dbt/context/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import os
import pytz
import voluptuous

from dbt.adapters.factory import get_adapter
Expand Down Expand Up @@ -288,6 +289,9 @@ def generate(model, project, flat_graph, provider=None):
"graph": flat_graph,
"log": log,
"model": model,
"modules": {
"pytz": pytz,
},
"post_hooks": post_hooks,
"pre_hooks": pre_hooks,
"ref": provider.ref(model, project, profile, flat_graph),
Expand Down
3 changes: 2 additions & 1 deletion dbt/tracking.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from snowplow_tracker import SelfDescribingJson, disable_contracts
from datetime import datetime

import pytz
import platform
import uuid
import yaml
Expand Down Expand Up @@ -44,7 +45,7 @@ def __init__(self):

self.id = None
self.invocation_id = str(uuid.uuid4())
self.run_started_at = datetime.now()
self.run_started_at = datetime.now(tz=pytz.utc)

def state(self):
return "do not track" if self.do_not_track else "tracking"
Expand Down
2 changes: 2 additions & 0 deletions dev_requirements.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
freezegun==0.3.9
nose>=1.3.7
mock>=1.3.0
pep8>=1.6.2
pytz==2017.2
bumpversion==0.5.3
coverage==4.2
tox==2.5.0
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
argparse>=1.2.1
freezegun==0.3.9
Jinja2>=2.8
pytz==2017.2
PyYAML>=3.11
psycopg2==2.7.1
sqlparse==0.2.3
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@
'scripts/dbt',
],
install_requires=[
'freezegun==0.3.9',
'Jinja2>=2.8',
'pytz==2017.2',
'PyYAML>=3.11',
'psycopg2==2.7.1',
'sqlparse==0.2.3',
Expand Down
10 changes: 10 additions & 0 deletions test/integration/025_timezones_test/models/timezones.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

{{
config(
materialized='table'
)
}}

select
'{{ run_started_at.astimezone(modules.pytz.timezone("America/New_York")) }}' as run_started_at_est,
'{{ run_started_at }}' as run_started_at_utc
55 changes: 55 additions & 0 deletions test/integration/025_timezones_test/test_timezones.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
from freezegun import freeze_time
from nose.plugins.attrib import attr
from test.integration.base import DBTIntegrationTest


class TestTimezones(DBTIntegrationTest):

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

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

@property
def models(self):
return "test/integration/025_timezones_test/models"

@property
def profile_config(self):
return {
'test': {
'outputs': {
'dev': {
'type': 'postgres',
'threads': 1,
'host': 'database',
'port': 5432,
'user': "root",
'pass': "password",
'dbname': 'dbt',
'schema': self.unique_schema()
},
},
'target': 'dev'
}
}

@property
def query(self):
return """
select
run_started_at_est,
run_started_at_utc
from {schema}.timezones
""".format(schema=self.unique_schema())

@freeze_time("2017-01-01 03:00:00", tz_offset=0)
@attr(type='postgres')
def test_run_started_at(self):
self.run_dbt(['run'])
result = self.run_sql(self.query, fetch='all')[0]
est, utc = result
self.assertEqual(utc, '2017-01-01 03:00:00+00:00')
self.assertEqual(est, '2016-12-31 22:00:00-05:00')

0 comments on commit 2afc202

Please sign in to comment.