Result, the table (custom) is correctly created but populated as per my request.
I tried a lot of variations there, lurked here and there in the DB folder (migrations) for examples already done, I found something similar and .. here we go, the present migration depends on itself and phpBB 3.2.1.
That's a multiple steps process due to the chance we might work on a large board
Code: Select all
<?php
namespace threedi\pia\migrations;
/*
* Creates the PIA's table and backups the User avatars's lot
*/
class m4_install_user_schema extends \phpbb\db\migration\migration
{
public function effectively_installed()
{
/* If doesn't exists go ahead */
return $this->db_tools->sql_table_exists($this->table_prefix . 'pia');
}
static public function depends_on()
{
return ['\phpbb\db\migration\data\v32x\v321',];
}
public function update_schema()
{
return [
'add_tables' => [
$this->table_prefix . 'pia' => [
'COLUMNS' => [
'pia_user_id' => ['UINT', null, 'auto_increment'],
'pia_user_avatar' => ['VCHAR', ''],
'pia_user_avatar_type' => ['VCHAR:255', ''],
'pia_user_avatar_width' => ['USINT', 0],
'pia_user_avatar_height' => ['USINT', 0],
],
'PRIMARY_KEY' => 'pia_user_id',
],
],
'add_columns' => [
$this->table_prefix . 'users' => [
'pia_avatar_ucp' => ['BOOL', 1, 'after' => 'user_avatar_height']
],
],
['custom', [[&$this, 'backup_user_avatar']]],
];
}
public function backup_user_avatar($block = 0)
{
$group = 500;
$sql = 'SELECT user_id, user_avatar, user_avatar_type, user_avatar_width, user_avatar_height
FROM ' . $this->table_prefix . 'users' . '
WHERE user_id <> ' . ANONYMOUS . '
AND (user_type <> ' . USER_IGNORE . ')
GROUP BY user_id';
$result = $this->db->sql_query_limit($sql, $group, $block);
$i = 0;
while ($row = $this->db->sql_fetchrow($result))
{
$i++;
$this->sql_query('INSERT INTO ' . $this->table_prefix . 'pia' . ' (pia_user_id, pia_avatar, pia_avatar_type, pia_avatar_width, pia_avatar_height) VALUES (' . (int) $row['user_id'] . ', ' . $row['user_avatar'] . ', ' . $row['user_avatar_type'] . ', ' . (int) $row['user_avatar_width'] . ', ' . (int) $row['user_avatar_height'] . ')');
}
$this->db->sql_freeresult($result);
if ($i < $group)
{
return;
}
return $block + $group;
}