/**
  * get_instance method
  *
  * Get singleton instance of self (Ai1ec_Database_Applicator).
  *
  * @return Ai1ec_Database_Applicator Initialized instance of self
  */
 public static function get_instance()
 {
     if (!self::$_instance instanceof self) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
 /**
  * install_schema function
  *
  * This function sets up the database, and upgrades it if it is out of date.
  *
  * @return void
  **/
 function install_schema()
 {
     global $wpdb;
     // If existing DB version is not consistent with current plugin's version,
     // or does not exist, then create/update table structure using dbDelta().
     if (Ai1ec_Meta::get_option('ai1ec_db_version') != AI1EC_DB_VERSION) {
         Ai1ec_Database_Applicator::get_instance()->remove_instance_duplicates();
         $structures = array();
         $schema = new Ai1ec_Database_Schema();
         if (!$schema->upgrade(AI1EC_DB_VERSION)) {
             throw new Ai1ec_Database_Schema_Exception('Failed to perform schema upgrade');
         }
         unset($schema);
         // =======================
         // = Create table events =
         // =======================
         $table_name = $wpdb->prefix . 'ai1ec_events';
         $sql = "CREATE TABLE {$table_name} (\n\t\t\t\tpost_id bigint(20) NOT NULL,\n\t\t\t\tstart int(10) UNSIGNED NOT NULL,\n\t\t\t\tend int(10) UNSIGNED,\n\t\t\t\tallday tinyint(1) NOT NULL,\n\t\t\t\tinstant_event tinyint(1) NOT NULL DEFAULT 0,\n\t\t\t\trecurrence_rules longtext,\n\t\t\t\texception_rules longtext,\n\t\t\t\trecurrence_dates longtext,\n\t\t\t\texception_dates longtext,\n\t\t\t\tvenue varchar(255),\n\t\t\t\tcountry varchar(255),\n\t\t\t\taddress varchar(255),\n\t\t\t\tcity varchar(255),\n\t\t\t\tprovince varchar(255),\n\t\t\t\tpostal_code varchar(32),\n\t\t\t\tshow_map tinyint(1),\n\t\t\t\tcontact_name varchar(255),\n\t\t\t\tcontact_phone varchar(32),\n\t\t\t\tcontact_email varchar(128),\n\t\t\t\tcontact_url varchar(255),\n\t\t\t\tcost varchar(255),\n\t\t\t\tticket_url varchar(255),\n\t\t\t\tical_feed_url varchar(255),\n\t\t\t\tical_source_url varchar(255),\n\t\t\t\tical_organizer varchar(255),\n\t\t\t\tical_contact varchar(255),\n\t\t\t\tical_uid varchar(255),\n\t\t\t\tshow_coordinates tinyint(1),\n\t\t\t\tlatitude decimal(20,15),\n\t\t\t\tlongitude decimal(20,15),\n\t\t\t\tfacebook_eid bigint(20),\n\t\t\t\tfacebook_user bigint(20),\n\t\t\t\tfacebook_status varchar(1) NOT NULL DEFAULT '',\n\t\t\t\tforce_regenerate tinyint(1) NOT NULL DEFAULT 0,\n\t\t\t\tPRIMARY KEY  (post_id),\n\t\t\t\tKEY feed_source (ical_feed_url)\n\t\t\t) CHARACTER SET utf8;";
         // ==========================
         // = Create table instances =
         // ==========================
         $table_name = $wpdb->prefix . 'ai1ec_event_instances';
         $sql .= "CREATE TABLE {$table_name} (\n\t\t\t\tid bigint(20) NOT NULL AUTO_INCREMENT,\n\t\t\t\tpost_id bigint(20) NOT NULL,\n\t\t\t\tstart int(10) UNSIGNED NOT NULL,\n\t\t\t\tend int(10) UNSIGNED NOT NULL,\n\t\t\t\tPRIMARY KEY  (id),\n\t\t\t\tUNIQUE KEY evt_instance (post_id,start)\n\t\t\t) CHARACTER SET utf8;";
         // ================================
         // = Create table category colors =
         // ================================
         $table_name = $wpdb->prefix . 'ai1ec_event_category_colors';
         $sql .= "CREATE TABLE {$table_name} (\n\t\t\t\tterm_id bigint(20) NOT NULL,\n\t\t\t\tterm_color varchar(255) NOT NULL,\n\t\t\t\tPRIMARY KEY  (term_id)\n\t\t\t) CHARACTER SET utf8;";
         if (Ai1ec_Database::instance()->apply_delta($sql)) {
             update_option('ai1ec_db_version', AI1EC_DB_VERSION);
         } else {
             trigger_error('Failed to upgrade DB schema', E_USER_WARNING);
         }
     }
 }