Permalink
Browse files

Improve error handling when moving table to a different schema

When moving an existing table to a different schema which already
contains a table of that name, this causes an error. With this commit we
try to detect this type of error as early as possible.

This commit also updates the error message for that case. The old error
message still mentioned the 'temporary flag' which isn't correct
anymore.

Also, when changing the schema fails, reset the dropdown box back to a
working schema to avoid any confusion about whether the change worked or
not.
  • Loading branch information...
MKleusberg committed Sep 4, 2017
1 parent 72d64ed commit f01ad409ff294cf5dea1cf035f589c1b0509b76f
Showing with 15 additions and 6 deletions.
  1. +2 −2 src/EditTableDialog.cpp
  2. +13 −4 src/sqlitedb.cpp
@@ -733,8 +733,8 @@ void EditTableDialog::changeSchema(const QString& schema)
{
if(!pdb.renameColumn(curTable, m_table, QString(), sqlb::FieldPtr(), 0, schema))
{
QMessageBox::warning(this, QApplication::applicationName(),
tr("Setting the temporary flag for the table failed. Error message:\n%1").arg(pdb.lastError()));
QMessageBox::warning(this, QApplication::applicationName(), tr("Changing the table schema failed. Error message:\n%1").arg(pdb.lastError()));
ui->comboSchema->setCurrentText(curTable.schema()); // Set it back to the original schema
}
}
}
@@ -1064,6 +1064,19 @@ bool DBBrowserDB::renameColumn(const sqlb::ObjectIdentifier& tablename, const sq
// 2) Include the addColumn() use case in here, so the calling side doesn't need to know anything about how this class handles table modifications.
// 3) Maybe rename this function to alterTable() or something

// If no new schema name has been set, we just use the old schema name
if(newSchemaName.isNull())
{
newSchemaName = tablename.schema();
} else {
// We're moving the table to a different schema. So check first if it doesn't already exist in the new schema.
if(getObjectByName(sqlb::ObjectIdentifier(newSchemaName, tablename.name())) != nullptr && newSchemaName != tablename.schema())
{
lastErrorMessage = tr("A table with the name '%1' already exists in schema '%2'.").arg(tablename.name()).arg(newSchemaName);
return false;
}
}

// Create table schema
const sqlb::TablePtr oldSchema = getObjectByName(tablename).dynamicCast<sqlb::Table>();

@@ -1122,10 +1135,6 @@ bool DBBrowserDB::renameColumn(const sqlb::ObjectIdentifier& tablename, const sq
newSchema.setField(index + move, to);
}

// If no new schema name has been set, we just use the old schema name
if(newSchemaName.isNull())
newSchemaName = tablename.schema();

// Create the new table
NoStructureUpdateChecks nup(*this);
if(!executeSQL(newSchema.sql(newSchemaName), true, true))

0 comments on commit f01ad40

Please sign in to comment.