2013-04-03 18:49:05 -07:00
< ? php
/** Exports one database ( e . g . development ) so that it can be synced with other database ( e . g . production )
2015-09-08 09:23:25 -07:00
* @ link https :// www . adminer . org / plugins / #use
2017-02-27 13:43:33 +01:00
* @ author Jakub Vrana , https :// www . vrana . cz /
2018-01-14 11:03:54 +01:00
* @ license https :// www . apache . org / licenses / LICENSE - 2.0 Apache License , Version 2.0
* @ license https :// www . gnu . org / licenses / gpl - 2.0 . html GNU General Public License , version 2 ( one or other )
2013-04-03 18:49:05 -07:00
*/
class AdminerDumpAlter {
2025-02-21 13:53:18 +01:00
2013-04-03 18:49:05 -07:00
function dumpFormat () {
2025-03-06 17:34:21 +01:00
if ( Adminer\DRIVER == 'server' ) {
2013-04-03 18:49:05 -07:00
return array ( 'sql_alter' => 'Alter' );
}
2014-09-18 12:06:56 +02:00
}
2025-02-21 13:53:18 +01:00
2025-03-11 18:20:11 +01:00
private function database () {
2013-04-03 18:49:05 -07:00
// drop old tables
$query = " SELECT TABLE_NAME, ENGINE, TABLE_COLLATION, TABLE_COMMENT FROM information_schema.TABLES WHERE TABLE_SCHEMA = DATABASE() " ;
echo " DELIMITER ;;
CREATE PROCEDURE adminer_alter ( INOUT alter_command text ) BEGIN
DECLARE _table_name , _engine , _table_collation varchar ( 64 );
DECLARE _table_comment varchar ( 64 );
DECLARE done bool DEFAULT 0 ;
DECLARE tables CURSOR FOR $query ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
OPEN tables ;
REPEAT
FETCH tables INTO _table_name , _engine , _table_collation , _table_comment ;
IF NOT done THEN
CASE _table_name " ;
2025-03-05 11:40:56 +01:00
foreach ( Adminer\get_rows ( $query ) as $row ) {
$comment = Adminer\q ( $row [ " ENGINE " ] == " InnoDB " ? preg_replace ( '~(?:(.+); )?InnoDB free: .*~' , '\1' , $row [ " TABLE_COMMENT " ]) : $row [ " TABLE_COMMENT " ]);
2014-09-18 12:06:56 +02:00
echo "
2025-03-05 11:40:56 +01:00
WHEN " . Adminer \ q( $row["TABLE_NAME"] ) . " THEN
2013-04-03 18:49:05 -07:00
" . (isset( $row["ENGINE"] ) ? " IF _engine != '$row[ENGINE]' OR _table_collation != '$row[TABLE_COLLATION]' OR _table_comment != $comment THEN
2025-03-05 14:51:55 +01:00
ALTER TABLE " . Adminer \ idf_escape( $row["TABLE_NAME"] ) . " ENGINE = $row [ ENGINE ] COLLATE = $row [ TABLE_COLLATION ] COMMENT = $comment ;
2013-04-03 18:49:05 -07:00
END IF " : " BEGIN END " ) . " ; " ;
2014-09-18 12:06:56 +02:00
}
echo "
2013-04-03 18:49:05 -07:00
ELSE
SET alter_command = CONCAT ( alter_command , 'DROP TABLE `' , REPLACE ( _table_name , '`' , '``' ), '`;\\n' );
END CASE ;
END IF ;
UNTIL done END REPEAT ;
CLOSE tables ;
END ;;
DELIMITER ;
CALL adminer_alter ( @ adminer_alter );
DROP PROCEDURE adminer_alter ;
SELECT @ adminer_alter ;
" ;
}
2025-02-21 13:53:18 +01:00
2013-04-03 18:49:05 -07:00
function dumpDatabase ( $db ) {
static $first = true ;
if ( $_POST [ " format " ] == " sql_alter " ) {
if ( $first ) {
$first = false ;
echo " SET @adminer_alter = ''; \n \n " ;
} else {
2025-03-11 18:20:11 +01:00
$this -> database ();
2013-04-03 18:49:05 -07:00
}
return true ;
}
}
2025-02-21 13:53:18 +01:00
2019-11-11 12:15:32 +01:00
function dumpTable ( $table , $style , $is_view = 0 ) {
2013-04-03 18:49:05 -07:00
if ( $_POST [ " format " ] == " sql_alter " ) {
2025-03-05 15:42:48 +01:00
$create = Adminer\create_sql ( $table , $_POST [ " auto_increment " ], $style );
2013-04-03 18:49:05 -07:00
if ( $is_view ) {
echo substr_replace ( $create , " OR REPLACE " , 6 , 0 ) . " ; \n \n " ;
} else {
echo substr_replace ( $create , " IF NOT EXISTS " , 12 , 0 ) . " ; \n \n " ;
// create procedure which iterates over original columns and adds new and removes old
2025-03-05 11:40:56 +01:00
$query = " SELECT COLUMN_NAME, COLUMN_DEFAULT, IS_NULLABLE, COLLATION_NAME, COLUMN_TYPE, EXTRA, COLUMN_COMMENT FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = " . Adminer\q ( $table ) . " ORDER BY ORDINAL_POSITION " ;
2013-04-03 18:49:05 -07:00
echo " DELIMITER ;;
CREATE PROCEDURE adminer_alter ( INOUT alter_command text ) BEGIN
DECLARE _column_name , _collation_name , after varchar ( 64 ) DEFAULT '' ;
DECLARE _column_type , _column_default text ;
DECLARE _is_nullable char ( 3 );
DECLARE _extra varchar ( 30 );
DECLARE _column_comment varchar ( 255 );
DECLARE done , set_after bool DEFAULT 0 ;
DECLARE add_columns text DEFAULT ' " ;
$fields = array ();
$after = " " ;
2025-03-05 11:40:56 +01:00
foreach ( Adminer\get_rows ( $query ) as $row ) {
2013-04-03 18:49:05 -07:00
$default = $row [ " COLUMN_DEFAULT " ];
2025-03-05 11:40:56 +01:00
$row [ " default " ] = ( $default !== null ? Adminer\q ( $default ) : " NULL " );
$row [ " after " ] = Adminer\q ( $after ); //! rgt AFTER lft, lft AFTER id doesn't work
2025-03-05 14:51:17 +01:00
$row [ " alter " ] = Adminer\escape_string (
2025-03-05 14:51:55 +01:00
Adminer\idf_escape ( $row [ " COLUMN_NAME " ])
2013-04-03 18:49:05 -07:00
. " $row[COLUMN_TYPE] "
. ( $row [ " COLLATION_NAME " ] ? " COLLATE $row[COLLATION_NAME] " : " " )
. ( $default !== null ? " DEFAULT " . ( $default == " CURRENT_TIMESTAMP " ? $default : $row [ " default " ]) : " " )
. ( $row [ " IS_NULLABLE " ] == " YES " ? " " : " NOT NULL " )
. ( $row [ " EXTRA " ] ? " $row[EXTRA] " : " " )
2025-03-05 11:40:56 +01:00
. ( $row [ " COLUMN_COMMENT " ] ? " COMMENT " . Adminer\q ( $row [ " COLUMN_COMMENT " ]) : " " )
2025-03-05 14:51:55 +01:00
. ( $after ? " AFTER " . Adminer\idf_escape ( $after ) : " FIRST " )
2013-04-03 18:49:05 -07:00
);
echo " , ADD $row[alter] " ;
$fields [] = $row ;
$after = $row [ " COLUMN_NAME " ];
}
echo " ';
DECLARE columns CURSOR FOR $query ;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1 ;
SET @ alter_table = '' ;
OPEN columns ;
REPEAT
FETCH columns INTO _column_name , _column_default , _is_nullable , _collation_name , _column_type , _extra , _column_comment ;
IF NOT done THEN
SET set_after = 1 ;
CASE _column_name " ;
2014-09-18 12:06:56 +02:00
foreach ( $fields as $row ) {
echo "
2025-03-05 11:40:56 +01:00
WHEN " . Adminer \ q( $row["COLUMN_NAME"] ) . " THEN
2013-04-03 18:49:05 -07:00
SET add_columns = REPLACE ( add_columns , ', ADD $row[alter]' , IF (
2025-02-20 17:58:00 +01:00
_column_default <=> $row [ default ]
AND _is_nullable = '$row[IS_NULLABLE]'
AND _collation_name <=> " . (isset( $row["COLLATION_NAME"] ) ? " '$row[COLLATION_NAME]' " : " NULL " ) . "
2025-03-05 11:40:56 +01:00
AND _column_type = " . Adminer \ q( $row["COLUMN_TYPE"] ) . "
2025-02-20 17:58:00 +01:00
AND _extra = '$row[EXTRA]'
2025-03-05 11:40:56 +01:00
AND _column_comment = " . Adminer \ q( $row["COLUMN_COMMENT"] ) . "
2025-02-20 17:58:00 +01:00
AND after = $row [ after ]
2014-09-18 12:06:56 +02:00
, '' , ', MODIFY $row[alter]' )); " ; //! don't replace in comment
}
echo "
2013-04-03 18:49:05 -07:00
ELSE
2014-09-18 12:06:56 +02:00
SET @ alter_table = CONCAT ( @ alter_table , ', DROP ' , '`' , REPLACE ( _column_name , '`' , '``' ), '`' );
2013-04-03 18:49:05 -07:00
SET set_after = 0 ;
END CASE ;
IF set_after THEN
SET after = _column_name ;
END IF ;
END IF ;
UNTIL done END REPEAT ;
CLOSE columns ;
IF @ alter_table != '' OR add_columns != '' THEN
2025-03-05 15:42:48 +01:00
SET alter_command = CONCAT ( alter_command , 'ALTER TABLE " . Adminer\table($table) . "' , SUBSTR ( CONCAT ( add_columns , @ alter_table ), 2 ), ';\\n' );
2013-04-03 18:49:05 -07:00
END IF ;
END ;;
DELIMITER ;
CALL adminer_alter ( @ adminer_alter );
DROP PROCEDURE adminer_alter ;
" ;
//! indexes
}
return true ;
}
}
2025-02-21 13:53:18 +01:00
2014-09-18 12:06:56 +02:00
function dumpData () {
if ( $_POST [ " format " ] == " sql_alter " ) {
return true ;
}
}
2025-03-11 18:20:11 +01:00
function dumpFooter () {
if ( $_POST [ " format " ] == " sql_alter " ) {
$this -> database ();
}
}
2013-04-03 18:49:05 -07:00
}