forked from OnlineBuddies/Modyllic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqldiff
executable file
·93 lines (82 loc) · 2.97 KB
/
sqldiff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#!/usr/bin/env php
<?php
/**
* Copyright © 2011 Online Buddies, Inc. - All Rights Reserved
*
* @package Modyllic
* @author bturner@online-buddies.com
*/
require_once "Modyllic/Commandline.php";
require_once "Modyllic/Diff.php";
require_once "Modyllic/Generator.php";
require_once "Modyllic/Generator/SQL.php";
$args = Modyllic_Commandline::getArgs(array(
'description' => 'Generate a PHP helper class for the stored procs in the schema',
'options' => array(
'fromschema' => array(
'short_name' => '-f',
'long_name' => '--from',
'action' => 'StoreArray',
),
'toschema' => array(
'short_name' => '-t',
'long_name' => '--to',
'action' => 'StoreArray',
),
'dialect' => array(
'short_name' => '-d',
'long_name' => '--dialect',
'description' => 'dialect to output in',
'action' => 'Dialect',
'default' => 'Modyllic_Generator_NativeSQL',
),
'only' => array(
'long_name' => '--only',
'description' => 'comma separated list of kinds of schema objects to process: database, sqlmeta, tables, views, routines, events, triggers',
'action' => 'StoreString',
'default' => implode(",",Modyllic_Generator_SQL::schema_types())
) ),
'arguments' => array(
'fromschema' => array('optional'=>true),
'toschema' => array('optional'=>true)
)));
try {
$only = preg_split('/\s*,\s*/',$args->options['only']);
Modyllic_Generator_SQL::validate_schema_types($only);
}
catch (Exception $e) {
Modyllic_Commandline::displayError($e->getMessage());
}
if ( (isset($args->options['fromschema']) or isset($args->options['toschema'])) and isset($args->args['fromschema']) ) {
Modyllic_Commandline::displayError( "You can't specify a schema both positionally and via options" );
}
$fromschema = array();
if ( isset($args->args['fromschema']) ) {
$fromschema[] = $args->args['fromschema'];
}
if ( isset($args->options['fromschema']) ) {
$fromschema += $args->options['fromschema'];
}
if ( count($fromschema) == 0 ) {
Modyllic_Commandline::displayError( 'You must specify a from schema to compare');
}
$toschema = array();
if ( isset($args->args['toschema']) ) {
$toschema[] = $args->args['toschema'];
}
if ( isset($args->options['toschema']) ) {
$toschema += $args->options['toschema'];
}
if ( count($fromschema) == 0 ) {
Modyllic_Commandline::displayError( 'You must specify a to schema to compare');
}
$from = Modyllic_Commandline::schema($fromschema);
$to = Modyllic_Commandline::schema($toschema);
$diff = new Modyllic_Diff( $from, $to );
if ( ! $diff->changeset->has_changes() ) {
print "-- No changes detected.\n";
exit(0);
}
$class = Modyllic_Generator::dialectToClass( $args->options['dialect'] );
$gen = new $class();
print $gen->alter_sql($diff, ';;', true, $only);