summaryrefslogtreecommitdiffstats
path: root/includes/admin
diff options
context:
space:
mode:
authorHarald Eilertsen <haraldei@anduin.net>2021-09-16 22:19:07 +0200
committerHarald Eilertsen <haraldei@anduin.net>2021-09-16 22:19:07 +0200
commitd3fdcf53bcaf4b143c316f3379190d0053a6036f (patch)
tree81c99de94b0359f95eb847292344ea0c5807f687 /includes/admin
parent7e00fa32ea8262de0a98ff78fb5be1dc16204aea (diff)
downloadgigologadmin-d3fdcf53bcaf4b143c316f3379190d0053a6036f.tar.gz
gigologadmin-d3fdcf53bcaf4b143c316f3379190d0053a6036f.tar.bz2
gigologadmin-d3fdcf53bcaf4b143c316f3379190d0053a6036f.zip
Clean up, fix and rename db tables.
This patch got a bit more involved than what was originally planned, but since we're messing with the tables I decided to do it all right away. - Moves the constraint definition to the CREATE TABLE statement for the concerts table. This replaces the existing KEY definition that it had. - Make sure the venues table is created before the concerts table so that the above mentioned constraint definition works. - Rename the tables. Use the wpdb-prefix and make the name a bit prettier. This caused some changes in the Concert and Venue classes, and for slightly silly reasons some test classes. The code actually turned out better (for the most part), but some refactoring can still be done. The column names remains unchanged for now.
Diffstat (limited to 'includes/admin')
-rw-r--r--includes/admin/register_db_tables.php68
1 files changed, 19 insertions, 49 deletions
diff --git a/includes/admin/register_db_tables.php b/includes/admin/register_db_tables.php
index 5ab42c7..ce0f25c 100644
--- a/includes/admin/register_db_tables.php
+++ b/includes/admin/register_db_tables.php
@@ -4,35 +4,30 @@
//
// SPDX-License-Identifier: AGPL-3.0-or-later
-if (!function_exists('giglogadmin_populate_countries')) {
- function giglogadmin_populate_countries(): void
- {
- global $wpdb;
- //not removing yet as I haven't yet checked where else it might be called
- }
-}
-
if ( !function_exists( "giglog_register_db_tables") )
{
/**
- * @return void
+ * Registers the tables used by the GiglogAdmin plugin
*/
- function giglog_register_db_tables()
+ function giglog_register_db_tables() : void
{
global $wpdb;
- // Clean out obsolete tables if they exist.
- $wpdb->query("DROP TABLE IF EXISTS "
- . "wpg_bands, wpg_concertlogs, wpg_files, wpg_logchanges, wpg_pressstatus");
- /* not sure if DB version needed for now, leaving code here in case we decide to use it. To be removed if not
- $db_version = get_option('giglogadmin_db_version');
- if ($db_version == 8) {
- return;
- }
- */
+ $tables = [];
+ $tables[] =
+ "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}giglogadmin_venues` (
+ `id` int(11) NOT NULL AUTO_INCREMENT,
+ `wpgvenue_name` VARCHAR(500) NOT NULL,
+ `wpgvenue_city` VARCHAR(250) DEFAULT NULL,
+ `wpgvenue_address` VARCHAR(2000) DEFAULT NULL,
+ `wpgvenue_webpage` VARCHAR(200) DEFAULT NULL,
+ `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ `updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
+ PRIMARY KEY (`id`)
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
- $concerts_table =
- "CREATE TABLE IF NOT EXISTS `wpg_concerts` (
+ $tables[] =
+ "CREATE TABLE IF NOT EXISTS `{$wpdb->prefix}giglogadmin_concerts` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`wpgconcert_name` VARCHAR(2000) NOT NULL,
`venue` int(11) NOT NULL,
@@ -45,44 +40,19 @@ if ( !function_exists( "giglog_register_db_tables") )
`created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
`updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
- KEY `wpgconcert_venue` (`venue`)
+ CONSTRAINT `wpgconcert_venue`
+ FOREIGN KEY (`venue`)
+ REFERENCES `{$wpdb->prefix}giglogadmin_venues` (`id`) ON DELETE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
- $venues_table =
- "CREATE TABLE IF NOT EXISTS `wpg_venues` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `wpgvenue_name` VARCHAR(500) NOT NULL,
- `wpgvenue_city` VARCHAR(250) DEFAULT NULL,
- `wpgvenue_address` VARCHAR(2000) DEFAULT NULL,
- `wpgvenue_webpage` VARCHAR(200) DEFAULT NULL,
- `created` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- `updated` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB DEFAULT CHARSET=utf8;";
-
- $tables = array(
- $concerts_table,
- $venues_table);
-
foreach($tables as $tabledef) {
$result = $wpdb->query($tabledef);
if ($result === false) {
error_log("Registering table failed.");
}
}
-
-
- $wpdb->query(
- "ALTER TABLE `wpg_concerts`
- ADD CONSTRAINT `wpgconcert_venue`
- FOREIGN KEY (`venue`)
- REFERENCES `wpg_venues` (`id`) ON DELETE NO ACTION;");
-
- // update_option("giglogadmin_db_version", 8);
}
giglog_register_db_tables();
}
-
-?>