-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathuslims_alter_column.php
153 lines (127 loc) · 4 KB
/
uslims_alter_column.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
142
143
144
145
146
147
148
149
150
151
152
<?php
# developer defines
$logging_level = 2;
# end of developer defines
$self = __FILE__;
$notes = <<<__EOD
usage: $self {options} {db_config_file}
rename table column name
Options
--help : print this information and exit
--db dbname : (required) specify the database name
--table table : (required) table name
--change from to : column name from to [N.B. requires mariaDB >= 10.5.2]
--drop name : column name to drop
__EOD;
require "utility.php";
$u_argv = $argv;
array_shift( $u_argv ); # first element is program name
$dbname = "";
$table = "";
$change_from = "";
$change_to = "";
$drop = "";
while( count( $u_argv ) && substr( $u_argv[ 0 ], 0, 1 ) == "-" ) {
switch( $u_argv[ 0 ] ) {
case "--help": {
echo $notes;
exit;
}
case "--db": {
array_shift( $u_argv );
if ( !count( $u_argv ) ) {
error_exit( "\nOption --db requires an argument\n\n$notes" );
}
$dbname = array_shift( $u_argv );
if ( empty( $dbname ) ) {
error_exit( "--db requires a non-empty value\n\n$notes" );
}
break;
}
case "--table": {
array_shift( $u_argv );
if ( !count( $u_argv ) ) {
error_exit( "\nOption --table requires an argument\n\n$notes" );
}
$table = array_shift( $u_argv );
if ( empty( $table ) ) {
error_exit( "--table requires a non-empty value\n\n$notes" );
}
break;
}
case "--drop": {
array_shift( $u_argv );
if ( !count( $u_argv ) ) {
error_exit( "\nOption --drop requires an argument\n\n$notes" );
}
$drop = array_shift( $u_argv );
if ( empty( $drop ) ) {
error_exit( "--drop requires a non-empty value\n\n$notes" );
}
break;
}
case "--change": {
array_shift( $u_argv );
if ( count( $u_argv ) < 2 ) {
error_exit( "\nOption --change requires two arguments\n\n$notes" );
}
$change_from = array_shift( $u_argv );
$change_to = array_shift( $u_argv );
if ( empty( $change_from ) ||
empty( $change_from ) ) {
error_exit( "--change requires to non-empty values\n\n$notes" );
}
break;
}
default:
error_exit( "\nUnknown option '$u_argv[0]'\n\n$notes" );
}
}
$config_file = "db_config.php";
if ( count( $u_argv ) ) {
$use_config_file = array_shift( $u_argv );
} else {
$use_config_file = $config_file;
}
if ( count( $u_argv ) ) {
echo $notes;
exit;
}
if ( !file_exists( $use_config_file ) ) {
fwrite( STDERR, "$self:
$use_config_file does not exist
to fix:
cp ${config_file}.template $use_config_file
and edit with appropriate values
")
;
exit(-1);
}
file_perms_must_be( $use_config_file );
require $use_config_file;
if ( empty( $dbname ) ||
empty( $table ) ) {
echo $notes;
exit;
}
if ( empty( $change_from ) && empty( $change_to ) && empty( $drop ) ) {
error_exit( "--change or --drop must be specified\n\n$notes" );
}
if ( !empty( $change_from ) && !empty( $drop ) ) {
error_exit( "--change and --drop can not both be specified\n\n$notes" );
}
$db_handle = mysqli_connect( $dbhost, $user, $passwd, "" );
if ( !$db_handle ) {
write_logl( "could not connect to mysql: $dbhost, $user exiting\n" );
exit(-1);
}
if ( !empty( $change_from ) && !empty( $change_to ) ) {
$q = "ALTER TABLE $dbname.$table RENAME COLUMN $change_from TO $change_to";
}
if ( !empty( $drop ) ) {
$q = "ALTER TABLE $dbname.$table DROP COLUMN $drop";
}
$res = mysqli_query( $db_handle, $q );
if ( !$res ) {
error_exit( "db query failed : $q\ndb query error: " . mysqli_error($db_handle) );
}