Skip to content

Commit

Permalink
Save new db 21 with JSON data field
Browse files Browse the repository at this point in the history
  • Loading branch information
dsblank committed Oct 13, 2024
1 parent 43ea2b2 commit 1350f5c
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 19 deletions.
3 changes: 3 additions & 0 deletions gramps/gen/db/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2698,3 +2698,6 @@ def get_schema_version(self):
def set_schema_version(self, value):
"""set the current schema version"""
self._set_metadata("version", str(value))

def upgrade_table_for_json_access(self, table_name):
pass
7 changes: 1 addition & 6 deletions gramps/gen/db/upgrade.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,7 @@ def gramps_upgrade_21(self):
self._txn_begin()

for table_name in self._get_table_func():
# FIXME: temp, move to dbapi
try:
self.dbapi.execute("ALTER TABLE %s ADD COLUMN json_data TEXT;" % table_name.lower())
self.dbapi.commit()
except Exception:
pass
self.upgrade_table_for_json_access(table_name.lower())

get_obj_from_handle = self._get_table_func(table_name, "handle_func")
get_handles = self._get_table_func(table_name, "handles_func")
Expand Down
8 changes: 5 additions & 3 deletions gramps/plugins/db/bsddb/bsddb.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,14 @@ def load(
self._txn_begin()
if new_t == "metadata":
sql = "REPLACE INTO metadata (setting, value) VALUES " "(?, ?)"
data_access = pickle
else:
sql = "INSERT INTO %s (handle, blob_data) VALUES " "(?, ?)" % new_t
sql = "INSERT INTO %s (handle, json_data) VALUES " "(?, ?)" % new_t
data_access = json

for key in dbmap.keys():
self.update()
data = pickle.loads(dbmap[key], encoding="utf-8")
data = data_access.loads(dbmap[key], encoding="utf-8")

if new_t == "metadata":
if key == b"version":
Expand Down Expand Up @@ -215,7 +217,7 @@ def load(
# These are list, but need to be set
data = set(data)

self.dbapi.execute(sql, [key.decode("utf-8"), pickle.dumps(data)])
self.dbapi.execute(sql, [key.decode("utf-8"), data_access.dumps(data)])

# get schema version from file if not in metadata
if new_t == "metadata" and schema_vers is None:
Expand Down
27 changes: 17 additions & 10 deletions gramps/plugins/db/dbapi/dbapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ class DBAPI(DbGeneric):
def _initialize(self, directory, username, password):
raise NotImplementedError

def upgrade_table_for_json_access(self, table_name):
try:
self.dbapi.execute("ALTER TABLE %s ADD COLUMN json_data TEXT;" % table_name)
self.dbapi.commit()
except Exception:
pass

def _setup_data_access(self, format):
"""
Called by subclasses to setup data access SQL field and method.
Expand Down Expand Up @@ -124,71 +131,71 @@ def _create_schema(self):
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"given_name TEXT, "
"surname TEXT, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE family "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE source "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE citation "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE event "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE media "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE place "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"enclosed_by VARCHAR(50), "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE repository "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE note "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
self.dbapi.execute(
"CREATE TABLE tag "
"("
"handle VARCHAR(50) PRIMARY KEY NOT NULL, "
"blob_data BLOB"
"json_data TEXT"
")"
)
# Secondary:
Expand Down

0 comments on commit 1350f5c

Please sign in to comment.