public function defineTables()
{
$St = Gdn::structure();
$DestStructure = clone $St;
$Tables =& $this->tables();
foreach ($Tables as $Table => $TableInfo) {
$Columns = $TableInfo['Columns'];
if (!is_array($Columns) || count($Columns) == 0) {
throw new Gdn_UserException(sprintf(t('The %s table is not in the correct format.', $Table)));
}
$St->table(self::TABLE_PREFIX . $Table);
// Get the structure from the destination database to match types.
try {
$DestStructure->reset()->get($Table);
} catch (Exception $Ex) {
// Trying to import into a non-existant table.
$Tables[$Table]['Skip'] = true;
continue;
}
//$DestColumns = $DestStructure->Columns();
$DestModified = false;
foreach ($Columns as $Name => $Type) {
if (!$Name) {
throw new Gdn_UserException(sprintf(t('The %s table is not in the correct format.'), $Table));
}
if ($DestStructure->columnExists($Name)) {
$StructureType = $DestStructure->columnTypeString($DestStructure->columns($Name));
} elseif ($DestStructure->columnExists($Type)) {
// Fix the table definition.
unset($Tables[$Table]['Columns'][$Name]);
$Tables[$Table]['Columns'][$Type] = '';
$Name = $Type;
$StructureType = $DestStructure->columnTypeString($DestStructure->columns($Type));
} elseif (!stringBeginsWith($Name, '_')) {
$StructureType = $Type;
if (!$StructureType) {
$StructureType = 'varchar(255)';
}
// This is a new column so it needs to be added to the destination table too.
$DestStructure->column($Name, $StructureType, null);
$DestModified = true;
} elseif ($Type) {
$StructureType = $Type;
} else {
$StructureType = 'varchar(255)';
}
$St->column($Name, $StructureType, null);
}
// Add a new ID column.
if (array_key_exists($Table . 'ID', $Columns)) {
$St->column('_NewID', $DestStructure->columnTypeString($Table . 'ID'), null)->column('_Action', array('Insert', 'Update'), null);
}
try {
if (!$this->isDbSource()) {
$St->set(true, true);
} else {
$St->reset();
}
if ($DestModified) {
$DestStructure->set();
}
} catch (Exception $Ex) {
// Since these exceptions are likely caused by a faulty import file they should be considered user exceptions.
throw new Gdn_UserException(sprintf(t('There was an error while trying to create the %s table (%s).'), $Table, $Ex->getMessage()));
//, $Ex);
}
}
return true;
}