I developed a little script to admin scripts:
import,
export,
sincronize from a db to another
delete
the schemas are created under metabase/schemas
But I found problems:
1) When a database already exists in a DBMS it don't put the diferent values from the original database.
2) When the original (from) database/table has a field to be created in
second, it does, but at the end of the list of the fields.
it can cause some problems in applications that use insert
into table values (,,,,,) without defined fields, just sequence.
the problems above don't happen when the database (to) doesn't exist and is created.
someone can help? or make any sugestions?
the script is the below: admin_schemas.php in directory metabase.
<?
/* Desenvolvido por Alexandre Miguel alexandremasbr@...
Sugestions are welcome
*/
//if (ob_get_level() == 0) ob_start();
$dir_schemas= getcwd()."/schemas";
//echo $dir_schemas;
echo ' Choose the action<br><br>
<form method="POST" >
<input type="radio"
name="action" value="import"> Import schema from database <br>
<input type="radio"
name="action" value="export"> Export schema to database. <br>
<input type="radio"
name="action" value="sincronize"> Sincronize schema/data from a
database to another database. <br>
<input type="radio"
name="action" value="delete"> Delete schema (only the file in the
disk)<br><br>
<input type="submit" value="Submit" >
</form>
<hr>';
if ($_POST[action]== "import") {
echo '
Usage: Connection-string<br>
<!--Example:
mysql://user:password@host/databasename?Options/Port=/var/lib/mysql/mysql.sock<br>-->
Example: mysql://root:password@localhost/database<br>
<form method="POST" >
Conection String <input type="text" size="80"name="import_conection" ><br>
Database/Schema Name <input
type="text" size="40" name="import_schema_name" ><br>
<input type="checkbox"
name="only_schema" value="only_schema"> Only Definition? (not the
data). <br><br>
<input type="submit" value="Submit" >
</form> ';
}elseif ($_POST[action]=="export") {
$files_schemas = listfiles("schemas");
echo "Choose a schema";
echo " <form method='POST' ><table>";
$path = $dir_schemas;
foreach ($files_schemas as $key=>$value) {
echo "<tr><td>
<input type='radio'
name='export_schema_name' value='$value'> $value
</td><td>";
;
# We may want a file size. NOTE: needs $path to stat
if( filesize( $path . "/" . $value ) >= 1024 ) {
# Size in kilobytes
print " " .
round( filesize( $path . "/" . $value ) / 1024, 1 ) . " KB\n";
} elseif( filesize( $path . "/" . $value ) >= 1048576 ) {
# Size in megabytes
print " " .
round( filesize( $path . "/" . $value ) / 1024 / 1024, 1 ) . " MB\n";
} else {
# Size in bytes
print " " . filesize( $path . "/" . $value ) . " bytes";
}
echo "</td><td>";
echo date ("F d Y H:i:s.", filemtime($dir_schemas."/".$value))."</td></tr>";
}
echo "</table>";
echo '<br>
Define a conection: <br>
<form method="POST" >
Example: mysql://root:password@localhost/database<br>
Conection
String <input type="text" size="80"name="export_conection"
><br><br>';
echo "<input type='submit' value='Submit' >
</form>";
} elseif ($_POST[action] == "delete") {
$files_schemas = listfiles("schemas");
echo "Choose a schema";
echo " <form method='POST' ><table>";
$path = $dir_schemas;
foreach ($files_schemas as $key=>$value) {
echo "<tr><td>
<input type='radio'
name='delete_schema_name' value='$value'> $value
</td><td>";
;
# We may want a file size. NOTE: needs $path to stat
if( filesize( $path . "/" . $value ) >= 1024 ) {
# Size in kilobytes
print " " .
round( filesize( $path . "/" . $value ) / 1024, 1 ) . " KB\n";
} elseif( filesize( $path . "/" . $value ) >= 1048576 ) {
# Size in megabytes
print " " .
round( filesize( $path . "/" . $value ) / 1024 / 1024, 1 ) . " MB\n";
} else {
# Size in bytes
print " " . filesize( $path . "/" . $value ) . " bytes";
}
echo "</td><td>";
echo date ("F d Y H:i:s.", filemtime($dir_schemas."/".$value))."</td></tr>";
}
echo "</table>";
echo "<input type='submit' value='Submit' ></form>";
} elseif ($_POST[action] == "sincronize") {
echo '
Database To Get Data/Schema<br>
<!--Example:
mysql://user:password@host/databasename?Options/Port=/var/lib/mysql/mysql.sock<br>-->
Example: mysql://root:password@localhost/database<br>
<form method="POST" >
Conection String <input
type="text" size="80"name="sinc_import_conection" ><br>
Database To Put Data/Schema<br>
<!--Example:
mysql://user:password@host/databasename?Options/Port=/var/lib/mysql/mysql.sock<br>-->
Example: mysql://root:password@localhost/database<br>
Conection String <input
type="text" size="80"name="sinc_export_conection" ><br>
Database/Schema Name <input
type="text" size="40" name="sinc_schema_name" ><br>
<input type="checkbox"
name="only_schema" value="only_schema"> Only Definition? (not the
data). <br><br>
<input type="submit" value="Submit" >
</form> ';
}
require("metabase_parser.php");
require("metabase_manager.php");
require("metabase_database.php");
require("metabase_interface.php");
require("metabase_lob.php");
require("xml_parser.php");
if (isset($_POST[import_conection]) && isset($_POST[import_schema_name])){
$arguments=array(
"Connection"=>$_POST[import_conection]
);
//unset($xml);
Function Dump($output) {
global $xml;
$xml .= $output;
}
set_time_limit(0);
$manager=new metabase_manager_class;
if(strlen($error=$manager->GetDefinitionFromDatabase($arguments))==0) {
//print_r($manager->database_definition);
unset($manager->fail_on_invalid_names);
if ($_POST[only_schema] == "only_schema") {
$error=$manager->DumpDatabase(array(
"Output"=>"Dump",
"EndOfLine"=>"\n",
"Definition"=> "1"
));
} else {
$error=$manager->DumpDatabase(array(
"Output"=>"Dump",
"EndOfLine"=>"\n",
)
);
}
} else {
echo "Error: $error\n";
}
if($manager->database) {
/* if(count($manager->warnings)>0)
echo "WARNING:\n",implode($manager->warnings,"!\n"),"<br>";
echo MetabaseDebugOutput($manager->database)."<br />";
ob_flush();
flush();
sleep(1);*/
$manager->CloseSetup();
}
//$xml = MetabaseDebugOutput($manager->database);
$filename = $dir_schemas."/".$_POST[import_schema_name].".schema";
//if (is_writable($filename)) {
// Em nosso exemplo, nós estamos abrindo $filename em modo de append (acréscimo).
// O ponteiro do arquivo estará no final dele desde
// que será aqui que $somecontent será escrito com fwrite().
if (!$handle = fopen($filename, 'w')) {
print "Erro abrindo arquivo ($filename)";
exit;
}
// Escrevendo $somecontent para o arquivo aberto.
if (!fwrite($handle, $xml)) {
print "Erro escrevendo no arquivo ($filename)";
echo $xml;
exit;
}
print "Sucess: writed <br><textarea
cols=120 rows=20>$xml</textarea><br> to file
($filename)";
fclose($handle);
echo "File ".$_POST[import_schema_name].".schema writed with sucess!<br>";
}
//export schema
if (isset($_POST[export_conection]) && isset($_POST[export_schema_name])) {
echo "Beginning to export";
ob_flush();
flush();;
//echo "Tamos aqui";
$ficheiro_do_esquema = $dir_schemas."/".$_POST[export_schema_name];
//echo "$ficheiro_do_esquema";
$variaveis=array();
$argumentos=array(
"Connection"=>$_POST[export_conection]
);
$gestor = new metabase_manager_class;
//Criar o objecto da classe de gestão do Metabase.
ini_set("memory_limit", "128M");
//ob_implicit_flush();
$sucesso=$gestor->UpdateDatabase($ficheiro_do_esquema,
$ficheiro_do_esquema.".before", $argumentos, $variaveis);
if(!$sucesso){
echo "Erro: ".$gestor->error."\n";
} else {
echo "<br />schema included with sucess";
}
//Se o procedimento de instalação falhou, exiba a mensagem de erro para determinar o que correu mal.
if(count($gestor->warnings)>0){
//ob_implicit_flush();
echo "AVISO:\n",implode($gestor->warnings,"!\n"),"\n";
}
}
if (isset($_POST[delete_schema_name])) {
if(unlink($dir_schemas."/".$_POST[delete_schema_name])){
echo "File deleted";
} else {
echo "some error in delete file";
}
}
// sincronize routine
if (isset($_POST[sinc_import_conection]) &&
isset($_POST[sinc_export_conection]) &&
isset($_POST[sinc_schema_name])){
//import destiny schema to get no errors
echo "1) Beginning first pass: import destiny schema";
ob_flush();
flush();
$arguments=array(
"Connection"=>$_POST[sinc_export_conection]
);
//unset($xml);
Function Dump($output) {
global $xml;
$xml .= $output;
}
set_time_limit(0);
$manager=new metabase_manager_class;
if(strlen($error=$manager->GetDefinitionFromDatabase($arguments))==0) {
//print_r($manager->database_definition);
unset($manager->fail_on_invalid_names);
if ($_POST[only_schema] == "only_schema") {
$error=$manager->DumpDatabase(array(
"Output"=>"Dump",
"EndOfLine"=>"\n",
"Definition"=> "1"
));
} else {
$error=$manager->DumpDatabase(array(
"Output"=>"Dump",
"EndOfLine"=>"\n",
)
);
}
} else {
echo "Error: $error\n";
}
if($manager->database) {
/* if(count($manager->warnings)>0)
echo "WARNING:\n",implode($manager->warnings,"!\n"),"<br>";
echo MetabaseDebugOutput($manager->database)."<br />";
ob_flush();
flush();
sleep(1);*/
$manager->CloseSetup();
}
//$xml = MetabaseDebugOutput($manager->database);
$filename = $dir_schemas."/".$_POST[sinc_schema_name].".schema.before";
//if (is_writable($filename)) {
// Em nosso exemplo, nós estamos abrindo $filename em modo de append (acréscimo).
// O ponteiro do arquivo estará no final dele desde
// que será aqui que $somecontent será escrito com fwrite().
if (!$handle = fopen($filename, 'w')) {
print "Erro abrindo arquivo ($filename)";
exit;
}
// Escrevendo $somecontent para o arquivo aberto.
if (!fwrite($handle, $xml)) {
print "Erro escrevendo no arquivo ($filename)";
echo $xml;
exit;
}
//print "Sucess: writed <br><textarea
cols=120 rows=20>$xml</textarea><br> to file
($filename)";
fclose($handle);
echo "File ".$_POST[sinc_schema_name].".schema writed with sucess!<br>
End of pass 1<hr>";
ob_flush();
flush();
///importing origin schema
echo "2) Beginning second pass: importing origin schema";
ob_flush();
flush();
$arguments=array(
"Connection"=>$_POST[sinc_import_conection]
);
unset($xml);
set_time_limit(0);
$manager=new metabase_manager_class;
if(strlen($error=$manager->GetDefinitionFromDatabase($arguments))==0) {
//print_r($manager->database_definition);
unset($manager->fail_on_invalid_names);
if ($_POST[only_schema] == "only_schema") {
$error=$manager->DumpDatabase(array(
"Output"=>"Dump",
"EndOfLine"=>"\n",
"Definition"=> "1"
));
} else {
$error=$manager->DumpDatabase(array(
"Output"=>"Dump",
"EndOfLine"=>"\n",
)
);
}
} else {
echo "Error: $error\n";
}
if($manager->database) {
/* if(count($manager->warnings)>0)
echo "WARNING:\n",implode($manager->warnings,"!\n"),"<br>";
echo MetabaseDebugOutput($manager->database)."<br />";
ob_flush();
flush();
sleep(1);*/
$manager->CloseSetup();
}
//$xml = MetabaseDebugOutput($manager->database);
$filename = $dir_schemas."/".$_POST[sinc_schema_name].".schema";
//if (is_writable($filename)) {
// Em nosso exemplo, nós estamos abrindo $filename em modo de append (acréscimo).
// O ponteiro do arquivo estará no final dele desde
// que será aqui que $somecontent será escrito com fwrite().
if (!$handle = fopen($filename, 'w')) {
print "Erro abrindo arquivo ($filename)";
exit;
}
// Escrevendo $somecontent para o arquivo aberto.
if (!fwrite($handle, $xml)) {
print "Erro escrevendo no arquivo ($filename)";
echo $xml;
exit;
}
//print "Sucess: writed <br><textarea
cols=120 rows=20>$xml</textarea><br> to file
($filename)";
fclose($handle);
echo "File ".$_POST[sinc_schema_name].".schema writed with sucess!<br>
End of pass 2<hr>";
ob_flush();
flush();
echo "3) Beginning pass 3 export schema/data to destiny database.";
ob_flush();
flush();
$ficheiro_do_esquema = $dir_schemas."/".$_POST[sinc_schema_name].".schema";
//echo "$ficheiro_do_esquema";
$variaveis=array();
$argumentos=array(
"Connection"=>$_POST[sinc_export_conection]
);
$gestor = new metabase_manager_class;
//Criar o objecto da classe de gestão do Metabase.
ini_set("memory_limit", "128M");
//ob_implicit_flush();
$sucesso=$gestor->UpdateDatabase($ficheiro_do_esquema,
$ficheiro_do_esquema.".before", $argumentos, $variaveis);
if(!$sucesso){
echo "Erro: ".$gestor->error."\n";
} else {
echo "<br />Schema included with sucess";
}
//Se o procedimento de instalação falhou, exiba a mensagem de erro para determinar o que correu mal.
if(count($gestor->warnings)>0){
//ob_implicit_flush();
echo "AVISO:\n",implode($gestor->warnings,"!\n"),"\n";
}
ob_flush();
flush();
}
function listfiles($dirname=".") {
$ext = array("schema");
$files = array();
if($handle = opendir($dirname)) {
while(false !== ($file = readdir($handle)))
for($i=0;$i<sizeof($ext);$i++)
if(strstr($file, ".".$ext[$i]))
$files[] = $file;
closedir($handle);
}
return($files);
}
?>
--
=========================
Alexandre Miguel de Andrade Souza
www.Tribufu.com - Favoritos On-Line
Agora você pode adicionar/acessar seus
favoritos de qualquer browser em qualquer
lugar