Skip to content

Commit 9e247ae

Browse files
author
Florian Schaal
committed
FS#2532 - Add possibility to rename databases
1 parent 168c514 commit 9e247ae

File tree

2 files changed

+125
-3
lines changed

2 files changed

+125
-3
lines changed

interface/web/sites/database_edit.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ function onShowEnd() {
154154

155155
if($this->id > 0) {
156156
//* we are editing a existing record
157-
$app->tpl->setVar("edit_disabled", 1);
157+
$edit_disabled = @($_SESSION["s"]["user"]["typ"] == 'admin')? 0 : 1; //* admin can change the database-name
158+
$app->tpl->setVar("edit_disabled", $edit_disabled);
158159
$app->tpl->setVar("server_id_value", $this->dataRecord["server_id"]);
159160
$app->tpl->setVar("database_charset_value", $this->dataRecord["database_charset"]);
160161
$app->tpl->setVar("limit_database_quota", $this->dataRecord["database_quota"]);
@@ -300,8 +301,11 @@ function onBeforeUpdate() {
300301
$dbname_prefix = $app->tools_sites->getPrefix($old_record['database_name_prefix'], $dbname_prefix);
301302
$this->dataRecord['database_name_prefix'] = $dbname_prefix;
302303

303-
if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
304-
$app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
304+
//* Only admin can change the database name
305+
if ($_SESSION["s"]["user"]["typ"] != 'admin') {
306+
if($old_record["database_name"] != $dbname_prefix . $this->dataRecord["database_name"]) {
307+
$app->tform->errorMessage .= $app->tform->wordbook["database_name_change_txt"].'<br />';
308+
}
305309
}
306310
if($old_record["database_charset"] != $this->dataRecord["database_charset"]) {
307311
$app->tform->errorMessage .= $app->tform->wordbook["database_charset_change_txt"].'<br />';

server/plugins-available/mysql_clientdb_plugin.inc.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,123 @@ function db_update($event_name, $data) {
275275
if($old_host_list != '') $old_host_list .= ',';
276276
$old_host_list .= 'localhost';
277277

278+
//* rename database
279+
if ( $data['new']['database_name'] != $data['old']['database_name'] ) {
280+
$old_name = $link->escape_string($data['old']['database_name']);
281+
$new_name = $link->escape_string($data['new']['database_name']);
282+
$timestamp = time();
283+
284+
$tables = $link->query("SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema='".$old_name."' AND TABLE_TYPE='BASE TABLE'");
285+
if ($tables->num_rows > 0) {
286+
while ($row = $tables->fetch_assoc()) {
287+
$tables_array[] = $row['TABLE_NAME'];
288+
}
289+
290+
//* save triggers, routines and events
291+
$triggers = $link->query("SHOW TRIGGERS FROM ".$old_name);
292+
if ($triggers->num_rows > 0) {
293+
while ($row = $triggers->fetch_assoc()) {
294+
$triggers_array[] = $row;
295+
}
296+
$app->log('Dumping triggers from '.$old_name, LOGLEVEL_DEBUG);
297+
$command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." ".$old_name." -d -t -R -E > ".$timestamp.$old_name.'.triggers';
298+
exec($command, $out, $ret);
299+
$app->system->chmod($timestamp.$old_name.'.triggers', 0600);
300+
if ($ret != 0) {
301+
unset($triggers_array);
302+
$app->system->unlink($timestamp.$old_name.'.triggers');
303+
$app->log('Unable to dump triggers from '.$old_name, LOGLEVEL_ERROR);
304+
}
305+
unset($out);
306+
}
307+
308+
//* save views
309+
$views = $link->query("SELECT TABLE_NAME FROM information_schema.tables WHERE table_schema='".$old_name."' and TABLE_TYPE='VIEW'");
310+
if ($views->num_rows > 0) {
311+
while ($row = $views->fetch_assoc()) {
312+
$views_array[] = $row;
313+
}
314+
foreach ($views_array as $_views) {
315+
$temp[] = $_views['TABLE_NAME'];
316+
}
317+
$app->log('Dumping views from '.$old_name, LOGLEVEL_DEBUG);
318+
$temp_views = implode(' ', $temp);
319+
$command = "mysqldump -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." ".$old_name." ".$temp_views." > ".$timestamp.$old_name.'.views';
320+
exec($command, $out, $ret);
321+
$app->system->chmod($timestamp.$old_name.'.views', 0600);
322+
if ($ret != 0) {
323+
unset($views_array);
324+
$app->system->unlink($timestamp.$old_name.'.views');
325+
$app->log('Unable to dump views from '.$old_name, LOGLEVEL_ERROR);
326+
}
327+
unset($out);
328+
unset($temp);
329+
unset($temp_views);
330+
}
331+
332+
//* create new database
333+
$this->db_insert($event_name, $data);
334+
335+
$link->query("show databases like '".$new_name."'");
336+
if ($link) {
337+
//* rename tables
338+
foreach ($tables_array as $table) {
339+
$table = $link->escape_string($table);
340+
$sql = "RENAME TABLE ".$old_name.".".$table." TO ".$new_name.".".$table;
341+
$link->query($sql);
342+
$app->log($sql, LOGLEVEL_DEBUG);
343+
if(!$link) {
344+
$app->log($sql." failed", LOGLEVEL_ERROR);
345+
}
346+
}
347+
348+
//* drop old triggers
349+
if (@is_array($triggers_array)) {
350+
foreach($triggers_array as $trigger) {
351+
$_trigger = $link->escape_string($trigger['Trigger']);
352+
$sql = "DROP TRIGGER ".$old_name.".".$_trigger;
353+
$link->query($sql);
354+
$app->log($sql, LOGLEVEL_DEBUG);
355+
unset($_trigger);
356+
}
357+
//* update triggers, routines and events
358+
$command = "mysql -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." ".$new_name." < ".$timestamp.$old_name.'.triggers';
359+
exec($command, $out, $ret);
360+
if ($ret != 0) {
361+
$app->log('Unable to import triggers for '.$new_name, LOGLEVEL_ERROR);
362+
} else {
363+
$app->system->unlink($timestamp.$old_name.'.triggers');
364+
}
365+
}
366+
367+
//* loading views
368+
if (@is_array($views_array)) {
369+
$command = "mysql -h ".escapeshellarg($clientdb_host)." -u ".escapeshellarg($clientdb_user)." -p".escapeshellarg($clientdb_password)." ".$new_name." < ".$timestamp.$old_name.'.views';
370+
exec($command, $out, $ret);
371+
if ($ret != 0) {
372+
$app->log('Unable to import views for '.$new_name, LOGLEVEL_ERROR);
373+
} else {
374+
$app->system->unlink($timestamp.$old_name.'.views');
375+
}
376+
}
377+
378+
//* drop old database
379+
$this->db_delete($event_name, $data);
380+
} else {
381+
$app->log('Connection to new databse '.$new_name.' failed', LOGLEVEL_ERROR);
382+
if (@is_array($triggers_array)) {
383+
$app->system->unlink($timestamp.$old_name.'.triggers');
384+
}
385+
if (@is_array($views_array)) {
386+
$app->system->unlink($timestamp.$old_name.'.views');
387+
}
388+
}
389+
390+
} else { //* SELECT TABLE_NAME error
391+
$app->log('Unable to rename database '.$old_name.' to '.$new_name, LOGLEVEL_ERROR);
392+
}
393+
}
394+
278395
// Create the database user if database was disabled before
279396
if($data['new']['active'] == 'y') {
280397
if($db_user) {
@@ -299,6 +416,7 @@ function db_update($event_name, $data) {
299416
//$this->process_host_list('DROP', $data['new']['database_name'], $db_user['database_user'], $db_user['database_password'], $old_host_list, $link);
300417
//$this->process_host_list('REVOKE', $data['new']['database_name'], $db_user['database_user'], $db_user['database_password'], $old_host_list, $link);
301418
}
419+
302420
}
303421
if($old_db_ro_user && $data['old']['database_user_id'] != $data['old']['database_ro_user_id']) {
304422
if($old_db_ro_user['database_user'] == 'root'){

0 commit comments

Comments
 (0)