|
|
|
@ -44,19 +44,15 @@ REFERENCES pbx_organizacao(id) ON DELETE CASCADE;
|
|
|
|
|
try { |
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
* Cria as empresas padrao. |
|
|
|
|
* Cria a coluna org_id em todas as tabelas. |
|
|
|
|
*/ |
|
|
|
|
CriaOrgPadrao($dbcon); |
|
|
|
|
|
|
|
|
|
//CriaCampo($dbcon); |
|
|
|
|
/* |
|
|
|
|
* Cria a coluna org_id em todas as tabelas. |
|
|
|
|
* Cria as empresas padrao. |
|
|
|
|
*/ |
|
|
|
|
CriaCampo($dbcon); |
|
|
|
|
|
|
|
|
|
//CriaOrgPadrao($dbcon); |
|
|
|
|
importIdTables($dbcon); |
|
|
|
|
|
|
|
|
|
corrigeDadosFila($dbcon); |
|
|
|
|
|
|
|
|
|
//corrigeDadosFila($dbcon); |
|
|
|
|
/* |
|
|
|
|
* Apaga todas as fk. |
|
|
|
|
*/ |
|
|
|
@ -80,16 +76,16 @@ try {
|
|
|
|
|
|
|
|
|
|
function CriaCampo($dbcon) |
|
|
|
|
{ |
|
|
|
|
$query = "select table_name from information_schema.tables a |
|
|
|
|
where table_schema = 'public' |
|
|
|
|
and table_type = 'BASE TABLE' |
|
|
|
|
and not exists(select '' from information_schema.columns where table_name = a.table_name and column_name = 'org_id');"; |
|
|
|
|
$query = "SELECT table_name FROM information_schema.tables a |
|
|
|
|
WHERE table_schema = 'public' |
|
|
|
|
AND table_type = 'BASE TABLE' |
|
|
|
|
AND NOT EXISTS(SELECT '' FROM information_schema.columns WHERE table_name = a.table_name AND column_name = 'org_id');"; |
|
|
|
|
|
|
|
|
|
if (!$result = pg_query($dbcon, $query)) { |
|
|
|
|
throw new Exception("Não foi possivel carregar a lista de tabelas."); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$tables = pg_fetch_all($result); |
|
|
|
|
$tables = pg_fetch_all($resultTables); |
|
|
|
|
|
|
|
|
|
$except = ['pbx_organizacao', 'pbx_organizacao_usuarios', 'pbx_usuarios', 'pbx_municipios']; |
|
|
|
|
|
|
|
|
@ -281,6 +277,51 @@ function ModificaIdx($dbcon)
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// function importIdTables($dbcon) |
|
|
|
|
// { |
|
|
|
|
// $tables = [ |
|
|
|
|
// 'pbx_parametros' => 'id', |
|
|
|
|
// 'pbx_features_featuremap' => 'id', |
|
|
|
|
// 'pbx_features_general' => 'id', |
|
|
|
|
// 'pbx_iax_general' => 'id', |
|
|
|
|
// 'pbx_facilidades' => 'id', |
|
|
|
|
// 'pbx_voicemail_general' => 'id', |
|
|
|
|
// 'pbx_sip_general' => 'id', |
|
|
|
|
// 'pbx_workflow_parametros' => 'wkf_id' |
|
|
|
|
// ]; |
|
|
|
|
|
|
|
|
|
// $org_id = GetOrganizacao(); |
|
|
|
|
// foreach ($tables as $table => $column) { |
|
|
|
|
// if (!$column) { |
|
|
|
|
// $column = 'id'; |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// $sql = "SELECT MAX($column) AS id FROM {$table};"; |
|
|
|
|
// $result = pg_query($dbcon, $sql); |
|
|
|
|
// $resp = pg_fetch_assoc($result); |
|
|
|
|
// if ($resp['id']) { |
|
|
|
|
// ___Gravalog("Não foi possível o ID da tabela {$table};"); |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
// $query = "DO $$ |
|
|
|
|
// BEGIN |
|
|
|
|
// IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = '{$table}' AND column_name = '{$column}') THEN |
|
|
|
|
// ALTER TABLE {$table} DROP COLUMN IF EXISTS {$column}; |
|
|
|
|
// END IF; |
|
|
|
|
// ALTER TABLE {$table} ADD COLUMN {$column} SERIAL PRIMARY KEY; |
|
|
|
|
// END $$;"; |
|
|
|
|
// $result = pg_query($dbcon, $query); |
|
|
|
|
|
|
|
|
|
// $id = $resp['id']; |
|
|
|
|
// $query = "CREATE TEMPORARY TABLE t{$table} AS SELECT * FROM {$table} WHERE $column = 1;"; |
|
|
|
|
// $query .= "ALTER TABLE t{$table} DROP COLUMN $column;"; |
|
|
|
|
// $query .= "INSERT INTO {$table} SELECT * FROM t{$table};"; |
|
|
|
|
// $query .= "UPDATE {$table} SET org_id = $org_id WHERE $column = $id"; |
|
|
|
|
// $result = pg_query($dbcon, $query); |
|
|
|
|
// pg_query($dbcon, "DROP TABLE IF EXISTS t{$table};"); |
|
|
|
|
// } |
|
|
|
|
// } |
|
|
|
|
|
|
|
|
|
function importIdTables($dbcon) |
|
|
|
|
{ |
|
|
|
|
$tables = [ |
|
|
|
@ -294,38 +335,79 @@ function importIdTables($dbcon)
|
|
|
|
|
'pbx_workflow_parametros' => 'wkf_id' |
|
|
|
|
]; |
|
|
|
|
|
|
|
|
|
$org_id = GetOrganizacao(); |
|
|
|
|
// Obter o ID da organização padrão |
|
|
|
|
$org_id = getOrganizationId($dbcon); |
|
|
|
|
|
|
|
|
|
foreach ($tables as $table => $column) { |
|
|
|
|
if (!$column) { |
|
|
|
|
$column = 'id'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$sql = "SELECT MAX($column) AS id FROM {$table};"; |
|
|
|
|
$result = pg_query($dbcon, $sql); |
|
|
|
|
$resp = pg_fetch_assoc($result); |
|
|
|
|
if ($resp['id']) { |
|
|
|
|
___Gravalog("Não foi possível o ID da tabela {$table};"); |
|
|
|
|
// Verificar se a coluna 'id' já existe e, se sim, removê-la |
|
|
|
|
dropColumnIfExists($dbcon, $table, $column); |
|
|
|
|
|
|
|
|
|
// Adicionar a coluna 'id' do tipo SERIAL |
|
|
|
|
addSerialColumn($dbcon, $table, $column); |
|
|
|
|
|
|
|
|
|
// Criar tabela temporária e transferir dados |
|
|
|
|
createTempTableAndTransferData($dbcon, $table, $column, $org_id); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function getOrganizationId($dbcon) |
|
|
|
|
{ |
|
|
|
|
$consulta = "SELECT id FROM pbx_organizacao po ORDER BY po.id ASC LIMIT 1;"; |
|
|
|
|
$retornoOrg = pg_query($dbcon, $consulta); |
|
|
|
|
|
|
|
|
|
if (!$retornoOrg) { |
|
|
|
|
throw new Exception('Não foi encontrado nenhuma organização padrão'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$linha = pg_fetch_assoc($retornoOrg); |
|
|
|
|
return $linha['id']; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function dropColumnIfExists($dbcon, $table, $column) |
|
|
|
|
{ |
|
|
|
|
$query = "DO $$ |
|
|
|
|
BEGIN |
|
|
|
|
IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = '{$table}' AND column_name = '{$column}') THEN |
|
|
|
|
ALTER TABLE {$table} DROP COLUMN IF EXISTS {$column}; |
|
|
|
|
END IF; |
|
|
|
|
ALTER TABLE {$table} ADD COLUMN {$column} SERIAL PRIMARY KEY; |
|
|
|
|
END $$;"; |
|
|
|
|
|
|
|
|
|
$result = pg_query($dbcon, $query); |
|
|
|
|
|
|
|
|
|
$id = $resp['id']; |
|
|
|
|
if (!$result) { |
|
|
|
|
throw new Exception("Erro ao remover a coluna '{$column}' da tabela '{$table}'"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function addSerialColumn($dbcon, $table, $column) |
|
|
|
|
{ |
|
|
|
|
$query = "ALTER TABLE {$table} ADD COLUMN {$column} SERIAL PRIMARY KEY;"; |
|
|
|
|
$result = pg_query($dbcon, $query); |
|
|
|
|
|
|
|
|
|
if (!$result) { |
|
|
|
|
throw new Exception("Erro ao adicionar a coluna '{$column}' na tabela '{$table}'"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function createTempTableAndTransferData($dbcon, $table, $column, $org_id) |
|
|
|
|
{ |
|
|
|
|
$query = "CREATE TEMPORARY TABLE t{$table} AS SELECT * FROM {$table} WHERE $column = 1;"; |
|
|
|
|
$query .= "ALTER TABLE t{$table} DROP COLUMN $column;"; |
|
|
|
|
$query .= "INSERT INTO {$table} SELECT * FROM t{$table};"; |
|
|
|
|
$query .= "UPDATE {$table} SET org_id = $org_id WHERE $column = $id"; |
|
|
|
|
$query .= "UPDATE {$table} SET org_id = $org_id WHERE $column = 1;"; |
|
|
|
|
|
|
|
|
|
$result = pg_query($dbcon, $query); |
|
|
|
|
pg_query($dbcon, "DROP TABLE IF EXISTS t{$table};"); |
|
|
|
|
|
|
|
|
|
if (!$result) { |
|
|
|
|
throw new Exception("Erro ao criar tabela temporária e transferir dados para '{$table}'"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
pg_query($dbcon, "DROP TABLE IF EXISTS t{$table};"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function corrigeDadosFila($dbcon) |
|
|
|
|
{ |
|
|
|
|