diff options
author | Florian Pritz <bluewind@xinu.at> | 2014-07-23 21:56:01 +0200 |
---|---|---|
committer | Florian Pritz <bluewind@xinu.at> | 2014-07-23 21:58:19 +0200 |
commit | cc72236f71c60396b69631656f8e0ad00a6301c8 (patch) | |
tree | a981833a17cba56e85682048a84e544e877af773 | |
parent | ce58ad70b8f3800adeaaadd46d8ed499bc21eda6 (diff) |
Move migration code to CLI callable function
This is necessary to prevent migrations from running multiple times in
parallel. A git hook can be used to run this after checkout so impact
should be fairly low.
Signed-off-by: Florian Pritz <bluewind@xinu.at>
-rw-r--r-- | INSTALL | 2 | ||||
-rw-r--r-- | application/config/routes.php | 1 | ||||
-rw-r--r-- | application/controllers/file.php | 12 | ||||
-rw-r--r-- | application/controllers/tools.php | 45 | ||||
-rw-r--r-- | application/controllers/user.php | 5 | ||||
-rw-r--r-- | application/core/MY_Controller.php | 19 |
6 files changed, 70 insertions, 14 deletions
@@ -8,7 +8,7 @@ * Change them to fit your needs - don't change config.php, use config-local.php to override it when needed -* The necessary database tables are created/updated automatically when accessing your pastebin +* run `php index.php tools update_database` to initialize your database * Copy htaccess.txt to .htaccess if you want shorter URLs (also set "$config['index_page'] = '';" in config-local.php) diff --git a/application/config/routes.php b/application/config/routes.php index 3ae891bfd..e0d963405 100644 --- a/application/config/routes.php +++ b/application/config/routes.php @@ -41,6 +41,7 @@ $route['default_controller'] = "file"; $route['user/(:any)'] = "user/$1"; $route['file/(:any)'] = "file/$1"; +$route['tools/(:any)'] = "tools/$1"; $route['(:any)'] = "file/index/$1"; $route['404_override'] = ''; diff --git a/application/controllers/file.php b/application/controllers/file.php index bb06e17d4..51d6cf156 100644 --- a/application/controllers/file.php +++ b/application/controllers/file.php @@ -31,16 +31,8 @@ class File extends MY_Controller { function index() { if ($this->input->is_cli_request()) { - echo "php index.php file <function> [arguments]\n"; - echo "\n"; - echo "Functions:\n"; - echo " cron Cronjob\n"; - echo " nuke_id <ID> Nukes all IDs sharing the same hash\n"; - echo "\n"; - echo "Functions that shouldn't have to be run:\n"; - echo " clean_stale_files Remove files without database entries\n"; - echo " update_file_metadata Update filesize and mimetype in database\n"; - exit; + $this->load->library("../controllers/tools"); + return $this->tools->index(); } // Try to guess what the user would like to do. $id = $this->uri->segment(1); diff --git a/application/controllers/tools.php b/application/controllers/tools.php new file mode 100644 index 000000000..b80dc5024 --- /dev/null +++ b/application/controllers/tools.php @@ -0,0 +1,45 @@ +<?php +/* + * Copyright 2014 Florian "Bluewind" Pritz <bluewind@server-speed.net> + * + * Licensed under AGPLv3 + * (see COPYING for full license text) + * + */ + +class Tools extends MY_Controller { + + function __construct() + { + parent::__construct(); + + $this->load->model('mfile'); + if (!$this->input->is_cli_request()) { + show_error("This can only be called via CLI"); + } + } + + function index() + { + echo "php index.php <controller> <function> [arguments]\n"; + echo "\n"; + echo "Functions:\n"; + echo " file cron Cronjob\n"; + echo " file nuke_id <ID> Nukes all IDs sharing the same hash\n"; + echo " user cron Cronjob\n"; + echo " tools update_database Update/Initialise the database\n"; + echo "\n"; + echo "Functions that shouldn't have to be run:\n"; + echo " file clean_stale_files Remove files without database entries\n"; + echo " file update_file_metadata Update filesize and mimetype in database\n"; + exit; + } + + function update_database() + { + $this->load->library('migration'); + if ( ! $this->migration->current()) { + show_error($this->migration->error_string()); + } + } +} diff --git a/application/controllers/user.php b/application/controllers/user.php index b4e2613ac..079f1665c 100644 --- a/application/controllers/user.php +++ b/application/controllers/user.php @@ -23,6 +23,11 @@ class User extends MY_Controller { function index() { + if ($this->input->is_cli_request()) { + $this->load->library("../controllers/tools"); + return $this->tools->index(); + } + $this->data["username"] = $this->muser->get_username(); $this->load->view('header', $this->data); diff --git a/application/core/MY_Controller.php b/application/core/MY_Controller.php index 043b52ef7..a98245b1b 100644 --- a/application/core/MY_Controller.php +++ b/application/core/MY_Controller.php @@ -21,9 +21,22 @@ class MY_Controller extends CI_Controller { $this->var = new StdClass(); $csrf_protection = true; - $this->load->library('migration'); - if ( ! $this->migration->current()) { - show_error($this->migration->error_string()); + // check if DB is up to date + if (!$this->input->is_cli_request()) { + if (!$this->db->table_exists('migrations')){ + show_error("Database not initialized. Can't find migrations table. Please run the migration script."); + } else { + $this->config->load("migration", true); + $target_version = $this->config->item("migration_version", "migration"); + + // TODO: wait 20 seconds for an update so requests don't get lost for short updates? + $row = $this->db->get('migrations')->row(); + + $current_version = $row ? $row->version : 0; + if ($current_version != $target_version) { + show_error("Database version is $current_version, we want $target_version. Please run the migration script."); + } + } } $old_path = getenv("PATH"); |