diff --git a/Tests/LibSQL/TestSqlDatabase.cpp b/Tests/LibSQL/TestSqlDatabase.cpp index a266706c48..4700f09efe 100644 --- a/Tests/LibSQL/TestSqlDatabase.cpp +++ b/Tests/LibSQL/TestSqlDatabase.cpp @@ -110,7 +110,7 @@ TEST_CASE(create_heap) ScopeGuard guard([]() { unlink("/tmp/test.db"); }); auto heap = SQL::Heap::construct("/tmp/test.db"); EXPECT(!heap->open().is_error()); - EXPECT_EQ(heap->version(), 0x00000001u); + EXPECT_EQ(heap->version(), SQL::Heap::current_version); } TEST_CASE(create_from_dev_random) 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 #include #include +#include #include #include #include @@ -58,6 +59,15 @@ ErrorOr 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 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 m_user_values { 0 }; HashMap m_write_ahead_log; };