Skip to content

Give each wordpress user their own database instance.#76

Closed
jzohrab wants to merge 1 commit intoHugoFara:devfrom
jzohrab:separate_lwt_database_for_each_wp_user
Closed

Give each wordpress user their own database instance.#76
jzohrab wants to merge 1 commit intoHugoFara:devfrom
jzohrab:separate_lwt_database_for_each_wp_user

Conversation

@jzohrab
Copy link

@jzohrab jzohrab commented Nov 13, 2022

Summary

Instead of having table name prefixes, we can just have LWT auto-create completely separate databases for each WP user, so each WP user can manage their own database. With this, we can get rid of $tbpref everywhere, and fix all the DB migrations.

This change is much simpler than creating user tables, and modifying all data structures and queries to support a multi-tenant db architecture!!!

Changes

Pretty minimal, actually. Essentially, every time a WP user tries to connect to the DB, the file inc/wp_logincheck.php just appends the user's WP ID to the $rootdbname specified in the connect file, and sets the new global $dbname which is used everywhere else.

Demo

I installed wordpress locally, and on the initial wp sample page I added a link to [lwt/wp_lwt_start.php](http://localhost:8888/wordpress/lwt/wp_lwt_start.php) as given in the docs. In WP admin, I created a few users. I then logged in to each user and clicked the link, and the databases were created. Each user can then install their own demo, create their own texts etc, and the $dbname is set correctly.

e.g., I created a new WP user, "newguy", which had user_id=4:

mysql> select ID, user_login from wp_users where user_login = 'newguy';
+----+------------+
| ID | user_login |
+----+------------+
|  4 | newguy     |
+----+------------+

Prior to newguy's wordpress login:

mysql> show databases;
+-----------------------+
| Database              |
+-----------------------+
...
| wordpress_lwt_1       |
| wordpress_lwt_2       |
| wordpress_lwt_3       |
+-----------------------+

newguy logs in and clicks the LWT link, and new db is created

mysql> show databases;
+-----------------------+
| Database              |
+-----------------------+
...
| wordpress_lwt_1       |
| wordpress_lwt_2       |
| wordpress_lwt_3       |
| wordpress_lwt_4       |       <<<<<<< Welcome, newguy
+-----------------------+

and everything works as expected. eg on the UI:

image

What this lets us do

  • Get rid of $tbpref everywhere, and a fair amount of trashy db update code.

** Docs needed for existing WP users to migrate**

For each WP user, the WP admin would need to do the following:

  • create a copy of the WP database with the proper name ($rootdbname + user id)
  • in that new db, drop all tables except those starting with the user id (and leave system table _lwt_general)
  • rename all tables, removing the user id prefix

(We might be able to write some helper scripts for this, but I personally would rely on the community for input. If they are savvy enough to set up the integrations etc for WP, then perhaps they can get into some other tasks too. We do the best we can, but this is still an open source project, and not a paid product!)

Instead of having table name prefixes, we can just have LWT auto-create
completely separate databases for each WP user, so each WP user can
manage their own database.
@jzohrab jzohrab closed this Nov 15, 2022
@jzohrab jzohrab deleted the separate_lwt_database_for_each_wp_user branch November 15, 2022 01:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant