Skip to content

Commit 6539abe

Browse files
boneliferclaude
andauthored
Fix enabling on a fresh PostgreSQL board (#12)
* Fix enabling on a fresh PostgreSQL board version_1_0_0 inserts the first seven Knowledge Base permission options with explicit ids, which leaves PostgreSQL's sequence for kb_options at 1. version_2_0_30 then let the sequence pick the id for kb_m_change_author, got 1 again, and stopped with a duplicate key, so the extension could not be enabled on a new PostgreSQL board. It now gives the option the next id itself, as version_1_0_0 does. Adds a regression test that builds kb_options the way version_1_0_0 does and runs version_2_0_30 on SQLite, MySQL and PostgreSQL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * Fix the search-match index rebuild on phpBB 3.3 with PostgreSQL On PostgreSQL, version_1_0_3 creates the search-match unique index as plain un_mtch. phpBB 3.3's database tools find an index by that name but drop it as {table}_un_mtch, so version_2_0_31 stopped with "index does not exist" and a fresh phpBB 3.3 PostgreSQL board could not enable the extension. version_2_0_31 now drops this table's un_mtch index under whichever name it really has before creating the four-column one. It leaves an index of that name on another table alone. The regression test runs the real migration chain for the table (version_1_0_0, 1_0_3 with its data step, 2_0_2, 2_0_15, then 2_0_31 twice) on SQLite, MySQL and PostgreSQL. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 313eac3 commit 6539abe

3 files changed

Lines changed: 197 additions & 1 deletion

File tree

‎migrations/version_2_0_30.php‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,15 @@ public function add_permission(): void
3232
$this->db->sql_freeresult($result);
3333
if (!$exists)
3434
{
35+
// An explicit id, like the options version_1_0_0 inserted: those
36+
// left PostgreSQL's sequence at 1, so letting it pick one fails
37+
// with a duplicate key on a fresh PostgreSQL board
38+
$result = $this->db->sql_query('SELECT MAX(auth_option_id) AS max_id FROM ' . $this->table_prefix . 'kb_options');
39+
$next_id = (int) $this->db->sql_fetchfield('max_id') + 1;
40+
$this->db->sql_freeresult($result);
41+
3542
$this->db->sql_query('INSERT INTO ' . $this->table_prefix . 'kb_options ' . $this->db->sql_build_array('INSERT', [
36-
'auth_option' => 'kb_m_change_author', 'is_global' => 0, 'is_local' => 1,
43+
'auth_option_id' => $next_id, 'auth_option' => 'kb_m_change_author', 'is_global' => 0, 'is_local' => 1,
3744
]));
3845
}
3946
}

‎migrations/version_2_0_31.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ public function rebuild_match_index(): void
5555
return;
5656
}
5757

58+
// On PostgreSQL, version_1_0_3 created the index as plain un_mtch, but
59+
// phpBB 3.3's tools find it by that name and then drop
60+
// {table}_un_mtch, which does not exist. Drop whichever is there by
61+
// its real name instead.
62+
if ($this->db->get_sql_layer() === 'postgres' && !($this->db_tools instanceof \phpbb\db\tools\doctrine))
63+
{
64+
// Only this table's: another table may own an index called un_mtch
65+
$result = $this->db->sql_query("SELECT indexname
66+
FROM pg_indexes
67+
WHERE tablename = '" . $this->db->sql_escape($table) . "'
68+
AND " . $this->db->sql_in_set('indexname', ['un_mtch', $table . '_un_mtch']));
69+
$indexes = array_column($this->db->sql_fetchrowset($result), 'indexname');
70+
$this->db->sql_freeresult($result);
71+
72+
foreach ($indexes as $index)
73+
{
74+
$this->db->sql_query('DROP INDEX ' . $index);
75+
}
76+
$this->db_tools->sql_create_unique_index($table, 'un_mtch', self::MATCH_INDEX_COLUMNS);
77+
return;
78+
}
79+
5880
// phpBB 4.0 stores the index under the shortened table name, for example
5981
// kbsrcwrdmtch_un_mtch, and only finds it by that full name
6082
$stored_name = 'un_mtch';
Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
<?php
2+
/**
3+
*
4+
* Knowledge Base extension for the phpBB Forum Software package
5+
*
6+
* @copyright (c) 2026, phpBB Modders, https://www.phpbbmodders.com/
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbbmodders\knowledgebase\migrations;
12+
13+
require_once __DIR__ . '/kb_extensions_test.php';
14+
require_once dirname(__DIR__) . '/migrations/version_1_0_0.php';
15+
require_once dirname(__DIR__) . '/migrations/version_1_0_3.php';
16+
require_once dirname(__DIR__) . '/migrations/version_2_0_2.php';
17+
require_once dirname(__DIR__) . '/migrations/version_2_0_15.php';
18+
require_once dirname(__DIR__) . '/migrations/version_2_0_30.php';
19+
require_once dirname(__DIR__) . '/migrations/version_2_0_31.php';
20+
if (!defined('CONFIG_TABLE')) define('CONFIG_TABLE', 'phpbb_config');
21+
22+
/**
23+
* Migrations that stopped a fresh PostgreSQL board from enabling the extension.
24+
*
25+
* version_2_0_30 adds kb_m_change_author after the seven options
26+
* version_1_0_0 inserted with explicit ids. On PostgreSQL those inserts leave
27+
* the table's sequence at 1, so the new option must not take its id from it.
28+
*
29+
* version_2_0_31 rebuilds the search-match index that version_1_0_3 created
30+
* as plain un_mtch on PostgreSQL, where phpBB 3.3's tools drop it as
31+
* {table}_un_mtch.
32+
*/
33+
class kb_options_id_migration_test extends \PHPUnit\Framework\TestCase
34+
{
35+
private $db;
36+
private $tools;
37+
private $sqlite_fixture;
38+
39+
protected function tearDown(): void
40+
{
41+
if ($this->tools instanceof \phpbb\db\tools\doctrine) $this->tools->get_connection()->close();
42+
if ($this->db) $this->db->sql_close();
43+
if ($this->sqlite_fixture) @unlink($this->sqlite_fixture);
44+
}
45+
46+
public function engines()
47+
{
48+
return [['sqlite'], ['mysql'], ['postgres']];
49+
}
50+
51+
private function connect(string $engine): void
52+
{
53+
if ($engine === 'sqlite')
54+
{
55+
$this->db = new \phpbb\db\driver\sqlite3();
56+
if (class_exists(\phpbb\db\tools\doctrine::class))
57+
{
58+
$this->sqlite_fixture = tempnam(sys_get_temp_dir(), 'kb-options-');
59+
$this->assertTrue($this->db->sql_connect($this->sqlite_fixture, '', '', ''));
60+
$this->tools = new \phpbb\db\tools\doctrine(\phpbb\db\doctrine\connection_factory::get_connection_from_params('sqlite3', $this->sqlite_fixture));
61+
$this->tools->set_table_prefix('phpbb_');
62+
}
63+
else
64+
{
65+
$this->assertTrue($this->db->sql_connect(':memory:', '', '', ''));
66+
$this->tools = new \phpbb\db\tools\sqlite3($this->db);
67+
}
68+
return;
69+
}
70+
71+
$port = getenv('KB_TEST_' . strtoupper($engine) . '_PORT');
72+
if (!$port) $this->markTestSkipped('Set the fixture database port to run integration tests');
73+
$this->assertTrue(ctype_digit($port));
74+
$this->db = $engine === 'mysql' ? new \phpbb\db\driver\mysqli() : new \phpbb\db\driver\postgres();
75+
$this->assertIsObject($this->db->sql_connect('127.0.0.1', $engine === 'mysql' ? 'root' : 'postgres', '', 'kb_fixture', (int) $port));
76+
if (class_exists(\phpbb\db\tools\doctrine::class))
77+
{
78+
$this->tools = new \phpbb\db\tools\doctrine(\phpbb\db\doctrine\connection_factory::get_connection_from_params(
79+
$engine === 'mysql' ? 'mysqli' : 'postgres', '127.0.0.1', $engine === 'mysql' ? 'root' : 'postgres', '', 'kb_fixture', $port
80+
));
81+
$this->tools->set_table_prefix('phpbb_');
82+
}
83+
else
84+
{
85+
$this->tools = $engine === 'mysql' ? new \phpbb\db\tools\tools($this->db) : new \phpbb\db\tools\postgres($this->db);
86+
}
87+
}
88+
89+
/** @dataProvider engines */
90+
public function test_the_author_permission_is_added_after_the_first_options_on_a_fresh_board($engine)
91+
{
92+
$this->connect($engine);
93+
foreach (['phpbb_kb_options', 'phpbb_kb_users', 'phpbb_kb_groups'] as $table)
94+
{
95+
if ($this->tools->sql_table_exists($table)) $this->tools->sql_table_drop($table);
96+
if ($this->db->get_sql_layer() === 'postgres') $this->db->sql_query('DROP SEQUENCE IF EXISTS ' . $table . '_seq');
97+
}
98+
99+
// The options table and its first seven options, exactly as version_1_0_0 makes them
100+
$config = new \phpbb\config\config([]);
101+
$first = new version_1_0_0($config, $this->db, $this->tools, '', 'php', 'phpbb_', []);
102+
$this->tools->sql_create_table('phpbb_kb_options', $first->update_schema()['add_tables']['phpbb_kb_options']);
103+
$first->update_kb_options_table();
104+
105+
(new version_2_0_30($config, $this->db, $this->tools, '', 'php', 'phpbb_', []))->add_permission();
106+
107+
$result = $this->db->sql_query('SELECT auth_option_id, auth_option FROM phpbb_kb_options ORDER BY auth_option_id');
108+
$options = [];
109+
while ($row = $this->db->sql_fetchrow($result))
110+
{
111+
$options[(int) $row['auth_option_id']] = $row['auth_option'];
112+
}
113+
$this->db->sql_freeresult($result);
114+
115+
$this->assertSame('kb_m_change_author', $options[8] ?? null);
116+
$this->assertCount(8, $options);
117+
118+
// Running it again adds nothing
119+
(new version_2_0_30($config, $this->db, $this->tools, '', 'php', 'phpbb_', []))->add_permission();
120+
$result = $this->db->sql_query('SELECT COUNT(*) AS total FROM phpbb_kb_options');
121+
$this->assertSame(8, (int) $this->db->sql_fetchfield('total'));
122+
$this->db->sql_freeresult($result);
123+
124+
$this->tools->sql_table_drop('phpbb_kb_options');
125+
if ($this->db->get_sql_layer() === 'postgres') $this->db->sql_query('DROP SEQUENCE IF EXISTS phpbb_kb_options_seq');
126+
}
127+
128+
/** @dataProvider engines */
129+
public function test_the_search_match_index_is_rebuilt_after_version_1_0_3_made_it($engine)
130+
{
131+
$this->connect($engine);
132+
$table = 'phpbb_kb_src_wrdmtch';
133+
foreach ([$table, 'phpbb_kb_articles'] as $name)
134+
{
135+
if ($this->tools->sql_table_exists($name)) $this->tools->sql_table_drop($name);
136+
}
137+
// version_1_0_3 also drops a kb_articles column
138+
$this->tools->sql_create_table('phpbb_kb_articles', ['COLUMNS' => ['article_id' => ['UINT', 0], 'bbcode_bitfield' => ['VCHAR:32', '']]]);
139+
140+
// The table as version_1_0_0, 1_0_3 (schema and data), 2_0_2 and 2_0_15 leave it
141+
$config = new \phpbb\config\config([]);
142+
$migration = fn($class) => new $class($config, $this->db, $this->tools, '', 'php', 'phpbb_', []);
143+
$this->tools->sql_create_table($table, $migration(version_1_0_0::class)->update_schema()['add_tables'][$table]);
144+
$this->tools->perform_schema_changes($migration(version_1_0_3::class)->update_schema());
145+
$migration(version_1_0_3::class)->add_key();
146+
$this->tools->perform_schema_changes($migration(version_2_0_2::class)->update_schema());
147+
$this->tools->perform_schema_changes($migration(version_2_0_15::class)->update_schema());
148+
$this->db->sql_query('INSERT INTO ' . $table . ' (article_id, word_id, title_match, descr_match) VALUES (1, 1, 0, 1)');
149+
150+
$migration(version_2_0_31::class)->rebuild_match_index();
151+
// Run again (an enable retried): still fine
152+
$migration(version_2_0_31::class)->rebuild_match_index();
153+
154+
// The match survives, and the four-column unique key holds
155+
$result = $this->db->sql_query('SELECT COUNT(*) AS total FROM ' . $table);
156+
$this->assertSame(1, (int) $this->db->sql_fetchfield('total'));
157+
$this->db->sql_freeresult($result);
158+
$this->db->sql_return_on_error(true);
159+
$duplicate = $this->db->sql_query('INSERT INTO ' . $table . ' (article_id, word_id, title_match, descr_match) VALUES (1, 1, 0, 1)');
160+
$this->db->sql_return_on_error(false);
161+
$this->assertFalse($duplicate, 'a duplicate match should be refused by the unique index');
162+
$this->assertNotFalse($this->db->sql_query('INSERT INTO ' . $table . ' (article_id, word_id, title_match, descr_match) VALUES (1, 1, 0, 0)'));
163+
164+
$this->tools->sql_table_drop($table);
165+
$this->tools->sql_table_drop('phpbb_kb_articles');
166+
}
167+
}

0 commit comments

Comments
 (0)