Files
chanbans/convert_db.py

140 lines
3.0 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# Convert between the old database format and the new one.
# You probably won't need to run this script.
import json
import sqlite3
from chanbans.db import get_db
def dict_factory(cursor, row):
d = {}
for idx, col in enumerate(cursor.description):
d[col[0]] = row[idx]
return d
# Create a new database too
old_db = get_db()
new_db = sqlite3.connect("new_db.db")
new_db.executescript(
"""
create table if not exists bans(
id integer primary key,
action varchar(5),
board varchar(10),
length varchar(10),
now varchar(30),
name varchar(100),
trip varchar(30),
com text,
time varchar(30),
sub varchar(100),
nsfw boolean,
thumb varchar(100),
ext varchar(10),
w int,
h int,
tn_w int,
tn_h int,
md5 varchar(100),
fsize integer,
filename text,
tim varchar(30),
thumb_path text,
reason varchar(200)
);
"""
)
old_db.row_factory = dict_factory
bans = old_db.execute("SELECT * FROM BANS").fetchall()
print("Converting", len(bans), "rows")
for ban in bans:
if 'post' not in ban:
print("ERROR: The database either does not exist, or already exists in the new format. Exiting")
raise SystemExit(1)
post = json.loads(ban["post"])
post["action"] = ban["action"]
# post['board'] = ban['board']
post["length"] = ban["length"]
post["thumb_path"] = ban["thumb_path"]
post["reason"] = ban["reason"]
if "trip" not in post:
post["trip"] = None
if "nsfw" not in post:
post["nsfw"] = False
if "thumb" not in post:
post["thumb"] = None
if "ext" not in post:
post["ext"] = None
if "w" not in post:
post["w"] = 0
else:
post['w'] = int(post['w'])
if "h" not in post:
post["h"] = 0
else:
post['h'] = int(post['h'])
if "tn_w" not in post:
post["tn_w"] = 0
else:
post['tn_w'] = int(post['tn_w'])
if "tn_h" not in post:
post["tn_h"] = 0
else:
post['tn_h'] = int(post['tn_h'])
if "md5" not in post:
post["md5"] = None
if "fsize" not in post:
post["fsize"] = None
if "filename" not in post:
post["filename"] = None
if "tim" not in post:
post["tim"] = None
new_db.execute(
"""
INSERT INTO bans (action, board, length, now, name, trip, com, time, sub, nsfw, thumb, ext, w, h, tn_w, tn_h, md5, fsize, filename, tim, thumb_path, reason)
VALUES (
:action,
:board,
:length,
:now,
:name,
:trip,
:com,
:time,
:sub,
:nsfw,
:thumb,
:ext,
:w,
:h,
:tn_w,
:tn_h,
:md5,
:fsize,
:filename,
:tim,
:thumb_path,
:reason
)
""",
post,
)
new_db.commit()