85 lines
2.4 KiB
Python
85 lines
2.4 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import json
|
|
import sqlite3
|
|
|
|
EXPORT_FOLDER = "exported"
|
|
|
|
METADATA = EXPORT_FOLDER + "/metadata.json"
|
|
|
|
with open(METADATA, "rt") as f:
|
|
meta = json.load(f)
|
|
|
|
for collection in meta["collections"]:
|
|
with open(EXPORT_FOLDER + "/collections/" + collection + "/metadata.json", "rt") as f:
|
|
coll_meta = json.load(f)
|
|
|
|
OUTPUT_FILE = collection + ".db3"
|
|
TABLE_NAME = coll_meta["id"]
|
|
|
|
with open(EXPORT_FOLDER + "/collections/" + collection + "/documents.json", "rt") as f:
|
|
coll_docs = json.load(f)
|
|
|
|
db = sqlite3.connect(OUTPUT_FILE)
|
|
cursor = db.cursor()
|
|
|
|
# Create structure
|
|
ftypes = {}
|
|
sql = 'CREATE TABLE IF NOT EXISTS "' + TABLE_NAME + '" ('
|
|
for field in coll_meta["fields"]:
|
|
ftypes[field["id"]] = field["type"]
|
|
sql += '"' + field["id"] + '" '
|
|
|
|
if field["type"] in ["number", "date", "bool"]:
|
|
sql += 'INTEGER'
|
|
elif field["type"] == "image_path":
|
|
sql += 'BLOB'
|
|
else:
|
|
sql += 'TEXT'
|
|
|
|
# NOTE: The Collections JSON export doesn't properly separate
|
|
# multi-select values. I.e. multiple selections will show up as
|
|
# a string with all the values separated by commas in the JSON.
|
|
|
|
sql += ', '
|
|
sql = sql[:-2] + ');'
|
|
cursor.execute(sql)
|
|
|
|
# Import documents as records
|
|
for doc in coll_docs:
|
|
ifields = []
|
|
iplaces = []
|
|
ivalues = []
|
|
for key, value in doc.items():
|
|
if not key in ftypes:
|
|
continue
|
|
ifields.append('"' + key + '"')
|
|
placeholder = "?"
|
|
if ftypes[key] == "bool":
|
|
if value:
|
|
value = 1
|
|
else:
|
|
value = 0
|
|
elif ftypes[key] == "date":
|
|
placeholder = "unixepoch(?)"
|
|
elif ftypes[key] == "image_path":
|
|
if value:
|
|
with open(EXPORT_FOLDER + "/collections/" + collection + "/media/" + value, "rb") as f:
|
|
value = f.read()
|
|
iplaces.append(placeholder)
|
|
ivalues.append(value)
|
|
|
|
sql = 'INSERT INTO "' + collection + '" (' + ', '.join(ifields) + ') VALUES (' + ', '.join(iplaces) + ');'
|
|
print(sql)
|
|
cursor.execute(sql, ivalues)
|
|
|
|
db.commit()
|
|
|
|
|
|
# TODO: subcollection handling - not needed for now
|
|
|
|
db.commit()
|
|
db.close()
|
|
|
|
print(repr(meta))
|