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

Make run_started_at timezone aware #553

Merged
merged 2 commits into from
Oct 9, 2017
Merged
Show file tree
Hide file tree
Changes from all 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
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 @@ -277,6 +278,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
Copy link
Contributor

Choose a reason for hiding this comment

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

i think these modules also need to be added to setup.py

Copy link
Member

Choose a reason for hiding this comment

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

they do

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