aboutsummaryrefslogtreecommitdiffstats
path: root/import.py
diff options
context:
space:
mode:
authorThibaut Horel <thibaut.horel@gmail.com>2012-11-09 23:41:01 +0100
committerThibaut Horel <thibaut.horel@gmail.com>2012-11-09 23:41:01 +0100
commit76d58425de6a1e3505c777921b1e58bf03370366 (patch)
tree602fff4b0de26eae33dee7378426cee9f375b5d9 /import.py
parent867e8acae887638beb5107fb7a5c9f3e403d06e0 (diff)
downloadfamille-flask-76d58425de6a1e3505c777921b1e58bf03370366.tar.gz
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
Diffstat (limited to 'import.py')
-rw-r--r--import.py49
1 files changed, 49 insertions, 0 deletions
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()
+