Skip to content

Commit d541d18

Browse files
Implements Table::populate() method
Signed-off-by: Jon Stovell <jonstovell@gmail.com>
1 parent 277b3d5 commit d541d18

5 files changed

Lines changed: 103 additions & 470 deletions

File tree

Sources/Db/Schema/Table.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,96 @@ public function dropIndex(string|DbIndex $index): bool
421421
);
422422
}
423423

424+
/**
425+
* Inserts initial data into the table.
426+
*
427+
* @param bool $replace Whether to replace rows that have ID conflicts.
428+
* Default: false.
429+
* @return int Number of inserted rows.
430+
*/
431+
public function populate(bool $replace = false): int
432+
{
433+
if (empty($this->initial_data)) {
434+
return 0;
435+
}
436+
437+
// Does this table auto-increment?
438+
$auto_col = null;
439+
440+
foreach ($this->columns as $column) {
441+
if (!empty($column->auto)) {
442+
$auto_col = $column->name;
443+
break;
444+
}
445+
}
446+
447+
$method = $replace ? 'replace' : 'ignore';
448+
$returnmode = isset($auto_col) ? 2 : 0;
449+
450+
// Only do this if we are replacing data or the table is empty.
451+
if ($method !== 'replace') {
452+
$request = Db::$db->query(
453+
'SELECT COUNT(*)
454+
FROM {db_prefix}{raw:table}',
455+
[
456+
'table' => $this->name,
457+
],
458+
);
459+
list($num_rows) = Db::$db->fetch_row($request);
460+
Db::$db->free_result($request);
461+
462+
if ($num_rows > 0) {
463+
return 0;
464+
}
465+
}
466+
467+
// Get the correct values for any placeholders.
468+
Lang::load('General+Maintenance', Config::$language);
469+
470+
$replacements = [
471+
'{$db_prefix}' => Db::$db->prefix,
472+
'{$attachdir}' => Config::$modSettings['attachmentUploadDir'] ?? json_encode([1 => Db::$db->escape_string(Config::$boarddir . '/attachments')]),
473+
'{$boarddir}' => Db::$db->escape_string(Config::$boarddir),
474+
'{$boardurl}' => Config::$boardurl,
475+
'{$enableCompressedOutput}' => defined('SMF_INSTALLING') ? ((int) !empty($_POST['compress'])) : (Config::$modSettings['enableCompressedOutput'] ?? 0),
476+
'{$databaseSession_enable}' => defined('SMF_INSTALLING') ? ((int) !empty($_POST['dbsession'])) : (Config::$modSettings['databaseSession_enable'] ?? 0),
477+
'{$smf_version}' => SMF_VERSION,
478+
'{$current_time}' => time(),
479+
'{$sched_task_offset}' => 82800 + mt_rand(0, 86399),
480+
'{$registration_method}' => defined('SMF_INSTALLING') ? ((int) !empty($_POST['reg_mode'])) : (Config::$modSettings['registration_method'] ?? 0),
481+
];
482+
483+
foreach (Lang::$txt as $key => $value) {
484+
if (substr($key, 0, 8) == 'default_') {
485+
$replacements['{$' . $key . '}'] = Db::$db->escape_string($value);
486+
}
487+
}
488+
489+
$replacements['{$default_reserved_names}'] = strtr($replacements['{$default_reserved_names}'], ['\\\\n' => '\\n']);
490+
491+
// Replace any placeholders in the initial data.
492+
foreach ($this->initial_data as $row_num => $row) {
493+
$this->initial_data[$row_num] = array_map(
494+
fn($v) => $replacements[$v] ?? $v,
495+
$row,
496+
);
497+
}
498+
499+
// Insert the initial data.
500+
$ids = Db::$db->insert(
501+
method: $method,
502+
table: '{db_prefix}' . $this->name,
503+
columns: Db::$db->getTypeIndicators('{db_prefix}' . $this->name, reset($this->initial_data)),
504+
data: array_map(fn($row) => array_values($row), $this->initial_data),
505+
keys: isset($auto_col) ? [$auto_col] : [],
506+
returnmode: $returnmode,
507+
);
508+
509+
$num_inserts = count($ids ?? $this->initial_data);
510+
511+
return $num_inserts;
512+
}
513+
424514
/***********************
425515
* Public static methods
426516
***********************/

Sources/Maintenance/Migration/v2_1/CalendarUpdates.php

Lines changed: 5 additions & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace SMF\Maintenance\Migration\v2_1;
1717

18-
use SMF\Db\DatabaseApi as Db;
18+
use SMF\Db\Schema;
1919
use SMF\Maintenance\Migration\MigrationBase;
2020

2121
class CalendarUpdates extends MigrationBase
@@ -29,216 +29,6 @@ class CalendarUpdates extends MigrationBase
2929
*/
3030
public string $name = 'Update holidays';
3131

32-
/*********************
33-
* Internal properties
34-
*********************/
35-
36-
private array $holidays = [
37-
['New Year\'s', '1004-01-01'],
38-
['Christmas', '1004-12-25'],
39-
['Valentine\'s Day', '1004-02-14'],
40-
['St. Patrick\'s Day', '1004-03-17'],
41-
['April Fools', '1004-04-01'],
42-
['Earth Day', '1004-04-22'],
43-
['United Nations Day', '1004-10-24'],
44-
['Halloween', '1004-10-31'],
45-
['Mother\'s Day', '2010-05-09'],
46-
['Mother\'s Day', '2011-05-08'],
47-
['Mother\'s Day', '2012-05-13'],
48-
['Mother\'s Day', '2013-05-12'],
49-
['Mother\'s Day', '2014-05-11'],
50-
['Mother\'s Day', '2015-05-10'],
51-
['Mother\'s Day', '2016-05-08'],
52-
['Mother\'s Day', '2017-05-14'],
53-
['Mother\'s Day', '2018-05-13'],
54-
['Mother\'s Day', '2019-05-12'],
55-
['Mother\'s Day', '2020-05-10'],
56-
['Mother\'s Day', '2021-05-09'],
57-
['Mother\'s Day', '2022-05-08'],
58-
['Mother\'s Day', '2023-05-14'],
59-
['Mother\'s Day', '2024-05-12'],
60-
['Mother\'s Day', '2025-05-11'],
61-
['Mother\'s Day', '2026-05-10'],
62-
['Mother\'s Day', '2027-05-09'],
63-
['Mother\'s Day', '2028-05-14'],
64-
['Mother\'s Day', '2029-05-13'],
65-
['Mother\'s Day', '2030-05-12'],
66-
['Father\'s Day', '2010-06-20'],
67-
['Father\'s Day', '2011-06-19'],
68-
['Father\'s Day', '2012-06-17'],
69-
['Father\'s Day', '2013-06-16'],
70-
['Father\'s Day', '2014-06-15'],
71-
['Father\'s Day', '2015-06-21'],
72-
['Father\'s Day', '2016-06-19'],
73-
['Father\'s Day', '2017-06-18'],
74-
['Father\'s Day', '2018-06-17'],
75-
['Father\'s Day', '2019-06-16'],
76-
['Father\'s Day', '2020-06-21'],
77-
['Father\'s Day', '2021-06-20'],
78-
['Father\'s Day', '2022-06-19'],
79-
['Father\'s Day', '2023-06-18'],
80-
['Father\'s Day', '2024-06-16'],
81-
['Father\'s Day', '2025-06-15'],
82-
['Father\'s Day', '2026-06-21'],
83-
['Father\'s Day', '2027-06-20'],
84-
['Father\'s Day', '2028-06-18'],
85-
['Father\'s Day', '2029-06-17'],
86-
['Father\'s Day', '2030-06-16'],
87-
['Summer Solstice', '2010-06-21'],
88-
['Summer Solstice', '2011-06-21'],
89-
['Summer Solstice', '2012-06-20'],
90-
['Summer Solstice', '2013-06-21'],
91-
['Summer Solstice', '2014-06-21'],
92-
['Summer Solstice', '2015-06-21'],
93-
['Summer Solstice', '2016-06-20'],
94-
['Summer Solstice', '2017-06-20'],
95-
['Summer Solstice', '2018-06-21'],
96-
['Summer Solstice', '2019-06-21'],
97-
['Summer Solstice', '2020-06-20'],
98-
['Summer Solstice', '2021-06-21'],
99-
['Summer Solstice', '2022-06-21'],
100-
['Summer Solstice', '2023-06-21'],
101-
['Summer Solstice', '2024-06-20'],
102-
['Summer Solstice', '2025-06-21'],
103-
['Summer Solstice', '2026-06-21'],
104-
['Summer Solstice', '2027-06-21'],
105-
['Summer Solstice', '2028-06-20'],
106-
['Summer Solstice', '2029-06-21'],
107-
['Summer Solstice', '2030-06-21'],
108-
['Vernal Equinox', '2010-03-20'],
109-
['Vernal Equinox', '2011-03-20'],
110-
['Vernal Equinox', '2012-03-20'],
111-
['Vernal Equinox', '2013-03-20'],
112-
['Vernal Equinox', '2014-03-20'],
113-
['Vernal Equinox', '2015-03-20'],
114-
['Vernal Equinox', '2016-03-20'],
115-
['Vernal Equinox', '2017-03-20'],
116-
['Vernal Equinox', '2018-03-20'],
117-
['Vernal Equinox', '2019-03-20'],
118-
['Vernal Equinox', '2020-03-20'],
119-
['Vernal Equinox', '2021-03-20'],
120-
['Vernal Equinox', '2022-03-20'],
121-
['Vernal Equinox', '2023-03-20'],
122-
['Vernal Equinox', '2024-03-20'],
123-
['Vernal Equinox', '2025-03-20'],
124-
['Vernal Equinox', '2026-03-20'],
125-
['Vernal Equinox', '2027-03-20'],
126-
['Vernal Equinox', '2028-03-20'],
127-
['Vernal Equinox', '2029-03-20'],
128-
['Vernal Equinox', '2030-03-20'],
129-
['Winter Solstice', '2010-12-21'],
130-
['Winter Solstice', '2011-12-22'],
131-
['Winter Solstice', '2012-12-21'],
132-
['Winter Solstice', '2013-12-21'],
133-
['Winter Solstice', '2014-12-21'],
134-
['Winter Solstice', '2015-12-22'],
135-
['Winter Solstice', '2016-12-21'],
136-
['Winter Solstice', '2017-12-21'],
137-
['Winter Solstice', '2018-12-21'],
138-
['Winter Solstice', '2019-12-22'],
139-
['Winter Solstice', '2020-12-21'],
140-
['Winter Solstice', '2021-12-21'],
141-
['Winter Solstice', '2022-12-21'],
142-
['Winter Solstice', '2023-12-22'],
143-
['Winter Solstice', '2024-12-21'],
144-
['Winter Solstice', '2025-12-21'],
145-
['Winter Solstice', '2026-12-21'],
146-
['Winter Solstice', '2027-12-22'],
147-
['Winter Solstice', '2028-12-21'],
148-
['Winter Solstice', '2029-12-21'],
149-
['Winter Solstice', '2030-12-21'],
150-
['Autumnal Equinox', '2010-09-23'],
151-
['Autumnal Equinox', '2011-09-23'],
152-
['Autumnal Equinox', '2012-09-22'],
153-
['Autumnal Equinox', '2013-09-22'],
154-
['Autumnal Equinox', '2014-09-23'],
155-
['Autumnal Equinox', '2015-09-23'],
156-
['Autumnal Equinox', '2016-09-22'],
157-
['Autumnal Equinox', '2017-09-22'],
158-
['Autumnal Equinox', '2018-09-23'],
159-
['Autumnal Equinox', '2019-09-23'],
160-
['Autumnal Equinox', '2020-09-22'],
161-
['Autumnal Equinox', '2021-09-22'],
162-
['Autumnal Equinox', '2022-09-23'],
163-
['Autumnal Equinox', '2023-09-23'],
164-
['Autumnal Equinox', '2024-09-22'],
165-
['Autumnal Equinox', '2025-09-22'],
166-
['Autumnal Equinox', '2026-09-23'],
167-
['Autumnal Equinox', '2027-09-23'],
168-
['Autumnal Equinox', '2028-09-22'],
169-
['Autumnal Equinox', '2029-09-22'],
170-
['Autumnal Equinox', '2030-09-22'],
171-
['Independence Day', '1004-07-04'],
172-
['Cinco de Mayo', '1004-05-05'],
173-
['Flag Day', '1004-06-14'],
174-
['Veterans Day', '1004-11-11'],
175-
['Groundhog Day', '1004-02-02'],
176-
['Thanksgiving', '2010-11-25'],
177-
['Thanksgiving', '2011-11-24'],
178-
['Thanksgiving', '2012-11-22'],
179-
['Thanksgiving', '2013-11-28'],
180-
['Thanksgiving', '2014-11-27'],
181-
['Thanksgiving', '2015-11-26'],
182-
['Thanksgiving', '2016-11-24'],
183-
['Thanksgiving', '2017-11-23'],
184-
['Thanksgiving', '2018-11-22'],
185-
['Thanksgiving', '2019-11-28'],
186-
['Thanksgiving', '2020-11-26'],
187-
['Thanksgiving', '2021-11-25'],
188-
['Thanksgiving', '2022-11-24'],
189-
['Thanksgiving', '2023-11-23'],
190-
['Thanksgiving', '2024-11-28'],
191-
['Thanksgiving', '2025-11-27'],
192-
['Thanksgiving', '2026-11-26'],
193-
['Thanksgiving', '2027-11-25'],
194-
['Thanksgiving', '2028-11-23'],
195-
['Thanksgiving', '2029-11-22'],
196-
['Thanksgiving', '2030-11-28'],
197-
['Memorial Day', '2010-05-31'],
198-
['Memorial Day', '2011-05-30'],
199-
['Memorial Day', '2012-05-28'],
200-
['Memorial Day', '2013-05-27'],
201-
['Memorial Day', '2014-05-26'],
202-
['Memorial Day', '2015-05-25'],
203-
['Memorial Day', '2016-05-30'],
204-
['Memorial Day', '2017-05-29'],
205-
['Memorial Day', '2018-05-28'],
206-
['Memorial Day', '2019-05-27'],
207-
['Memorial Day', '2020-05-25'],
208-
['Memorial Day', '2021-05-31'],
209-
['Memorial Day', '2022-05-30'],
210-
['Memorial Day', '2023-05-29'],
211-
['Memorial Day', '2024-05-27'],
212-
['Memorial Day', '2025-05-26'],
213-
['Memorial Day', '2026-05-25'],
214-
['Memorial Day', '2027-05-31'],
215-
['Memorial Day', '2028-05-29'],
216-
['Memorial Day', '2029-05-28'],
217-
['Memorial Day', '2030-05-27'],
218-
['Labor Day', '2010-09-06'],
219-
['Labor Day', '2011-09-05'],
220-
['Labor Day', '2012-09-03'],
221-
['Labor Day', '2013-09-02'],
222-
['Labor Day', '2014-09-01'],
223-
['Labor Day', '2015-09-07'],
224-
['Labor Day', '2016-09-05'],
225-
['Labor Day', '2017-09-04'],
226-
['Labor Day', '2018-09-03'],
227-
['Labor Day', '2019-09-02'],
228-
['Labor Day', '2020-09-07'],
229-
['Labor Day', '2021-09-06'],
230-
['Labor Day', '2022-09-05'],
231-
['Labor Day', '2023-09-04'],
232-
['Labor Day', '2024-09-02'],
233-
['Labor Day', '2025-09-01'],
234-
['Labor Day', '2026-09-07'],
235-
['Labor Day', '2027-09-06'],
236-
['Labor Day', '2028-09-04'],
237-
['Labor Day', '2029-09-03'],
238-
['Labor Day', '2030-09-02'],
239-
['D-Day', '1004-06-06'],
240-
];
241-
24232
/****************
24333
* Public methods
24434
****************/
@@ -248,24 +38,17 @@ class CalendarUpdates extends MigrationBase
24838
*/
24939
public function execute(): bool
25040
{
41+
$table = new Schema\v2_1\CalendarHolidays();
42+
25143
$this->query(
25244
'DELETE FROM {db_prefix}calendar_holidays
25345
WHERE title in ({array_string:titles})',
25446
[
255-
'titles' => array_unique(array_column($this->holidays, 0)),
47+
'titles' => array_unique(array_column($table->initial_data, 'title')),
25648
],
25749
);
25850

259-
Db::$db->insert(
260-
'ignore',
261-
'{db_prefix}calendar_holidays',
262-
[
263-
'title' => 'string-60',
264-
'event_date' => 'date',
265-
],
266-
$this->holidays,
267-
['id_holiday'],
268-
);
51+
$table->populate();
26952

27053
return true;
27154
}

0 commit comments

Comments
 (0)