I have two data structure dumps that I generated from MySQL workbench. Now I would like to add them or include them in a PHP script and use them as variables:
var $sourceStruct = ''; //structure dump of the reference database
var $destStruct = ''; //structure dump of database to update
I am following this script which compares two databases and synchronizes them but author did not give any explanation how to include the dumps in this script?
Usage:
var $sourceStruct = 'CREATE TABLE ...'; //add structure dump of the reference database here
var $destStruct = 'CREATE TABLE ...'; //add structure dump of database to update
$updater = new dbStructUpdater();
$updates = $updater->getUpdates($sourceStruct, $destStruct);
Result:
$updates == array (
[0]=>"ALTER TABLE `b` MODIFY `name` varchar(255) NOT NULL",
...
)
Well, if you just want to get the dumps into the variables then read the files with PHP's built-infile_get_contents()
function like this:
var $sourceStruct = file_get_contents('source_dump.sql') or die('Could not read source SQL dump!');
var $destStruct = file_get_contents('destination_dump.sql') or die('Could not read destination SQL dump!');
$updater = new dbStructUpdater();
$updates = $updater->getUpdates($sourceStruct, $destStruct);
foreach ($updates as $update) {
print $update . ';' . PHP_EOL;
}
This should generate all the SQL queries to update your destination database.
On my side, the problem was that I didn't get the library on PHPClasses because I didn't want to create an account. So I looked on Google to find this same library elsewhere and I got it available for installation with Composer at this place: https://packagist.org/packages/eliasfarah/db-struct-sync
I created a new folder and inside it I run this command:
composer require eliasfarah/db-struct-sync
This downloaded the project and created the following content:
You could download the source code from GitHub: https://github.com/eliasfarah/dbStructSync
As it didn't work with my PHP 7 installation, I did some manual changes (not a good idea if we want to update the lib but it just was a test for your question). You could create a patch and add it to your own composer.json so that it's applied after installation.
The short opening tag at line 1 should be replaced by the long one:<?
becomes<?php
At line 63, the class constructor should be renamed:
/**
* Constructor
* @access public
*/
function dbStructUpdater()
{
$this->init();
}
becomes:
/**
* Constructor
* @access public
*/
function __construct()
{
$this->init();
}
I didn't have any MySQL dump so I used the ones provided by the tests in the library itself.
I wanted to use the Composer's autoload file like we usually do but it didn't seem to work, probably because the lib is old and doesn't respect common conventions. So I directly included the PHP file like this:
<?php
// Normal way to go not working :-(
//include 'vendor/autoload.php';
// Quick and dirty solution to have the class.
include 'vendor/eliasfarah/db-struct-sync/dbStruct.php';
$src = file_get_contents('vendor/eliasfarah/db-struct-sync/tests/left_1.sql');
$dest = file_get_contents('vendor/eliasfarah/db-struct-sync/tests/right_1.sql');
$updater = new dbStructUpdater();
$queries = $updater->getUpdates($src, $dest);
var_export($queries);
array (
0 => 'ALTER TABLE `personalizer_config` MODIFY `cookyExpires` varchar(20) NOT NULL default \'30\'',
1 => 'ALTER TABLE `personalizer_config` ADD `cron_id` varchar(255) NOT NULL',
2 => 'ALTER TABLE `personalizer_config` MODIFY `enable_roles` tinyint(4) NOT NULL',
3 => 'ALTER TABLE `personalizer_config` MODIFY `group_table` varchar(255) NOT NULL default \'group\'',
4 => 'ALTER TABLE `personalizer_config` MODIFY `ldapswitch` tinyint(4) NOT NULL',
5 => 'ALTER TABLE `personalizer_config` MODIFY `masterPassword` varchar(32) NOT NULL',
6 => 'ALTER TABLE `personalizer_config` MODIFY `relations_table` varchar(255) NOT NULL default \'relations\'',
7 => 'ALTER TABLE `personalizer_config` MODIFY `std_group_tpl_none` varchar(255) NOT NULL',
8 => 'ALTER TABLE `personalizer_config` MODIFY `std_group_tpl_own` varchar(100) NOT NULL',
9 => 'ALTER TABLE `personalizer_config` MODIFY `std_group_tpl_sub` varchar(255) NOT NULL',
10 => 'ALTER TABLE `personalizer_config` MODIFY `stdCutOuts` varchar(255) NOT NULL',
11 => 'ALTER TABLE `personalizer_config` MODIFY `stdUserFields` varchar(255) NOT NULL',
12 => 'ALTER TABLE `personalizer_config` MODIFY `user_table` varchar(255) NOT NULL default \'user\'',
)
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
DBMS is a database management system. It is designed to change, search, add and delete information in the database. There are many DBMSs designed for similar purposes with different features. One of the most popular is MySQL.
It is a software tool designed to work with relational SQL databases. It is easy to learn even for site owners who are not professional programmers or administrators. MySQL DBMS also allows you to export and import data, which is convenient when moving large amounts of information.
https://www.mysql.com/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.