Skip to content

Feat/tenant export import clean#482

Open
aivuk wants to merge 9 commits into
mainfrom
feat/tenant-export-import-clean
Open

Feat/tenant export import clean#482
aivuk wants to merge 9 commits into
mainfrom
feat/tenant-export-import-clean

Conversation

@aivuk

@aivuk aivuk commented Apr 30, 2026

Copy link
Copy Markdown
Member

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

  • Tested manually
  • GitHub issue linked
  • Changelist updated
  • Backward and forward compatible with aula-frontend/releases
  • Doesn't need update in the database
  • Must be deployed ASAP (HOTFIX)
  • Needs update of docs.aula.de (repo)

aivuk added 6 commits April 30, 2026 16:47
- 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.
@aivuk
aivuk requested a review from nikola-maric-aula April 30, 2026 14:57
@nikola-maric-aula

This comment was marked as outdated.

Comment thread app/Console/Commands/ImportTenant.php Outdated
Comment on lines +132 to +139
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}`@`%`");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done, give it a look now. I'm saving it to a separate sql, setup.sql.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@aivuk where? i don't see your change to this branch?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, I pushed to the wrong branch, feat/tenant-export-import. I cherry picked now and put it here.

@nikola-maric-aula nikola-maric-aula May 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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:

  1. infra repo
  2. mariadb init scripts in BE for local docker-compose
  3. BE code for export/import of Tenant data

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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 for ON .$database., and IDENTIFIED BY .$password....
  • …|^/\* catches a lot of unrelated comments, at least on my system/dev setup. something like grep -B1 won't cut it either.
  • for a second I thought about SELECT * FROM sys.user but 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?

Comment on lines +81 to +82
"CREATE USER `{{DB_USER}}`@`%` IDENTIFIED BY '{{DB_PASS}}';",
"GRANT {$grants} ON `{{DB_NAME}}`.* TO `{{DB_USER}}`@`%`;",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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).)|^/\*'

@leonard-haas
leonard-haas requested a review from bikubi July 20, 2026 12:40
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.

3 participants