Skip to content

Commit f3962db

Browse files
authored
fix(runtime): close PROJ-1 stability gaps and align user_logs schema parity (#10)
- Fixes a PROJ-1 runtime stability issue in `tag_subscription` user listing by resolving the listing user safely from view context and guarding edit-link checks. - Adds additive migration `20260218221000_ensure_user_logs_table_exists` to ensure `user_logs` exists with foreign key to `users`. - Aligns migration DDL with canonical schema by using `ENGINE=InnoDB DEFAULT CHARSET=utf8`. - Uses `SHOW TABLES LIKE ?` for efficient table-existence checks. - Updates `db/schema.sql` and `db/table_schema/production/user_logs.php` to keep migrated and schema-based installs consistent. - Addresses Copilot feedback for schema parity and migration query behavior.
1 parent cccecdc commit f3962db

File tree

4 files changed

+75
-11
lines changed

4 files changed

+75
-11
lines changed
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1-
<?php $has_subscriptions = !empty($user->tag_subscriptions) && $user->tag_subscriptions->size() > 0; ?>
1+
<?php
2+
$listingUser = $this->user ?? ($user ?? null);
3+
$subscriptions = $listingUser ? $listingUser->tag_subscriptions : null;
4+
$has_subscriptions = $subscriptions && $subscriptions->size() > 0;
5+
?>
26

37
<?php if (!$has_subscriptions) : ?>
48
<?= $this->t('sub_none') ?>
59
<?php else : ?>
6-
<?= $this->tag_subscription_listing($user) ?>
10+
<?= $this->tag_subscription_listing($listingUser) ?>
711
<?php endif ?>
812

9-
<?php if (current_user()->id == $user->id) : ?>
13+
<?php if ($listingUser && current_user()->id == $listingUser->id) : ?>
1014
(<?= $this->linkTo($this->t('sub_edit'), 'tag_subscription#index') ?>)
1115
<?php endif ?>
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
class EnsureUserLogsTableExists extends Rails\ActiveRecord\Migration\Base
3+
{
4+
public function up()
5+
{
6+
if (!$this->tableExists('user_logs')) {
7+
$this->execute(<<<SQL
8+
CREATE TABLE `user_logs` (
9+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
10+
`user_id` int(11) NOT NULL,
11+
`ip_addr` varchar(46) NOT NULL,
12+
`created_at` datetime NOT NULL,
13+
PRIMARY KEY (`id`),
14+
KEY `created_at` (`created_at`),
15+
KEY `user_id` (`user_id`)
16+
) ENGINE=InnoDB DEFAULT CHARSET=utf8
17+
SQL
18+
);
19+
}
20+
21+
if (!$this->foreignKeyExists('user_logs', 'fk_user_logs__user_id')) {
22+
$this->execute(
23+
"ALTER TABLE `user_logs` ADD CONSTRAINT `fk_user_logs__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE"
24+
);
25+
}
26+
}
27+
28+
private function tableExists($tableName)
29+
{
30+
$stmt = $this->connection->executeSql("SHOW TABLES LIKE ?", $tableName);
31+
$row = $stmt->fetch(\PDO::FETCH_NUM);
32+
33+
return $row !== false;
34+
}
35+
36+
private function foreignKeyExists($tableName, $constraintName)
37+
{
38+
$stmt = $this->connection->executeSql(
39+
"SELECT COUNT(*) AS count_value FROM information_schema.TABLE_CONSTRAINTS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND CONSTRAINT_NAME = ? AND CONSTRAINT_TYPE = 'FOREIGN KEY'",
40+
$tableName,
41+
$constraintName
42+
);
43+
$row = $stmt->fetch(\PDO::FETCH_ASSOC);
44+
45+
return $row && (int)$row['count_value'] > 0;
46+
}
47+
}

db/schema.sql

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,16 @@ CREATE TABLE `user_blacklisted_tags` (
423423
UNIQUE KEY `user_id` (`user_id`)
424424
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
425425

426+
CREATE TABLE `user_logs` (
427+
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
428+
`user_id` int(11) NOT NULL,
429+
`ip_addr` varchar(46) NOT NULL,
430+
`created_at` datetime NOT NULL,
431+
PRIMARY KEY (`id`),
432+
KEY `created_at` (`created_at`),
433+
KEY `user_id` (`user_id`)
434+
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
435+
426436
CREATE TABLE `user_records` (
427437
`id` bigint(20) NOT NULL AUTO_INCREMENT,
428438
`user_id` int(11) NOT NULL,
@@ -600,6 +610,9 @@ ALTER TABLE `tag_implications`
600610
ALTER TABLE `user_blacklisted_tags`
601611
ADD CONSTRAINT `fk_user_bl_tags__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;
602612

613+
ALTER TABLE `user_logs`
614+
ADD CONSTRAINT `fk_user_logs__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;
615+
603616
ALTER TABLE `user_records`
604617
ADD CONSTRAINT `fk_user_records__reported_by` FOREIGN KEY (`reported_by`) REFERENCES `users` (`id`) ON DELETE CASCADE,
605618
ADD CONSTRAINT `fk_user_records__user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE;
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,34 @@
11
<?php
22
return array (
3-
0 =>
3+
0 =>
44
array (
5-
'id' =>
5+
'id' =>
66
array (
77
'type' => 'int(11) unsigned',
88
'default' => NULL,
99
),
10-
'user_id' =>
10+
'user_id' =>
1111
array (
1212
'type' => 'int(11)',
1313
'default' => NULL,
1414
),
15-
'ip_addr' =>
15+
'ip_addr' =>
1616
array (
1717
'type' => 'varchar(46)',
1818
'default' => NULL,
1919
),
20-
'created_at' =>
20+
'created_at' =>
2121
array (
2222
'type' => 'datetime',
2323
'default' => NULL,
2424
),
2525
),
26-
1 =>
26+
1 =>
2727
array (
28-
'pri' =>
28+
'pri' =>
2929
array (
3030
0 => 'id',
3131
),
3232
),
3333
)
34-
;
34+
;

0 commit comments

Comments
 (0)