Commit 34db3e2d authored by Eric Windham's avatar Eric Windham Committed by GitHub

Merge pull request #3545 from wpninjas/db-tracker

Db tracker
parents 2c280df3 d8004fad
## Description
This document describes the architecture of the Ninja Forms database layer.
## Current Structure (1.1)
This section contains the current structure of the Ninja Forms data layer and is updated each time that structure changes. For legacy data, please see the individual version notes.
### Forms
_**nf3_forms**_ (Table of individual Forms)
* id (The unique ID of the Form)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* title (The displayable title of the Form)
* longtext
* COLLATE DATABASE_DEFAULT
* key (The administrative key of the Form)
* longtext
* COLLATE DATABASE_DEFAULT
* created_at (The date/time the Form was created)
* timestamp
* NOT NULL
* DEFAULT CURRENT_TIMESTAMP
* ON UPDATE CURRENT_TIMESTAMP
* updated_at (The date/time the Form was last updated)
* datetime
* views (The number of times the Form has been viewed)
* int(11)
* subs (The Form's number of lifetime Submissions)
* int(11)
_**nf3_form_meta**_ (Table of Settings assoicated with each Form)
* id (The unique ID of the Setting)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* parent_id (The Form ID this Setting is associated with)
* int(11)
* NOT NULL
* Foreign Key ON *nf3_forms* id
* key (The administrative key of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
* NOT NULL
* value (The value of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
### Fields
_**nf3_fields**_ (Table of individual Fields)
* id (The unique ID of the Field)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* label (The displayable label of the Field)
* longtext
* COLLATE DATABASE_DEFAULT
* key (The administrative key of the Field)
* longtext
* COLLATE DATABASE_DEFAULT
* type (The type of Field this record represents)
* longtext
* COLLATE DATABASE_DEFAULT
* parent_id (The Form ID this Field is associated with)
* int(11)
* NOT NULL
* Foreign Key ON *nf3_forms* id
* created_at (The date/time the Field was created)
* timestamp
* NOT NULL
* DEFAULT CURRENT_TIMESTAMP
* ON UPDATE CURRENT_TIMESTAMP
* updated_at (The date/time the Field was last updated)
* datetime
_**nf3_field_meta**_ (Table of Settings associated with each Field)
* id (The unique ID of the Setting)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* parent_id (The Field ID this Setting is associated with)
* int(11)
* NOT NULL
* Foreign Key ON *nf3_fields* id
* key (The administrative key of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
* NOT NULL
* value (The value of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
### Actions
_**nf3_actions**_ (Table of individual Actions)
* id (The unique ID of the Action)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primrary Key
* title (The displayable title of the Action)
* longtext
* COLLATE DATABASE_DEFAULT
* key (The administrative key of the Action)
* longtext
* COLLATE DATABASE_DEFAULT
* type (The type of Action this record represents)
* longtext
* COLLATE DATABASE_DEFAULT
* active (Whether or not the Action is active)
* tinyint(1)
* DEFAULT 1
* parent_id (The Form ID this Action is associated with)
* int(11)
* NOT NULL
* Foreign Key ON *nf3_forms* id
* created_at (The date/time the Action was created)
* timestamp
* NOT NULL
* DEFAULT CURRENT_TIMESTAMP
* ON UPDATE CURRENT_TIMESTAMP
* updated_at (The date/time the Action was last updated)
* datetime
_**nf3_action_meta**_ (Table of Settings associated with each Action)
* id (The unique ID of the Setting)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary KEY
* parent_id (The Action ID this Setting is associated with)
* int(11)
* NOT_NULL
* Foreign Key ON *nf3_actions* id
* key (The administrative key of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
* NOT NULL
* value (The value of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
### Objects
_**nf3_objects**_ (Table of non-structured Objects)
* id (The unique ID of the Object)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primrary Key
* type (The type of Object this record represents)
* longtext
* COLLATE DATABASE_DEFAULT
* title (The displayable title of the Object)
* longtext
* COLLATE DATABASE_DEFAULT
* created_at (The date/time the Object was created)
* timestamp
* NOT NULL
* DEFAULT CURRENT_TIMESTAMP
* ON UPDATE CURRENT_TIMESTAMP
* updated_at (The date/time the Object was last updated)
* datetime
_**nf3_object_meta**_ (Table of Settings associated with each Object)
* id (The unique ID of the Setting)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary KEY
* parent_id (The Object ID this Setting is associated with)
* int(11)
* NOT_NULL
* Foreign Key ON *nf3_objects* id
* key (The administrative key of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
* NOT NULL
* value (The value of the Setting)
* longtext
* COLLATE DATABASE_DEFAULT
_**nf3_relationships**_ (Table of Relationships between Objects)
* id (The unique ID of the Relationship)
* int(11)
* NOT NULL
* AUTO_INCREMENT
* Primary KEY
* child_id (The child Object ID this record is associated with)
* int(11)
* NOT_NULL
* Foreign Key ON *nf3_objects* id
* child_type (The type of Object represented by child_id)
* longtext
* COLLATE DATABASE_DEFAULT
* NOT NULL
* parent_id (The parent Object ID this record is associated with)
* int(11)
* NOT_NULL
* Foreign Key ON *nf3_objects* id
* parent_type (The type of Object represented by parent_id)
* longtext
* COLLATE DATABASE_DEFAULT
* NOT NULL
* created_at (The date/time the Relationship was created)
* timestamp
* NOT NULL
* DEFAULT CURRENT_TIMESTAMP
* ON UPDATE CURRENT_TIMESTAMP
* updated_at (The date/time the Relationship was last updated)
* datetime
_**options**_ (The default WordPress options table)
* option_name = 'nf_form_%' WHERE % = *nf3_forms* id
* option_value = Serialized JSON Object (The Ninja Forms Cache)
### Submissions
_**posts**_ (The default WordPress posts table)
* id (The unique ID of the Post)
* bigint(20)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* post_type = 'nf_sub'
_**postmeta**_ (The default WordPress postmeta table)
* meta_id (The unique ID of the Metadata)
* bigint(20)
* NOT NULL
* AUTO_INCREMENT
* Primary Key
* post_id (The Post ID this Metadata is associated with)
* bigint(20)
* NOT NULL
* DEFAULT 0
* Foreign Key ON *posts* id
* meta_key (The identifiable key by which this record is referenced)
* varchar(255)
* COLLATE DATABASE_DEFAULT
* meta_value (The value of this record)
* longtext
* COLLATE DATABASE_DEFAULT
### Upgrades
_**nf3_upgrades**_ (Table of Forms as they exist in the current structure and the stage of upgrade currently applied to each Form)
* id (The unique ID of the Form)
* int(11)
* NOT NULL
* Primary Key
* cache (The Ninja Forms cache as it was retrieved from the *options* table)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* stage (The current upgrade stage of the Form)
* int(11)
* NOT NULL
* DEFAULT 0
## Version 1.0
Defined initial structure for Ninja Forms data layer.
### Forms
_**nf3_forms**_ (Table of individual Forms)
......@@ -251,4 +523,23 @@ _**postmeta**_ (The default WordPress postmeta table)
* COLLATE DATABASE_DEFAULT
* meta_value (The value of this record)
* longtext
* COLLATE DATABASE_DEFAULT
\ No newline at end of file
* COLLATE DATABASE_DEFAULT
## Version 1.1
Defined tracker table for data structure updates.
### Upgrades
_**nf3_upgrades**_ (Table of Forms as they exist in the current structure and the stage of upgrade currently applied to each Form)
* id (The unique ID of the Form)
* int(11)
* NOT NULL
* Primary Key
* cache (The Ninja Forms cache as it was retrieved from the *options* table)
* longtext
* COLLATE utf8mb4_general_ci (fallback to utf8_general_ci)
* stage (The current upgrade stage of the Form)
* int(11)
* NOT NULL
* DEFAULT 0
\ No newline at end of file
......@@ -24,7 +24,16 @@ abstract class NF_Abstracts_Migration
public function charset_collate()
{
global $wpdb;
return $wpdb->get_charset_collate();
// If our mysql version is 5.5.3 or higher...
if ( version_compare( $wpdb->db_version(), '5.5.3', '>=' ) ) {
// We can use mb4.
return 'DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci';
} // Otherwise...
else {
// We use standard utf8.
return 'DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci';
}
}
public function _run()
......
......@@ -16,6 +16,7 @@ class NF_Database_Migrations
$this->migrations[ 'object_meta' ] = new NF_Database_Migrations_ObjectMeta();
$this->migrations[ 'relationships' ] = new NF_Database_Migrations_Relationships();
$this->migrations[ 'settings' ] = new NF_Database_Migrations_Settings();
$this->migrations[ 'upgrades' ] = new NF_Database_Migrations_Upgrades();
}
public function migrate()
......
<?php if ( ! defined( 'ABSPATH' ) ) exit;
class NF_Database_Migrations_Upgrades extends NF_Abstracts_Migration
{
public function __construct()
{
parent::__construct(
'nf3_upgrades',
'nf_migration_create_table_upgrades'
);
}
public function run()
{
$query = "CREATE TABLE IF NOT EXISTS {$this->table_name()} (
`id` INT(11) NOT NULL,
`cache` LONGTEXT,
`stage` INT(11) NOT NULL DEFAULT 0,
PRIMARY KEY ( id )
) {$this->charset_collate()};";
dbDelta( $query );
}
}
......@@ -199,6 +199,15 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
define( 'NF_PLUGIN_URL', self::$url );
}
$saved_version = get_option( 'ninja_forms_version' );
// If we have a recorded version...
// AND that version is less than our current version...
if ( $saved_version && version_compare( $saved_version, self::VERSION, '<' ) ) {
// We just upgraded the plugin.
$plugin_upgrade = true;
} else {
$plugin_upgrade = false;
}
update_option( 'ninja_forms_version', self::VERSION );
// If we've not recorded our db version...
if ( ! get_option( 'ninja_forms_db_version' ) ) {
......@@ -361,6 +370,13 @@ if( get_option( 'ninja_forms_load_deprecated', FALSE ) && ! ( isset( $_POST[ 'nf
require_once( self::$dir . 'includes/Integrations/EDD/EDD_SL_Plugin_Updater.php');
}
require_once self::$dir . 'includes/Integrations/EDD/class-extension-updater.php';
// If Ninja Forms was just upgraded...
if ( $plugin_upgrade ) {
// Ensure all of our tables have been defined.
$migrations = new NF_Database_Migrations();
$migrations->migrate();
}
}
add_action( 'admin_notices', array( self::$instance, 'admin_notices' ) );
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment