Skip to content

Commit

Permalink
Fix sql duplicate service_url error (#735)
Browse files Browse the repository at this point in the history
* Fix sql duplicate service url error

* update migration description

* fix en

---------

Co-authored-by: Till <[email protected]>
  • Loading branch information
ssrahn and tgloeggl committed Aug 4, 2023
1 parent 9323a3b commit 49e56c7
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
33 changes: 28 additions & 5 deletions lib/Routes/Config/ConfigAddEdit.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,39 @@ public function __invoke(Request $request, Response $response, $args)
$json = $this->getRequestData($request);

$config_checked = false;
$duplicate_url = false;

// check, if a config with the same data already exists:
$config = reset(Config::findBySql('service_url = ?', [$json['config']['service_url']]));
if ($args['id']) {
$config = Config::find($args['id']);
} else {
$config = reset(Config::findBySql('service_url = ?', [$json['config']['service_url']]));

if (!$config) {
// PUT request - edit config
if ($config && $config->id !== (int)$args['id']) {
$duplicate_url = true;
}
else {
$config = Config::find($args['id']);
}
}
else {
// POST request - create config
if ($config) {
$duplicate_url = true;
} else {
$config = new Config;
}
}
// Throw error if the url is already used
if ($duplicate_url) {
return $this->createResponse([
'message'=> [
'type' => 'error',
'text' => sprintf(
_('Eine Konfiguration mit der angegebenen URL ist bereits vorhanden: "%s"'),
$json['config']['service_url']
)
],
], $response);
}

$new_settings = [];
$stored_config = $config->toArray();
Expand Down
33 changes: 33 additions & 0 deletions migrations/074_update_config_key.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

class UpdateConfigKey extends Migration
{
public function description()
{
return 'Update oc_config key to only use the service_url';
}

public function up()
{
$db = DBManager::get();

$db->exec("ALTER TABLE `oc_config`
DROP INDEX service_url,
ADD UNIQUE KEY(service_url)
");

SimpleOrMap::expireTableScheme();
}

public function down()
{
$db = DBManager::get();

$db->exec("ALTER TABLE `oc_config`
DROP INDEX service_url,
ADD UNIQUE KEY(service_url,service_user,service_password)
");

SimpleOrMap::expireTableScheme();
}
}

0 comments on commit 49e56c7

Please sign in to comment.