bool BaseMigrationTableService::removeMigration(const QString &migrationName, const CommandExecution::CommandExecutionContext &context) const { const QString versionTableName = context.migrationConfig().migrationVersionTableName; const QString query = QString("DELETE FROM %1 WHERE version = %2") .arg(context.helperRepository().quoteService().quoteTableName(versionTableName)) .arg(context.helperRepository().quoteService().quoteString(migrationName)); ::qDebug() << "removing entry to version table. complete query looks like:"; ::qDebug() << query; QSqlQuery sqlQuery = context.database().exec(query); if (sqlQuery.lastError().isValid()) { ::qDebug() << Q_FUNC_INFO << sqlQuery.lastError().text(); return false; } return true; }
bool SqliteDropColumnService::execute(const Commands::ConstCommandPtr &command, CommandExecution::CommandExecutionContext &context) const { QSharedPointer<const Commands::DropColumn> dropColumn(command.staticCast<const Commands::DropColumn>()); Structure::Table table( context.helperRepository().sqlStructureService().getTableDefinition(dropColumn->tableName(), context.database()) ); Structure::Table::Builder alteredTable(dropColumn->tableName()); const Structure::Column* originalColumn = Q_NULLPTR; foreach (const Structure::Column &column, table.columns()) { if (column.name() == dropColumn->columnName()) { originalColumn = &column; } else { alteredTable << column; } } if (!originalColumn) { ::qWarning() << "Column not found" << dropColumn->tableName() << dropColumn->columnName(); return false; } QString tempTableName = QString("%1%2").arg(context.migrationConfig().temporaryTablePrefix, dropColumn->tableName()); bool success = BaseSqlRenameTableService::execute(Commands::RenameTable(dropColumn->tableName(), tempTableName), context); if (!success) return false; success = BaseSqlCreateTableService::execute(Commands::CreateTable(alteredTable), context); if (!success) return false; const QString copyQuery = QString("INSERT INTO %1 SELECT %2 FROM %3") .arg(table.name()) .arg(table.columnNames().join(", ")) .arg(tempTableName); success = CommandExecution::BaseCommandExecutionService::executeQuery(copyQuery, context); if (!success) return false; success = BaseSqlDropTableService::execute(Commands::DropTable(tempTableName), context); if (success && context.isUndoUsed()) { context.setUndoCommand(Commands::CommandPtr(new Commands::AddColumn(*originalColumn, dropColumn->tableName()))); } return success; }
QStringList BaseMigrationTableService::migrationList(const CommandExecution::CommandExecutionContext &context) const { const QString versionTableName = context.migrationConfig().migrationVersionTableName; QSqlQuery sqlQuery = context.database().exec( QString("SELECT version FROM %1") .arg(context.helperRepository().quoteService().quoteTableName(versionTableName))); QStringList migrationList; if (sqlQuery.lastError().isValid()) { ::qDebug() << Q_FUNC_INFO << sqlQuery.lastError().text(); } else { while (sqlQuery.next()) { migrationList << sqlQuery.value(0).toString(); } } return migrationList; }