Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use separate *.sqlite files for each database version #401

Merged
merged 1 commit into from Jan 1, 2019
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -57,23 +57,25 @@ WorkspaceLibraryDb::WorkspaceLibraryDb(Workspace& ws)
qDebug("Load workspace library database...");

// open SQLite database
FilePath dbFilePath = ws.getLibrariesPath().getPathTo("cache.sqlite");
mDb.reset(new SQLiteDatabase(dbFilePath)); // can throw
mFilePath = ws.getLibrariesPath().getPathTo(
QString("cache_v%1.sqlite").arg(sCurrentDbVersion));
mDb.reset(new SQLiteDatabase(mFilePath)); // can throw
This conversation was marked as resolved by ubruhin

This comment has been minimized.

Copy link
@dbrgn

dbrgn Dec 29, 2018

Member

Do we ever delete old database files, or will they accumulate over the years?

This comment has been minimized.

Copy link
@ubruhin

ubruhin Dec 29, 2018

Author Member

No we don't remove old databases because we can't know if the user still wants to open the workspace with an older application version. But I think we won't introduce a new database version very often, so there won't be too many old databases. And if the user really cares about these few megabytes, he could remove them by himself.


// if the db has an old version, just remove the whole db and create a new one
// Check database version - actually it must match the version in the
// filename, but if not (e.g. due to a mistake by us) we just remove the whole
// database and create a new one.
This conversation was marked as resolved by ubruhin

This comment has been minimized.

Copy link
@dbrgn

dbrgn Dec 29, 2018

Member

Is this just the cache database? Could we cause harm by removing the database without user interaction?

This comment has been minimized.

Copy link
@ubruhin

ubruhin Dec 29, 2018

Author Member

Yes it's just the cache database. It will be rebuilt automatically, so it's harmless to remove it.

int dbVersion = getDbVersion();
if (dbVersion < sCurrentDbVersion) {
qInfo() << "Library database version" << dbVersion
<< "is outdated -> update triggered";
if (dbVersion != sCurrentDbVersion) {
qCritical() << "Library database version" << dbVersion << "is wrong!";
mDb.reset();
QFile(dbFilePath.toStr()).remove();
mDb.reset(new SQLiteDatabase(dbFilePath)); // can throw
createAllTables(); // can throw
setDbVersion(sCurrentDbVersion); // can throw
QFile(mFilePath.toStr()).remove();
mDb.reset(new SQLiteDatabase(mFilePath)); // can throw
createAllTables(); // can throw
setDbVersion(sCurrentDbVersion); // can throw
}

// create library scanner object
mLibraryScanner.reset(new WorkspaceLibraryScanner(mWorkspace));
mLibraryScanner.reset(new WorkspaceLibraryScanner(mWorkspace, mFilePath));
connect(mLibraryScanner.data(), &WorkspaceLibraryScanner::started, this,
&WorkspaceLibraryDb::scanStarted, Qt::QueuedConnection);
connect(mLibraryScanner.data(), &WorkspaceLibraryScanner::progressUpdate,
@@ -68,6 +68,9 @@ class WorkspaceLibraryDb final : public QObject {
explicit WorkspaceLibraryDb(Workspace& ws);
~WorkspaceLibraryDb() noexcept;

// Getters: Attributes
const FilePath& getFilePath() const noexcept { return mFilePath; }

// Getters: Library Elements by their UUID
QMultiMap<Version, FilePath> getComponentCategories(const Uuid& uuid) const;
QMultiMap<Version, FilePath> getPackageCategories(const Uuid& uuid) const;
@@ -153,7 +156,8 @@ class WorkspaceLibraryDb final : public QObject {

// Attributes
Workspace& mWorkspace;
QScopedPointer<SQLiteDatabase> mDb; ///< the SQLite database "cache.sqlite"
FilePath mFilePath; ///< path to the SQLite database
QScopedPointer<SQLiteDatabase> mDb; ///< the SQLite database
QScopedPointer<WorkspaceLibraryScanner> mLibraryScanner;

// Constants
@@ -41,8 +41,9 @@ using namespace library;
* Constructors / Destructor
******************************************************************************/

WorkspaceLibraryScanner::WorkspaceLibraryScanner(Workspace& ws) noexcept
: QThread(nullptr), mWorkspace(ws), mAbort(false) {
WorkspaceLibraryScanner::WorkspaceLibraryScanner(
Workspace& ws, const FilePath& dbFilePath) noexcept
: QThread(nullptr), mWorkspace(ws), mDbFilePath(dbFilePath), mAbort(false) {
}

WorkspaceLibraryScanner::~WorkspaceLibraryScanner() noexcept {
@@ -83,9 +84,7 @@ void WorkspaceLibraryScanner::run() noexcept {
libraries.append(mWorkspace.getRemoteLibraries().values());

// open SQLite database
FilePath dbFilePath =
mWorkspace.getLibrariesPath().getPathTo("cache.sqlite");
SQLiteDatabase db(dbFilePath); // can throw
SQLiteDatabase db(mDbFilePath); // can throw

// begin database transaction
SQLiteDatabase::TransactionScopeGuard transactionGuard(db); // can throw
@@ -23,7 +23,7 @@
/*******************************************************************************
* Includes
******************************************************************************/
#include <librepcb/common/exceptions.h>
#include <librepcb/common/fileio/filepath.h>

#include <QtCore>

@@ -67,7 +67,7 @@ class WorkspaceLibraryScanner final : public QThread {

public:
// Constructors / Destructor
explicit WorkspaceLibraryScanner(Workspace& ws) noexcept;
WorkspaceLibraryScanner(Workspace& ws, const FilePath& dbFilePath) noexcept;
WorkspaceLibraryScanner(const WorkspaceLibraryScanner& other) = delete;
~WorkspaceLibraryScanner() noexcept;

@@ -101,6 +101,7 @@ class WorkspaceLibraryScanner final : public QThread {

private: // Data
Workspace& mWorkspace;
FilePath mDbFilePath;
volatile bool mAbort;
};

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.