Commit 4ac2fce4 authored by Boudewijn Rempt's avatar Boudewijn Rempt

Finalize first version of the resouce cache db schema

Differential revision: https://phabricator.kde.org/T8775
parent feccb93a
......@@ -107,7 +107,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
QStringList tables = QStringList() << "version_information"
<< "origin_types"
<< "resource_types"
<< "stores"
<< "storages"
<< "tags"
<< "resources"
<< "translations"
......@@ -129,25 +129,28 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
if (dbTables.contains("version_information")) {
// Verify the version number
QSqlQuery query;
query.exec("SELECT database_version, krita_version, creation_date"
"FROM version_information"
"ORDER BY id DESC LIMIT 1");
if (query.size() > 0) {
query.first();
QString schemaVersion = query.value(0).toString();
QString kritaVersion = query.value(1).toString();
QString creationDate = query.value(2).toString();
infoResources << "Database version" << schemaVersion
<< "Krita version that created the database" << kritaVersion
<< "At" << creationDate;
if (schemaVersion != databaseVersion) {
warnResources << "Database schema is outdated, migration is needed";
schemaIsOutDated = true;
QFile f(":/get_version_information.sql");
if (f.open(QFile::ReadOnly)) {
QSqlQuery query(f.readAll());
if (query.size() > 0) {
query.first();
QString schemaVersion = query.value(0).toString();
QString kritaVersion = query.value(1).toString();
QString creationDate = query.value(2).toString();
infoResources << "Database version" << schemaVersion
<< "Krita version that created the database" << kritaVersion
<< "At" << creationDate;
if (schemaVersion != databaseVersion) {
warnResources << "Database schema is outdated, migration is needed";
schemaIsOutDated = true;
}
}
}
else {
return QSqlError("Error executing SQL", "Could not open get_version_information.sql", QSqlError::StatementError);
}
}
if (allTablesPresent && !schemaIsOutDated) {
......@@ -159,9 +162,8 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
Q_FOREACH(const QString &table, tables) {
QFile f(":/create_" + table + ".sql");
if (f.open(QFile::ReadOnly)) {
QString sql(f.readAll());
QSqlQuery query;
if (!query.exec(sql)) {
if (!query.exec(f.readAll())) {
qWarning() << "Could not create table" << table;
return db.lastError();
}
......@@ -184,8 +186,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll();
Q_FOREACH(const QString &originType, originTypes) {
QSqlQuery query;
query.prepare(sql);
QSqlQuery query(sql);
query.addBindValue(originType);
if (!query.exec()) {
qWarning() << "Could not insert" << originType << db.lastError() << query.executedQuery();
......@@ -210,8 +211,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
if (f.open(QFile::ReadOnly)) {
QString sql = f.readAll();
Q_FOREACH(const QString &resourceType, resourceTypes) {
QSqlQuery query;
query.prepare(sql);
QSqlQuery query(sql);
query.addBindValue(resourceType);
if (!query.exec()) {
qWarning() << "Could not insert" << resourceType << db.lastError() << query.executedQuery();
......@@ -235,7 +235,7 @@ QSqlError KisResourceCacheDb::Private::initDb(const QString &location)
query.addBindValue(KritaVersionWrapper::versionString());
query.addBindValue(QDateTime::currentDateTimeUtc().toString());
if (!query.exec()) {
qWarning() << "Could not insert the current version" << db.lastError() << query.executedQuery();
qWarning() << "Could not insert the current version" << db.lastError() << query.executedQuery() << query.boundValues();
return db.lastError();
}
infoResources << "Filled version table";
......
......@@ -25,7 +25,6 @@
#include <kritaresources_export.h>
/**
* @brief The KisResourceCacheDb class encapsulates the database that
* caches information about the resources available to the user.
......
......@@ -5,7 +5,7 @@
<file alias="fill_version_information.sql">sql/fill_version_information.sql</file>
<file alias="create_origin_types.sql">sql/create_origin_types.sql</file>
<file alias="fill_origin_types.sql">sql/fill_origin_types.sql</file>
<file alias="create_stores.sql">sql/create_stores.sql</file>
<file alias="create_storages.sql">sql/create_storages.sql</file>
<file alias="create_tags.sql">sql/create_tags.sql</file>
<file alias="create_resource_types.sql">sql/create_resource_types.sql</file>
<file alias="fill_resource_types.sql">sql/fill_resource_types.sql</file>
......@@ -13,5 +13,6 @@
<file alias="create_translations.sql">sql/create_translations.sql</file>
<file alias="create_versioned_resources.sql">sql/create_versioned_resources.sql</file>
<file alias="create_resource_tags.sql">sql/create_resource_tags.sql</file>
<file alias="get_version_information.sql">sql/get_version_information.sql</file>
</qresource>
</RCC>
CREATE TABLE IF NOT EXISTS origin_types (
id INTEGER PRIMARY KEY,
name TEXT
)
id INTEGER PRIMARY KEY
, name TEXT
);
CREATE TABLE IF NOT EXISTS resource_tags (
id INTEGER PRIMARY KEY,
resource_id INTEGER,
tag_id INTEGER,
FOREIGN KEY(resource_id) REFERENCES resources(id)
FOREIGN KEY(tag_id) REFERENCES tags(id)
)
id INTEGER PRIMARY KEY
, resource_id INTEGER
, tag_id INTEGER
, FOREIGN KEY(resource_id) REFERENCES resources(id)
, FOREIGN KEY(tag_id) REFERENCES tags(id)
);
CREATE TABLE IF NOT EXISTS resource_types (
id INTEGER PRIMARY KEY,
name TEXT
)
id INTEGER PRIMARY KEY
, name TEXT
);
CREATE TABLE IF NOT EXISTS resources (
id INTEGER PRIMARY KEY,
type TEXT
)
id INTEGER PRIMARY KEY
, resource_type_id INTEGER
, name TEXT
, tooltip TEXT
, icon BLOB
, FOREIGN KEY(resource_type_id) REFERENCES resource_types(id)
);
CREATE TABLE IF NOT EXISTS storages (
id INTEGER PRIMARY KEY
, origin_type_id INTEGER
, name TEXT
, location TEXT
, FOREIGN KEY(origin_type_id) REFERENCES origin_types(id)
);
CREATE TABLE IF NOT EXISTS stores (
id INTEGER PRIMARY KEY,
origin_type_id INTEGER,
FOREIGN KEY(origin_type_id) REFERENCES origin_types(id)
)
CREATE TABLE IF NOT EXISTS tags (
id INTEGER PRIMARY KEY,
name TEXT
)
id INTEGER PRIMARY KEY
, name TEXT
, storage_id INTEGER
, FOREIGN KEY(storage_id) REFERENCES storages(id)
);
CREATE TABLE IF NOT EXISTS translations (
id INTEGER PRIMARY KEY,
name TEXT
)
id INTEGER PRIMARY KEY
, original_id INTEGER
, locale TEXT
, translation TEXT
);
CREATE TABLE IF NOT EXISTS version_information (
id INTEGER PRIMARY KEY AUTOINCREMENT
, database_version TEXT
, krita_version TEXT
, creation_date TEXT
)
id INTEGER PRIMARY KEY AUTOINCREMENT
, database_version TEXT
, krita_version TEXT
, creation_date TEXT
);
CREATE TABLE IF NOT EXISTS versioned_resources (
id INTEGER PRIMARY KEY,
name TEXT
)
id INTEGER PRIMARY KEY
, resource_id INTEGER
, storage_id INTEGER
, version INTEGER
, location TEXT
, datestamp TEXT
, FOREIGN KEY(resource_id) REFERENCES resources(id)
, FOREIGN KEY(storage_id) REFERENCES storages(id)
);
INSERT INTO version_information (
database_version
, krita_version
, creation_date
)
database_version
, krita_version
, creation_date )
VALUES (
?
,?
,?
, ?
, ?
);
SELECT (
database_version
, krita_version
, creation_date (
FROM version_information
ORDER BY id
DESC
LIMIT 1;
......@@ -46,7 +46,7 @@ void TestResourceCacheDb::testCreateDatabase()
QStringList tables = QStringList() << "version_information"
<< "origin_types"
<< "resource_types"
<< "stores"
<< "storages"
<< "tags"
<< "resources"
<< "translations"
......
......@@ -802,6 +802,12 @@
<extends>QPushButton</extends>
<header>kis_color_button.h</header>
</customwidget>
<customwidget>
<class>KisFileNameRequester</class>
<extends>QWidget</extends>
<header location="global">kis_file_name_requester.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment