Skip to content

Commit f951d84

Browse files
committed
Move duplicate migration code to a helper class
1 parent a819ece commit f951d84

File tree

3 files changed

+81
-44
lines changed

3 files changed

+81
-44
lines changed

migrations/helper.php

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
*
4+
* Board Rules extension for the phpBB Forum Software package.
5+
*
6+
* @copyright (c) 2017 phpBB Limited <https://www.phpbb.com>
7+
* @license GNU General Public License, version 2 (GPL-2.0)
8+
*
9+
*/
10+
11+
namespace phpbb\boardrules\migrations;
12+
13+
/**
14+
* Migration helper
15+
*
16+
* This class contains methods that may be shared among
17+
* board rules' migration classes.
18+
*
19+
* @package phpbb\boardrules\migrations
20+
*/
21+
class helper
22+
{
23+
/**
24+
* @var \phpbb\db\driver\driver_interface
25+
*/
26+
protected $db;
27+
28+
/**
29+
* @var string The table prefix
30+
*/
31+
protected $table_prefix;
32+
33+
/**
34+
* Constructor
35+
*
36+
* @param \phpbb\db\driver\driver_interface $db
37+
* @param string $table_prefix
38+
*/
39+
public function __construct(\phpbb\db\driver\driver_interface $db, $table_prefix)
40+
{
41+
$this->db = $db;
42+
$this->table_prefix = $table_prefix;
43+
}
44+
45+
/**
46+
* Change rule_language values from lang_id to lang_iso
47+
*
48+
* @param string $new_column Name of the column to be updated
49+
* @param string $old_column Name of the column with old data
50+
*/
51+
public function change_rule_language($new_column, $old_column)
52+
{
53+
// Get installed language identifiers and iso codes
54+
$sql = 'SELECT lang_id, lang_iso
55+
FROM ' . LANG_TABLE;
56+
$result = $this->db->sql_query($sql);
57+
$rows = $this->db->sql_fetchrowset($result);
58+
$this->db->sql_freeresult($result);
59+
60+
// Update the languages in the boardrules table, from id to iso
61+
if (!empty($rows))
62+
{
63+
$this->db->sql_transaction('begin');
64+
65+
foreach ($rows as $row)
66+
{
67+
$sql = 'UPDATE ' . $this->table_prefix . "boardrules
68+
SET $new_column = '" . $this->db->sql_escape($row['lang_iso']) . "'
69+
WHERE $old_column = " . (int) $row['lang_id'];
70+
$this->db->sql_query($sql);
71+
}
72+
73+
$this->db->sql_transaction('commit');
74+
}
75+
}
76+
}

migrations/v20x/m15_update_lang_schema.php

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -81,27 +81,7 @@ public function update_data()
8181
*/
8282
public function change_rule_language()
8383
{
84-
// Get installed language identifiers and iso codes
85-
$sql = 'SELECT lang_id, lang_iso
86-
FROM ' . LANG_TABLE;
87-
$result = $this->db->sql_query($sql);
88-
$rows = $this->db->sql_fetchrowset($result);
89-
$this->db->sql_freeresult($result);
90-
91-
// Update the languages in the boardrules table, from id to iso
92-
if (!empty($rows))
93-
{
94-
$this->db->sql_transaction('begin');
95-
96-
foreach ($rows as $row)
97-
{
98-
$sql = 'UPDATE ' . $this->table_prefix . "boardrules
99-
SET rule_language = '" . $this->db->sql_escape($row['lang_iso']) . "'
100-
WHERE rule_language = " . (int) $row['lang_id'];
101-
$this->db->sql_query($sql);
102-
}
103-
104-
$this->db->sql_transaction('commit');
105-
}
84+
$helper = new \phpbb\boardrules\migrations\helper($this->db, $this->table_prefix);
85+
$helper->change_rule_language('rule_language', 'rule_language');
10686
}
10787
}

migrations/v20x/m16_update_lang_postgres.php

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,9 @@ public function alter_rule_language()
8787
// Add an index for the rule_language column
8888
$this->db_tools->sql_create_index($boardrules_table, 'rule_language', array('rule_language'));
8989

90-
// Get installed language identifiers and iso codes
91-
$sql = 'SELECT lang_id, lang_iso
92-
FROM ' . LANG_TABLE;
93-
$result = $this->db->sql_query($sql);
94-
$rows = $this->db->sql_fetchrowset($result);
95-
$this->db->sql_freeresult($result);
96-
97-
// Update the languages in the boardrules table, from id to iso
98-
if (!empty($rows))
99-
{
100-
$this->db->sql_transaction('begin');
101-
102-
foreach ($rows as $row)
103-
{
104-
$sql = 'UPDATE ' . $boardrules_table . "
105-
SET rule_language = '" . $this->db->sql_escape($row['lang_iso']) . "'
106-
WHERE old_rule_language = " . (int) $row['lang_id'];
107-
$this->db->sql_query($sql);
108-
}
109-
110-
$this->db->sql_transaction('commit');
111-
}
90+
// Set the new rule_language values
91+
$helper = new \phpbb\boardrules\migrations\helper($this->db, $this->table_prefix);
92+
$helper->change_rule_language('rule_language', 'old_rule_language');
11293

11394
// Drop the temporary column
11495
$this->db_tools->sql_column_remove($boardrules_table, 'old_rule_language');

0 commit comments

Comments
 (0)