Implements importer for old DB.

This commit is contained in:
Dominik Picheta 2018-05-21 23:12:08 +01:00
commit 3d860e9654
2 changed files with 86 additions and 7 deletions

66
src/importer.nim Normal file
View file

@ -0,0 +1,66 @@
import db_sqlite, strutils
let origFile = "nimforum.db-21-05-18"
let targetFile = "nimforum-blank.db"
let orig = open(connection=origFile, user="", password="",
database="nimforum")
let target = open(connection=targetFile, user="", password="",
database="nimforum")
block:
let fields = "id, name, views, modified"
for thread in getAllRows(orig, sql("select $1 from thread;" % fields)):
target.exec(
sql("""
insert into thread($1)
values (?, ?, ?, ?)
""" % fields),
thread
)
block:
let fields = "id, name, password, email, creation, salt, status, lastOnline"
for person in getAllRows(orig, sql("select $1 from person;" % fields)):
target.exec(
sql("""
insert into person($1)
values (?, ?, ?, ?, ?, ?, ?, ?)
""" % fields),
person
)
block:
let fields = "id, author, ip, content, thread, creation"
for post in getAllRows(orig, sql("select $1 from post;" % fields)):
target.exec(
sql("""
insert into post($1)
values (?, ?, ?, ?, ?, ?)
""" % fields),
post
)
block:
let fields = "id, name"
for t in getAllRows(orig, sql("select $1 from thread_fts;" % fields)):
target.exec(
sql("""
insert into thread_fts($1)
values (?, ?)
""" % fields),
t
)
block:
let fields = "id, content"
for p in getAllRows(orig, sql("select $1 from post_fts;" % fields)):
target.exec(
sql("""
insert into post_fts($1)
values (?, ?)
""" % fields),
p
)
echo("Imported!")

View file

@ -97,12 +97,13 @@ proc initialiseDb(admin: tuple[username, password, email: string],
db.exec sql"create index PersonStatusIdx on person(status);"
# Create default user.
let salt = makeSalt()
let password = makePassword(admin.password, salt)
db.exec(sql"""
insert into person (id, name, password, email, salt, status)
values (1, ?, ?, ?, ?, ?);
""", admin.username, password, admin.email, salt, $Admin)
if admin.username.len != 0:
let salt = makeSalt()
let password = makePassword(admin.password, salt)
db.exec(sql"""
insert into person (id, name, password, email, salt, status)
values (1, ?, ?, ?, ?, ?);
""", admin.username, password, admin.email, salt, $Admin)
# -- Post
@ -228,6 +229,11 @@ proc initialiseConfig(
backup(path, some(pretty(j)))
writeFile(path, pretty(j))
proc question(q: string): string =
while result.len == 0:
stdout.write(q)
result = stdin.readLine()
when isMainModule:
if paramCount() > 0:
case paramStr(1)
@ -265,5 +271,12 @@ when isMainModule:
admin=("admin", "admin", "admin@localhost.local"),
dbPath
)
of "--blank":
let dbPath = "nimforum-blank.db"
echo("Initialising blank DB...")
initialiseDb(
admin=("", "", ""),
dbPath
)
else:
quit("--dev|--test")
quit("--dev|--test|--prod")