Skip to content

Update table creation for PostgreSQL compatibility#32

Merged
alikon merged 6 commits intomainfrom
postgresql
Feb 25, 2026
Merged

Update table creation for PostgreSQL compatibility#32
alikon merged 6 commits intomainfrom
postgresql

Conversation

@alikon
Copy link
Copy Markdown
Owner

@alikon alikon commented Feb 25, 2026

Summary by Sourcery

Add PostgreSQL-compatible creation and existence checks for plugin database tables while preserving MySQL behavior.

New Features:

  • Introduce PostgreSQL-aware table existence checks for the magic login and GitHub issues plugin tables using the current schema.
  • Add PostgreSQL-specific CREATE TABLE and index definitions for the magic login token and GitHub issues tables.

Enhancements:

  • Retain existing MySQL table creation logic alongside new PostgreSQL branches to support multiple database backends.

@sourcery-ai
Copy link
Copy Markdown

sourcery-ai bot commented Feb 25, 2026

Reviewer's Guide

Adds PostgreSQL-aware table existence checks and PostgreSQL-compatible DDL for the magiclogin_tokens and github_issues tables, while preserving existing MySQL-specific creation logic behind a server-type conditional.

ER diagram for magiclogin_tokens and github_issues tables

erDiagram
    magiclogin_tokens {
        int id PK
        int user_id
        varchar token
        datetime expires
        timestamp created
        varchar ip_address
        text user_agent
    }

    github_issues {
        int id PK
        datetime execution
        smallint openi
        smallint closedi
        smallint openp
        smallint closedp
    }

    %% No explicit foreign key relationships defined between these tables
Loading

File-Level Changes

Change Details Files
Make magiclogin token table creation work on both MySQL and PostgreSQL.
  • Adjust information_schema query to branch on server type, using current_schema() for PostgreSQL and DATABASE() for others
  • Introduce PostgreSQL-specific CREATE TABLE for magiclogin_tokens with SERIAL primary key, TIMESTAMP columns, and a named unique constraint on token
  • Create separate PostgreSQL indexes for user_id, expires, and ip_address using CREATE INDEX IF NOT EXISTS
  • Retain existing MySQL CREATE TABLE definition and execute it only for non-PostgreSQL servers
  • Minor cleanup of docblock comment and variable spacing
src/plugins/system/magiclogin/script.php
Make github_issues task table creation compatible with PostgreSQL while keeping MySQL logic.
  • Change table existence check to only filter by table_name in information_schema.tables and branch schema condition on server type
  • Add PostgreSQL-specific CREATE TABLE for github_issues with SERIAL id, TIMESTAMP execution, and SMALLINT counters
  • Create PostgreSQL execution index via separate CREATE INDEX IF NOT EXISTS statement
  • Wrap original MySQL CREATE TABLE and index definition in an else-branch for non-PostgreSQL databases
src/plugins/task/githubissues/script.php

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've left some high level feedback:

  • The PostgreSQL branch builds a single SQL string containing both CREATE TABLE and CREATE INDEX; if the database layer doesn’t support multiple statements per query, this should be split into two separate setQuery/execute calls to avoid runtime failures.
  • The PostgreSQL-specific SQL is now embedded as a raw string while the rest of the code uses the query builder and quoteName; consider using the query builder (or at least consistent quoting utilities) for the new DDL to keep identifier quoting and prefix handling uniform across database types.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The PostgreSQL branch builds a single SQL string containing both `CREATE TABLE` and `CREATE INDEX`; if the database layer doesn’t support multiple statements per query, this should be split into two separate `setQuery`/`execute` calls to avoid runtime failures.
- The PostgreSQL-specific SQL is now embedded as a raw string while the rest of the code uses the query builder and `quoteName`; consider using the query builder (or at least consistent quoting utilities) for the new DDL to keep identifier quoting and prefix handling uniform across database types.

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@alikon
Copy link
Copy Markdown
Owner Author

alikon commented Feb 25, 2026

@sourcery-ai review

Copy link
Copy Markdown

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue, and left some high level feedback:

  • The logic for checking table existence and creating tables/indexes is duplicated between magiclogin and githubissues; consider extracting a shared helper or method to centralize the getServerType() branching and reduce repetition.
  • Instead of hardcoding "#__..." in the SQL strings, consider using $db->quoteName() and Joomla’s table prefix replacement to avoid potential quoting and prefix substitution issues across database drivers.
  • You are calling $db->setQuery($query); $db->execute(); repeatedly for each index creation; you could simplify by combining related DDL statements where supported or by wrapping this pattern in a small utility to make the code more compact and maintainable.
Prompt for AI Agents
Please address the comments from this code review:

## Overall Comments
- The logic for checking table existence and creating tables/indexes is duplicated between `magiclogin` and `githubissues`; consider extracting a shared helper or method to centralize the `getServerType()` branching and reduce repetition.
- Instead of hardcoding `"#__..."` in the SQL strings, consider using `$db->quoteName()` and Joomla’s table prefix replacement to avoid potential quoting and prefix substitution issues across database drivers.
- You are calling `$db->setQuery($query); $db->execute();` repeatedly for each index creation; you could simplify by combining related DDL statements where supported or by wrapping this pattern in a small utility to make the code more compact and maintainable.

## Individual Comments

### Comment 1
<location path="src/plugins/system/magiclogin/script.php" line_range="215-224" />
<code_context>
-
-                            $db->setQuery($query);
-                            $db->execute();
+                            if ($db->getServerType() === 'postgresql') {
+                                $query = 'CREATE TABLE IF NOT EXISTS "#__github_issues" (
+                                        "id" SERIAL PRIMARY KEY,
+                                        "execution" TIMESTAMP DEFAULT NULL,
+                                        "openi" SMALLINT NOT NULL DEFAULT 0,
+                                        "closedi" SMALLINT NOT NULL DEFAULT 0,
+                                        "openp" SMALLINT NOT NULL DEFAULT 0,
+                                        "closedp" SMALLINT NOT NULL DEFAULT 0
+                                    );';
+                                $db->setQuery($query);
+                                $db->execute();

+                                $query ='CREATE INDEX IF NOT EXISTS "idx_execution" 
</code_context>
<issue_to_address>
**suggestion:** Avoid double-executing the last PostgreSQL index creation and make query execution branch-local.

In the PostgreSQL branch you call `$db->setQuery($query); $db->execute();` for each statement, then after the `if/else` you call them again with the last `$query`, causing the last index creation (e.g. `idx_magiclogin_tokens_ip_address`) to run twice. Even though `IF NOT EXISTS` makes this safe, it’s redundant and obscures control flow. Move the final `setQuery/execute` into the non-PostgreSQL branch so each branch runs all of its own queries explicitly.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Removed redundant database query execution.
@alikon alikon merged commit 78f248c into main Feb 25, 2026
24 checks passed
@alikon alikon deleted the postgresql branch February 25, 2026 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant