<?php /** @noinspection PhpUnhandledExceptionInspection */
/**
 * Upgrades the database from version 1.12.0 to version 1.13.0
 * DO NOT call this directly, it will fail.
 * To be included from classes/core/updater.class.php.
 * Assumes that logging has already been init()ed. Log class will throw ServerException if not.
 */
Log::info("In updater, from 1.12.0 to 1.13.0...");

Log::info("Adding firstinspectiontime column to plate table...");
database::addOrAlterColumn('plate','firstinspectiontime','DATETIME NULL AFTER createtime, ADD INDEX (firstinspectiontime)');
Log::info("...done. Populating firstinspectiontime column...");

$result=database::queryGetAll('select plateid, min(imageddatetime) AS firstinspectiontime from imagingsession GROUP BY plateid;');
if(isset($result['rows'])){ $result=$result['rows']; }
if(!empty($result)){
	Log::info("...".count($result)."plates to update...");
	foreach($result as $row){
		database::query(
			'UPDATE plate set firstinspectiontime=:firstinspectiontime WHERE id=:plateid;',
			[':firstinspectiontime'=>$row['firstinspectiontime'],':plateid'=>$row['plateid']]
		);
	}
}

Log::info("...done");

Log::info("...update done");
