From 76d58425de6a1e3505c777921b1e58bf03370366 Mon Sep 17 00:00:00 2001 From: Thibaut Horel Date: Fri, 9 Nov 2012 23:41:01 +0100 Subject: Import old database. The database is in the file new.db. dump,sql contains a sqlite3 compliant dump. To regenerate the database from the dump: $ sqlite3 old.db < dump.sql $ python createdb.py -s schema.sql -d new.db $ python import.py old.db new.db --- import.py | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 import.py (limited to 'import.py') diff --git a/import.py b/import.py new file mode 100644 index 0000000..976dce4 --- /dev/null +++ b/import.py @@ -0,0 +1,49 @@ +import sqlite3 +import sys + +def convert_date(date): + return "T".join(date.split(" ")) + +def import_users(old_conn, new_conn): + for row in old_conn.execute("select * from users"): + address = old_conn.execute("select * from adress where id=?", (row["adress_id"],)).fetchone() + new_conn.execute("insert into users (id, user_name, password, birthday, " + "nameday, notify, email, phone, address_line1, address_line2, " + "address_city_line) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", + (row["id"], row["name"], row["password"], + "/".join((str(row["birthday"]), str(row["birthmonth"]))), + "/".join((str(row["feastday"]), str(row["feastmonth"]))), + row["notify"], row["email"], row["cellphone"], + address["line1"], address["line2"], " ".join((address["zip"], + address["city"])))) + new_conn.commit() + +def import_news(old_conn, new_conn): + for row in old_conn.execute("select * from news"): + new_conn.execute("insert into news (id, title, date, content, content_cache, " + "user_id) VALUES(?, ?, ?, ?, ?, ?)", + (row["id"], row["title"], convert_date(row["date"]), + row["raw"], row["news"], row["auteur_id"])) + new_conn.commit() + +def import_comments(old_conn, new_conn): + for row in old_conn.execute("select * from news_comments"): + new_conn.execute("insert into comments (id, date, content_cache, user_id, news_id) " + "values (?, ?, ?, ?, ?)", + (row["id"], convert_date(row["date"]), row["comment"], + row["auteur_id"], row["news_id"])) + new_conn.commit() + +if __name__ == "__main__": + old = sys.argv[1] + new = sys.argv[2] + old_conn = sqlite3.connect(old) + old_conn.row_factory = sqlite3.Row + new_conn = sqlite3.connect(new) + new_conn.row_factory = sqlite3.Row + import_users(old_conn, new_conn) + import_news(old_conn, new_conn) + import_comments(old_conn, new_conn) + old_conn.close() + new_conn.close() + -- cgit v1.2.3-70-g09d2