Feat/tenant export import clean#482
Conversation
- Use setInternal() instead of $tenant->data = [] so stancl stores tenancy_db_name/username/password in the correct attributes - Create the MySQL user and grant privileges after CREATE DATABASE, matching the grants used by PermissionControlledMySQLDatabaseManager
Drop DatabaseTransactions and DB mocking — let DDL execute for real so the tests verify CREATE DATABASE, CREATE USER, and GRANT actually run. Clean up created databases and users manually after each test.
This comment was marked as outdated.
This comment was marked as outdated.
| DB::statement("CREATE DATABASE `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); | ||
| $grants = implode(', ', [ | ||
| 'ALTER', 'ALTER ROUTINE', 'CREATE', 'CREATE ROUTINE', 'CREATE TEMPORARY TABLES', 'CREATE VIEW', | ||
| 'DELETE', 'DROP', 'EVENT', 'EXECUTE', 'INDEX', 'INSERT', 'LOCK TABLES', 'REFERENCES', 'SELECT', | ||
| 'SHOW VIEW', 'TRIGGER', 'UPDATE', | ||
| ]); | ||
| DB::statement("CREATE USER `{$dbUser}`@`%` IDENTIFIED BY '{$dbPass}'"); | ||
| DB::statement("GRANT {$grants} ON `{$dbName}`.* TO `{$dbUser}`@`%`"); |
There was a problem hiding this comment.
idea: can we somehow get these statements in the export (DB dump), so we don't have to encode them in the code?
i'm just worried that if something similar comes in the future we'll forget to add it on all places..
There was a problem hiding this comment.
done, give it a look now. I'm saving it to a separate sql, setup.sql.
There was a problem hiding this comment.
@aivuk where? i don't see your change to this branch?
There was a problem hiding this comment.
sorry, I pushed to the wrong branch, feat/tenant-export-import. I cherry picked now and put it here.
There was a problem hiding this comment.
@aivuk ok tnx, but that's not what i meant with the original comment.. i think that SQL code should never be hardcoded in any way or form in this code.. because when we change how we setup those grants in infra repo, then we will forget to update the code here.. so what i'm suggesting is to make this export functionality include such commands rather than rely on maintaining them in:
- infra repo
- mariadb init scripts in BE for local docker-compose
- BE code for export/import of Tenant data
There was a problem hiding this comment.
Sorry, I don't get. This command is to be used to export and import for local development, we will need the SQL code and grant somewhere. Currently I tested it and it is working. What exactly do you want? Yes, if we change the way that the users access the database we will need to change this. If we migrate to postgres also. I can't see a solution that will not requires changes if we change everything that we can imagine
There was a problem hiding this comment.
@aivuk hey, sorry i missed this comment.. i can see that it works, but i think that wherever we can eliminate potential human error we should do that, and it's actually not so complex here, otherwise i wouldn't mention it.. i already wrote another comment below that you probably missed, but i'll copy/paste here the command that should also export all user privileges together with the database:
mariadb-dump --single-transaction --system=users --user=... --host=... --port=...| grep -E '(.`{{DB_USER}}`.@.(localhost|%|127.0.0.1|::1|172).)|^/\*'it's basically what we're doing in the backups script already in the beginning of this file: https://github.com/aula-app/ansible-role-aula/blob/main/templates/backup/backup_aula.sh.j2
There was a problem hiding this comment.
@aivuk I've tried implementing @nikola-maric-aula's proposed solution, but
- since parameters can be overridden on import, there were nasty replaces like
s/TO .$user./TO '{{DB_USER}}'/, and similarly forON .$database., andIDENTIFIED BY .$password.... …|^/\*catches a lot of unrelated comments, at least on my system/dev setup. something likegrep -B1won't cut it either.- for a second I thought about
SELECT * FROM sys.userbut I think that's less portable
⇒ the code got pretty nasty. Instead, I'd simply cross-reference the two only places (unless I missed somewhere) where they are spelled out.
Good enough?
| "CREATE USER `{{DB_USER}}`@`%` IDENTIFIED BY '{{DB_PASS}}';", | ||
| "GRANT {$grants} ON `{{DB_NAME}}`.* TO `{{DB_USER}}`@`%`;", |
There was a problem hiding this comment.
@aivuk something like this should capture all the lines including that user (create user, grants) and all the lines that are mysql/mariadb comment instructions:
mariadb-dump --single-transaction --system=users --user=... --host=... --port=...| grep -E '(.`{{DB_USER}}`.@.(localhost|%|127.0.0.1|::1|172).)|^/\*'
Context
Fix https://github.com/aula-app/aula-manager/issues/42
A tenant can be now exported with: php artisan tenant:export --code
This generates a .tar.gz file containing a json with the instance details and a sql for the instance database.
The tenant can be import with: php artisan tenant:import --code=NEW-CODE --name=NEW-NAME ./tenant_INSTANCE_CODE_20260430_141745.tar.gz