-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrename.php
75 lines (63 loc) · 1.83 KB
/
rename.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
/*
* @Name: Mysql Mariadb Rename Database PHP
* @Author: Max Base
* @Date: 2020-12-26
* @Repository: https://github.com/basemax/mysql-mariadb-rename-database-php
*/
// Limit
ini_set('max_execution_time', 0);
set_time_limit(0);
// Config
$servername = "localhost";
$username = "root";
$password = "01";
$dbname = "stock";
$nextDbname= "newDB";
// Global variables
$tables = [];
// Open
$mysqli = new mysqli($servername, $username, $password, $dbname);
if($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
// Open NextDB: not required
// $mysqliNext = new mysqli($servername, $username, $password, $nextDbname);
// if($mysqliNext->connect_error) {
// die("Connection failed: " . $mysqliNext->connect_error);
// }
// Open transactions
// https://dev.mysql.com/doc/refman/8.0/en/commit.html
// https://mariadb.com/kb/en/start-transaction/
// https://mariadb.com/kb/en/transactions/
// https://mariadb.com/kb/en/mariadb-transactions-and-isolation-levels-for-sql-server-users/
$mysqli->autocommit(FALSE);
$sql = "SHOW tables;";
$result = mysqli_query($mysqli, $sql);
if(mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_assoc($result)) {
if(isset($row["Tables_in_".$dbname])) {
$tables[] = $row["Tables_in_".$dbname];
}
}
}
// Print tables
print_r($tables);
// Move tables
$mysqli->query("SET FOREIGN_KEY_CHECKS = 0;");
foreach($tables as $table) {
$newTable = $nextDbname. ".".$table;
$table = $dbname.".".$table;
// $mysqli->query("RENAME TABLE $table TO $newTable;");
$mysqli->query("ALTER TABLE $table RENAME $newTable;");
}
$mysqli->query("SET FOREIGN_KEY_CHECKS = 1;");
// Close transactions
$mysqli->commit();
// Drop old tables: not required
// foreach($tables as $table) {
// $mysqli->query("DROP TABLE ".$table);
// }
// Close
mysqli_close($mysqli);
// mysqli_close($mysqliNext);