annotate Lib/Deployment/Batch/CDBIUpdate.pm @ 21:fafe56cfcd69

temp commit
author Sergey
date Wed, 30 Sep 2009 10:25:41 +0400
parents 03e58a454b20
children 16ada169ca75
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
1 use strict;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
2 package Deployment::Batch::CDBIUpdate;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
3 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
4 use base qw(Deployment::Batch::Generic);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
5
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
6 use DBI;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
7 use Schema::DataSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
8 use Schema::DataSource::CDBIBuilder;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
9
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
10
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
11 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
12 DeclareProperty DataSchemaFile => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
13 DeclareProperty DataSourceDir => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
14 DeclareProperty DSNamespace => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
15 DeclareProperty DBConnection => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
16 DeclareProperty DBTraitsClass => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
17 DeclareProperty SchemaPrev => ACCESS_READ;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
18 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
19
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
20 sub CTOR {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
21 my ($this,%args) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
22
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
23 $this->{$DataSchemaFile} = $args{'Source'} or die new Exception('A data shema file is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
24 $this->{$DataSourceDir} = $args{'Output'} or die new Exception('A directory for a data source is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
25 $this->{$DSNamespace} = $args{'Namespace'} || 'DataSource';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
26 $this->{$DBTraitsClass} = $args{'DBTraits'} or die new Exception('A DBTraitsClass is required');
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
27
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
28 (my $modname = $args{'DBTraits'}.'.pm') =~ s/::/\//g;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
29 $this->Log("Loading DBTraits '$modname'");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
30 require $modname;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
31 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
32
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
33 sub Run {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
34 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
35
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
36 $this->{$DBConnection} = $this->Context->{Connection};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
37
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
38 my $prefix = $this->{$DSNamespace}.'::';
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
39
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
40 my $schemaDS = new Schema::DataSource(DataSourceBuilder => new Schema::DataSource::CDBIBuilder);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
41 $schemaDS->BuildSchema($this->{$DataSchemaFile});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
42
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
43 my $schemaDB = $schemaDS->DataSourceBuilder->BuildDBSchema();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
44 (my $fname = $this->{$DataSourceDir}.$this->{$DSNamespace}.'.pm') =~ s/::/\//g;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
45
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
46 # we are in the immediate mode, so the file will be backupped immediatelly;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
47 $this->Log("Backup $fname");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
48 Deployment::Batch->Backup( File => $fname );
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
49
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
50 $this->Log("Write the datasource '$this->{$DSNamespace}' to '$this->{$DataSourceDir}'");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
51 $schemaDS->DataSourceBuilder->WriteModules($fname,$prefix);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
52
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
53 if ($this->{$DBConnection}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
54 $this->Log("Update the database '$this->{$DBConnection}[0]'");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
55
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
56 $this->{$SchemaPrev} = $this->UpdateDBToSchema($schemaDB);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
57
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
58 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
59 $schemaDB->Dispose;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
60 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
61
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
62 sub Rollback {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
63 my ($this) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
64
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
65 if ($this->{$SchemaPrev}) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
66 $this->Log("Rallback the DB schema");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
67 $this->UpdateDBToSchema($this->{$SchemaPrev})->Dispose;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
68 $this->{$SchemaPrev}->Dispose;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
69 delete $this->{$SchemaPrev};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
70 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
71
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
72 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
73
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
74 sub UpdateDBToSchema {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
75 my ($this,$schemaDB) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
76 my $dbh = DBI->connect(@{$this->{$DBConnection}}) or die new Exception('Failed to connect to the database',@{$this->{$DBConnection}});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
77 my $SchemaSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
78
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
79 if (UNIVERSAL::can($this->{$DBTraitsClass},'GetMetaTable')) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
80 $SchemaSource = new Deployment::CDBI::SQLSchemeSource (MetaTable => $this->{$DBTraitsClass}->GetMetaTable($dbh));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
81 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
82 die new Exception("Can't get a meta table",$this->{$DBTraitsClass});
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
83 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
84
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
85 my $schemaDBOld = $SchemaSource->ReadSchema($schemaDB->Name);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
86
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
87 my $updater = $this->{$DBTraitsClass}->new(SrcSchema => $schemaDBOld, DstSchema => $schemaDB);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
88 $updater->UpdateSchema();
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
89
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
90 $dbh->do($_) or die new Exception('Failed to execute the sql statement', $_) foreach $updater->Handler->Sql;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
91
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
92 $SchemaSource->SaveSchema($schemaDB);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
93 return $schemaDBOld;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
94 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
95
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
96 sub DESTROY {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
97 my $this = shift;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
98
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
99 $this->{$SchemaPrev}->Dispose if $this->{$SchemaPrev};
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
100 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
101
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
102 package Deployment::CDBI::SQLSchemeSource;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
103 use Common;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
104 use Data::Dumper;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
105 use MIME::Base64;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
106 use Storable qw(nstore_fd fd_retrieve);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
107 our @ISA = qw(Object);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
108
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
109 BEGIN {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
110 DeclareProperty MetaTable => ACCESS_NONE;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
111 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
112
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
113 sub ReadSchema {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
114 my ($this,$name) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
115
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
116 my $schema = decode_base64($this->{$MetaTable}->ReadProperty("db_schema_$name"));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
117 if ($schema) {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
118 open my $hvar,"<",\$schema or die new Exception("Failed to create a handle to the variable");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
119 return fd_retrieve($hvar);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
120 } else {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
121 return new Schema::DB(Name => $name, Version => 0);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
122 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
123 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
124
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
125 sub SaveSchema {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
126 my ($this,$schema) = @_;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
127
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
128 my $name = $schema->Name;
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
129
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
130 my $data = "";
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
131 {
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
132 open my $hvar,">",\$data or die new Exception("Failed to create a handle to the variable");
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
133 nstore_fd($schema,$hvar);
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
134 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
135
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
136 $this->{$MetaTable}->SetProperty("db_schema_$name",encode_base64($data));
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
137 }
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
138
03e58a454b20 Создан репозитарий
Sergey
parents:
diff changeset
139 1;