summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2022-12-11 11:41:22 -0500
committerTim Flynn <trflynn89@pm.me>2022-12-14 09:21:30 -0500
commitb5fd96b7ac0e6ded8119819077cfeb6c272bd391 (patch)
tree0a180f28568dc0eace2f75f296d239b81fe976ca /Userland/Libraries
parentd5ed07fdc45ea3b6b4ffa204c639fef14576dde8 (diff)
downloadserenity-b5fd96b7ac0e6ded8119819077cfeb6c272bd391.zip
LibSQL: Ungracefully handle database version incompatibilities
In the long run, this is obviously a bad way to handle version changes to the SQL database files. We will want to migrate old databases to new formats. Until we figure out a good way to do that, wipe old databases so that we don't crash trying to read incompatible data.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibSQL/Heap.cpp12
-rw-r--r--Userland/Libraries/LibSQL/Heap.h4
2 files changed, 14 insertions, 2 deletions
diff --git a/Userland/Libraries/LibSQL/Heap.cpp b/Userland/Libraries/LibSQL/Heap.cpp
index 25a7dbfbe6..b7a87a8c9e 100644
--- a/Userland/Libraries/LibSQL/Heap.cpp
+++ b/Userland/Libraries/LibSQL/Heap.cpp
@@ -8,6 +8,7 @@
#include <AK/Format.h>
#include <AK/QuickSort.h>
#include <LibCore/IODevice.h>
+#include <LibCore/System.h>
#include <LibSQL/Heap.h>
#include <LibSQL/Serializer.h>
#include <sys/stat.h>
@@ -58,6 +59,15 @@ ErrorOr<void> Heap::open()
initialize_zero_block();
}
+ // FIXME: We should more gracefully handle version incompatibilities. For now, we drop the database.
+ if (m_version != current_version) {
+ dbgln_if(SQL_DEBUG, "Heap file {} opened has incompatible version {}. Deleting for version {}.", name(), m_version, current_version);
+ m_file = nullptr;
+
+ TRY(Core::System::unlink(name()));
+ return open();
+ }
+
dbgln_if(SQL_DEBUG, "Heap file {} opened. Size = {}", name(), size());
return {};
}
@@ -248,7 +258,7 @@ void Heap::update_zero_block()
void Heap::initialize_zero_block()
{
- m_version = 0x00000001;
+ m_version = current_version;
m_schemas_root = 0;
m_tables_root = 0;
m_table_columns_root = 0;
diff --git a/Userland/Libraries/LibSQL/Heap.h b/Userland/Libraries/LibSQL/Heap.h
index c76626a950..a703b9a7aa 100644
--- a/Userland/Libraries/LibSQL/Heap.h
+++ b/Userland/Libraries/LibSQL/Heap.h
@@ -33,6 +33,8 @@ class Heap : public Core::Object {
C_OBJECT(Heap);
public:
+ static constexpr inline u32 current_version = 1;
+
virtual ~Heap() override;
ErrorOr<void> open();
@@ -104,7 +106,7 @@ private:
u32 m_schemas_root { 0 };
u32 m_tables_root { 0 };
u32 m_table_columns_root { 0 };
- u32 m_version { 0x00000001 };
+ u32 m_version { current_version };
Array<u32, 16> m_user_values { 0 };
HashMap<u32, ByteBuffer> m_write_ahead_log;
};