Skip to content

Instantly share code, notes, and snippets.

@Fedjmike
Last active April 24, 2016 14:43
Show Gist options
  • Save Fedjmike/cca5cdea870019c721562c6416b05025 to your computer and use it in GitHub Desktop.
Save Fedjmike/cca5cdea870019c721562c6416b05025 to your computer and use it in GitHub Desktop.
Convert user and action creation datetimes from ISO 8601 (EDT) to Unix timestamps
import arrow
from model import Model
def convert_actions():
with Model() as model:
model.db.executescript(
"""drop table if exists actions_2;
create table actions_2 (
id integer primary key,
user_id integer not null,
object_id integer not null,
type integer not null, -- model.ActionType enum
creation integer not null, -- Unix timestamp
foreign key (user_id) references users(id),
foreign key (object_id) references objects(id)
);"""
)
for id, user_id, object_id, type, creation in model.query("select * from actions"):
print(creation)
new_creation = arrow.get(creation).replace(hours=+4).timestamp
model.insert("insert into actions_2 (id, user_id, object_id, type, creation)"
"values (?, ?, ?, ?, ?)", id, user_id, object_id, type, new_creation)
model.execute("alter table actions rename to actions_1")
model.execute("alter table actions_2 rename to actions")
def convert_users():
with Model() as model:
model.db.executescript(
"""drop table if exists users_2;
create table users_2 (
id integer primary key,
name text not null,
pw_hash text not null,
email text,
fullname text,
creation integer not null -- Unix timestamp
);"""
)
for id, name, pw_hash, email, fullname, creation in model.query("select * from users"):
print(creation)
new_creation = arrow.get(creation).replace(hours=+4).timestamp
model.insert("insert into users_2 (id, name, pw_hash, email, fullname, creation)"
"values (?, ?, ?, ?, ?, ?)", id, name, pw_hash, email, fullname, new_creation)
model.execute("alter table users rename to users_1")
model.execute("alter table users_2 rename to users")
convert_users()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment