Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit dacc945

Browse files
matksMathieu Ferment
authored and
Mathieu Ferment
committed
Add compare_sql_tables.php script
1 parent 5c16301 commit dacc945

File tree

3 files changed

+149
-0
lines changed

3 files changed

+149
-0
lines changed

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
composer.phar
33
vendor/*
44

5+
config.php
56

67
# Vagrant
78
.vagrant/

‎compare_sql_tables.php‎

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
// credentials file
4+
require 'config.php';
5+
6+
/**
7+
* @param array $config
8+
*
9+
* @return PDO
10+
*/
11+
function getPDO(array $config)
12+
{
13+
$pdo = new PDO(
14+
sprintf(
15+
'%s:host=%s;port=%d;dbname=%s',
16+
$config['driver'],
17+
$config['hostname'],
18+
$config['port'],
19+
$config['database']
20+
),
21+
$config['user'],
22+
$config['password']
23+
);
24+
25+
return $pdo;
26+
}
27+
28+
/**
29+
* Validate console input
30+
*
31+
* Die if input is not valid
32+
*
33+
* @param array $argv user inputs
34+
*/
35+
function validateInput($argv)
36+
{
37+
$isValid = false;
38+
39+
if (count($argv) === 1) {
40+
$isValid = true;
41+
}
42+
43+
if (!$isValid) {
44+
echo 'php compare_sql_tables.php' . PHP_EOL;
45+
die(1);
46+
}
47+
}
48+
49+
validateInput($argv);
50+
51+
require 'Util/TextColorWriter.php';
52+
53+
$pdo1 = getPDO($config);
54+
$pdo2 = getPDO($config);
55+
56+
// implement these functions in config.php file
57+
$orderedColumns = getOrderedColumns();
58+
$orderBy = getOrderBy();
59+
60+
$query1 = "SELECT $orderedColumns from $table1 ORDER BY $orderBy";
61+
$query2 = "SELECT $orderedColumns from $table2 ORDER BY $orderBy";
62+
63+
$pdo1->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
64+
$stmt1 = $pdo1->query($query1);
65+
66+
if (!$stmt1) {
67+
throw new \RuntimeException(join(' : ', $pdo1->errorInfo()));
68+
}
69+
70+
$pdo2->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false);
71+
$stmt2 = $pdo2->query($query2);
72+
73+
if (!$stmt1) {
74+
throw new \RuntimeException(join(' : ', $pdo2->errorInfo()));
75+
}
76+
77+
$errors = array();
78+
$i = 0;
79+
while ($line = $stmt1->fetch(\PDO::FETCH_NUM)) {
80+
if (!$stmt1) {
81+
throw new RuntimeException(join(' : ', $pdo1->errorInfo()));
82+
}
83+
84+
$otherLine = $stmt2->fetch(\PDO::FETCH_NUM);
85+
if (!$stmt2) {
86+
throw new RuntimeException(join(' : ', $pdo2->errorInfo()));
87+
}
88+
89+
$string1 = implode(';', $line);
90+
$string2 = implode(';', $otherLine);
91+
92+
if ($string1 != $string2) {
93+
$errors[] = $string1 . ' / ' . $string2;
94+
}
95+
96+
$i++;
97+
echo "033円[G$i lines analysed";
98+
}
99+
echo PHP_EOL;
100+
101+
$stmt1->closeCursor();
102+
$stmt2->closeCursor();
103+
104+
105+
// echo result
106+
echo TextColorWriter::textColor('SQL TABLE COMPARISON:', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
107+
108+
if (empty($errors)) {
109+
echo TextColorWriter::textColor('Table content is identical', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
110+
} else {
111+
echo TextColorWriter::textColor(count($errors) . 'different lines :', TextColorWriter::BASH_PROMPT_GREEN) . PHP_EOL;
112+
foreach($errors as $e) {
113+
echo "$e" . PHP_EOL;
114+
}
115+
}

‎config.php.dist‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
// pgsql
4+
$config = array(
5+
'driver' => 'pgsql',
6+
'hostname' => 'localhost',
7+
'port' => 5432,
8+
'database' => 'database',
9+
'user' => 'user',
10+
'password' => 'pwd'
11+
);
12+
13+
function getOrderedColumns()
14+
{
15+
$columns = array(
16+
'id',
17+
'date',
18+
'attr1',
19+
'attr2'
20+
);
21+
22+
$o = implode(',', $columns);
23+
24+
return $o;
25+
}
26+
27+
$table1 = 'table1';
28+
$table2 = 'table2';
29+
30+
function getOrderBy()
31+
{
32+
return 'id';
33+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /