forked from hholzgra/change_definer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange_definer.php
141 lines (91 loc) · 4.11 KB
/
change_definer.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
// ===== configration part starts =====
$for_real = true; // execute DDL statements
$verbose = true; // show DDL statements
$db_host = "127.0.0.1";
$db_user = "root";
$db_pass = "";
$old_definer_user = "admin";
$old_definer_host = "localhost";
$new_definer_user = "foo";
$new_definer_host = "bar";
// ===== configration part ends =====
$old_definer_sql = "`".$old_definer_user."`@`".$old_definer_host."`";
$old_definer=$old_definer_user."@".$old_definer_host;
$new_definer_sql = "`$new_definer_user`@`$new_definer_host`";
function ddl($query)
{
global $verbose, $for_real;
if ($verbose) {
echo "Query: $query\n\n";
}
if ($for_real) {
mysql_query($query) or die(mysql_error());
}
}
mysql_connect($db_host, $db_user, $db_pass) or die("can't connect to mysql");
// modify views
$get_views = "SELECT * FROM INFORMATION_SCHEMA.VIEWS WHERE DEFINER = '$old_definer'";
$res = mysql_query($get_views) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "modifying view $row[TABLE_SCHEMA].$row[TABLE_NAME]\n";
mysql_select_db($row["TABLE_SCHEMA"]);
$set = "SET SESSION character_set_client='$row[CHARACTER_SET_CLIENT]', collation_connection='$row[COLLATION_CONNECTION]'";
$alter = "ALTER DEFINER=$new_definer_sql VIEW `$row[TABLE_NAME]` AS $row[VIEW_DEFINITION]";
ddl($set);
ddl($alter);
}
mysql_free_result($res);
// modify triggers
$get_triggers = "SELECT * FROM INFORMATION_SCHEMA.TRIGGERS WHERE DEFINER = '$old_definer'";
$res = mysql_query($get_triggers) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "modifying trigger $row[TRIGGER_SCHEMA].$row[TRIGGER_NAME] on table $row[EVENT_OBJECT_SCHEMA].$row[EVENT_OBJECT_TABLE]\n";
mysql_select_db($row["TRIGGER_SCHEMA"]);
$lock = "LOCK TABLES `$row[EVENT_OBJECT_TABLE]` WRITE";
$unlock = "UNLOCK TABLES";
$drop = "DROP TRIGGER `$row[TRIGGER_NAME]`";
$show = "SHOW CREATE TRIGGER `$row[TRIGGER_NAME]`";
$res2 = mysql_query($show) or die($show.":".mysql_error());
$row2 = mysql_fetch_assoc($res2);
mysql_free_result($res2);
$set = "SET SESSION character_set_client='$row[CHARACTER_SET_CLIENT]', collation_connection='$row[COLLATION_CONNECTION]'";
$sql_mode = "SET SESSION SQL_MODE = '$row2[sql_mode]'";
$create = preg_replace('|^CREATE DEFINER=('.preg_quote($old_definer_sql).')|', "CREATE DEFINER=$new_definer_sql", $row2["SQL Original Statement"]);
ddl($set);
ddl($sql_mode);
ddl($lock);
ddl($drop);
ddl($create);
ddl($unlock);
}
// modify events
$get_events = "SELECT * FROM INFORMATION_SCHEMA.EVENTS WHERE DEFINER = '$old_definer'";
$res = mysql_query($get_events) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "modifying event $row[EVENT_SCHEMA].$row[EVENT_NAME]\n";
$set = "SET SESSION character_set_client='$row[CHARACTER_SET_CLIENT]', collation_connection='$row[COLLATION_CONNECTION]'";
// one extra option is needed after EVENT, ON COMPLETION is the simplest so we take this
$alter = "ALTER DEFINER=$new_definer_sql EVENT `$row[EVENT_SCHEMA]`.`$row[EVENT_NAME]` ON COMPLETION $row[ON_COMPLETION]";
ddl($set);
ddl($alter);
}
// modify procedures and functions
$get_procedures = "SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE DEFINER = '$old_definer'";
$res = mysql_query($get_procedures) or die(mysql_error());
while($row = mysql_fetch_assoc($res)) {
echo "modifying ".strtolower($row['ROUTINE_TYPE'])." $row[ROUTINE_SCHEMA].$row[ROUTINE_NAME]\n";
mysql_select_db($row["ROUTINE_SCHEMA"]);
$drop = "DROP $row[ROUTINE_TYPE] `$row[ROUTINE_NAME]`";
$show = "SHOW CREATE $row[ROUTINE_TYPE] `$row[ROUTINE_NAME]`";
$res2 = mysql_query($show) or die($show.":".mysql_error());
$row2 = mysql_fetch_assoc($res2);
mysql_free_result($res2);
$set = "SET SESSION character_set_client='$row[CHARACTER_SET_CLIENT]', collation_connection='$row[COLLATION_CONNECTION]'";
$sql_mode = "SET SESSION SQL_MODE = '$row2[sql_mode]'";
$create = preg_replace('|^CREATE DEFINER=('.preg_quote($old_definer_sql).')|', "CREATE DEFINER=$new_definer_sql", $row2["Create ".ucwords(strtolower($row['ROUTINE_TYPE']))]);
ddl($set);
ddl($sql_mode);
ddl($drop);
ddl($create);
}