PostgreSQL-For-Wordpress / postgresql-for-wordpress

A maintained fork of https://wordpress.org/plugins/postgresql-for-wordpress/
GNU General Public License v2.0
209 stars 68 forks source link

Use of PostgreSQL keywords in table column names #120

Open hahnn opened 1 month ago

hahnn commented 1 month ago

In the SQL table creation attempt below, the column name end is used, however end is a reserved PostgreSQL keyword then it triggers an error and the SQL table cannot be created.

So, the end column name can be used at the condition it's enclosed between " characters in PostgreSQL: that's the way to use reserved keywords in PostgreSQL.

So instead of having:

end datetime DEFAULT NULL,

We should have:

"end" datetime DEFAULT NULL,

Here below is the full output from the plugin:

[1717318176.0314] Error running :
CREATE TABLE wp_aioseo_notifications (
                                        id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
                                        slug varchar(13) NOT NULL,
                                        title text NOT NULL,
                                        content longtext NOT NULL,
                                        type varchar(64) NOT NULL,
                                        level text NOT NULL,
                                        notification_id bigint(20) unsigned DEFAULT NULL,
                                        notification_name varchar(255) DEFAULT NULL,
                                        start datetime DEFAULT NULL,
                                        end datetime DEFAULT NULL,
                                        button1_label varchar(255) DEFAULT NULL,
                                        button1_action varchar(255) DEFAULT NULL,
                                        button2_label varchar(255) DEFAULT NULL,
                                        button2_action varchar(255) DEFAULT NULL,
                                        dismissed tinyint(1) NOT NULL DEFAULT 0,
                                        created datetime NOT NULL,
                                        updated datetime NOT NULL,
                                        PRIMARY KEY (id),
                                        UNIQUE KEY ndx_aioseo_notifications_slug (slug),
                                        KEY ndx_aioseo_notifications_dates (start, end),
                                        KEY ndx_aioseo_notifications_type (type),
                                        KEY ndx_aioseo_notifications_dismissed (dismissed)
                                ) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_520_ci;
---- converted to ----
CREATE TABLE IF NOT EXISTS wp_aioseo_notifications (
                                        id bigserial,
                                        slug varchar(13) NOT NULL,
                                        title text NOT NULL,
                                        content text NOT NULL,
                                        type varchar(64) NOT NULL,
                                        level text NOT NULL,
                                        notification_id bigint  DEFAULT NULL,
                                        notification_name varchar(255) DEFAULT NULL,
                                        start timestamp DEFAULT NULL,
                                        end timestamp DEFAULT NULL,
                                        button1_label varchar(255) DEFAULT NULL,
                                        button1_action varchar(255) DEFAULT NULL,
                                        button2_label varchar(255) DEFAULT NULL,
                                        button2_action varchar(255) DEFAULT NULL,
                                        dismissed smallint NOT NULL DEFAULT 0,
                                        created timestamp NOT NULL,
                                        updated timestamp NOT NULL,
                                        PRIMARY KEY (id)
                                );
CREATE UNIQUE INDEX IF NOT EXISTS wp_aioseo_notifications_ndx_aioseo_notifications_slug ON wp_aioseo_notifications (slug);
CREATE INDEX IF NOT EXISTS wp_aioseo_notifications_ndx_aioseo_notifications_dates ON wp_aioseo_notifications (start, end);
CREATE INDEX IF NOT EXISTS wp_aioseo_notifications_ndx_aioseo_notifications_type ON wp_aioseo_notifications (type);
CREATE INDEX IF NOT EXISTS wp_aioseo_notifications_ndx_aioseo_notifications_dismissed ON wp_aioseo_notifications (dismissed);
----> ERREUR:  erreur de syntaxe sur ou près de « end »
LINE 11:      end timestamp DEFAULT NULL,
              ^
---------------------
mattbucci commented 1 month ago

This is an easy one I think. "end" should just be added to this list: https://github.com/PostgreSQL-For-Wordpress/postgresql-for-wordpress/blob/d8b83c88e29b70e8dc0fb37dc351049f52427f80/pg4wp/rewriters/CreateTableSQLRewriter.php#L147

Any others we're missing @hahnn ?