| 1 |
<?php |
| 2 |
namespace Depicter\Database; |
| 3 |
|
| 4 |
use Averta\WordPress\Database\Migration as BaseMigration; |
| 5 |
|
| 6 |
// no direct access allowed |
| 7 |
if ( ! defined('ABSPATH') ) { |
| 8 |
die(); |
| 9 |
} |
| 10 |
|
| 11 |
/** |
| 12 |
* Migration for custom tables |
| 13 |
* |
| 14 |
* @package Depicter\Database |
| 15 |
*/ |
| 16 |
class Migration extends BaseMigration { |
| 17 |
|
| 18 |
/** |
| 19 |
* Current tables migration version |
| 20 |
*/ |
| 21 |
const MIGRATION_VERSION = "0.4.9"; |
| 22 |
|
| 23 |
/** |
| 24 |
* Prefix for version option name |
| 25 |
*/ |
| 26 |
const VERSION_PREFIX = "depicter_"; |
| 27 |
|
| 28 |
/** |
| 29 |
* Table prefix |
| 30 |
*/ |
| 31 |
const TABLE_PREFIX = 'depicter_'; |
| 32 |
|
| 33 |
/** |
| 34 |
* Table names |
| 35 |
*/ |
| 36 |
protected $table_names = [ 'documents', 'options', 'meta', 'leads', 'lead_fields', 'queue_jobs', 'document_analytics' ]; |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* Create documents table |
| 41 |
* |
| 42 |
* @since 1.0 |
| 43 |
* @return null |
| 44 |
*/ |
| 45 |
protected function create_table_documents() { |
| 46 |
|
| 47 |
$sql_create_table = "CREATE TABLE {$this->documents} ( |
| 48 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 49 |
name text NOT NULL, |
| 50 |
slug varchar(100) NOT NULL, |
| 51 |
type varchar(50) NOT NULL DEFAULT 'custom', |
| 52 |
author bigint(20) unsigned NOT NULL DEFAULT '0', |
| 53 |
sections_count mediumint NOT NULL DEFAULT '0', |
| 54 |
created_at datetime DEFAULT NULL, |
| 55 |
modified_at datetime DEFAULT NULL, |
| 56 |
thumbnail varchar(255) NOT NULL, |
| 57 |
content longtext NOT NULL, |
| 58 |
status varchar(20) NOT NULL DEFAULT 'draft', |
| 59 |
parent bigint(20) unsigned NOT NULL DEFAULT '0', |
| 60 |
password varchar(255) NOT NULL DEFAULT '', |
| 61 |
PRIMARY KEY (id), |
| 62 |
KEY created_at (created_at), |
| 63 |
KEY slug (slug) |
| 64 |
) {$this->charset_collate()};\n"; |
| 65 |
|
| 66 |
$this->dbDelta( $sql_create_table ); |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
/** |
| 71 |
* Create options table |
| 72 |
* |
| 73 |
* @since 1.0 |
| 74 |
* @return null |
| 75 |
*/ |
| 76 |
public function create_table_options() { |
| 77 |
|
| 78 |
$sql_create_table = "CREATE TABLE {$this->options} ( |
| 79 |
id mediumint unsigned NOT NULL AUTO_INCREMENT, |
| 80 |
option_name varchar(191) NOT NULL DEFAULT '', |
| 81 |
option_value text NOT NULL DEFAULT '', |
| 82 |
PRIMARY KEY (id), |
| 83 |
UNIQUE KEY option_name (option_name) |
| 84 |
) {$this->charset_collate()};\n"; |
| 85 |
|
| 86 |
$this->dbDelta( $sql_create_table ); |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* Create meta table |
| 91 |
* |
| 92 |
* @since 1.0 |
| 93 |
* @return null |
| 94 |
*/ |
| 95 |
public function create_table_meta() { |
| 96 |
|
| 97 |
$sql_create_table = "CREATE TABLE {$this->meta} ( |
| 98 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 99 |
relation varchar(30) NOT NULL DEFAULT '', |
| 100 |
relation_id bigint(20) NOT NULL, |
| 101 |
meta_key varchar(30) NOT NULL DEFAULT '', |
| 102 |
meta_value text NOT NULL DEFAULT '', |
| 103 |
PRIMARY KEY (id) |
| 104 |
) {$this->charset_collate()};\n"; |
| 105 |
|
| 106 |
$this->dbDelta( $sql_create_table ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Create meta table |
| 111 |
* |
| 112 |
* @since 1.0 |
| 113 |
* @return null |
| 114 |
*/ |
| 115 |
public function create_table_leads() { |
| 116 |
|
| 117 |
$sql_create_table = "CREATE TABLE {$this->leads} ( |
| 118 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 119 |
source_id bigint(20) unsigned NOT NULL, |
| 120 |
content_id bigint(20) NOT NULL, |
| 121 |
content_name varchar(150) NOT NULL, |
| 122 |
created_at datetime DEFAULT NULL |
| 123 |
) {$this->charset_collate()};\n"; |
| 124 |
|
| 125 |
$this->dbDelta( $sql_create_table ); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Create meta table |
| 130 |
* |
| 131 |
* @since 1.0 |
| 132 |
* @return null |
| 133 |
*/ |
| 134 |
public function create_table_lead_fields() { |
| 135 |
|
| 136 |
$sql_create_table = "CREATE TABLE {$this->lead_fields} ( |
| 137 |
id bigint(20) unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY, |
| 138 |
lead_id bigint(20) unsigned NOT NULL, |
| 139 |
name varchar(255) NOT NULL, |
| 140 |
type varchar(50) NOT NULL DEFAULT '', |
| 141 |
value text NOT NULL DEFAULT '', |
| 142 |
created_at datetime DEFAULT NULL, |
| 143 |
updated_at datetime DEFAULT NULL |
| 144 |
) {$this->charset_collate()};\n"; |
| 145 |
|
| 146 |
$this->dbDelta( $sql_create_table ); |
| 147 |
} |
| 148 |
|
| 149 |
public function create_table_queue_jobs() { |
| 150 |
|
| 151 |
$sql_create_table = "CREATE TABLE {$this->queue_jobs} ( |
| 152 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 153 |
queue VARCHAR(50) NOT NULL DEFAULT 'default', |
| 154 |
payload LONGTEXT NOT NULL, |
| 155 |
attempts TINYINT UNSIGNED NOT NULL DEFAULT 0, |
| 156 |
reserved_at DATETIME DEFAULT NULL, |
| 157 |
available_at DATETIME NOT NULL, |
| 158 |
created_at DATETIME NOT NULL, |
| 159 |
status VARCHAR(20) NOT NULL DEFAULT 'pending', -- pending, processing, failed, completed |
| 160 |
last_error TEXT DEFAULT NULL, |
| 161 |
PRIMARY KEY (id), |
| 162 |
KEY status (status), |
| 163 |
KEY available_at (available_at), |
| 164 |
KEY queue (queue) |
| 165 |
) {$this->charset_collate()};\n"; |
| 166 |
|
| 167 |
$this->dbDelta($sql_create_table); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Create document_analytics_events table for tracking document impressions/submissions |
| 172 |
* |
| 173 |
* @since 1.0 |
| 174 |
* @return null |
| 175 |
*/ |
| 176 |
public function create_table_document_analytics() { |
| 177 |
|
| 178 |
$sql_create_table = "CREATE TABLE {$this->document_analytics} ( |
| 179 |
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 180 |
source_id BIGINT(20) UNSIGNED NOT NULL, |
| 181 |
event_type VARCHAR(20) NOT NULL, |
| 182 |
session_id CHAR(32) NOT NULL, |
| 183 |
user_id BIGINT(20) UNSIGNED NOT NULL DEFAULT 0, |
| 184 |
ip_address VARBINARY(16) NOT NULL, |
| 185 |
created_at DATETIME NOT NULL, |
| 186 |
PRIMARY KEY (id), |
| 187 |
KEY idx_document_analytics_performance (source_id, event_type, created_at), |
| 188 |
KEY idx_document_analytics_session (session_id, created_at), |
| 189 |
CONSTRAINT fk_document_analytics_source |
| 190 |
FOREIGN KEY (source_id) |
| 191 |
REFERENCES {$this->documents} (id) |
| 192 |
ON DELETE CASCADE |
| 193 |
) {$this->charset_collate()};\n"; |
| 194 |
|
| 195 |
$this->dbDelta($sql_create_table); |
| 196 |
} |
| 197 |
} |
| 198 |
|