Skip to content

Commit

Permalink
Fixed: Sqlite database skip ALTER QUERY
Browse files Browse the repository at this point in the history
  • Loading branch information
randhirexpresstech committed May 31, 2024
1 parent c1818f3 commit 6a76d38
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 64 deletions.
15 changes: 13 additions & 2 deletions mlw_quizmaster2.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,17 @@ public function __construct() {
$this->add_hooks();
}

/**
* Check if database is sql lite
*
* @since 9.0.2
*
* @return boolean database is sqlite or not
*/
public function is_sqlite_db() {
return ( ( defined( 'DATABASE_TYPE' ) && 'sqlite' === DATABASE_TYPE ) || ( defined( 'DB_ENGINE' ) && 'sqlite' === DB_ENGINE ) );
}

/**
* Check admin capabilities.
*
Expand Down Expand Up @@ -200,7 +211,7 @@ public function get_failed_alter_table_queries() {
*/
public function wpdb_alter_table_query( $query ) {
// Check if admin or empty query.
if ( empty( $query ) || ! function_exists( 'is_admin' ) || ! is_admin() ) {
if ( empty( $query ) || ! function_exists( 'is_admin' ) || ! is_admin() || $this->is_sqlite_db() ) {
return false;
}

Expand Down Expand Up @@ -831,7 +842,7 @@ public function qsm_admin_notices() {

// Get failed alter table query list.
$failed_queries = $this->get_failed_alter_table_queries();
if ( ! empty( $failed_queries ) && 0 < count( $failed_queries ) ) {
if ( ! $this->is_sqlite_db() && ! empty( $failed_queries ) && 0 < count( $failed_queries ) ) {
?>
<div class="notice notice-warning is-dismissible qmn-database-user-incorrect-permission">
<p><?php esc_html_e( "It seems your database user doesn't have permission to ALTER TABLE. Please ensure the necessary permissions are in place or contact your hosting provider.", "quiz-master-next" ); ?> <a href="#" qmnnonce="<?php echo wp_create_nonce( 'qmn_check_db' ); ?>" class="button button-primary check-db-fix-btn" ><?php esc_html_e( "Check If Already Fixed", "quiz-master-next" ); ?></a> </p>
Expand Down
22 changes: 14 additions & 8 deletions php/admin/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ function link_featured_image( $quiz_id ) {
*/
function qsm_add_author_column_in_db() {
global $mlwQuizMasterNext;

// return if sqlite db
if ( $mlwQuizMasterNext->is_sqlite_db() ) {
return;
}

if ( 1 !== intval( get_option( 'qsm_update_db_column', '' ) ) ) {

global $wpdb;
Expand All @@ -78,7 +84,7 @@ function qsm_add_author_column_in_db() {
);

if ( empty( $table_col_obj ) ) {
$mlwQuizMasterNext->wpdb_alter_table_query( 'ALTER TABLE ' . $table . ' ADD ' . $col_name . ' ' . $col_def );
$wpdb->query( 'ALTER TABLE ' . $table . ' ADD ' . $col_name . ' ' . $col_def );
}
}
}
Expand All @@ -96,7 +102,7 @@ function qsm_add_author_column_in_db() {
)
);
if ( empty( $table_result_col_obj ) ) {
if ( $mlwQuizMasterNext->wpdb_alter_table_query( "ALTER TABLE $result_table_name ADD form_type INT NOT NULL" ) ) {
if ( $wpdb->query( "ALTER TABLE $result_table_name ADD form_type INT NOT NULL" ) ) {
update_option( 'qsm_update_result_db_column', '1' );
} else {
$mlwQuizMasterNext->log_manager->add( 'Error Creating Column form_type in' . $result_table_name, "Tried {$wpdb->last_query} but got {$wpdb->last_error}.", 0, 'error' );
Expand All @@ -118,7 +124,7 @@ function qsm_add_author_column_in_db() {
)
);
if ( ! empty( $table_quiz_col_obj ) ) {
if ( $mlwQuizMasterNext->wpdb_alter_table_query( "ALTER TABLE $quiz_table_name CHANGE `system` `quiz_system` INT(11) NOT NULL;" ) ) {
if ( $wpdb->query( "ALTER TABLE $quiz_table_name CHANGE `system` `quiz_system` INT(11) NOT NULL;" ) ) {
update_option( 'qsm_update_quiz_db_column', '1' );
} else {
$mlwQuizMasterNext->log_manager->add( 'Error Changing Columns system,quiz_system in' . $quiz_table_name, "Tried {$wpdb->last_query} but got {$wpdb->last_error}.", 0, 'error' );
Expand All @@ -140,7 +146,7 @@ function qsm_add_author_column_in_db() {
), ARRAY_A
);
if ( isset( $table_quiz_result_obj['DATA_TYPE'] ) && 'text' === $table_quiz_result_obj['DATA_TYPE'] ) {
if ( $mlwQuizMasterNext->wpdb_alter_table_query( "ALTER TABLE $result_table_name CHANGE `quiz_results` `quiz_results` LONGTEXT;" ) ) {
if ( $wpdb->query( "ALTER TABLE $result_table_name CHANGE `quiz_results` `quiz_results` LONGTEXT;" ) ) {
update_option( 'qsm_update_result_db_column_datatype', '1' );
} else {
$mlwQuizMasterNext->log_manager->add( 'Error Changing Columns quiz_results in' . $result_table_name, "Tried {$wpdb->last_query} but got {$wpdb->last_error}.", 0, 'error' );
Expand All @@ -163,7 +169,7 @@ function qsm_add_author_column_in_db() {
)
);
if ( empty( $table_result_col_obj ) ) {
if ( $mlwQuizMasterNext->wpdb_alter_table_query( "ALTER TABLE $question_table_name ADD deleted_question_bank INT NOT NULL" ) ) {
if ( $wpdb->query( "ALTER TABLE $question_table_name ADD deleted_question_bank INT NOT NULL" ) ) {
$inc_val = $total_count_val + 1;
update_option( 'qsm_add_new_column_question_table_table', $inc_val );
} else {
Expand All @@ -185,7 +191,7 @@ function qsm_add_author_column_in_db() {
)
);
if ( empty( $table_result_col_obj ) ) {
if ( $mlwQuizMasterNext->wpdb_alter_table_query( "ALTER TABLE $result_table_name ADD page_url varchar(255) NOT NULL" ) ) {
if ( $wpdb->query( "ALTER TABLE $result_table_name ADD page_url varchar(255) NOT NULL" ) ) {
update_option( 'qsm_update_result_db_column_page_url', '1' );
} else {
$error = $wpdb->last_error;
Expand All @@ -208,7 +214,7 @@ function qsm_add_author_column_in_db() {
)
);
if ( empty( $table_result_col_obj ) ) {
if ( $mlwQuizMasterNext->wpdb_alter_table_query( "ALTER TABLE $result_table_name ADD page_name varchar(255) NOT NULL" ) ) {
if ( $wpdb->query( "ALTER TABLE $result_table_name ADD page_name varchar(255) NOT NULL" ) ) {
update_option( 'qsm_update_result_db_column_page_name', '1' );
} else {
$mlwQuizMasterNext->log_manager->add( 'Error Creating Column page_name in' . $result_table_name, "Tried {$wpdb->last_query} but got {$wpdb->last_error}.", 0, 'error' );
Expand All @@ -235,7 +241,7 @@ function qsm_add_author_column_in_db() {

foreach ( $tables_to_convert as $table ) {
$query = "ALTER TABLE $table CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;";
$result = $mlwQuizMasterNext->wpdb_alter_table_query($query);
$result = $wpdb->query($query);

if ( ! $result ) {
$success = false;
Expand Down
Loading

0 comments on commit 6a76d38

Please sign in to comment.