Skip to content

Commit a62a484

Browse files
committed
Database upgrade with new tables created on core update
1 parent 0223af3 commit a62a484

File tree

3 files changed

+68
-9
lines changed

3 files changed

+68
-9
lines changed

admin/controller/tools/update.php

+18-2
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
use function Vvveb\url;
3232

3333
class Update extends Base {
34-
private $steps = ['checkPermissions', 'download', 'unzip', /*'backup',*/ 'copySystem', /* 'copyAdmin',*/ 'copyApp', 'copyInstall', 'copyCore', 'copyConfig', 'copyPublic', 'copyPublicAdmin', 'copyPublicMedia', 'setPermissions', 'cleanUp', 'clearCache', 'complete'];
34+
private $steps = ['checkPermissions', 'download', 'unzip', 'copyInstall', /*'backup',*/ 'copySystem', /* 'copyAdmin',*/ , 'createNewTables', 'copyApp', 'copyCore', 'copyConfig', 'copyPublic', 'copyPublicAdmin', 'copyPublicMedia', 'setFilePermissions', 'cleanUp', 'clearCache', 'complete'];
3535

3636
function __construct() {
3737
$this->update = new UpdateSys();
@@ -187,6 +187,22 @@ function backup() {
187187
return $result;
188188
}
189189

190+
function createNewTables() {
191+
$result = false;
192+
193+
try {
194+
$result = $this->update->createNewTables();
195+
} catch (\Exception $e) {
196+
$this->view->errors[] = $e->getMessage();
197+
}
198+
199+
if ($result) {
200+
$this->view->info[] = __('Create new tables successful!');
201+
}
202+
203+
return $result;
204+
}
205+
190206
function unzip() {
191207
$errorInstallMsg = __('Error unzipping update!');
192208
$result = true;
@@ -273,7 +289,7 @@ function copyPublicMedia() {
273289
return $this->copy('PublicMedia');
274290
}
275291

276-
function setPermissions() {
292+
function setFilePermissions() {
277293
$errorInstallMsg = __('Error setting permissions');
278294
$result = false;
279295

system/import/sql.php

+12-4
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,20 @@ function multiQuery($sql, $filename = '') {
149149
}
150150
}
151151

152-
return $result;
152+
$return = $this->db->insert_id ?: $this->db->affected_rows ?: $result;
153+
154+
if (! $return && ! $this->db->errorCode()) {
155+
$return = true;
156+
}
157+
158+
return $return;
153159
}
154160
}
155161

156162
return true;
157163
}
158164

159-
function createTables() {
165+
function createTables($files = []) {
160166
if (DB_ENGINE == 'sqlite') {
161167
//try to speed up install
162168
$query = 'pragma journal_mode = WAL;pragma synchronous = normal;pragma temp_store = memory;pragma mmap_size = 30000000000;PRAGMA writable_schema = 1;';
@@ -185,7 +191,9 @@ function createTables() {
185191
$glob = ['', '*/*/', '*/'];
186192

187193
//$files = glob($name, GLOB_BRACE);
188-
$files = globBrace($this->sqlPath, ['', '**/'], '*.sql');
194+
if (!$files) {
195+
$files = globBrace($this->sqlPath, ['', '**/', '*/*/'], '*.sql');
196+
}
189197

190198
foreach ($files as $filename) {
191199
$sql = file_get_contents($filename);
@@ -246,7 +254,7 @@ function insertData($filter = []) {
246254
$glob = ['', '*/*/', '*/'];
247255

248256
//$files = glob($name, GLOB_BRACE);
249-
$files = globBrace($this->sqlPath, ['', '**/'], '*.sql');
257+
$files = globBrace($this->sqlPath, ['', '**/', '*/*/'], '*.sql');
250258

251259
foreach ($files as $filename) {
252260
$name = basename($filename);

system/update.php

+38-3
Original file line numberDiff line numberDiff line change
@@ -184,13 +184,13 @@ function copyInstall() {
184184
}
185185

186186
function copyCore() {
187-
$skipFolders = ['plugins', 'public', 'storage', 'system', 'app', 'admin', 'install', 'config', 'env.php'];
187+
$skipFolders = ['plugins', 'public', 'storage', 'system', 'app', 'admin', 'install', 'config', 'vendor', 'env.php'];
188188

189189
return $this->copyFolder($this->workDir, DIR_ROOT, $skipFolders);
190190
}
191191

192192
function copyConfig() {
193-
$skip = ['plugins.php', 'mail.php', 'sites.php', 'app.php', 'admin.php', 'routes.php', 'env.php'];
193+
$skip = ['plugins.php', 'mail.php', 'sites.php', 'app.php', 'admin.php', 'app-routes.php'];
194194

195195
return $this->copyFolder($this->workDir . DS . 'config', DIR_ROOT . DS . 'config', $skip);
196196
}
@@ -214,6 +214,41 @@ function copyPublicMedia() {
214214
return $this->copyFolder($this->workDir . DS . 'public' . DS . 'media', DIR_PUBLIC . DS . 'media', $skipFolders);
215215
}
216216

217+
function createNewTables() {
218+
$db = \Vvveb\System\Db::getInstance();
219+
$tableNames = $db->getTableNames();
220+
221+
$driver = DB_ENGINE;
222+
$sqlPath = DIR_ROOT . "install/sql/$driver/";
223+
$files = \Vvveb\globBrace($sqlPath, ['', '*/*/'], '*.sql');
224+
225+
$diff = [];
226+
//if the number of tables is less than in the install dir
227+
if (count($tableNames) < count($files)) {
228+
$tableSql = [];
229+
//get table names from sql files
230+
foreach ($files as $filename) {
231+
$tableSql[] = basename($filename, '.sql');
232+
}
233+
//get the names of missing tables
234+
$diff = array_diff($tableSql, $tableNames);
235+
}
236+
237+
$sqlFiles = [];
238+
//get files for missing tables
239+
foreach ($diff as $key => $tableName) {
240+
$sqlFiles[] = $files[$key];
241+
}
242+
243+
//create missing tables
244+
if ($sqlFiles) {
245+
$sqlImport = new \Vvveb\System\Import\Sql();
246+
$sqlImport->createTables($sqlFiles);
247+
}
248+
249+
return true;
250+
}
251+
217252
function clearCache() {
218253
return CacheManager::delete();
219254
}
@@ -235,7 +270,7 @@ function install($zipFile) {
235270
//$dest = substr(DIR_ROOT,0, -1); //remove trailing slash
236271
rrmdir($this->workDir);
237272
//plugins and themes are updated individually
238-
$skipFolders = ['plugins', 'public' . DS . 'themes', 'storage'];
273+
$skipFolders = ['plugins', 'public' . DS . 'themes', 'storage', 'vendor'];
239274

240275
rcopy($this->workDir, DIR_ROOT, $skipFolders);
241276
rrmdir($this->workDir);

0 commit comments

Comments
 (0)