Centralize db initialization

This commit is contained in:
Jeremy Dormitzer 2018-09-19 11:47:51 -04:00
parent e05c554af2
commit f0b424e4a6
No known key found for this signature in database
GPG Key ID: 04F17C0F5A32C320
10 changed files with 125 additions and 135 deletions

View File

@ -85,24 +85,4 @@ function create_local_activity( $activity ) {
}
return $activity;
}
function create_activities_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_activities (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
activitypub_id VARCHAR(255) UNIQUE NOT NULL,
activity TEXT NOT NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE UNIQUE INDEX ACTIVITIES_ACTIVITYPUB_ID_INDEX
ON pterotype_activitypub_activities (activitypub_id);
"
);
}
?>

View File

@ -61,20 +61,6 @@ function get_user_actor( $user ) {
return $actor;
}
function create_actors_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_actors(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
slug VARCHAR(64) UNIQUE NOT NULL,
type VARCHAR(64) NOT NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
}
/*
For every user in the WP instance, create a new actor row for that user
if it doesn't already exist

View File

@ -17,19 +17,4 @@ function create_block( $actor_id, $blocked_actor_url ) {
return new \WP_Error( 'db_error', __( 'Error inserting block row', 'pterotype' ) );
}
}
function create_blocks_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_blocks(
actor_id INT UNSIGNED NOT NULL,
blocked_actor_url TEXT NOT NULL,
FOREIGN KEY blocks_actor_fk(actor_id)
REFERENCES pterotype_activitypub_actors(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
}
?>

120
inc/db.php Normal file
View File

@ -0,0 +1,120 @@
<?php
/*
Poor man's migration system
*/
namespace db;
/*
It's okay to add new queries to this function, but don't ever delete queries.
*/
function run_migrations() {
$previous_version = get_option( 'pterotype_previously_migrated_version' );
if ( !$previously_migrated_version ) {
$previous_version = '0.0.0';
}
if ( version_compare( $previous_version, PTEROTYPE_VERSION, '<' ) ) {
if ( version_compare( $previous_version, '0.0.1', '<' ) ) {
migration_0_0_1();
}
}
update_option( 'pterotype_previously_migrated_version', PTEROTYPE_VERSION );
}
function migration_0_0_1() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_activities (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
activitypub_id VARCHAR(255) UNIQUE NOT NULL,
activity TEXT NOT NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE UNIQUE INDEX ACTIVITIES_ACTIVITYPUB_ID_INDEX
ON pterotype_activitypub_activities (activitypub_id);
"
);
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_objects (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
activitypub_id VARCHAR(255) UNIQUE NOT NULL,
object TEXT NOT NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE UNIQUE INDEX OBJECT_ACTIVITYPUB_ID_INDEX
ON pterotype_activitypub_objects (activitypub_id);
"
);
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_outbox (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
actor_id INT UNSIGNED NOT NULL,
activity_id INT UNSIGNED NOT NULL,
FOREIGN KEY outbox_activity_fk(activity_id)
REFERENCES pterotype_activitypub_activities(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_actors(
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
slug VARCHAR(64) UNIQUE NOT NULL,
type VARCHAR(64) NOT NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_likes (
actor_id INT UNSIGNED NOT NULL,
object_id INT UNSIGNED NOT NULL,
PRIMARY KEY (actor_id, object_id),
FOREIGN KEY likes_actor_fk(actor_id)
REFERENCES pterotype_activitypub_actors(id),
FOREIGN KEY likes_object_fk(object_id)
REFERENCES pterotype_activitypub_objects(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_following(
actor_id INT UNSIGNED NOT NULL,
object_id INT UNSIGNED NOT NULL,
state VARCHAR(64) NOT NULL,
PRIMARY KEY (actor_id, object_id),
FOREIGN KEY following_actor_fk(actor_id)
REFERENCES pterotype_activitypub_actors(id),
FOREIGN KEY following_object_fk(object_id)
REFERENCES pterotype_activitypub_objects(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE TABLE pterotype_activitypub_blocks(
actor_id INT UNSIGNED NOT NULL,
blocked_actor_url TEXT NOT NULL,
FOREIGN KEY blocks_actor_fk(actor_id)
REFERENCES pterotype_activitypub_actors(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
}
?>

View File

@ -14,23 +14,4 @@ function request_follow( $actor_id, $object_id ) {
)
);
}
function create_following_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_following(
actor_id INT UNSIGNED NOT NULL,
object_id INT UNSIGNED NOT NULL,
state VARCHAR(64) NOT NULL,
PRIMARY KEY (actor_id, object_id),
FOREIGN KEY following_actor_fk(actor_id)
REFERENCES pterotype_activitypub_actors(id),
FOREIGN KEY following_object_fk(object_id)
REFERENCES pterotype_activitypub_objects(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
}
?>

View File

@ -1,14 +1,9 @@
<?php
namespace init;
require_once plugin_dir_path( __FILE__ ) . '/outbox.php';
require_once plugin_dir_path( __FILE__ ) . '/api.php';
require_once plugin_dir_path( __FILE__ ) . '/objects.php';
require_once plugin_dir_path( __FILE__ ) . '/activities.php';
require_once plugin_dir_path( __FILE__ ) . '/actors.php';
require_once plugin_dir_path( __FILE__ ) . '/likes.php';
require_once plugin_dir_path( __FILE__ ) . '/following.php';
require_once plugin_dir_path( __FILE__ ) . '/blocks.php';
require_once plugin_dir_path( __FILE__ ) . '/db.php';
add_action( 'rest_api_init', function() {
\api\register_routes();
@ -20,13 +15,7 @@ add_action( 'user_register', function( $user_id ) {
} );
add_action( 'pterotype_init', function() {
\activities\create_activities_table();
\objects\create_object_table();
\outbox\create_outbox_table();
\actors\create_actors_table();
\db\run_migrations();
\actors\initialize_user_actors();
\likes\create_likes_table();
\following\create_following_table();
\blocks\create_blocks_table();
} );
?>

View File

@ -7,22 +7,4 @@ function create_like( $actor_id, $object_id ) {
'pterotype_activitypub_likes', array( 'actor_id' => $actor_id, 'object_id' => $object_id )
);
}
function create_likes_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_likes (
actor_id INT UNSIGNED NOT NULL,
object_id INT UNSIGNED NOT NULL,
PRIMARY KEY (actor_id, object_id),
FOREIGN KEY likes_actor_fk(actor_id)
REFERENCES pterotype_activitypub_actors(id),
FOREIGN KEY likes_object_fk(object_id)
REFERENCES pterotype_activitypub_objects(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
}
?>

View File

@ -142,24 +142,4 @@ function delete_object( $object ) {
}
return $res;
}
function create_object_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_objects (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
activitypub_id VARCHAR(255) UNIQUE NOT NULL,
object TEXT NOT NULL
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
$wpdb->query(
"
CREATE UNIQUE INDEX OBJECT_ACTIVITYPUB_ID_INDEX
ON pterotype_activitypub_objects (activitypub_id);
"
);
}
?>

View File

@ -146,20 +146,4 @@ function wrap_object_in_create( $actor_slug, $object ) {
);
return $activity;
}
function create_outbox_table() {
global $wpdb;
$wpdb->query(
"
CREATE TABLE IF NOT EXISTS pterotype_activitypub_outbox (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
actor_id INT UNSIGNED NOT NULL,
activity_id INT UNSIGNED NOT NULL,
FOREIGN KEY outbox_activity_fk(activity_id)
REFERENCES pterotype_activitypub_activities(id)
)
ENGINE=InnoDB DEFAULT CHARSET=utf8;
"
);
}
?>

View File

@ -4,7 +4,10 @@ Plugin Name: Pterotype
*/
require_once plugin_dir_path( __FILE__ ) . 'inc/init.php';
define( 'PTEROTYPE_VERSION' '0.0.1' );
function pterotype_init() {
update_option( 'pterotype_version', PTEROTYPE_VERSION );
do_action( 'pterotype_init' );
}