Skip to content

Commit

Permalink
Fix a bunch of errors are dumped while importing default style database
Browse files Browse the repository at this point in the history
on SECOND launch of a new profile

We weren't creating the stylemetadata table after importing the initial
style database on first launch, so on the second launch we'd be
attempting reimport the style database and violate a bunch of unique
constraints.

Correctly create the stylemetadata table after the initial import to
avoid this.
  • Loading branch information
nyalldawson committed Jan 31, 2022
1 parent 3ae6231 commit c13c4ca
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 10 deletions.
30 changes: 21 additions & 9 deletions src/core/symbology/qgsstyle.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,10 @@ QgsStyle *QgsStyle::defaultStyle() // static
sDefaultStyle->createDatabase( styleFilename );
if ( QFile::exists( QgsApplication::defaultStylePath() ) )
{
sDefaultStyle->importXml( QgsApplication::defaultStylePath() );
if ( sDefaultStyle->importXml( QgsApplication::defaultStylePath() ) )
{
sDefaultStyle->createStyleMetadataTableIfNeeded();
}
}
}
else
Expand Down Expand Up @@ -3243,13 +3246,12 @@ void QgsStyle::clearCachedTags( QgsStyle::StyleEntity type, const QString &name
mCachedTags[ type ].remove( name );
}

void QgsStyle::upgradeIfRequired()
bool QgsStyle::createStyleMetadataTableIfNeeded()
{
// make sure metadata table exists
QString query = qgs_sqlite3_mprintf( "SELECT name FROM sqlite_master WHERE name='stylemetadata'" );
sqlite3_statement_unique_ptr statement;
int rc;
int dbVersion = 0;
statement = mCurrentDB.prepare( query, rc );

if ( rc != SQLITE_OK || sqlite3_step( statement.get() ) != SQLITE_ROW )
Expand All @@ -3260,15 +3262,25 @@ void QgsStyle::upgradeIfRequired()
"key TEXT UNIQUE,"\
"value TEXT);" );
runEmptyQuery( query );
query = qgs_sqlite3_mprintf( "INSERT INTO stylemetadata VALUES (NULL, '%q', '%q')", "version", "31200" );
query = qgs_sqlite3_mprintf( "INSERT INTO stylemetadata VALUES (NULL, '%q', '%q')", "version", QString::number( Qgis::versionInt() ).toUtf8().constData() );
runEmptyQuery( query );

dbVersion = 31200;
return true;
}
else
{
query = qgs_sqlite3_mprintf( "SELECT value FROM stylemetadata WHERE key='version'" );
statement = mCurrentDB.prepare( query, rc );
return false;
}
}

void QgsStyle::upgradeIfRequired()
{
// make sure metadata table exists
int dbVersion = 0;
if ( !createStyleMetadataTableIfNeeded() )
{
const QString query = qgs_sqlite3_mprintf( "SELECT value FROM stylemetadata WHERE key='version'" );
int rc;
sqlite3_statement_unique_ptr statement = mCurrentDB.prepare( query, rc );
if ( rc == SQLITE_OK && sqlite3_step( statement.get() ) == SQLITE_ROW )
{
dbVersion = statement.columnAsText( 0 ).toInt();
Expand All @@ -3280,7 +3292,7 @@ void QgsStyle::upgradeIfRequired()
// do upgrade
if ( importXml( QgsApplication::defaultStylePath(), dbVersion ) )
{
query = qgs_sqlite3_mprintf( "UPDATE stylemetadata SET value='%q' WHERE key='version'", QString::number( Qgis::versionInt() ).toUtf8().constData() );
const QString query = qgs_sqlite3_mprintf( "UPDATE stylemetadata SET value='%q' WHERE key='version'", QString::number( Qgis::versionInt() ).toUtf8().constData() );
runEmptyQuery( query );
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/core/symbology/qgsstyle.h
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,10 @@ class CORE_EXPORT QgsStyle : public QObject

void clearCachedTags( StyleEntity type, const QString &name );


/**
* Returns TRUE if style metadata table did not exist and was newly created.
*/
bool createStyleMetadataTableIfNeeded();
void upgradeIfRequired();

/**
Expand Down

0 comments on commit c13c4ca

Please sign in to comment.