Skip to content

Commit

Permalink
Merge pull request #9 from NamelessCoder/feature/update-mode
Browse files Browse the repository at this point in the history
[FEATURE] Add "update" mode
  • Loading branch information
bmack committed Jul 28, 2020
2 parents 4047b88 + fbb08f2 commit b3f1598
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,17 @@ In this case, create a file `conf/site_dev.yaml` for the local setup. It looks l
Other files for staging, production etc. can be created accordingly.

The option `mode` describes whether to truncate the database table before the entries are added.
but could also be set to `append`.
but could also be set to `append` or `update`. If set to `append` the entries will be written as
pure insert without regard whether the record already exists.

If `mode` is set to `replace` the table is first truncated, then entries are inserted.

If `mode` is set to `update` the following happens:

* A check is made if the entry contains a `uid` property. If it does not, the entry is inserted as
a new record (like it would happen if `mode` was set to `append`).
* If entry does contain a `uid` property, the script checks if the record exist in the table and
if it does, an SQL update is done to update the record. If it does not exist, it is inserted.

The file accepts more than one table, thus, all language records could be added as well, however
this could be done in a generic `sites.yaml` file which works for all environments.
Expand Down
30 changes: 26 additions & 4 deletions src/Command/SiteImportCommandController.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,19 @@ public function fromFileCommand($file)
$this->outputLine('Emptied database table "' . $config['table'] . '"');
}
foreach ($config['entries'] as $entry) {
$conn->insert($config['table'], $entry);
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
if ($mode === 'update' && isset($entry['uid'])) {
$identifiers = ['uid' => $entry['uid']];
if ($conn->count('uid', $config['table'], $identifiers)) {
$conn->update($config['table'], $entry, $identifiers);
$this->outputLine('Updated (mode=update, entry has uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
} else {
$conn->insert($config['table'], $entry);
$this->outputLine('Added (mode=update, entry does not have uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
}
} else {
$conn->insert($config['table'], $entry);
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
}
}
} else {
if (!($GLOBALS['TYPO3_DB'] instanceof DatabaseConnection)) {
Expand All @@ -57,8 +68,19 @@ public function fromFileCommand($file)
$this->outputLine('Emptied database table "' . $config['table'] . '"');
}
foreach ($config['entries'] as $entry) {
$conn->exec_INSERTquery($config['table'], $entry);
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
if ($mode === 'update' && isset($entry['uid'])) {
$condition = sprintf('uid = %d', $entry['uid']);
if ($conn->exec_SELECTcountRows('uid', $config['table'], $condition)) {
$conn->exec_UPDATEquery($config['table'], $condition, $entry);
$this->outputLine('Updated (mode=update, entry has uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
} else {
$conn->exec_INSERTquery($config['table'], $entry);
$this->outputLine('Added (mode=update, entry does not have uid): ' . json_encode($entry) . ' to database table ' . $config['table']);
}
} else {
$conn->exec_INSERTquery($config['table'], $entry);
$this->outputLine('Added ' . json_encode($entry) . ' to database table ' . $config['table']);
}
}
}
}
Expand Down

0 comments on commit b3f1598

Please sign in to comment.