<?php

/**
 These changes fix the following issues with dbDelta:

 -  Can't add an AUTO_INCREMENT column to a table that already exists
 -  Can't change which column is the primary key
 -  Can't change key definitions
 */

require_once( __DIR__ . '/wp-load.php' );
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
global $wpdb;

$tables = array();


$tables[ "col1(PK), col2" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1)
)";

$tables[ "col1, col2(PK)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col2)
)";

$tables[ "col1, col2, col3(AI/PK)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	col3 int(255) NOT NULL AUTO_INCREMENT,
	PRIMARY KEY  (col3)
)";

$tables[ "col1(PK), col2, col3(AI)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	col3 int(100) NOT NULL AUTO_INCREMENT,
	UNIQUE KEY thename (col3),
	PRIMARY KEY  (col1)
)";

$tables[ "col1(PK(50)), col2" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1(50))
)";

$tables[ "col1(PK(75), col2" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1(75))
)";

$tables[ "col1, col2, col3(PK)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	col3 int(255) NOT NULL,
	PRIMARY KEY  (col3)
)";

$tables[ "col1(PK), col2(FULLTEXT)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1),
	FULLTEXT KEY myidx (col2)
)";

$tables[ "col1(PK), col2(BTREE)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1),
	KEY myidx (col2)
)";

$tables[ "col1(PK), col2(UNIQUE)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1),
	UNIQUE KEY myidx (col2)
)";

$tables[ "col1(PK), col2, Index(col2,col1)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1),
	KEY myidx (col2,col1)
)";

$tables[ "col1(PK), col2, Index(col1,col2)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1),
	INDEX myidx (col1,col2)
)";

$tables[ "col1(PK), col2, PK(col1,col2)" ] = "CREATE TABLE dbdelta_tests (
	col1 varchar(100),
	col2 varchar(100),
	PRIMARY KEY  (col1,col2)
)";




/**
 * Test dbDelta.
 *
 * 1) Delete the table
 * 2) Create the table (and make sure it worked)
 * 3) dbDelta the table (and make sure nothing happens)
 * 4) dbDelta the new table def (and make sure something happens)
 * 4) dbDelta the new table def (and make sure nothing happens)
 */
function dbDelta_test( $test_name, $table1, $table2 ) {
	global $wpdb;
	$wpdb->query("DROP TABLE IF EXISTS dbdelta_tests");

	$res1 = dbDelta( $table1 );

	if ( empty( $res1 ) ) {
		return -1; // "Couldn't create table 1\n";
	}

	$res2 = dbDelta( $table1 );

	if ( !empty( $res2 ) ) {
		error_log(print_r($res2,true));
		return -2; // "Table 1 changes were incomplete\n";
	}

	$res3 = dbDelta( $table2 );

	if ( empty( $res3 ) && $table1 !== $table2 ) {
		return -3; // "Couldn't apply table 2 changes\n";
	}

	$res4 = dbDelta( $table2 );

	if ( !empty( $res4 ) ) {
		error_log(print_r($res4,true));
		return -4; // "Table 2 changes were incomplete\n";
	}

	return 0; // "OK!\n";
}


/**
 * Command line handling / run tests.
 */

$testId = 0;
$keys = array_keys( $tables );
$colwidth = 25;

/**
 * These are test numbers where the second table is a subset of the first, so there are no changes to be made.
 *
 * Unfortunately, adding additional tests breaks these numbers.
 */
$non_zero_expected_results = array(
	4 => -3,
	5 => -3,
	39 => -3,
	43 => -3,
	44 => -3,
	52 => -3,
	57 => -3,
	65 => -3,
	69 => -3,
	91 => -3,
	95 => -3,
	96 => -3,
	104 => -3,
	108 => -3,
	109 => -3,
	117 => -3,
	121 => -3,
	122 => -3,
	130 => -3,
	134 => -3,
	135 => -3,
	143 => -3,
	147 => -3,
	148 => -3,
);

$results_map = array(
	-1 => "Couldn't create table 1",
	-2 => "Table 1 changes were incomplete",
	-3 => "Couldn't apply table 2 changes",
	-4 => "Table 2 changes were incomplete",
	'OK' => 'OK',
);

print "Usage: php ./test.php [test number]\n";
print "Results meanings:\n";

foreach( $results_map as $k => $res ) {
	print "$k : $res\n";
}

print "\n";

foreach( $keys as $from) {
	foreach( $keys as $to) {
		if ( !isset( $argv[1] ) || $argv[1] == $testId ) {

			print str_pad( $testId,3,' ', STR_PAD_RIGHT);
			print ': ';
			print str_pad( "$from => $to", 80, ' ', STR_PAD_RIGHT);


			print ': ';
			$res = dbDelta_test( '', $tables[$from], $tables[$to] );
			$res = ( $res === 0 || (isset($non_zero_expected_results[ $testId ]) &&  $res === $non_zero_expected_results[ $testId ] ) ) ? 'OK' : $res;
			print $res;
			print "\n";
		}

		$testId++;
	}
}

print "\n";
