PHP Classes

File: src/Updates/1.4.0/21-unique_ids.php

Recommend this page to a friend!
  Classes of Scott Arciszewski   CMS Airship   src/Updates/1.4.0/21-unique_ids.php   Download  
File: src/Updates/1.4.0/21-unique_ids.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CMS Airship
Content management system with security features
Author: By
Last change:
Date: 7 years ago
Size: 974 bytes
 

Contents

Class file image Download
<?php
declare(strict_types=1);

/**
 * This script runs when upgrading to v1.4.0 from an earlier version.
 * It adds the uniqueid column to the hull_blog_post_versions table then
 * retcons a uniqueid for each existing blog post version.
 */

$db = \Airship\get_database();

$db->exec('ALTER TABLE hull_blog_post_versions ADD uniqueid TEXT;');

foreach (
$db->run('SELECT * FROM hull_blog_post_versions') as $ver) {
   
// Get a unique ID:
   
do {
       
$unique = \Airship\uniqueId();
       
$exists = $db->exists(
           
'SELECT count(*) FROM hull_blog_post_versions WHERE uniqueid = ?',
           
$unique
       
);
    } while (
$exists);

   
// Now assign it.
   
$db->update(
       
'hull_blog_post_versions',
        [
           
'uniqueid' =>
               
$unique
       
],
        [
           
'versionid' =>
               
$ver['versionid']
        ]
    );
}

// Finally...
$db->exec('CREATE UNIQUE INDEX ON hull_blog_post_versions(uniqueid);');