void DatabaseImpl::dumpDatabase() { QFile dump( "dbdump.txt" ); if ( !dump.open( QIODevice::WriteOnly | QIODevice::Text ) ) { tDebug() << "Couldn't open dbdump.txt for writing!"; Q_ASSERT( false ); } else { QTextStream dumpout( &dump ); TomahawkSqlQuery query = newquery(); query.exec( "SELECT * FROM oplog" ); while ( query.next() ) { dumpout << "ID: " << query.value( 0 ).toInt() << endl << "GUID: " << query.value( 2 ).toString() << endl << "Command: " << query.value( 3 ).toString() << endl << "Singleton: " << query.value( 4 ).toBool() << endl << "JSON: " << ( query.value( 5 ).toBool() ? qUncompress( query.value( 6 ).toByteArray() ) : query.value( 6 ).toByteArray() ) << endl << endl << endl; } } }
void DatabaseImpl::init() { m_lastartid = m_lastalbid = m_lasttrkid = 0; TomahawkSqlQuery query = newquery(); // make sqlite behave how we want: query.exec( "PRAGMA foreign_keys = ON" ); }
DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent ) : QObject( (QObject*) parent ) , m_lastartid( 0 ) , m_lastalbid( 0 ) , m_lasttrkid( 0 ) { QTime t; t.start(); bool schemaUpdated = openDatabase( dbname ); tDebug( LOGVERBOSE ) << "Opened database:" << t.elapsed(); TomahawkSqlQuery query = newquery(); query.exec( "SELECT v FROM settings WHERE k='dbid'" ); if ( query.next() ) { m_dbid = query.value( 0 ).toString(); } else { m_dbid = uuid(); query.exec( QString( "INSERT INTO settings(k,v) VALUES('dbid','%1')" ).arg( m_dbid ) ); } tLog() << "Database ID:" << m_dbid; // make sqlite behave how we want: query.exec( "PRAGMA auto_vacuum = FULL" ); query.exec( "PRAGMA synchronous = ON" ); query.exec( "PRAGMA foreign_keys = ON" ); //query.exec( "PRAGMA temp_store = MEMORY" ); tDebug( LOGVERBOSE ) << "Tweaked db pragmas:" << t.elapsed(); // in case of unclean shutdown last time: query.exec( "UPDATE source SET isonline = 'false'" ); // schemaUpdated = true; // REMOVE ME m_fuzzyIndex = new FuzzyIndex( *this, schemaUpdated ); if ( schemaUpdated ) QTimer::singleShot( 0, this, SLOT( updateIndex() ) ); tDebug( LOGVERBOSE ) << "Loaded index:" << t.elapsed(); if ( qApp->arguments().contains( "--dumpdb" ) ) { dumpDatabase(); ::exit( 0 ); } }
Tomahawk::DatabaseImpl::DatabaseImpl( const QString& dbname ) { QTime t; t.start(); // Signals for splash screen must be connected here connect( this, SIGNAL( schemaUpdateStarted() ), qApp, SLOT( onSchemaUpdateStarted() ) ); connect( this, SIGNAL( schemaUpdateStatus( QString ) ), qApp, SLOT( onSchemaUpdateStatus( QString ) ) ); connect( this, SIGNAL( schemaUpdateDone() ), qApp, SLOT( onSchemaUpdateDone() ) ); bool schemaUpdated = openDatabase( dbname ); tDebug( LOGVERBOSE ) << "Opened database:" << t.elapsed(); TomahawkSqlQuery query = newquery(); query.exec( "SELECT v FROM settings WHERE k='dbid'" ); if ( query.next() ) { m_dbid = query.value( 0 ).toString(); } else { m_dbid = uuid(); query.exec( QString( "INSERT INTO settings(k,v) VALUES('dbid','%1')" ).arg( m_dbid ) ); } tLog() << "Database ID:" << m_dbid; init(); query.exec( "PRAGMA auto_vacuum = FULL" ); query.exec( "PRAGMA synchronous = NORMAL" ); tDebug( LOGVERBOSE ) << "Tweaked db pragmas:" << t.elapsed(); // in case of unclean shutdown last time: query.exec( "UPDATE source SET isonline = 'false'" ); query.exec( "DELETE FROM oplog WHERE source IS NULL AND singleton = 'true'" ); m_fuzzyIndex = new Tomahawk::DatabaseFuzzyIndex( this, schemaUpdated ); tDebug( LOGVERBOSE ) << "Loaded index:" << t.elapsed(); if ( qApp->arguments().contains( "--dumpdb" ) ) { dumpDatabase(); ::exit( 0 ); } }
bool DatabaseImpl::updateSchema( int currentver ) { qDebug() << "Create tables... old version is" << currentver; QString sql( get_tomahawk_sql() ); QStringList statements = sql.split( ";", QString::SkipEmptyParts ); db.transaction(); foreach( const QString& sl, statements ) { QString s( sl.trimmed() ); if( s.length() == 0 ) continue; qDebug() << "Executing:" << s; TomahawkSqlQuery query = newquery(); query.exec( s ); }
bool DatabaseImpl::updateSchema( int oldVersion ) { // we are called here with the old database. we must migrate it to the CURRENT_SCHEMA_VERSION from the oldVersion if ( oldVersion == 0 ) // empty database, so create our tables and stuff { tLog() << "Create tables... old version is" << oldVersion; QString sql( get_tomahawk_sql() ); QStringList statements = sql.split( ";", QString::SkipEmptyParts ); m_db.transaction(); foreach ( const QString& sl, statements ) { QString s( sl.trimmed() ); if ( s.length() == 0 ) continue; tLog() << "Executing:" << s; TomahawkSqlQuery query = newquery(); query.exec( s ); }
DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent ) : QObject( (QObject*) parent ) , m_lastartid( 0 ) , m_lastalbid( 0 ) , m_lasttrkid( 0 ) { db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" ); db.setDatabaseName( dbname ); if ( !db.open() ) { qDebug() << "FAILED TO OPEN DB"; throw "failed to open db"; // TODO } QSqlQuery qry = QSqlQuery( db ); bool schemaUpdated = false; qry.exec( "SELECT v FROM settings WHERE k='schema_version'" ); if ( qry.next() ) { int v = qry.value( 0 ).toInt(); qDebug() << "Current schema is" << v << this->thread(); if ( v != CURRENT_SCHEMA_VERSION ) { QString newname = QString("%1.v%2").arg(dbname).arg(v); qDebug() << endl << "****************************" << endl; qDebug() << "Schema version too old: " << v << ". Current version is:" << CURRENT_SCHEMA_VERSION; qDebug() << "Moving" << dbname << newname; qDebug() << endl << "****************************" << endl; qry.clear(); qry.finish(); db.close(); db.removeDatabase( "tomahawk" ); if( QFile::rename( dbname, newname ) ) { db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" ); db.setDatabaseName( dbname ); if( !db.open() ) throw "db moving failed"; TomahawkSqlQuery query = newquery(); query.exec( "PRAGMA auto_vacuum = FULL" ); schemaUpdated = updateSchema( v ); } else { Q_ASSERT( false ); QTimer::singleShot( 0, qApp, SLOT( quit() ) ); return; } } } else { schemaUpdated = updateSchema( 0 ); } TomahawkSqlQuery query = newquery(); query.exec( "SELECT v FROM settings WHERE k='dbid'" ); if( query.next() ) { m_dbid = query.value( 0 ).toString(); } else { m_dbid = uuid(); query.exec( QString( "INSERT INTO settings(k,v) VALUES('dbid','%1')" ).arg( m_dbid ) ); } qDebug() << "Database ID:" << m_dbid; // make sqlite behave how we want: query.exec( "PRAGMA synchronous = ON" ); query.exec( "PRAGMA foreign_keys = ON" ); //query.exec( "PRAGMA temp_store = MEMORY" ); // in case of unclean shutdown last time: query.exec( "UPDATE source SET isonline = 'false'" ); m_fuzzyIndex = new FuzzyIndex( *this, schemaUpdated ); }
DatabaseImpl::DatabaseImpl( const QString& dbname, Database* parent ) : QObject( (QObject*) parent ) , m_lastartid( 0 ) , m_lastalbid( 0 ) , m_lasttrkid( 0 ) { bool schemaUpdated = false; int version = getDatabaseVersion( dbname ); if ( version > 0 && version != CURRENT_SCHEMA_VERSION ) { QString newname = QString( "%1.v%2" ).arg( dbname ).arg( version ); tLog() << endl << "****************************" << endl; tLog() << "Schema version too old: " << version << ". Current version is:" << CURRENT_SCHEMA_VERSION; tLog() << "Moving" << dbname << newname; tLog() << "If the migration fails, you can recover your DB by copying" << newname << "back to" << dbname; tLog() << endl << "****************************" << endl; QFile::copy( dbname, newname ); { db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" ); db.setDatabaseName( dbname ); if( !db.open() ) throw "db moving failed"; TomahawkSqlQuery query = newquery(); query.exec( "PRAGMA auto_vacuum = FULL" ); schemaUpdated = updateSchema( version ); if ( !schemaUpdated ) { Q_ASSERT( false ); QTimer::singleShot( 0, qApp, SLOT( quit() ) ); } } } else { db = QSqlDatabase::addDatabase( "QSQLITE", "tomahawk" ); db.setDatabaseName( dbname ); if ( !db.open() ) { tLog() << "Failed to open database" << dbname; throw "failed to open db"; // TODO } if ( version < 0 ) schemaUpdated = updateSchema( 0 ); } TomahawkSqlQuery query = newquery(); query.exec( "SELECT v FROM settings WHERE k='dbid'" ); if( query.next() ) { m_dbid = query.value( 0 ).toString(); } else { m_dbid = uuid(); query.exec( QString( "INSERT INTO settings(k,v) VALUES('dbid','%1')" ).arg( m_dbid ) ); } tLog() << "Database ID:" << m_dbid; // make sqlite behave how we want: query.exec( "PRAGMA synchronous = ON" ); query.exec( "PRAGMA foreign_keys = ON" ); //query.exec( "PRAGMA temp_store = MEMORY" ); // in case of unclean shutdown last time: query.exec( "UPDATE source SET isonline = 'false'" ); m_fuzzyIndex = new FuzzyIndex( *this, schemaUpdated ); }