forked from SimplesIP/pabx-app
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
97 lines
3.2 KiB
97 lines
3.2 KiB
#!/usr/bin/php -q |
|
<?php |
|
//error_reporting(E_ERROR); |
|
//ini_set('display_errors', "Off"); |
|
include("util/util.php"); |
|
define("MOVE_SUFIXO_TABLE", "__BK__"); |
|
define("MOVE_MAX_ERROS", 5); |
|
define("MOVE_NUM_REGISTROS", 500); |
|
|
|
/* |
|
* Informe a tabela a ser movida na linha de comando, para mais de uma tabela informe separadas por ";". |
|
*/ |
|
$tables = isset($argv[1]) ? trim($argv[1]) : ''; |
|
|
|
/* |
|
* Arquivo de log. |
|
*/ |
|
$pathLog = GetFilePath($argv[0]) . "move-table.log"; |
|
try { |
|
if (!$tables) { |
|
GeraExcept("Informe a tabela a ser movida!"); |
|
} |
|
|
|
$connStr = ''; |
|
$connStr = !$connStr ? GetDefStrDb() : $connStr; |
|
$conn = pg_connect($connStr); |
|
if (!$conn) { |
|
GeraExcept("Não foi possível estabelecer uma conexão com o banco de dados!"); |
|
} |
|
|
|
$erros = 0; |
|
$movido = 0; |
|
$lineErro = ''; |
|
/* |
|
* Cria array com as tabelas a serem movidas. |
|
*/ |
|
$tables = explode(';', $tables); |
|
|
|
foreach ($tables as $table) { |
|
/* |
|
* Tabela destino. |
|
*/ |
|
$mvTable = sprintf("%s%s", $table, MOVE_SUFIXO_TABLE); |
|
$lineErro = ''; |
|
try { |
|
if (!$table) |
|
break; |
|
/* |
|
* Apaga a tabela destino caso exista. |
|
*/ |
|
if (!pg_query(sprintf("drop table if exists %s", $mvTable))) { |
|
GeraExcept(sprintf("Não foi possivel apagar a tabela %s!", $mvTable)); |
|
} |
|
|
|
/* |
|
* Cria a tabela destino. |
|
*/ |
|
if (!pg_query(sprintf("select * into %s from %s where 1=2", $mvTable, $table))) { |
|
GeraExcept(sprintf("Não foi possivel criar a tabela %s!", $mvTable)); |
|
} |
|
|
|
/* |
|
* Recupera o numero de registros da tabela a ser movida. |
|
*/ |
|
if (!$result = pg_query(sprintf("select count(*) from %s where ", $table))) { |
|
GeraExcept(sprintf("Não foi possivel recuperar o numero de registros da tabela %s!", $table)); |
|
} |
|
$row = pg_fetch_row($result); |
|
$registros = $row[0]; |
|
|
|
$numCiclos = (int) ($registros / MOVE_NUM_REGISTROS) + (($registros % MOVE_NUM_REGISTROS) ? 1 : 0); |
|
$movido = 0; |
|
for ($i = 0; $i < $numCiclos; $i++) { |
|
$query = sprintf("insert into %s select * from %s where limit %s offset %s", $mvTable, $table, MOVE_NUM_REGISTROS, ($i * MOVE_NUM_REGISTROS)); |
|
if (!pg_query($query)) { |
|
echo (sprintf("Não foi possivel inserir os registros de %s a %s da tabela %s!", $movido, ($i * MOVE_NUM_REGISTROS), $mvTable)); |
|
} |
|
$movido = ($i * MOVE_NUM_REGISTROS); |
|
|
|
echo sprintf("Movendo de: %s a %s da tabela %s!\n", ($i * MOVE_NUM_REGISTROS), $registros, $table); |
|
} |
|
|
|
echo sprintf("Tabela %s movida!\n\n", $table); |
|
} catch (Exception $ex) { |
|
echo sprintf("%s\n", $ex->getMessage()); |
|
if (++$erros == MOVE_MAX_ERROS) { |
|
echo sprintf("A %s não pode ser movida\n", $table); |
|
break; |
|
} |
|
} |
|
} |
|
echo sprintf("Exportacao finalizada!\n\n"); |
|
exit(0); |
|
} catch (Exception $ex) { |
|
echo sprintf("%s\n", $ex->getMessage()); |
|
} |
|
?>
|