Fix installation in SQLite with a table prefix

Created on 13 February 2017, almost 8 years ago
Updated 24 January 2024, 10 months ago

Problem/Motivation

Installing Drupal 11-dev website in SQLite with a table prefix creates 2 db files:

  • the correct one (e.g., .ht.sqlite), but it's empty (with only the sqlite_master table),
  • and the other one with the prefix actually appended to a file name (e.g., .ht.sqlite-prefix_), with all the tables originally named (not prefixed).
-rw-r--r-- 1 m m    4096 Jan 19 10:38 .ht.sqlite
-rw-r--r-- 1 m m 2498560 Jan 19 10:45 .ht.sqlite-prefix_

A snippet from the auto-generated settings.php is correct:

$databases['default']['default'] = array (
  'database' => 'sites/default/files/.ht.sqlite',
  'prefix' => 'prefix_',
  'driver' => 'sqlite',
  'namespace' => 'Drupal\\sqlite\\Driver\\Database\\sqlite',
  'autoload' => 'core/modules/sqlite/src/Driver/Database/sqlite/',
);

The site works, no errors reported by the installation process and no errors on the admin/reports/dblog page. But the site uses incorrect DB file.

In Drupal versions < 11 the installation process fails with errors (according to the original issue report and comments).

Steps to reproduce

During the installation process, on the 'Set up database' step, select SQLite database type, in advanced options set the table name prefix. Continue installation.

Proposed resolution

Fix and allow table prefixes in SQLite DB.

Merge request link

Remaining tasks

User interface changes

Shouldn't be any.

API changes

Data model changes

Release notes snippet

Original report

@cozzamara:

Hi I found an issue on installation process. If I use a SQLite database and set table prefix option, the installation fails because the table prefix is used as database name suffix.

🐛 Bug report
Status

Active

Version

11.0 🔥

Component
SQLite driver 

Last updated 2 months ago

No maintainer
Created by

🇮🇹Italy cozzamara

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • 🇺🇸United States hyperlogos

    This still seems to be a thing in D10?

    Problem is, I need to use a prefix because I want to use views_database_connector

    I guess I can't use sqlite?

  • Status changed to Active over 1 year ago
  • 🇸🇰Slovakia poker10

    Tested this on clean Drupal 10.1.x-dev minimal installation profile and if you select SQLite and enter a prefix, there is an error:

    Failed to connect to database. The database engine reports the following message: SQLSTATE[HY000]: General error: 14 unable to open database: sites/default/files/.ht.sqlite-minimal_: ATTACH DATABASE :database_file AS :database; Array ( [:database_file] => sites/default/files/.ht.sqlite-minimal_ [:database] => minimal_ ) .
    - Does the database file exist?
    - Does web server have permission to write to the database file?
    - Does the web server have permission to write to the directory the database file should be created in?

    Database is created with the name sites/default/files/.ht.sqlite, but the installer is looking for sites/default/files/.ht.sqlite-minimal_

    Therefore switching version to D10, thanks!

  • 🇺🇦Ukraine pingwin4eg Zaporizhia 🇺🇦

    In D11 the situation is a bit different, but the main problem is the same. Updated IS.

  • 🇺🇦Ukraine pingwin4eg Zaporizhia 🇺🇦
  • 🇺🇦Ukraine pingwin4eg Zaporizhia 🇺🇦
  • 🇺🇦Ukraine pingwin4eg Zaporizhia 🇺🇦

    I found out that the SQLite driver uses database attachment concept instead of actual table prefixes. Not sure whether this is OK or not, and why it was implemented this way.

    If it's OK, then the settings.php documentation needs to mention that, the prefix is better be prepended to a DB file name or appended to a base name before the file extension, and reported errors need to be fixed.

  • 🇺🇦Ukraine pingwin4eg Zaporizhia 🇺🇦

    Drush error appears even without a table prefix, removing it from IS. It was caused by xdebug.

Production build 0.71.5 2024