diff --git a/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php b/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php index 48e2487f..c186e3cd 100755 --- a/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php +++ b/asterisk/var_lib_asterisk/scripts/manutencaoDB/importaEmpresa.php @@ -46,27 +46,26 @@ try { /* * Cria a coluna org_id em todas as tabelas. */ - //CriaCampo($dbcon); + CriaCampo($dbcon); /* - * Cria as empresas padrao. - */ - //CriaOrgPadrao($dbcon); + * Cria as empresas padrao. + */ + CriaOrgPadrao($dbcon); + importIdTables($dbcon); - //corrigeDadosFila($dbcon); + corrigeDadosFila($dbcon); /* - * Apaga todas as fk. - */ - // ApagaFK(); - + * Apaga todas as fk. + */ + ApagaFK($dbcon); /* * Modifica todas as chaves primarias. */ - // ModificaPK(); - + ModificaPK($dbcon); /* * Modifica os indices. */ - //ModificaIdx($dbcon); + ModificaIdx($dbcon); echo "Script finalizado com sucesso!\n"; @@ -81,23 +80,21 @@ function CriaCampo($dbcon) 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)) { + $result = pg_query($dbcon, $query); + if (!$result) { throw new Exception("Não foi possivel carregar a lista de tabelas."); } - - $tables = pg_fetch_all($resultTables); - + $tables = pg_fetch_all($result); $except = ['pbx_organizacao', 'pbx_organizacao_usuarios', 'pbx_usuarios', 'pbx_municipios']; - + foreach ($tables as $table) { if (in_array($table["table_name"], $except)) { continue; } - $query = "DO $$ - BEGIN - IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = '{$table["table_name"]}' AND column_name = 'org_id') THEN - ALTER TABLE {$table["table_name"]} DROP COLUMN IF EXISTS org_id; + BEGIN + IF EXISTS (SELECT 1 FROM information_schema.columns WHERE table_name = '{$table["table_name"]}' AND column_name = 'org_id') THEN + ALTER TABLE {$table["table_name"]} DROP COLUMN IF EXISTS org_id; END IF; END $$;"; $query .= sprintf("alter table %s add org_id int not null default 0;", $table["table_name"]); @@ -222,16 +219,45 @@ function ModificaPK($dbcon) } $tables = pg_fetch_all($result); - foreach ($tables as $table) { - // Remove a pk - $query = sprintf("ALTER TABLE \"%s\" DROP CONSTRAINT \"%s\";", $table["table_name"], $table["constraint_name"]); - if (!$result = pg_query($dbcon, $query)) { - throw new Exception(sprintf("Não foi remover a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); - } - // Recria a pk - $query = sprintf("ALTER TABLE \"%s\" ADD CONSTRAINT \"%s\" PRIMARY KEY (%s,org_id);", $table["table_name"], $table["constraint_name"], $table["key_columns"]); - if (!$result = pg_query($dbcon, $query)) { - throw new Exception(sprintf("Não foi criar a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + // foreach ($tables as $table) { + // // Remove a pk + // $query = sprintf("ALTER TABLE \"%s\" DROP CONSTRAINT \"%s\";", $table["table_name"], $table["constraint_name"]); + // if (!$result = pg_query($dbcon, $query)) { + // throw new Exception(sprintf("Não foi remover a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + // } + // // Recria a pk + // $query = sprintf("ALTER TABLE \"%s\" ADD CONSTRAINT \"%s\" PRIMARY KEY (%s,org_id);", $table["table_name"], $table["constraint_name"], $table["key_columns"]); + // if (!$result = pg_query($dbcon, $query)) { + // throw new Exception(sprintf("Não foi criar a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + // } + // } + + foreach ($tables as $table) { + // Verifica se a tabela é pbx_organizacao_usuarios + //Quando uma tabela for pbx_organizacao_usuarios, o org_id será id_organizacao; + if ($table["table_name"] != "pbx_organizacao_usuarios") { + // Remove a pk + $query = sprintf("ALTER TABLE \"%s\" DROP CONSTRAINT \"%s\";", $table["table_name"], $table["constraint_name"]); + if (!$result = pg_query($dbcon, $query)) { + throw new Exception(sprintf("Não foi possível remover a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + } + + // Recria a pk + $query = sprintf("ALTER TABLE \"%s\" ADD CONSTRAINT \"%s\" PRIMARY KEY (%s, org_id);", $table["table_name"], $table["constraint_name"], $table["key_columns"]); + if (!$result = pg_query($dbcon, $query)) { + throw new Exception(sprintf("Não foi possível criar a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + } + }else{ + $query = sprintf("ALTER TABLE \"%s\" DROP CONSTRAINT \"%s\";", $table["table_name"], $table["constraint_name"]); + if (!$result = pg_query($dbcon, $query)) { + throw new Exception(sprintf("Não foi possível remover a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + } + + // Recria a pk + $query = sprintf("ALTER TABLE \"%s\" ADD CONSTRAINT \"%s\" PRIMARY KEY (%s, id_organizacao);", $table["table_name"], $table["constraint_name"], $table["key_columns"]); + if (!$result = pg_query($dbcon, $query)) { + throw new Exception(sprintf("Não foi possível criar a constraint \"%s\" da tabela \"%s\"", $table["constraint_name"], $table["table_name"])); + } } } }