New version of database, add last_access_time field

This commit is contained in:
Gregory Soutade 2015-12-04 17:01:41 +01:00
parent 1d22f425e9
commit cc66b612ef
2 changed files with 27 additions and 7 deletions

View File

@ -28,7 +28,7 @@
*/ */
$MAX_ENTRY_LEN = 512; $MAX_ENTRY_LEN = 512;
$USERS_PATH = "./users/"; $USERS_PATH = "./users/";
$TARGET_DB_VERSION = 1; $TARGET_DB_VERSION = 2;
function sanitize($val) function sanitize($val)
{ {
@ -106,15 +106,35 @@ function _migrate_0($user, $db)
return 0; return 0;
} }
function _migrate_1($user, $db)
{
try {
$db->query("CREATE TABLE conf(db_version INTEGER, last_access_time INTEGER)");
$db->query("INSERT INTO conf VALUES(2, 0)");
}
catch(Exception $e)
{
$db->close();
echo "<div class=\"error\">Unable to load database for user $user ! : $e</div>";
return -1;
}
return 0;
}
function migrate_database($user, $db) function migrate_database($user, $db)
{ {
global $TARGET_DB_VERSION; global $TARGET_DB_VERSION;
$migration_functions = ['_migrate_0']; $migration_functions = ['_migrate_0', '_migrate_1'];
$version = $db->querySingle("SELECT version FROM db_version"); $version = $db->querySingle("SELECT db_version FROM conf");
if ($version == false) if ($version == false || $version == -1)
$version = 0; {
$version = $db->querySingle("SELECT version FROM db_version");
if ($version == false || $version == -1)
$version = 0;
}
for($i=$version; $i<$TARGET_DB_VERSION; $i++) for($i=$version; $i<$TARGET_DB_VERSION; $i++)
{ {

View File

@ -1,3 +1,3 @@
CREATE TABLE gpass(login VARCHAR(512) PRIMARY KEY, password VARCHAR(512), shadow_login VARCHAR(32), salt VARCHAR(32), access_token VARCHAR(32)); CREATE TABLE gpass(login VARCHAR(512) PRIMARY KEY, password VARCHAR(512), shadow_login VARCHAR(32), salt VARCHAR(32), access_token VARCHAR(32));
CREATE TABLE db_version(version INTEGER); CREATE TABLE conf(db_version INTEGER, last_access_time INTEGER);
INSERT INTO db_version VALUES (1); INSERT INTO conf VALUES (2, 0);