Forums / Code Collaboration / Comparing tables structure fromdifferent databases (versions)

Comparing tables structure fromdifferent databases (versions)

Views: 3,572

Results 1 to 4 of 4
17 Oct 2023, 4:25 PM
#1
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
414
Plugin Contributions:
6

Comparing tables structure fromdifferent databases (versions)

Comparing files with a tool like Winmerge is easy but when it comes to database...

To compare structures changes in Zen Cart tables between two ZC versions or between a default version and a cart with lots of plugins can be pretty hard.

I found a way to compare two tables using MySQL derivated tables. It might be usefull to others, so here it is:
This query compares tables 'customers' from two different databases (here 'vanilla' and 'prod').

SELECT column_name,ordinal_position,data_type,column_type FROM 
( SELECT column_name,ordinal_position, data_type,column_type,COUNT(1) rowcount FROM information_schema.columns WHERE 
( (table_schema='vanilla' AND table_name='customers') OR (table_schema='prod' AND table_name='customers') ) 
GROUP BY column_name,ordinal_position, data_type,column_type HAVING COUNT(1)=1 ) A;

Try it by changing tables and databases names.
The result is a table containing differences bettween the two compared tables.

By the way I would like to know how do you do this kind of comparaison. Is there a specialized tool I don't know of?
Tested on ZC 1.5.8a database, PHP 8.1 and MySQL 8.034

ZC v2.2.2 PHP 8.4+ MySQL 8.4+

17 Oct 2023, 6:33 PM
#2
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
414
Plugin Contributions:
6

Re: Comparing tables structure fromdifferent databases (versions)

pilou2:

...
I found a way to compare two tables using MySQL derivated tables. It might be usefull to others, so here it is:
This query compares tables 'customers' from two different databases (here 'vanilla' and 'prod').

SELECT column_name,ordinal_position,data_type,column_type FROM
( SELECT column_name,ordinal_position, data_type,column_type,COUNT(1) rowcount FROM information_schema.columns WHERE
( (table_schema='vanilla' AND table_name='customers') OR (table_schema='prod' AND table_name='customers') )
GROUP BY column_name,ordinal_position, data_type,column_type HAVING COUNT(1)=1 ) A;

> ...

Well it seems I was tired when I did this post... I use derived table and not 'derivated'... And the SQL query is not optimized at all, there are some useless stuff left over...
The following query returns differences between tables 'products' in databases 'vanilla' and 'prod' based on GROUP BY parameters:

SELECT db as 'Database',column_name,ordinal_position,data_type,column_type FROM
( SELECT table_schema as db,column_name,ordinal_position,data_type,column_type FROM information_schema.columns WHERE
( (table_schema='vanilla' AND table_name='products') OR (table_schema='prod' AND table_name='products') )
GROUP BY column_name,data_type,column_type HAVING COUNT(1)=1 ) as A
ORDER BY ordinal_position ASC;

ZC v2.2.2 PHP 8.4+ MySQL 8.4+

17 Oct 2023, 7:49 PM
#3
swguy avatar

swguy

Administrator

Join Date:
Feb 2006
Location:
Tampa Bay, Florida
Posts:
10,729
Plugin Contributions:
56

Re: Comparing tables structure fromdifferent databases (versions)

phpMyAdmin has an option to export STRUCTURE only. This will allow you to do a quick comparison.

That Software Guy. My Store: Zen Cart Support
Available for hire - See my ad in Services
Plugin Moderator, Documentation Curator, Chief Cook and Bottle-Washer.
Do you benefit from Zen Cart? Then please support the project.

18 Oct 2023, 7:48 AM
#4
pilou2 avatar

pilou2

Zen Follower

Join Date:
Jun 2008
Location:
Japan
Posts:
414
Plugin Contributions:
6

Re: Comparing tables structure fromdifferent databases (versions)

swguy:

phpMyAdmin has an option to export STRUCTURE only. This will allow you to do a quick comparison.

I actually use this function for backup of structure (for developpement) and comparaison too in combinaison with WinMerge. It is probably the quickest way I know to compare but I am looking for a more 'direct' way that could be incorporated in a script too.
The SQL query although powerfull needs to be tweaked for each type of comparaison, which makes it a little bit complicated to use. I am now thinking that a PHP script with parameters for the query is the way to go. I just need time to code something...

ZC v2.2.2 PHP 8.4+ MySQL 8.4+