diff options
| author | Thibaut Horel <thibaut.horel@gmail.com> | 2012-11-10 12:08:56 +0100 |
|---|---|---|
| committer | Thibaut Horel <thibaut.horel@gmail.com> | 2012-11-10 12:08:56 +0100 |
| commit | f96f407ebaf86fcf2cca1638527f1dc80d2b035c (patch) | |
| tree | 0762ef11feb8047ca01575179584af5a89a43cb3 | |
| parent | 1ae302ff76adcb188dad5383e73c5ade158ffd8a (diff) | |
| download | famille-flask-f96f407ebaf86fcf2cca1638527f1dc80d2b035c.tar.gz | |
Follow up to previous commit.
* Make flask db connnection also use date converters to avoid crashing
when displaying dates
* Update the import script to import dates from the old databse as timestamps
By the way, I strongly disagree with this choice of using timestamps, for the
following reasons:
* why would timestamps be a better representation of times than ISO 8601? they
cannot represent times before the epoch, they are more easily subject to "year
2000"-like bugs. No wonder why the vast majority of web protocols use ISO 8601
instead of timestamps.
* Even MySQL internally uses ISO 8601 strings to store datetime fields
* This solution requires to write unsupported sqlite code: there is no
"timestamp" datatype in sqlite3, so you implicitely rely on sqlite3 casting this
to a string. You also rely on the python doing implicit conversions from the
database to datetime objects. The funniest part is that the way Python deals
with this implicit conversion is by storing the datetime object as a "%Y-%m-%d
%H:%M:%S" string in the database (which is by the way, not even standard ISO
8601:2004)! No timestamp anywhere to be seen, even though
this is what is stated in schema.sql... This looks insane to me...
Having explicit and standard conventions at the expense of two additional lines
of code does not seems that insane in comparison to Python's insanity.
python. The funniest part is that they way Python does this implicit conversion
is by internally storing the
| -rw-r--r-- | famille.py | 3 | ||||
| -rw-r--r-- | import.py | 5 | ||||
| -rw-r--r-- | new.db | bin | 106496 -> 106496 bytes |
3 files changed, 5 insertions, 3 deletions
@@ -52,7 +52,8 @@ def query_db(query, args=(), one=False): @app.before_request def before_request(): - conn = sqlite3.connect(app.config['DATABASE']) + conn = sqlite3.connect(app.config['DATABASE'], + detect_types=sqlite3.PARSE_DECLTYPES) conn.row_factory = sqlite3.Row g.db = conn @@ -1,8 +1,9 @@ import sqlite3 import sys +from datetime import datetime def convert_date(date): - return "T".join(date.split(" ")) + return datetime.strptime(date, "%Y-%m-%d %H:%M:%S") def import_users(old_conn, new_conn): for row in old_conn.execute("select * from users"): @@ -39,7 +40,7 @@ if __name__ == "__main__": new = sys.argv[2] old_conn = sqlite3.connect(old) old_conn.row_factory = sqlite3.Row - new_conn = sqlite3.connect(new) + new_conn = sqlite3.connect(new, detect_types=sqlite3.PARSE_DECLTYPES) new_conn.row_factory = sqlite3.Row import_users(old_conn, new_conn) import_news(old_conn, new_conn) Binary files differ |
