Skip to main content

Migrasi All Struktur MYSQL in server to Postgre database

<!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;\">&nbsp;</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

Popular posts from this blog

FPDF dengan CodeIgniter

Cetak Surat Keputusan Controller: <?php //File in controller named surat_keputusan.php defined('BASEPATH') OR exit('No direct script access allowed'); class Cetak_surat_keputusan extends CI_Controller { public function __construct()     {         parent::__construct();         $this->load->helper('url');         $this->load->database();                $this->db->select();         $this->db->from('surat.config_sk');                $query = $this->db->get();                 return $query->result();             } public function index() {      ...

Token_Model

<?php defined('BASEPATH') OR exit('No direct script access allowed'); class Token_model extends CI_Model { public function __construct() { parent::__construct(); $this->load->library('session'); $this->load->helper('url'); } public function token_feeder() { $username = '*****'; $password = '*****'; $data_sesi=array( 'username'=>$username, 'password'=>$password, 'sudah_login'=>true, ); $this->session->set_userdata($data_sesi); $username=$this->session->userdata('username'); $password=$this->session->userdata('password'); $mytoken = array('act'=>'GetToken', 'username'=>$username, 'password'=>$password); $payload = json_encode($mytoken); $ch = curl_init('http://192.168.30.99:8082/ws/live2.php'); curl_setopt($ch, CURLOPT_RETURNTRANS...

Layar Biru versi PHP Bagian 1 (file prefil_dbf.php)

file config.php <?php $db_uname = 'root'; $db_passwd = ''; $db_name = 'layar_biru'; //database yang dipilih $db_host = 'localhost'; $xbase_dir = 'D:\ACADEMIC\htdocs\layar_biru\files'; $die_on_mysql_error = false; // when investigating errors, set this to true $from_encoding=""; //Encoding of database, e.g. CP866 or empty, if convert is not required     file prefil.dbf   <?php include "config.php";            // please copy the config.sample.php and edit the correct fields include "classes/XBase/Table.php"; include "classes/XBase/Column.php"; include "classes/XBase/Record.php"; include "classes/DBFhandler.php"; use XBase\Table;  // Initializing vars ini_set( 'memory_limit', '2048M' ); set_time_limit( 0 ); $time_start = time(); $files = scandir($xbase_dir) or die ("Error! Could not open directory '$xbase_dir'."); $conn = new mysqli($db_host,...