Move busui to seperate repository
[bus.git] / update.php
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
 
//
// Open Web Analytics - An Open Source Web Analytics Framework
//
// Copyright 2006 - 2010 Peter Adams. All rights reserved.
//
// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Id$
//
 
/**
 * Abstract Update Class
 * 
 * Performs an Update for a specific module
 *
 * @author      Peter Adams <peter@openwebanalytics.com>
 * @copyright   Copyright &copy; 2008 Peter Adams <peter@openwebanalytics.com>
 * @license     http://www.gnu.org/copyleft/gpl.html GPL v2.0
 * @category    owa
 * @package     owa
 * @version             $Revision$            
 * @since               owa 1.0.0
 */
 
class owa_update extends owa_base {
        
        /**
         * Module Name
         *
         * Name of the module that his update is invoked under. This is set by the
         * factory.
         *
         * @var string
         */
        var $module_name;
        
        /**
         * Schema Version Number
         *
         * Version number of the schema that will be in place after update is applied.
         *
         * This is set by the module's update method from the concrete class filename 
         * when it creates the concrete version of this update class.  This ensures 
         * that the schema version number is only set in one place (the file name) and 
         * that only one concrete update class can ever be applied for a particular 
         * schema version.
         *
         * @var integer
         */
        var $schema_version;
        
        var $is_cli_mode_required;
        
        function __construct() {
                                
                return parent::__construct();
        }
        
        function isCliModeRequired() {
                
                return $this->is_cli_mode_required;
        }
        
        /**
         * Applies an update
         *
         * @return boolean
         */
        function apply() {
                
                // check for schema version. abort if not present or else updates will get out of sync.
                if (empty($this->schema_version)) {
                        $this->e->notice(sprintf("Aborting %s Update (%s): Schema Version Number is not set.", get_class(), $this->module_name));
                        return false;
                }
                
                $current_version = $this->c->get($this->module_name, 'schema_version');
                
                // check to see that you are applying an update that was successfully applied
                if ($current_version === $this->schema_version) { 
                        $this->e->notice(sprintf("Aborting %s Update (%s): Update has already ben applied.", get_class(), $this->module_name));
                        return false;
                }
                
                // execute pre update proceadure
                $ret = $this->pre();
                
                if ($ret == true):
                
                        $this->e->notice("Pre Update Proceadure Suceeded");
                        
                        // execute actual update proceadure
                        $ret = $this->up();
        
                        if ($ret == true):
                        
                                // execute post update proceadure
                                $ret = $this->post();
                
                                if ($ret == true):
                                        $this->e->notice("Post Update Proceadure Suceeded");
                                        $this->c->persistSetting($this->module_name, 'schema_version', $this->schema_version);
                                        $this->c->save();
                                        return true;
                                else:
                                        $this->e->notice("Post Update Proceadure Failed");
                                        return false;
                                endif;
                        else:
                                $this->e->notice("Update Proceadure Failed");
                                return false;
                        endif;
                else:
                        $this->e->notice("Pre Update Proceadure Failed");
                        return false;
                endif;
                
        }
        
        
        /**
         * Rollsback an update
         *
         * @return boolean
         */
        function rollback() {
                
                $current_version = $this->c->get($this->module_name, 'schema_version');
                
                // check to see that you are rolling back either an update that was successfully applied or one that might have failed.
                // we dont want people applying rollbacks out of sequence.
                if ($current_version === $this->schema_version || $current_version === $this->schema_version - 1) {
                        $ret = $this->down();
                        if ($ret) {
                                // only touch the current schema number if needed
                                
                                $prior_version = $current_version - 1;
 
                                if ($current_version === $this->schema_version) {
                                        $this->c->persistSetting($this->module_name, 'schema_version', $prior_version);
                                        $this->c->save();
                                        $this->e->notice("Rollback succeeded to version: $prior_version.");
                                } else {
                                        $this->e->notice("Rollback succeeded to version: $current_version.");
                                }
                                
                        } else {
                                $this->e->notice("Rollback failed.");
                        }                       
                } else {
                        $this->e->notice(sprintf('Rollback of update %s cannot be applied because it does not appear that it update %s has been applied to your instance. Your current schema version is only %s', $this->schema_version, $this->schema_version, $current_version));
                }
                
                return true;
        }
        
        /**
         * Abstract Pre-update hook
         *
         * @return boolean
         */
        function pre() {
                
                return true;
        }
        
        /**
         * Abstract Post-update hook
         *
         * @return boolean
         */
        function post() {
                
                return true;
        }
        
        /**
         * Abstract Method for update 
         *
         * @return boolean
         */
        function up() {
        
                return false;
        }
        
        /**
         * Abstract Method for reversing an update
         *
         * @return boolean
         */
        function down() {
                
                return false;
        }
                
}
 
?>