File: sql_alter_pg.php

package info (click to toggle)
jffnms 0.8.3dfsg.1-2.1etch1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 4,928 kB
  • ctags: 5,050
  • sloc: php: 19,981; sh: 266; makefile: 91; perl: 52
file content (168 lines) | stat: -rw-r--r-- 5,892 bytes parent folder | download | duplicates (3)
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?
/* This file is part of JFFNMS
 * Copyright (C) <2002-2005> Javier Szyszlican <javier@szysz.com>
 * This program is licensed under the GNU GPL, full terms in the LICENSE file
 */
    error_reporting(7);
/*

ALTER TABLE interface_types CHANGE COLUMN autodiscovery_parameters autodiscovery_parameters varchar(200) NOT NULL default ''; # was varchar(40) NOT NULL default ''


ALTER TABLE interface_types ADD COLUMN autodiscovery_parameters_new varchar(200) default ''; 
UPDATE interface_types set autodiscovery_parameters_new = autodiscovery_parameters;
ALTER TABLE interface_types DROP COLUMN autodiscovery_parameters;
ALTER TABLE interface_types RENAME COLUMN autodiscovery_parameters_new to autodiscovery_parameters;

ALTER TABLE `slas_cond` CHANGE `condition` `cond` CHAR( 250 ) NOT NULL;




ALTER TABLE `events` CHANGE COLUMN `interface` `interface` char(150) NOT NULL; # was char(40) NOT NULL default ''
ALTER TABLE `hosts` CHANGE COLUMN `name` `name` char(255) NOT NULL; # was char(30) NOT NULL default ''
ALTER TABLE `interfaces_values` CHANGE COLUMN `value` `value` varchar(3000) NOT NULL; # was char(250) NOT NULL default ''
ALTER TABLE `nad_hosts` CHANGE COLUMN `description` `description` varchar(3000) NOT NULL; # was char(250) NOT NULL default ''
ALTER TABLE `slas_cond` CHANGE `condition` `cond` CHAR( 250 ) NOT NULL;
ALTER TABLE `slas` CHANGE `state` `state` INT( 10 ) NOT NULL DEFAULT '3';


*/

    $types["tinyint(1)"]="int2";
    $types["tinyint(3)"]="int2";
    $types["tinyint(10)"]="int2";
    $types["int(5)"]="int2";
    $types["int(6)"]="int2";
    $types["int(10)"]="int4";
    $types["decimal(12,2)"]="float8";
    $types["char(10)"]="varchar(10)";
    $types["char(60)"]="varchar(60)";
    $types["char(100)"]="varchar(100)";
    $types["char(150)"]="varchar(150)";
    $types["char(255)"]="varchar(255)";
    $types["varchar(3000)"]="varchar(3000)";
    $types["varchar(30)"]="varchar(30)";
    $types["char(250)"]="varchar(250)";
    $types["longtext"]="text";    
    $types["varchar(100)"]="varchar(100)";

    $fp = fopen("php://stdin","r");
    
    while ($line = fgets($fp)) {
	$new_sql="";
	//echo $line;
	$line = str_replace("`","", trim($line));
	//var_dump($line);
	//CHANGE
	if (	preg_match("/ALTER TABLE (\S+) CHANGE COLUMN (\S+) (\S+) (\S.+) NOT NULL default (\S+);/",$line,$parts) ||
		preg_match("/ALTER TABLE (\S+) CHANGE COLUMN (\S+) (\S+) (\S.+) DEFAULT (\S+) NOT NULL;/",$line,$parts)	||
		preg_match("/ALTER TABLE (\S+) CHANGE COLUMN (\S+) (\S+) (\S.+) NOT NULL;/",$line,$parts) ||
                preg_match("/ALTER TABLE (\S+) CHANGE (\S+) (\S+) (\S.+) NOT NULL;/",$line,$parts) ||
                preg_match("/ALTER TABLE (\S+) CHANGE (\S+) (\S+) (\S.+) NOT NULL DEFAULT (\S+);/i",$line,$parts)

		) {
	    //var_dump($parts);
	
	    $table = $parts[1];
	    $old_field = $parts[2];
	    $new_field = $parts[3];
	    $req_type = $parts[4];
	    $def = $parts[5];
	    
	    $type = $types[str_replace(" ","",strtolower($req_type))];
	    
	    if ($type!=NULL) {
		$temp_field = $new_field."_new_convert";

		$new_sql .=	"ALTER TABLE $table ADD COLUMN $temp_field $type; \n";
	        if (!empty($def)) $new_sql .=	"ALTER TABLE $table ALTER COLUMN $temp_field SET DEFAULT $def;\n";
		$new_sql .=	"UPDATE $table set $temp_field = $old_field;\n";
	        $new_sql .=	"ALTER TABLE $table DROP COLUMN $old_field CASCADE;\n";
	        $new_sql .=	"ALTER TABLE $table RENAME COLUMN $temp_field to $new_field;\n";
	    } else
		$new_sql = "BAD TYPE: ".$req_type." - ".$line."\n";
	}

	//ADD
	if (preg_match("/ALTER TABLE (\S+) ADD COLUMN (\S+) (\S.+) NOT NULL default (\S+);/",$line,$parts) || 
	    preg_match("/ALTER TABLE (\S+) ADD (\S+) (\S.+) DEFAULT (\S+) NOT NULL ;/",$line,$parts)    ) {

	    $table = $parts[1];
	    $new_field = $parts[2];
	    $type = $parts[3];
	    $def = $parts[4];

	    $type = $types[str_replace(" ","",strtolower($type))];
	    
	    $new_sql .=	"ALTER TABLE $table ADD COLUMN $new_field $type;\n";
	    $new_sql .=	"UPDATE $table SET $new_field = $def;\n";
	    $new_sql .=	"ALTER TABLE $table ALTER COLUMN $new_field SET DEFAULT $def;\n";
	}

	//ADD INDEX
	if (preg_match("/ALTER TABLE (\S+) add index (\S+) \((\S.+)\);/i",$line,$parts)) {
	    //var_dump($parts);
	
	    $table = $parts[1];
	    $field = $parts[2];
	    $fields = $parts[3];

	    $new_sql .=	"CREATE INDEX $field"."_".$table."_index on $table ($fields);\n";
	}

	//DROP INDEX
	if (preg_match("/ALTER TABLE (\S+) drop index (\S+);/",$line,$parts)) {
	    //var_dump($parts);
	
	    $table = $parts[1];
	    $field = $parts[2];

	    $new_sql .=	"DROP INDEX $field"."_".$table."_index;\n";
	}

	//CREATE TABLE - omit it 
	if (preg_match("/CREATE TABLE (\S.+)/",$line,$parts)) 
	    $new_sql = "";

	//ONLY FOR PG
	if (preg_match("/--- PG --- (\S.+)/",$line,$parts)) {
	    //var_dump($parts);
	
	    $new_sql .=	$parts[1]."\n";
	}

        //ONLY FOR MY - OMIT IT
        if (preg_match("/--- MY ---/",$line,$parts)) {
            //var_dump($parts);

    	    unset($new_sql);
        }

	//DROP TABLE
	if (preg_match("/DROP TABLE (\S+);/",$line,$parts)) {
	    $table = $parts[1];

	    $new_sql .=	"DROP TABLE $table CASCADE;\n";
	}

	//SET AUTONUMERIC
	//SELECT SETVAL('pollers_poller_groups_id_seq',(select case when max(id)>1000 then max(id) else 1000 end from pollers_poller_groups));
	//ALTER TABLE profiles_values AUTO_INCREMENT = 300;
	if (preg_match("/ALTER TABLE (\S+) AUTO_INCREMENT = (\d+);/",$line,$parts)) {
	    //var_dump($parts);
	
	    $table = $parts[1];
	    $value = $parts[2]-1;

	    $new_sql .=	"SELECT SETVAL('".$table."_id_seq',(select case when max(id)>$value then max(id) else $value end from ".$table."));\n";
	}
	
	if (isset($new_sql)) {
	    if (empty($new_sql)) 
		$new_sql = $line."\n"; //if it was not converted show the old line, no parsing required
	    echo $new_sql;	
	}
    }
    fclose ($fp);
?>