1111 * repair and update these tables and metadata in case of database corruption.
1212 */
1313class WP_SQLite_Configurator {
14+ /**
15+ * The name of the database.
16+ *
17+ * @var string
18+ */
19+ private $ db_name ;
20+
1421 /**
1522 * The SQLite driver instance.
1623 *
@@ -35,13 +42,16 @@ class WP_SQLite_Configurator {
3542 /**
3643 * Constructor.
3744 *
45+ * @param string $db_name The name of the database.
3846 * @param WP_SQLite_Driver $driver The SQLite driver instance.
3947 * @param WP_SQLite_Information_Schema_Builder $schema_builder The information schema builder instance.
4048 */
4149 public function __construct (
50+ string $ db_name ,
4251 WP_SQLite_Driver $ driver ,
4352 WP_SQLite_Information_Schema_Builder $ schema_builder
4453 ) {
54+ $ this ->db_name = $ db_name ;
4555 $ this ->driver = $ driver ;
4656 $ this ->schema_builder = $ schema_builder ;
4757 $ this ->schema_reconstructor = new WP_SQLite_Information_Schema_Reconstructor (
@@ -62,6 +72,20 @@ public function ensure_database_configured(): void {
6272 if ( version_compare ( $ version , $ db_version ) > 0 ) {
6373 $ this ->configure_database ();
6474 }
75+
76+ // Ensure that the database name used in the current session corresponds
77+ // to the database name that is stored in the information schema tables.
78+ $ db_name = $ this ->driver ->get_saved_database_name ();
79+ if ( $ this ->db_name !== $ db_name ) {
80+ throw new WP_SQLite_Driver_Exception (
81+ $ this ->driver ,
82+ sprintf (
83+ "Incorrect database name. The database was created with name '%s', but '%s' is used in the current session. " ,
84+ $ db_name ,
85+ $ this ->db_name ,
86+ )
87+ );
88+ }
6589 }
6690
6791 /**
@@ -81,6 +105,7 @@ public function configure_database(): void {
81105 $ this ->schema_builder ->ensure_information_schema_tables ();
82106 $ this ->schema_reconstructor ->ensure_correct_information_schema ();
83107 $ this ->save_current_driver_version ();
108+ $ this ->save_current_database_name ();
84109 } catch ( Throwable $ e ) {
85110 $ this ->driver ->execute_sqlite_query ( 'ROLLBACK ' );
86111 throw $ e ;
@@ -103,6 +128,43 @@ private function ensure_global_variables_table(): void {
103128 );
104129 }
105130
131+ /**
132+ * Save the current database name.
133+ *
134+ * This method saves the current database name to the database.
135+ */
136+ public function save_current_database_name (): void {
137+ /*
138+ * If a record with an existing database name value is already stored in
139+ * the information schema, we need to use that value. This ensures correct
140+ * migration from older driver versions without the database name variable.
141+ */
142+ $ information_schema_db_name = $ this ->driver ->execute_sqlite_query (
143+ sprintf (
144+ 'SELECT table_schema FROM %s LIMIT 1 ' ,
145+ $ this ->driver ->get_connection ()->quote_identifier (
146+ $ this ->schema_builder ->get_table_name ( false , 'tables ' )
147+ )
148+ )
149+ )->fetchColumn ();
150+
151+ if ( false !== $ information_schema_db_name ) {
152+ $ db_name = $ information_schema_db_name ;
153+ } else {
154+ $ db_name = $ this ->db_name ;
155+ }
156+
157+ $ this ->driver ->execute_sqlite_query (
158+ sprintf (
159+ 'INSERT INTO %s (name, value) VALUES (?, ?) ON CONFLICT(name) DO UPDATE SET value = ? ' ,
160+ $ this ->driver ->get_connection ()->quote_identifier (
161+ WP_SQLite_Driver::GLOBAL_VARIABLES_TABLE_NAME
162+ )
163+ ),
164+ array ( WP_SQLite_Driver::DATABASE_NAME_VARIABLE_NAME , $ db_name )
165+ );
166+ }
167+
106168 /**
107169 * Save the current SQLite driver version.
108170 *
0 commit comments