diff --git a/settings/Config.Example.ini b/settings/Config.Example.ini index 97035bb9a..823d6be27 100644 --- a/settings/Config.Example.ini +++ b/settings/Config.Example.ini @@ -135,6 +135,16 @@ ffmpeg_max_threads = 8 ; Movie generation queue movie_queue = "api_movie" +[database] +; Table names for the movies, screenshots, and client_states tables. Override +; these if those tables live under different names in your database (e.g. a +; shared database, or a per-instance naming scheme). Defaults shown; leaving +; them unset keeps the canonical names, so no change is required for a normal +; install. Every query in the API resolves the table through these values. +db_table_movies = "movies" +db_table_screenshots = "screenshots" +db_table_client_states = "client_states" + [statistics] ; Whether basic usage statistics should be collected by Helioviewer.org. ; Currently the information collected is very basic and does not include diff --git a/src/Config.php b/src/Config.php index e67c46b07..e5b6ceab3 100644 --- a/src/Config.php +++ b/src/Config.php @@ -121,6 +121,19 @@ private function _setAdditionalParams() { define('HV_LOG_DIR', HV_ROOT_DIR . '/../log'); define('HV_CACHE_URL', HV_WEB_ROOT_URL . '/cache'); + // Database table names. Overridable from Config.ini (db_table_movies, + // db_table_screenshots, db_table_client_states); default to the + // canonical names so existing deployments need no config change. + if ( !defined('HV_DB_TABLE_MOVIES') ) { + define('HV_DB_TABLE_MOVIES', 'movies'); + } + if ( !defined('HV_DB_TABLE_SCREENSHOTS') ) { + define('HV_DB_TABLE_SCREENSHOTS', 'screenshots'); + } + if ( !defined('HV_DB_TABLE_CLIENT_STATES') ) { + define('HV_DB_TABLE_CLIENT_STATES', 'client_states'); + } + // 1 au in meters (http://maia.usno.navy.mil/NSFA/IAU2009_consts.html) define('HV_CONSTANT_AU', 149597870700); // Solar radius in arc-seconds at 1 au diff --git a/src/Database/ClientState.php b/src/Database/ClientState.php index dfac0314b..701ddefcd 100644 --- a/src/Database/ClientState.php +++ b/src/Database/ClientState.php @@ -24,7 +24,7 @@ public function upsert(array $state): string $state_json = json_encode($state); $state_key = hash('sha256',$state_json); - $create_sql = "REPLACE INTO client_states(id, state) VALUES ('%s','%s')"; + $create_sql = "REPLACE INTO " . HV_DB_TABLE_CLIENT_STATES . "(id, state) VALUES ('%s','%s')"; $create_state_sql = sprintf($create_sql, $state_key, $this->link->real_escape_string($state_json)); // intentionally let exception thrown @@ -40,7 +40,7 @@ public function upsert(array $state): string */ public function find(string $state_key): ?array { - $find_sql = "SELECT * FROM client_states WHERE id = '%s' LIMIT 1"; + $find_sql = "SELECT * FROM " . HV_DB_TABLE_CLIENT_STATES . " WHERE id = '%s' LIMIT 1"; $find_state_sql = sprintf($find_sql, $this->link->real_escape_string($state_key)); @@ -66,7 +66,7 @@ public function find(string $state_key): ?array */ public function all(int $limit = 100): ?array { - $all_sql = "SELECT * FROM client_states LIMIT $limit"; + $all_sql = "SELECT * FROM " . HV_DB_TABLE_CLIENT_STATES . " LIMIT $limit"; $query_result = $this->query($all_sql); @@ -85,7 +85,7 @@ public function all(int $limit = 100): ?array */ public function update(string $state_key, array $state): bool { - $find_sql = "SELECT * FROM client_states WHERE id = '%s' LIMIT 1"; + $find_sql = "SELECT * FROM " . HV_DB_TABLE_CLIENT_STATES . " WHERE id = '%s' LIMIT 1"; $find_state_sql = sprintf($find_sql, $this->link->real_escape_string($state_key)); @@ -96,7 +96,7 @@ public function update(string $state_key, array $state): bool return false; } - $update_sql = "UPDATE client_states SET state = '%s' WHERE id = '%s'"; + $update_sql = "UPDATE " . HV_DB_TABLE_CLIENT_STATES . " SET state = '%s' WHERE id = '%s'"; $update_state_sql = sprintf($update_sql, $this->link->real_escape_string(json_encode($state)), $this->link->real_escape_string($state_key)); diff --git a/src/Database/ImgIndex.php b/src/Database/ImgIndex.php index 51b7562b4..52ab95ab3 100644 --- a/src/Database/ImgIndex.php +++ b/src/Database/ImgIndex.php @@ -73,7 +73,7 @@ public function insertScreenshot($date, $imageScale, $roi, $watermark, $layers, $sql = sprintf( - "INSERT INTO screenshots " + "INSERT INTO " . HV_DB_TABLE_SCREENSHOTS . " " . "SET " . "id " . " = NULL, " . "timestamp " . " = CURRENT_TIMESTAMP, " @@ -137,7 +137,7 @@ public function getMovieInformation($movieId) { $sql = sprintf( "SELECT movies.*, movieFormats.movieId, movieFormats.format, movieFormats.status, movieFormats.procTime, movieFormats.modified, ST_AsText(regionOfInterest) AS roi " - . "FROM movies " + . "FROM " . HV_DB_TABLE_MOVIES . " movies " . "LEFT JOIN " . "movieFormats ON movies.id = movieFormats.movieId " . "WHERE " @@ -169,7 +169,7 @@ public function storeMovieProperties($movieId, $startDate, $endDate, $this->_dbConnect(); $sql = sprintf( - "UPDATE movies " + "UPDATE " . HV_DB_TABLE_MOVIES . " " . "SET " . "startDate " . " ='%s', " . "endDate " . " ='%s', " @@ -217,7 +217,7 @@ public function finishedBuildingMovieFrames($movieId, $buildTimeStart, $this->_dbConnect(); $sql = sprintf( - "UPDATE movies " + "UPDATE " . HV_DB_TABLE_MOVIES . " " . "SET " . "buildTimeStart='%s', " . "buildTimeEnd='%s' " @@ -350,7 +350,7 @@ public function getScreenshot($screenshotId) { $sql = sprintf( "SELECT * " - . "FROM screenshots " + . "FROM " . HV_DB_TABLE_SCREENSHOTS . " " . "WHERE id = %d " . "LIMIT 1;", (int)$screenshotId @@ -1631,7 +1631,7 @@ public function getScreenshotMetadata($screenshotId) { $this->_dbConnect(); $sql = sprintf('SELECT *, ST_AsText(regionOfInterest) as roi ' . - 'FROM screenshots WHERE id=%d LIMIT 1;', + 'FROM ' . HV_DB_TABLE_SCREENSHOTS . ' WHERE id=%d LIMIT 1;', (int)$screenshotId ); try { diff --git a/src/Database/MovieDatabase.php b/src/Database/MovieDatabase.php index a6c1c5948..127ad7ba6 100644 --- a/src/Database/MovieDatabase.php +++ b/src/Database/MovieDatabase.php @@ -84,7 +84,7 @@ public function insertMovie($startTime, $endTime, $reqObservationDate, $imageSca $old_events_labels_bool = false; $sql = sprintf( - 'INSERT INTO movies ' + 'INSERT INTO ' . HV_DB_TABLE_MOVIES . ' ' . 'SET ' . 'id ' . ' = NULL, ' . 'timestamp ' . ' = CURRENT_TIMESTAMP, ' @@ -282,7 +282,7 @@ public function getMovieStatistics($n=100) { . 'queueNum, ' . 'TIMESTAMPDIFF(SECOND, buildTimeStart, buildTimeEnd) ' . 'AS time ' - . 'FROM movies ' + . 'FROM ' . HV_DB_TABLE_MOVIES . ' ' . 'WHERE ' . 'TIMESTAMPDIFF(SECOND, buildTimeStart, buildTimeEnd) ' . '> 0 ' @@ -360,7 +360,7 @@ public function getSharedVideos($num, $skip, $since, $force=false) { . 'youtube.keywords, youtube.shared, movies.imageScale, movies.dataSourceString, movies.eventSourceString, ' . 'movies.movieLength, movies.width, movies.height, movies.startDate, movies.endDate ' . 'FROM youtube ' - . 'LEFT JOIN movies ' + . 'LEFT JOIN ' . HV_DB_TABLE_MOVIES . ' movies ' . 'ON movies.id = youtube.movieId ' . 'WHERE ' . 'youtube.shared>0 AND ' @@ -427,7 +427,7 @@ public function getSharedVideosByTime($num, $skip, $date) { . 'youtube.keywords, youtube.thumbnail, youtube.shared, youtube.checked, movies.imageScale, movies.dataSourceString, movies.eventSourceString, ' . 'movies.movieLength, movies.width, movies.height, movies.startDate, movies.endDate, ST_AsText(regionOfInterest) as roi ' . 'FROM youtube ' - . 'LEFT JOIN movies ' + . 'LEFT JOIN ' . HV_DB_TABLE_MOVIES . ' movies ' . 'ON movies.id = youtube.movieId ' . 'WHERE ' . 'youtube.shared>0 AND ' @@ -493,7 +493,7 @@ public function getMovieMetadata($movieId) { $this->_dbConnect(); $sql = sprintf('SELECT *, ST_AsText(regionOfInterest) as roi ' - . 'FROM movies WHERE movies.id=%d LIMIT 1;', + . 'FROM ' . HV_DB_TABLE_MOVIES . ' movies WHERE movies.id=%d LIMIT 1;', (int)$movieId ); try { diff --git a/src/Database/Statistics.php b/src/Database/Statistics.php index a105dad6b..77d712a6d 100644 --- a/src/Database/Statistics.php +++ b/src/Database/Statistics.php @@ -603,7 +603,7 @@ public function getUsageStatistics($resolution, $dateStart = null, $dateEnd = nu $sqlScreenshots = sprintf( "SELECT dataSourceString " - . "FROM movies " + . "FROM " . HV_DB_TABLE_MOVIES . " " . "WHERE " . "timestamp BETWEEN '%s' AND '%s' ;", $this->_dbConnection->link->real_escape_string($dateStartStr), @@ -655,7 +655,7 @@ public function getUsageStatistics($resolution, $dateStart = null, $dateEnd = nu $sqlScreenshots = sprintf( "SELECT dataSourceString " - . "FROM screenshots " + . "FROM " . HV_DB_TABLE_SCREENSHOTS . " " . "WHERE " . "timestamp BETWEEN '%s' AND '%s' ;", $this->_dbConnection->link->real_escape_string($dateStartStr), diff --git a/tests/unit_tests/database/ClientStateTest.php b/tests/unit_tests/database/ClientStateTest.php index 95319745a..27ee03298 100644 --- a/tests/unit_tests/database/ClientStateTest.php +++ b/tests/unit_tests/database/ClientStateTest.php @@ -27,7 +27,7 @@ public function testItShouldCreateClientState(): void // just a precaution to clean database after we are done $this->createdIds[] = $client_state->upsert($state); - $count_sql = sprintf("SELECT COUNT(*) FROM client_states WHERE id = '%s'", hash('sha256', json_encode($state))); + $count_sql = sprintf("SELECT COUNT(*) FROM " . HV_DB_TABLE_CLIENT_STATES . " WHERE id = '%s'", hash('sha256', json_encode($state))); $result = $client_state->query($count_sql); @@ -49,7 +49,7 @@ public function testItShouldOnlyCreateDBInstancePerClientState(): void $client_state->upsert($state); $client_state->upsert($state); - $count_sql = sprintf("SELECT COUNT(*) FROM client_states WHERE id = '%s'", hash('sha256', json_encode($state))); + $count_sql = sprintf("SELECT COUNT(*) FROM " . HV_DB_TABLE_CLIENT_STATES . " WHERE id = '%s'", hash('sha256', json_encode($state))); $result = $client_state->query($count_sql); @@ -103,7 +103,7 @@ public function testItShouldNotFindNonExistingStates(): void public function tearDown(): void { $client_state = new ClientState(); - $client_state->query(sprintf("DELETE FROM client_states WHERE id in ('%s') LIMIT %d", join("','", $this->createdIds), count($this->createdIds))); + $client_state->query(sprintf("DELETE FROM " . HV_DB_TABLE_CLIENT_STATES . " WHERE id in ('%s') LIMIT %d", join("','", $this->createdIds), count($this->createdIds))); }