Skip to content

Commit cfcde6c

Browse files
committed
Modified change logic
Changed the way changes are dealt with. SqlDiff::compare() now returns an array of "Change" objects. Next task to create subclasses of this to allow easier management of different types of changes (eg. Add, Edit)
1 parent 1949166 commit cfcde6c

File tree

2 files changed

+87
-17
lines changed

2 files changed

+87
-17
lines changed

‎change.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
namespace SqlDiff;
3+
4+
class Change
5+
{
6+
private $type;
7+
private $attribute;
8+
private $from;
9+
private $from_col_key;
10+
private $to;
11+
private $to_col_key;
12+
13+
public function __construct($type, $attribute, $from, $from_col_key, $to, $to_col_key)
14+
{
15+
$this->type = $type;
16+
$this->attribute = $attribute;
17+
$this->from = $from;
18+
$this->to = $to;
19+
$this->from_col_key = $from_col_key;
20+
$this->to_col_key = $to_col_key;
21+
}
22+
23+
public function type()
24+
{
25+
return $this->type;
26+
}
27+
28+
public function table_name()
29+
{
30+
return $this->from->name;
31+
}
32+
33+
public function column_name()
34+
{
35+
return $this->from->columns[$this->from_col_key]->name;
36+
}
37+
38+
public function attribute()
39+
{
40+
return $this->attribute;
41+
}
42+
43+
public function old_value()
44+
{
45+
return $this->from->columns[$this->from_col_key]->{$this->attribute};
46+
}
47+
48+
public function new_value()
49+
{
50+
return $this->new_column()->{$this->attribute};
51+
}
52+
53+
public function new_column()
54+
{
55+
return $this->to->columns[$this->to_col_key];
56+
}
57+
}

‎sqldiff.php

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ static public function autoload($name)
1616
$name = preg_replace("/([A-Z]{1})/", "_$1", $name);
1717
// Remove any underscores directly after a \
1818
$name = str_replace("\\_", "\\", $name);
19-
// Append our main namespace
19+
// Prepend our main namespace
2020
$name = "sqldiff".strtolower($name);
2121

2222
$base = dirname(dirname(__FILE__))."/";
@@ -35,12 +35,12 @@ public function __construct(Datasource $from, Datasource $to)
3535
{
3636
$this->from = $from;
3737
$this->to = $to;
38-
39-
$this->compare();
4038
}
4139

42-
private function compare()
40+
public function compare()
4341
{
42+
$changes = array();
43+
4444
$from_tables = $this->from->get_tables();
4545
$to_tables = $this->to->get_tables();
4646

@@ -50,54 +50,67 @@ private function compare()
5050
{
5151
if($from_table->name == $to_table->name)
5252
{
53-
$this->compare_tables($from_table, $to_table);
53+
$these_changes = $this->compare_tables($from_table, $to_table);
54+
$changes = array_merge($changes, $these_changes);
5455
break;
5556
}
5657
}
5758
}
59+
60+
return $changes;
5861
}
5962

6063
private function compare_tables($from, $to)
6164
{
65+
$changes = array();
6266
$no_match = array();
6367
// For each of the columns in $from check to see if they exist in $to
64-
foreach($from->columns as $from_column)
68+
foreach($from->columns as $from_key => $from_column)
6569
{
66-
foreach($to->columns as $to_column)
70+
foreach($to->columns as $to_key => $to_column)
6771
{
6872
if($from_column->name == $to_column->name)
6973
{
70-
$this->compare_columns($from_column, $to_column);
74+
$these_changes = $this->compare_columns($from, $to, $from_key, $to_key);
75+
$changes = array_merge($changes, $these_changes);
7176
unset($no_match[$from_column->name]);
7277
break;
7378
}
7479
else
7580
{
76-
$no_match[$from_column->name] = true;
81+
$no_match[$from_column->name] = array($from, $from_key);
7782
}
7883
}
7984
}
8085

8186
if(!empty($no_match))
8287
{
83-
/**
84-
* @todo Deal with new columns that are in $from but not $to
85-
*/
88+
foreach($no_match as $name => $details)
89+
{
90+
$changes[] = new \SqlDiff\Change("add", null, $details[0], $details[1], null, null);
91+
}
8692
}
93+
return $changes;
8794
}
8895

89-
private function compare_columns($from, $to)
96+
private function compare_columns($from, $to, $from_key, $to_key)
9097
{
98+
$changes = array();
99+
$from_col = $from->columns[$from_key];
100+
$to_col = $to->columns[$to_key];
101+
91102
// Check the column type
92-
if($from->type != $to->type)
103+
if($from_col->type != $to_col->type)
93104
{
94-
echo $from->name." type: ".$from->type." -> ".$to->type.PHP_EOL;
105+
$changes[] = new \SqlDiff\Change("edit", "type", $from, $from_key, $to, $to_key);
95106
}
96107

97108
// Check the column length
98-
if($from->length != $to->length)
109+
if($from_col->length != $to_col->length)
99110
{
100-
echo $from->name." length: ".$from->length." -> ".$to->length.PHP_EOL;
111+
$changes[] = new \SqlDiff\Change("edit", "length", $from, $from_key, $to, $to_key);
101112
}
113+
114+
return $changes;
102115
}
103116
}

0 commit comments

Comments
 (0)