Skip to main content
Home
Drupal life hacks

Main navigation

  • Drupal
  • React
  • WP
  • Contact
  • About
User account menu
  • Log in

Breadcrumb

  1. Home

Extended Review of Backward Compatibility Questions When Upgrading to Drupal 11

By admin, 7 August, 2024

When upgrading to Drupal 11, it's crucial to consider the impact on backward compatibility, especially when upgrading to PHP 8.3 and MySQL/Percona 8.0. Here's an extended review of the key areas of concern:

1. PHP 8.3 Compatibility

a. Deprecated Functions and Features

- Issue: PHP 8.3 removes many functions and features that were deprecated in previous versions. If your existing Drupal site or modules rely on these deprecated functions, they will break.
- Example: The each() function, which is often used in loops, is deprecated in PHP 7.2 and removed in PHP 8.0.
- Solution: Replace instances of each() with foreach loops or other alternatives.

 // Deprecated
 while (list($key, $value) = each($array)) {
     // ...
 }
// Compatible
 foreach ($array as $key => $value) {
     // ...
 }

b. Strict Typing and Error Handling

- Issue: PHP 8.3 has stricter type enforcement and error handling, which can cause issues if your code does not conform to these stricter standards.
- Example: In PHP 8.3, attempting to access an undefined array key will throw a warning, whereas in older versions it would silently return null.
- Solution: Ensure all array accesses are checked for existence before use.

 // Potential issue in PHP 8.3
 $value = $array['key'];
// Compatible
 $value = $array['key'] ?? null;

c. New Language Features

- Issue: New features in PHP 8.3, such as readonly properties, are not recognized by older PHP versions, making your code non-backward compatible.
- Example: Using readonly properties in your code will cause syntax errors in PHP versions below 8.1.
- Solution: Avoid using new features if you need to maintain backward compatibility, or conditionally use them.

 // PHP 8.1 and above
 class Example {
     public readonly string $property;
    public function __construct(string $property) {
         $this->property = $property;
     }
 }
// Older versions: do not use readonly properties

 

2. MySQL/Percona 8.0 Compatibility

a. SQL Mode Changes

- Issue: MySQL 8.0 introduces changes in SQL modes that affect how queries are parsed and executed.
- Example: The ONLY_FULL_GROUP_BY mode is enabled by default in MySQL 8.0, which requires that GROUP BY queries specify all selected columns.
- Solution: Update your queries to conform to the new SQL mode requirements.

 sql
 -- Non-compliant in MySQL 8.0
 SELECT id, name FROM users GROUP BY id;
-- Compliant
 SELECT id, name FROM users GROUP BY id, name;

b. Character Set and Collation Changes

- Issue: MySQL 8.0 changes the default character set to utf8mb4 and collation to utf8mb4_0900_ai_ci, which can affect database interactions if your tables use different settings.
- Example: Older databases using utf8 may encounter issues with character encoding.
- Solution: Convert your tables to utf8mb4 to ensure compatibility.

 sql
 ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

c. Removed Features

- Issue: Certain features and syntax supported in earlier versions of MySQL are deprecated or removed in MySQL 8.0.
- Example: The TIMESTAMP default value of 0000-00-00 00:00:00 is no longer valid.
- Solution: Update database schemas to use valid defaults.

 sql
 -- Invalid in MySQL 8.0
 CREATE TABLE example (
     created_at TIMESTAMP DEFAULT '0000-00-00 00:00:00'
 );
-- Valid
 CREATE TABLE example (
     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );

 

3. Specific Considerations for Drupal

a. Module and Theme Compatibility

- Issue: Contributed modules and themes developed for older versions of Drupal may not be compatible with the latest PHP and MySQL versions.
- Example: A module using the deprecated each() function will not work with PHP 8.3.
- Solution: Check for updates and patches for your modules and themes, or manually update them to ensure compatibility.

b. Custom Code

- Issue: Any custom code written for your Drupal site needs to be reviewed and potentially updated for compatibility with PHP 8.3 and MySQL 8.0.
- Example: Custom scripts using deprecated PHP functions or MySQL features will fail after the upgrade.
- Solution: Conduct a code audit and refactor any incompatible code.

c. Drupal Core Updates

- Issue: Older versions of Drupal core may not be compatible with PHP 8.3 and MySQL 8.0.
- Example: Drupal 7 core will not work with PHP 8.3 without significant modifications.
- Solution: Upgrade to the latest version of Drupal core, which is Drupal 11 in this case, to ensure full compatibility.

 

Steps to Mitigate Compatibility Issues

1. Review and Update Code:
  - Conduct a thorough review of your custom code, modules, and themes. Update any deprecated or incompatible functions and features.

2. Test in a Staging Environment:
  - Set up a staging environment that mirrors your production setup. Test the entire site after upgrading PHP and MySQL to identify and fix any issues before making the changes live.

3. Use Compatibility Layers or Polyfills:
  - In some cases, compatibility layers or polyfills can be used to maintain compatibility with older versions of PHP or MySQL. However, this is generally a temporary solution.

4. Consult Documentation:
  - Refer to the official documentation for Drupal, PHP, and MySQL to understand the specific compatibility requirements and recommended upgrade paths.

5. Seek Professional Help:
  - If the upgrade process seems complex or if there are critical compatibility issues, consider seeking help from experienced developers who are familiar with Drupal, PHP, and MySQL upgrades.

By understanding these potential issues and taking proactive steps to address them, you can ensure a smooth upgrade process and maintain backward compatibility with previous versions of Drupal.

 

Added:

 

Preparing for a MySQL Version Upgrade on a Hosting Account

When your hosting provider upgrades the MySQL version for all databases in your account at once, it’s essential to take specific steps to ensure a smooth transition. This comprehensive guide will help you prepare for such an upgrade, addressing potential issues and ensuring that your Drupal site, as well as any other applications, remain functional.

 

Steps to Prepare for the MySQL Upgrade

1. Backup All Databases:
  - Before the upgrade, take complete backups of all databases in your hosting account. This ensures you have a recovery point if anything goes wrong during the migration.

  mysqldump -u [username] -p[password] --all-databases > all_databases_backup.sql

2. Identify Dependencies:
  - List all applications, including your Drupal site and any other CMS or custom applications, that depend on the MySQL databases. Ensure that you check for compatibility with MySQL 8.0.

3. Review MySQL 8.0 Changes:
  - Study the MySQL 8.0 release notes to understand the key changes and deprecated features that might affect your databases and applications.

4. Check Application Compatibility:
  - Drupal Compatibility: Ensure that your Drupal version is compatible with MySQL 8.0. If you are using Drupal 10 or upgrading to Drupal 11, you should be compatible.
  - Module Compatibility: Verify that all contributed and custom modules are compatible with MySQL 8.0.
  - Other Applications: Ensure other applications or scripts on your hosting account are compatible with MySQL 8.0.

5. Update SQL Queries and Code:
  - Review and update any custom SQL queries and code to comply with MySQL 8.0 standards.
  - Example: Handling ONLY_FULL_GROUP_BY Mode

    sql
    -- Non-compliant
    SELECT id, name FROM users GROUP BY id;
   -- Compliant
    SELECT id, name FROM users GROUP BY id, name;

6. Upgrade Staging Environment:
  - Set up a staging environment that mirrors your production setup. Upgrade this environment to MySQL 8.0 and thoroughly test all applications to identify and fix any issues.

7. Update Database Schemas:
  - Ensure that your database schemas are compatible with MySQL 8.0. Convert tables to the utf8mb4 character set if necessary.

  sql
  ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

 

Detailed Considerations

a. SQL Mode Changes

- Issue: MySQL 8.0 has ONLY_FULL_GROUP_BY mode enabled by default.
- Solution: Ensure all GROUP BY queries are compliant.

 sql
 -- Non-compliant in MySQL 8.0
 SELECT id, name FROM users GROUP BY id;
-- Compliant
 SELECT id, name FROM users GROUP BY id, name;

b. Character Set and Collation

- Issue: Default character set changed to utf8mb4 and collation to utf8mb4_0900_ai_ci.
- Solution: Convert tables and columns to match the new defaults.

 sql
 ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;

c. Removed Features

- Issue: Some features deprecated in earlier versions are removed.
- Solution: Update schemas and code to remove reliance on deprecated features.

 sql
 -- Invalid in MySQL 8.0
 CREATE TABLE example (
     created_at TIMESTAMP DEFAULT '0000-00-00 00:00:00'
 );
-- Valid
 CREATE TABLE example (
     created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
 );

 

Testing and Validation

1. Functional Testing:
  - Test all functionalities of your Drupal site and other applications to ensure they work correctly with MySQL 8.0. Pay particular attention to data queries, form submissions, and content management operations.

2. Performance Testing:
  - Monitor performance to ensure that the upgrade to MySQL 8.0 does not adversely affect the speed and responsiveness of your applications.

3. Error Logging and Debugging:
  - Enable detailed error logging during the testing phase to catch and debug any issues that arise due to the upgrade.

 

Post-Upgrade Steps

1. Monitor Applications:
  - After the upgrade, closely monitor your applications for any issues. Pay attention to error logs, performance metrics, and user feedback.

2. Regular Backups:
  - Continue to perform regular backups of your databases to ensure data integrity and recoverability.

3. Stay Informed:
  - Keep abreast of updates and patches for MySQL, Drupal, and any other applications to ensure continued compatibility and security.

 

Documentation and Resources

- MySQL 8.0 Release Notes: MySQL 8.0 Release Notes
- Drupal 11 Documentation: Upgrading to Drupal 11
- Drupal Module Repository: Check for module updates and patches on Drupal.org.

By following these steps, you can ensure a smooth transition to MySQL 8.0 on your hosting account, maintaining compatibility and functionality for all your applications, including your Drupal site.

 

PS. Before the joy of releasing Drupal version 11, if all your sites are not only Drupal, but also WordPress and others, and are located on a hosting that uses a different version of MySQL than 8.0, think very carefully and weigh the risks before updating the version. Perhaps for version 11 of Drupal there was a need to increase the version of the databases, but this can become and is becoming a problem for easy upgrade of Drupal versions.

Tags

  • #Drupal Planet

Comments

About text formats

Restricted HTML

  • Allowed HTML tags: <a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id>
  • Lines and paragraphs break automatically.
  • Web page addresses and email addresses turn into links automatically.
Powered by Drupal