Skip to content

Commit

Permalink
Use timezone-aware timestamp columns
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Jul 31, 2017
1 parent a4ca3d5 commit bc3a003
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions datastream/backends/influxdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,13 @@ def _create_schema(self):
pending_backprocess boolean NOT NULL DEFAULT false
)''')

# Schema version.
cursor.execute("SELECT obj_description('datastream.streams'::regclass, 'pg_class')")
try:
streams_version = int(cursor.fetchone()[0])
except TypeError:
streams_version = 0

try:
cursor.execute('SAVEPOINT add_timestamp_columns')
cursor.execute('''ALTER TABLE datastream.streams
Expand All @@ -880,6 +887,18 @@ def _create_schema(self):
# Ignore error when columns already exist. We need to support PostgreSQL <= 9.5.
cursor.execute('ROLLBACK TO SAVEPOINT add_timestamp_columns')

# Migration v1: use explicit timestamp with time zone columns.
if streams_version <= 0:
cursor.execute('''ALTER TABLE datastream.streams
ALTER COLUMN latest_datapoint TYPE timestamp with time zone
USING latest_datapoint AT TIME ZONE 'UTC',
ALTER COLUMN earliest_datapoint TYPE timestamp with time zone
USING earliest_datapoint AT TIME ZONE 'UTC'
''')

# Bump version.
cursor.execute("COMMENT ON TABLE datastream.streams IS '1'")

# Create a GIN index if one doesn't yet exist.
cursor.execute('SELECT to_regclass(\'datastream.streams_tags\')')
if cursor.fetchone()[0] is None:
Expand Down

0 comments on commit bc3a003

Please sign in to comment.