PHP code example of peterhufner / php-mysql-git

1. Go to this page and download the library: Download peterhufner/php-mysql-git library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

peterhufner / php-mysql-git example snippets


 
$phpMySqlGit = new PhpMySqlGit\PhpMySqlGit([
    'connectionString' => 'mysql:host=DATABASE-HOST;port=DATABASE-PORT;dbname=DATABASE-NAME',
	'username'         => 'DATABASE-USERNAME',
	'password'         => 'USER-PASSWORD',
]);

$structureDirectory = 'PATH/TO/DIRECTORY/WHERE/STRUCTURE/SHOULD/STORED';

// save the structure of the current database to a directory
$phpMySqlGit->saveStructure($structureDirectory);

// save the data of some tables
$phpMySqlGit->saveData($structureDirectory, [
	'TABLE1',
	'ANOTHER_TABLE'
]);

// ouput all statements that are necessary to change the database according to stored structure
echo(implode("\n\n", $phpMySqlGit->configureDatabase($structureDirectory)));

// ouput the insert statements to the stored data
echo(implode("\n\n", $phpMySqlGit->configureData($structureDirectory)));




$phpMySqlGit = new PhpMySqlGit\PhpMySqlGit([
    // specify the database name in the connection string
    //'connectionString' => 'mysql:host=DATABASE-HOST;port=DATABASE-PORT;dbname=DATABASE-NAME',
    // specify the database name later
	'connectionString' => 'mysql:host=DATABASE-HOST;port=DATABASE-PORT;',
	'username'         => 'DATABASE-USERNAME',
	'password'         => 'USER-PASSWORD',
]);

// if you haven't specified the database name in the connection string, then do it here
$phpMySqlGit->setDbname("DATABASE-NAME");

$structureDirectory = 'PATH/TO/DIRECTORY/WHERE/STRUCTURE/SHOULD/STORED';

// if you want to ensure that a charset and collation is used globally ignoring the local used
$phpMySqlGit->setOverwriteCharset(true);
// utf8mb4 is the default, so there is no need to specify it again - just here to demonstrate
$phpMySqlGit->setCharset('utf8mb4');
$phpMySqlGit->setCollation('utf8mb4_unicode_ci');

// these defaults are also available for engine and row format
$phpMySqlGit->setRowFormat('DYNAMIC'); // DYNAMIC is the default
$phpMySqlGit->setEngine('InnoDB');     // InnoDB is the default
$phpMySqlGit->setOverwriteRowFormat(true); //false is default
$phpMySqlGit->setOverwriteEngine(true); // false is default

// you also can omit charset, engine and row format - so ignore the checking completely
$phpMySqlGit->setIgnoreCharset(true);
$phpMySqlGit->setIgnoreEngine(true);
$phpMySqlGit->setIgnoreRowFormat(true);

// when generating statements to change database, foreign keys are dropped before and created afterwards, to ensure the databse structure can be changed.
// defaults to false
// you should disable it only when you have a reason (for example a bug in php-mysql-git)
$phpMySqlGit->setSkipForeignKeyChecks(true or false);

// when using with data you can disable foreign key checks, but be careful it can damage the database when data is not consistent
// this leads to the generation of the statement: SET FOREIGN_KEY_CHECKS = 0; so this is done in the database server
$phpMySqlGit->setForeignKeyChecksForData(false);

// mod to create files with
$phpMySqlGit->setFileMod('0664'); //default

// mod to create directories with
$phpMySqlGit->setDirMod('0775'); //default

// and now the real stuff
// save the structure of the current database to a directory
$phpMySqlGit->saveStructure($structureDirectory);

// save the data of some tables
// if you call saveData only with the path, the data of all tables is saved
// if you specify some tables, the data is stored (and later inserted) in order of the appearance in the array
// with the correct order of data, you can insert data with foreign key checks enabled
$phpMySqlGit->saveData($structureDirectory, [
	'TABLE1',
	'ANOTHER_TABLE'
]);

// save data but exclude columns
// if you call saveData with an array as third argument, you can exclude columns
// an element in that array with an integer as key is an exclude for all tables
// an element with tablename as key and value as column or an array of columns exclude only for that table
$phpMySqlGit->saveData(
    $structureDirectory,
    [], // meaning all tables
    [
        'uid',                                        // exclude the column uid for all tables
        'TABLE_NAME' => ['lastUpdate', 'createDate'], // exclude two columns for specific table
        'ANOTHER_TABLE' => 'status',                  // exclude one column for specific table
    ]
);

// Passing true as the 4th. argument to saveData, will create data files with an numeric incremental index, in order of the occurence of the table-name in the table-Array
// If the table-array (2th. argument) is empty, the order of table of databaseserver is used.
// When using the index-feature you can ensure that the created Insert-Statements of configureData are in the same order as you have saveData called.
// But be careful with the index-feature, when you use saveData multiple times with different tables. It may creates multiple data files per table with different numeric index.
$phpMySqlGit->saveData($structureDirectory, [], [], true);


// ouput all statements that are necessary to change the database according to stored structure
echo(implode("\n\n", $phpMySqlGit->configureDatabase($structureDirectory)));

// ouput the insert statements to the stored data
echo(implode("\n\n", $phpMySqlGit->configureData($structureDirectory)));

$phpMySqlGit = new PhpMySqlGit\PhpMySqlGit(new PDO("mysql:host=DATABASE-HOST;port=DATABASE-PORT;", "DATABASE-USER"));

$phpMySqlGit = new PhpMySqlGit\PhpMySqlGit(...);
$phpMySqlGit->setCharset('my_everywhere_wanted_charset');
$phpMySqlGit->setCollation('my_everywhere_wanted_collation');
$phpMySqlGit->setOverwriteCharset(true);

$phpMySqlGit->saveStructure(...); // will store your defined charset and collation, but not that from your used database

$phpMySqlGit->configureDatabase(...); // will check and change database, tables, columns to your defined charset, regardless which charset you have stored in structure



// only allow access through command line
if (php_sapi_name() !== "cli") {
	exit();
}
tor
$cli = new \PhpMySqlGit\CommandLineInterface();

// optional path to structure, when provided it will overwrite any delcaration in the CLI-Call
$cli->setDataDir('PATH/TO/STRUCTUREDIR');

$cli->execute();
require_once 'src/PhpMySqlGit/autoload.php';
shell script
# data dir defined in cli.php
php cli.php --connectionString="mysql:host=127.0.0.1;port=3306;" --username=demouser --setOverwriteCharset=true --setDbName=sakila --saveStructure

# data dir defined in cli.php, but call extra args on saveData
php cli.php --connectionString="mysql:host=127.0.0.1;port=3306;" --username=demouser --setOverwriteCharset=true --setDbName=sakila --saveStructure --saveData='[null,["film", "film_actor"]]'

# provide path to strcuture dir inline
php cli.php --connectionString="mysql:host=127.0.0.1;port=3306;" --username=demouser --setOverwriteCharset=true --setDbName=sakila --saveStructure --saveData='["/PATH/TO/STRUCTUREDIR",["film", "film_actor"]]'

# use a prepared PhpMySqlGit-Instance and a default strcuture path, what means less args in CLI-Call
php cli.php --setDbName=sakila --saveStructure --saveData='[null,["film", "film_actor"]]'