From 9c6029fcfb47c30a7c3548a038147f0854ecb28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Herman=20=C3=98ie=20Kolden?= Date: Sat, 29 Jun 2024 08:07:29 +0200 Subject: [PATCH] db: store ints as ints, not floats DatabaseULog.write_ulog complained about wrong value types during struct packing. --- pyulog/db.py | 2 +- pyulog/sql/pyulog.4.sql | 67 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 pyulog/sql/pyulog.4.sql diff --git a/pyulog/db.py b/pyulog/db.py index d28acb3..6d14905 100644 --- a/pyulog/db.py +++ b/pyulog/db.py @@ -49,7 +49,7 @@ class DatabaseULog(ULog): contsructor will throw an exception. See the documentation of "ulog_migratedb" for more information. ''' - SCHEMA_VERSION = 3 + SCHEMA_VERSION = 4 @staticmethod def get_db_handle(db_path): diff --git a/pyulog/sql/pyulog.4.sql b/pyulog/sql/pyulog.4.sql new file mode 100644 index 0000000..94f0842 --- /dev/null +++ b/pyulog/sql/pyulog.4.sql @@ -0,0 +1,67 @@ +BEGIN; +PRAGMA foreign_keys=off; + +-- Change REAL timestamps to INT. SQLITE only supports INT64, but ULog +-- timestamps are UINT64. We accept losing 1 bit at the top end, since 2^63 +-- microseconds = 400,000 years. which should be enough. + +CREATE TABLE IF NOT EXISTS ULog_tmp ( + Id INTEGER PRIMARY KEY AUTOINCREMENT, + SHA256Sum TEXT UNIQUE, + FileVersion INT, + StartTimestamp INT, -- changed from REAL + LastTimestamp INT, -- changed from REAL + CompatFlags TEXT, + IncompatFlags TEXT, + SyncCount INT, + HasSync BOOLEAN +); +INSERT OR IGNORE INTO ULog_tmp (Id, SHA256Sum, FileVersion, StartTimestamp, LastTimestamp, CompatFlags, IncompatFlags, SyncCount, HasSync) SELECT Id, SHA256Sum, FileVersion, StartTimestamp, LastTimestamp, CompatFlags, IncompatFlags, SyncCount, HasSync FROM ULog; + +CREATE TABLE IF NOT EXISTS ULogMessageDropout_tmp ( + Timestamp INT, -- changed from REAL + Duration INT, -- changed from FLOAT + ULogId INT REFERENCES ULog (Id) ON DELETE CASCADE +); +INSERT OR IGNORE INTO ULogMessageDropout_tmp SELECT * FROM ULogMessageDropout; + +CREATE TABLE IF NOT EXISTS ULogMessageLogging_tmp ( + LogLevel INT, + Timestamp INT, + Message TEXT, + ULogId INT REFERENCES ULog (Id) ON DELETE CASCADE +); +INSERT OR IGNORE INTO ULogMessageLogging_tmp SELECT * FROM ULogMessageLogging; + +CREATE TABLE IF NOT EXISTS ULogMessageLoggingTagged_tmp ( + LogLevel INT, + Timestamp INT, -- changed from REAL + Tag INT, + Message TEXT, + ULogId INT REFERENCES ULog (Id) ON DELETE CASCADE +); +INSERT OR IGNORE INTO ULogMessageLoggingTagged_tmp SELECT * FROM ULogMessageLoggingTagged; + +CREATE TABLE IF NOT EXISTS ULogChangedParameter_tmp ( + Timestamp INT, -- changed from REAL + Key TEXT, + Value BLOB, + ULogId INT REFERENCES ULog (Id) ON DELETE CASCADE +); +INSERT OR IGNORE INTO ULogChangedParameter_tmp SELECT * FROM ULogChangedParameter; + + +DROP TABLE ULog; +DROP TABLE ULogMessageDropout; +DROP TABLE ULogMessageLogging; +DROP TABLE ULogMessageLoggingTagged; +DROP TABLE ULogChangedParameter; + +ALTER TABLE ULog_tmp RENAME TO ULog; +ALTER TABLE ULogMessageDropout_tmp RENAME TO ULogMessageDropout; +ALTER TABLE ULogMessageLogging_tmp RENAME TO ULogMessageLogging; +ALTER TABLE ULogMessageLoggingTagged_tmp RENAME TO ULogMessageLoggingTagged; +ALTER TABLE ULogChangedParameter_tmp RENAME TO ULogChangedParameter; + +PRAGMA foreign_keys=on; +COMMIT;