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

Menggenerate nomor id

<?php //config $database = "dopo"; $tabel = "po"; $kolom_generate ="id"; $kolom_referensi = "id"; /* Database connection start */ $servername = "localhost"; $username = "root"; $password = ""; $dbname = $database; $mysqli = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error()); $q1 = "select * from $tabel order by $kolom_referensi DESC"; /* Database connection end */ echo '<table>'; // connect to the database // number of results to show per page $per_page = 10000; // figure out the total pages in the database if ($result = $mysqli->query($q1)) { if ($result->num_rows != 0) { $total_results = $result->num_rows; // ceil() returns the next highest integer value by rounding up value if necessary $total_pages = ceil($total_results / $per_page); // check if the 'page' variab...

Membuat Codeigniter PDF di CPANEL

1. Download FPDF dan copykan di folder application/thirdparty/pdf/ 2. Buat file Fpdf_gen.php di libraries. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); class Fpdf_gen { public function __construct() {  require_once APPPATH.'third_party/fpdf/fpdf-1.8.php'; define('FPDF_FONTPATH', APPPATH.'third_party/fpdf/font/'); $pdf = new FPDF(); $pdf->AddPage(); $CI =& get_instance(); $CI->fpdf = $pdf; } public function Footer() { $this->fpdf->SetY(-15); $this->fpdf->SetFont('Arial','I',8); $this->fpdf->SetTextColor(128); $this->fpdf->Cell(0,10,'Page ',0,0,'C'); } } 3. Buat file pdf.php di controller <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); defined('BASEPATH') OR exit('No direct script access allowed'); class Pdf extends CI_Controller {   public f...

CRUD dengan vue.js

pada tutorial kali ini kita akan belajar cara menampilkan data mengunakan  Vue JS  di framework  Codeigniter,  di dutorial sebelumnya kita sudah mempelajari cara Crud mengunakan Vue JS dan Codeigniter, kalian bisa lihat tutorialnya disini  CRUD CODEIGNITER DAN VUE JS  . kali ini kita akan mencoba menampilkan data yang ada pada database dengan framwork codeigniter dan vue js, untuk koneksi database di codeigniter adalah : <?php defined('BASEPATH') OR exit('No direct script access allowed'); $active_group = 'default'; $query_builder = TRUE; $db['default'] = array(     'dsn'    => '',     'hostname' => 'localhost',     'username' => 'root',     'password' => 'root',     'database' => 'civue',     'dbdriver' => 'mysqli',     'dbprefix' => '',     'pconnect' => FALSE,     ...