-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema.pl
77 lines (57 loc) · 2.16 KB
/
schema.pl
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
#!perl
=pod
=head1 NAME
schema.pl - Command line script to turn XML database schemas into RDBMS specific DDL statements.
=head1 SYNOPSIS
perl schema.pl DATABASE.xml RDBMS_profile
# Where DATABASE.xml is the database schema definition in XML
# and RDBMS is a CompareRDBMS configured database
# Examples
perl schema.pl sample_schema.xml MySQL_5
perl schema.pl sample_schema.xml PostgreSQL_9
perl schema.pl sample_schema.xml Oracle_11g
perl schema.pl sample_schema.xml SQLServer_2012
=head1 DESCRIPTION
schema.pl provides simple command line access to UnifyRDBMS's Schema.pm module.
Therefore, allowing the generation of RDBMS specific DDL statements from a generic
XML schema outside of application code.
=cut
use XML::TreePP;
use DBI;
use Data::Dumper;
$Data::Dumper::Indent = 1;
use strict;
use warnings;
use Schema;
# Validate the correct arguments are passed
die( 'Must pass XML schema filename' ) unless $ARGV[0];
die( "XML schema file $ARGV[0] does not exist" ) unless -e $ARGV[0];
die( 'Must pass RDBMS profile ID' ) unless $ARGV[1];
die( "RDBMS profile $ARGV[1] does not exist" ) unless -e "profiles/$ARGV[1].xml";
my ( $db_schema, $profile ) = @ARGV;
# Get associated database connection
my $xml = XML::TreePP->new();
$xml->set( use_ixhash => 1 );
$xml->set( force_array => [ qw( connection ) ] );
my $db_list = $xml->parsefile( 'db_config.xml' )->{rdbms}->{connection};
my $DBCONFIG;
foreach my $connection ( @$db_list ) {
next unless $connection->{profile} eq $profile;
$DBCONFIG = $connection;
last;
}#foreach
# Validate the profile has a database associated with it
die( "The profile $profile doesn't have an associated database connectino set in CompareRDBMS" ) unless $DBCONFIG;
my $dbh = DBI->connect( $DBCONFIG->{dsn}, $DBCONFIG->{username}, $DBCONFIG->{password},
{ RaiseError => 0, PrintError => 0, PrintWarn => 0, AutoCommit => 1 } ) || die( "Cannot connect to $DBCONFIG->{label} database $DBCONFIG->{db}" );
my $schema = Schema->new(
dbh => $dbh,
profile => "profiles/$profile.xml",
schema => $db_schema,
format => 1,
);
my $ddl = $schema->create(
drop => 1,
output => 'separate',
);
print $ddl;