Skip to content

Commit

Permalink
Using only sql statement, removed python and logging
Browse files Browse the repository at this point in the history
  • Loading branch information
jrods authored and amercader committed Feb 28, 2018
1 parent d39056a commit 2421db6
Showing 1 changed file with 8 additions and 27 deletions.
35 changes: 8 additions & 27 deletions ckan/migration/versions/085_adjust_activity_timestamps.py
@@ -1,33 +1,14 @@
# encoding: utf-8

import pytz
import tzlocal
import logging

log = logging.getLogger(__name__)
import datetime

def upgrade(migrate_engine):
"""
The script assumes that the current timestamp was recorded with the server's current set timezone
"""
local_tz = tzlocal.get_localzone()
"""
The script assumes that the current timestamp was recorded with the server's current set timezone
"""
utc_now = datetime.datetime.utcnow()
local_now = datetime.datetime.now()

with migrate_engine.begin() as connection:
sql = "select id, timestamp from activity"
results = connection.execute(sql)

log.info("Adjusting Activity timestamp, server's localtime zone: {tz}".format(tz=local_tz))

for row in results:
id, timestamp = row

timestamp = timestamp.replace(tzinfo=local_tz)
to_utc = timestamp.astimezone(pytz.utc)
to_utc = to_utc.replace(tzinfo=None)

update_sql = "update activity set timestamp = %s where id = %s"

connection.execute(update_sql, to_utc, id)

log.info("""Adjusting Activity timestamp to UTC for {id}: {old_ts} --> {new_ts}
""".format(id=id, old_ts=timestamp, new_ts=to_utc))
sql = "update activity set timestamp = timestamp + (%s - %s);"
connection.execute(sql, utc_now, local_now)

0 comments on commit 2421db6

Please sign in to comment.