Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding migration script to adjust current activity timestamps to utc
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# encoding: utf-8 | ||
|
||
import pytz | ||
import tzlocal | ||
import logging | ||
|
||
log = logging.getLogger(__name__) | ||
|
||
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() | ||
|
||
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)) |