<!DOCTYPE html>
<html>
<head>
<title>Migrasi Struktur</title>
</head>
<body>
<!-- Progress bar holder -->
<link rel="stylesheet" href="progress/bootstrap.min.css">
<div id="progress" class="progress-bar progress-bar-striped active" style="width:500px;border:1px solid #000;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
</br>
<?php
@include "config-postgre.php"; // please copy the config.sample.php and edit the correct fields
@include "config.php"; // please copy the config.sample.php and edit the correct fields
// Initializing vars
ini_set( 'memory_limit', '2048M' );
set_time_limit( 0 );
$time_start = time();
/* $conn_postgre = pg_connect("host=$db_host dbname=$db_name user=$db_uname password=$db_passwd"); */
$conn_postgre = pg_connect("host=$db_host user=$db_uname password=$db_passwd");
$link = mysqli_connect($db_host_1,$db_uname_1,$db_passwd_1) or die ('Error connecting to mysql: ' . mysqli_error($link).'\r\n');
$sql="SHOW DATABASES";
if (!($result_database=mysqli_query($link,$sql))) {
printf("Error: %s\n", mysqli_error($link));
}
while( $row_database = mysqli_fetch_row( $result_database ) ){
if (($row_database[0]!="information_schema") && ($row_database[0]!="mysql")) {
//name_table
echo "<table>";
/* echo "<tr><td><strong>".$row_database[0]."</strong></td></tr>"; */
$postgre_sqls_delete = 'DROP DATABASE "'.$row_database[0].'" ';
$postgre_sqls_create = 'CREATE DATABASE "'.$row_database[0].'";';
$sql_check = "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower('".$row_database[0]."')";
$result_check =
$result = pg_query($conn_postgre, $sql_check);
if (!$result) {
echo "query did not execute";
}
if (pg_num_rows($result) == 0) {
pg_query($conn_postgre, $postgre_sqls_create);
}
$sql_1="SHOW TABLES";
$conn_mysql = new mysqli($db_host_1, $db_uname_1, $db_passwd_1, $row_database[0]) or die ("Error connecting to mysql $mysqli->connect_error");
if (!($result_tabel=mysqli_query($conn_mysql,$sql_1))) {
printf("Error: %s\n", mysqli_error($conn_mysql));
}
while( $row_tabel = mysqli_fetch_row( $result_tabel ) ){
if (($row_tabel[0]!="information_schema") && ($row_tabel[0]!=$row_database[0])) {
//name_table
$conn_postgre_1 = pg_connect("host=$db_host dbname=".$row_database[0]." user=$db_uname password=$db_passwd");
/* echo "<tr><td>Tabel :" .$row_tabel[0]."</br>"; */
$sql_create_table = 'CREATE TABLE IF NOT EXISTS "'. $row_tabel[0] .'"(';
/* echo $sql_create_table; */
$array_type = array(
'json' => 'json',
'bit' => 'bit varying',
'year' => 'smallint',
'tinyint' => 'smallint',
'smallint' => 'smallint',
'mediumint' => 'int',
'int' => 'int',
'bigint' => 'bigint',
'float' => 'real',
'double' => 'double precision',
'double precision' => 'double precision',
'numeric' => 'numeric',
'decimal' => 'decimal',
'char' => 'character',
'varchar' => 'character varying',
'date' => 'date',
'time' => 'time',
'datetime' => 'timestamp',
'timestamp' =>'timestamp',
'geometry' =>'geometry',
'point' =>'point',
'linestring' =>'line',
'polygon' =>'polygon',
'enum' =>'character varying(255)',
'set' =>'character varying(255)',
'tinytext' =>'text',
'mediumtext' =>'text',
'longtext' =>'text',
'text' =>'text',
'varbinary' =>'bytea',
'binary' =>'bytea',
'tinyblob' =>'bytea',
'mediumblob' =>'bytea',
'longblob' =>'bytea',
'blob' =>'bytea'
);
$sql_coloumn = "SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_SCHEMA = '".$row_database[0]."'
AND
TABLE_NAME = '".$row_tabel[0]."' ";
$result_coloumn=mysqli_query($conn_mysql,$sql_coloumn);
$rowcount=mysqli_num_rows($result_coloumn);
$i=1;
$line = array();
while( $row_coloumn = mysqli_fetch_row( $result_coloumn )) {
// Calculate the percentation
$percent = intval($i/$rowcount * 100)."%";
// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#000000;\"> </div>";
document.getElementById("information").innerHTML=" '.$i.' kolom tabel '.$row_tabel[0].' sedang diproses.";
</script>';
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();
// Sleep one second so we can see the delay
sleep(0);
$line[] = '"'.$row_coloumn[0].' " '. $array_type[$row_coloumn[1]];
$i++;
} ;
$str = implode(", ", $line);
/* echo $str; */
$sql_akhir = ") WITH (oids = false);";
/* echo $sql_akhir; */
$sql_final = $sql_create_table . $str . $sql_akhir;
/* echo $sql_final; */
/* if (pg_query($conn_postgre_1, $sql_final)){
echo ".";
} */
/* echo $sql_coloumn; */
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>
</body>
</html>
config:
<?php
$db_uname = 'postgres';
$db_passwd = ',,,,,';
$db_name = 'layar_biru';
$db_host = 'localhost';
$die_on_mysql_error = false; // when investigating errors, set this to true
<?php
$db_uname_1 = 'root';
$db_passwd_1 = '';
$db_name_1 = 'layar_biru';
$db_host_1 = 'localhost';
$die_on_mysql_error = false; // when investigating errors, set this to true
<html>
<head>
<title>Migrasi Struktur</title>
</head>
<body>
<!-- Progress bar holder -->
<link rel="stylesheet" href="progress/bootstrap.min.css">
<div id="progress" class="progress-bar progress-bar-striped active" style="width:500px;border:1px solid #000;"></div>
<!-- Progress information -->
<div id="information" style="width"></div>
</br>
<?php
@include "config-postgre.php"; // please copy the config.sample.php and edit the correct fields
@include "config.php"; // please copy the config.sample.php and edit the correct fields
// Initializing vars
ini_set( 'memory_limit', '2048M' );
set_time_limit( 0 );
$time_start = time();
/* $conn_postgre = pg_connect("host=$db_host dbname=$db_name user=$db_uname password=$db_passwd"); */
$conn_postgre = pg_connect("host=$db_host user=$db_uname password=$db_passwd");
$link = mysqli_connect($db_host_1,$db_uname_1,$db_passwd_1) or die ('Error connecting to mysql: ' . mysqli_error($link).'\r\n');
$sql="SHOW DATABASES";
if (!($result_database=mysqli_query($link,$sql))) {
printf("Error: %s\n", mysqli_error($link));
}
while( $row_database = mysqli_fetch_row( $result_database ) ){
if (($row_database[0]!="information_schema") && ($row_database[0]!="mysql")) {
//name_table
echo "<table>";
/* echo "<tr><td><strong>".$row_database[0]."</strong></td></tr>"; */
$postgre_sqls_delete = 'DROP DATABASE "'.$row_database[0].'" ';
$postgre_sqls_create = 'CREATE DATABASE "'.$row_database[0].'";';
$sql_check = "SELECT datname FROM pg_catalog.pg_database WHERE lower(datname) = lower('".$row_database[0]."')";
$result_check =
$result = pg_query($conn_postgre, $sql_check);
if (!$result) {
echo "query did not execute";
}
if (pg_num_rows($result) == 0) {
pg_query($conn_postgre, $postgre_sqls_create);
}
$sql_1="SHOW TABLES";
$conn_mysql = new mysqli($db_host_1, $db_uname_1, $db_passwd_1, $row_database[0]) or die ("Error connecting to mysql $mysqli->connect_error");
if (!($result_tabel=mysqli_query($conn_mysql,$sql_1))) {
printf("Error: %s\n", mysqli_error($conn_mysql));
}
while( $row_tabel = mysqli_fetch_row( $result_tabel ) ){
if (($row_tabel[0]!="information_schema") && ($row_tabel[0]!=$row_database[0])) {
//name_table
$conn_postgre_1 = pg_connect("host=$db_host dbname=".$row_database[0]." user=$db_uname password=$db_passwd");
/* echo "<tr><td>Tabel :" .$row_tabel[0]."</br>"; */
$sql_create_table = 'CREATE TABLE IF NOT EXISTS "'. $row_tabel[0] .'"(';
/* echo $sql_create_table; */
$array_type = array(
'json' => 'json',
'bit' => 'bit varying',
'year' => 'smallint',
'tinyint' => 'smallint',
'smallint' => 'smallint',
'mediumint' => 'int',
'int' => 'int',
'bigint' => 'bigint',
'float' => 'real',
'double' => 'double precision',
'double precision' => 'double precision',
'numeric' => 'numeric',
'decimal' => 'decimal',
'char' => 'character',
'varchar' => 'character varying',
'date' => 'date',
'time' => 'time',
'datetime' => 'timestamp',
'timestamp' =>'timestamp',
'geometry' =>'geometry',
'point' =>'point',
'linestring' =>'line',
'polygon' =>'polygon',
'enum' =>'character varying(255)',
'set' =>'character varying(255)',
'tinytext' =>'text',
'mediumtext' =>'text',
'longtext' =>'text',
'text' =>'text',
'varbinary' =>'bytea',
'binary' =>'bytea',
'tinyblob' =>'bytea',
'mediumblob' =>'bytea',
'longblob' =>'bytea',
'blob' =>'bytea'
);
$sql_coloumn = "SELECT COLUMN_NAME, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_SCHEMA = '".$row_database[0]."'
AND
TABLE_NAME = '".$row_tabel[0]."' ";
$result_coloumn=mysqli_query($conn_mysql,$sql_coloumn);
$rowcount=mysqli_num_rows($result_coloumn);
$i=1;
$line = array();
while( $row_coloumn = mysqli_fetch_row( $result_coloumn )) {
// Calculate the percentation
$percent = intval($i/$rowcount * 100)."%";
// Javascript for updating the progress bar and information
echo '<script language="javascript">
document.getElementById("progress").innerHTML="<div style=\"width:'.$percent.';background-color:#000000;\"> </div>";
document.getElementById("information").innerHTML=" '.$i.' kolom tabel '.$row_tabel[0].' sedang diproses.";
</script>';
// This is for the buffer achieve the minimum size in order to flush data
echo str_repeat(' ',1024*64);
// Send output to browser immediately
flush();
// Sleep one second so we can see the delay
sleep(0);
$line[] = '"'.$row_coloumn[0].' " '. $array_type[$row_coloumn[1]];
$i++;
} ;
$str = implode(", ", $line);
/* echo $str; */
$sql_akhir = ") WITH (oids = false);";
/* echo $sql_akhir; */
$sql_final = $sql_create_table . $str . $sql_akhir;
/* echo $sql_final; */
/* if (pg_query($conn_postgre_1, $sql_final)){
echo ".";
} */
/* echo $sql_coloumn; */
}
}
echo "</td>";
echo "</tr>";
echo "</table>";
}
}
?>
</body>
</html>
config:
<?php
$db_uname = 'postgres';
$db_passwd = ',,,,,';
$db_name = 'layar_biru';
$db_host = 'localhost';
$die_on_mysql_error = false; // when investigating errors, set this to true
<?php
$db_uname_1 = 'root';
$db_passwd_1 = '';
$db_name_1 = 'layar_biru';
$db_host_1 = 'localhost';
$die_on_mysql_error = false; // when investigating errors, set this to true
Comments
Post a Comment