From 86a6a413edb698d7e80ceb077132f3abac1780fa Mon Sep 17 00:00:00 2001 From: solocla Date: Sat, 3 Oct 2026 14:11:56 +0200 Subject: [PATCH] multischool migration --- .../20261003120418_create_schools_table.php | 52 ++++++++++++ ...0261003120419_create_school_user_table.php | 40 ++++++++++ ...03120420_add_idschool_to_domain_tables.php | 79 +++++++++++++++++++ db/migrations/AddIdschoolToDomainTables.php | 79 +++++++++++++++++++ db/migrations/CreateSchoolUserTable.php | 40 ++++++++++ db/migrations/CreateSchoolsTable.php | 52 ++++++++++++ 6 files changed, 342 insertions(+) create mode 100644 db/migrations/20261003120418_create_schools_table.php create mode 100644 db/migrations/20261003120419_create_school_user_table.php create mode 100644 db/migrations/20261003120420_add_idschool_to_domain_tables.php create mode 100644 db/migrations/AddIdschoolToDomainTables.php create mode 100644 db/migrations/CreateSchoolUserTable.php create mode 100644 db/migrations/CreateSchoolsTable.php diff --git a/db/migrations/20261003120418_create_schools_table.php b/db/migrations/20261003120418_create_schools_table.php new file mode 100644 index 0000000..3a6ca15 --- /dev/null +++ b/db/migrations/20261003120418_create_schools_table.php @@ -0,0 +1,52 @@ +table('schools', [ + 'id' => false, + 'primary_key' => ['idschool'], + 'engine' => 'InnoDB', + 'collation' => 'utf8mb4_unicode_ci', + ]) + ->addColumn('idschool', 'integer', ['signed' => false, 'identity' => true]) + ->addColumn('name', 'string', ['limit' => 191]) + ->addColumn('slug', 'string', ['limit' => 100, 'comment' => 'usato per URL/sottodominio/widget']) + ->addColumn('email', 'string', ['limit' => 191, 'null' => true]) + ->addColumn('phone', 'string', ['limit' => 40, 'null' => true]) + ->addColumn('address', 'string', ['limit' => 255, 'null' => true]) + ->addColumn('city', 'string', ['limit' => 100, 'null' => true]) + ->addColumn('postcode', 'string', ['limit' => 20, 'null' => true]) + ->addColumn('country', 'string', ['limit' => 2, 'default' => 'IT']) + ->addColumn('vat_number', 'string', ['limit' => 30, 'null' => true]) + ->addColumn('logo', 'string', ['limit' => 191, 'null' => true]) + ->addColumn('timezone', 'string', ['limit' => 50, 'default' => 'Europe/Rome']) + ->addColumn('locale', 'string', ['limit' => 5, 'default' => 'it']) + ->addColumn('currency', 'string', ['limit' => 3, 'default' => 'EUR']) + ->addColumn('status', 'string', ['limit' => 20, 'default' => 'active', 'comment' => 'active|suspended|trial']) + ->addColumn('created_at', 'timestamp', ['null' => true, 'default' => 'CURRENT_TIMESTAMP']) + ->addColumn('updated_at', 'timestamp', ['null' => true, 'default' => null, 'update' => 'CURRENT_TIMESTAMP']) + ->addIndex(['slug'], ['unique' => true, 'name' => 'schools_slug_unique']) + ->addIndex(['status'], ['name' => 'schools_status_index']) + ->create(); + + // Seregno = scuola 1 (dati da completare poi dal pannello) + $this->table('schools')->insert([ + 'idschool' => 1, + 'name' => 'Scuola Yoga Seregno', + 'slug' => 'seregno', + 'city' => 'Seregno', + 'country' => 'IT', + ])->saveData(); + } + + public function down(): void + { + $this->table('schools')->drop()->save(); + } +} diff --git a/db/migrations/20261003120419_create_school_user_table.php b/db/migrations/20261003120419_create_school_user_table.php new file mode 100644 index 0000000..404a094 --- /dev/null +++ b/db/migrations/20261003120419_create_school_user_table.php @@ -0,0 +1,40 @@ +table('school_user', [ + 'id' => false, + 'primary_key' => ['idschooluser'], + 'engine' => 'InnoDB', + 'collation' => 'utf8mb4_unicode_ci', + ]) + ->addColumn('idschooluser', 'integer', ['signed' => false, 'identity' => true]) + ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => 'auth_users.id']) + ->addColumn('idschool', 'integer', ['signed' => false]) + ->addColumn('school_role', 'string', ['limit' => 20, 'default' => 'student', 'comment' => 'student|teacher|admin|owner']) + ->addColumn('active', 'char', ['limit' => 1, 'default' => 'Y']) + ->addColumn('joined_at', 'timestamp', ['null' => true, 'default' => 'CURRENT_TIMESTAMP']) + ->addIndex(['user_id', 'idschool'], ['unique' => true, 'name' => 'school_user_unique']) + ->addIndex(['idschool'], ['name' => 'school_user_idschool_index']) + ->addForeignKey('user_id', 'auth_users', 'id', ['delete' => 'CASCADE', 'update' => 'NO_ACTION']) + ->addForeignKey('idschool', 'schools', 'idschool', ['delete' => 'CASCADE', 'update' => 'NO_ACTION']) + ->create(); + + // Tutti gli utenti esistenti appartengono a Seregno (1) + $this->execute(" + INSERT INTO school_user (user_id, idschool, school_role) + SELECT id, 1, 'student' FROM auth_users + "); + } + + public function down(): void + { + $this->table('school_user')->drop()->save(); + } +} diff --git a/db/migrations/20261003120420_add_idschool_to_domain_tables.php b/db/migrations/20261003120420_add_idschool_to_domain_tables.php new file mode 100644 index 0000000..77f1b5f --- /dev/null +++ b/db/migrations/20261003120420_add_idschool_to_domain_tables.php @@ -0,0 +1,79 @@ +table($name) + ->addColumn('idschool', 'integer', [ + 'signed' => false, + 'null' => false, + 'default' => 1, // TEMPORANEO: verrà rimosso quando tutte le INSERT passeranno idschool + ]) + ->addIndex(['idschool'], ['name' => "idx_{$name}_idschool"]) + ->update(); + } + + // Slug eventi: unico per scuola, non più globale + $this->table('events') + ->removeIndexByName('events_slug_unique') + ->addIndex(['idschool', 'slug'], ['unique' => true, 'name' => 'events_school_slug_unique']) + ->update(); + + // Impostazioni: una sola riga per scuola + $this->table('option') + ->addIndex(['idschool'], ['unique' => true, 'name' => 'option_idschool_unique']) + ->update(); + } + + public function down(): void + { + $this->table('option') + ->removeIndexByName('option_idschool_unique') + ->update(); + + $this->table('events') + ->removeIndexByName('events_school_slug_unique') + ->addIndex(['slug'], ['unique' => true, 'name' => 'events_slug_unique']) + ->update(); + + foreach (self::TABLES as $name) { + $this->table($name) + ->removeIndexByName("idx_{$name}_idschool") + ->removeColumn('idschool') + ->update(); + } + } +} diff --git a/db/migrations/AddIdschoolToDomainTables.php b/db/migrations/AddIdschoolToDomainTables.php new file mode 100644 index 0000000..77f1b5f --- /dev/null +++ b/db/migrations/AddIdschoolToDomainTables.php @@ -0,0 +1,79 @@ +table($name) + ->addColumn('idschool', 'integer', [ + 'signed' => false, + 'null' => false, + 'default' => 1, // TEMPORANEO: verrà rimosso quando tutte le INSERT passeranno idschool + ]) + ->addIndex(['idschool'], ['name' => "idx_{$name}_idschool"]) + ->update(); + } + + // Slug eventi: unico per scuola, non più globale + $this->table('events') + ->removeIndexByName('events_slug_unique') + ->addIndex(['idschool', 'slug'], ['unique' => true, 'name' => 'events_school_slug_unique']) + ->update(); + + // Impostazioni: una sola riga per scuola + $this->table('option') + ->addIndex(['idschool'], ['unique' => true, 'name' => 'option_idschool_unique']) + ->update(); + } + + public function down(): void + { + $this->table('option') + ->removeIndexByName('option_idschool_unique') + ->update(); + + $this->table('events') + ->removeIndexByName('events_school_slug_unique') + ->addIndex(['slug'], ['unique' => true, 'name' => 'events_slug_unique']) + ->update(); + + foreach (self::TABLES as $name) { + $this->table($name) + ->removeIndexByName("idx_{$name}_idschool") + ->removeColumn('idschool') + ->update(); + } + } +} diff --git a/db/migrations/CreateSchoolUserTable.php b/db/migrations/CreateSchoolUserTable.php new file mode 100644 index 0000000..ec80732 --- /dev/null +++ b/db/migrations/CreateSchoolUserTable.php @@ -0,0 +1,40 @@ +table('school_user', [ + 'id' => false, + 'primary_key' => ['idschooluser'], + 'engine' => 'InnoDB', + 'collation' => 'utf8mb4_unicode_ci', + ]) + ->addColumn('idschooluser', 'integer', ['signed' => false, 'identity' => true]) + ->addColumn('user_id', 'integer', ['signed' => false, 'comment' => 'auth_users.id']) + ->addColumn('idschool', 'integer', ['signed' => false]) + ->addColumn('school_role', 'string', ['limit' => 20, 'default' => 'student', 'comment' => 'student|teacher|admin|owner']) + ->addColumn('active', 'char', ['limit' => 1, 'default' => 'Y']) + ->addColumn('joined_at', 'timestamp', ['null' => true, 'default' => 'CURRENT_TIMESTAMP']) + ->addIndex(['user_id', 'idschool'], ['unique' => true, 'name' => 'school_user_unique']) + ->addIndex(['idschool'], ['name' => 'school_user_idschool_index']) + ->addForeignKey('user_id', 'auth_users', 'id', ['delete' => 'CASCADE', 'update' => 'NO_ACTION']) + ->addForeignKey('idschool', 'schools', 'idschool', ['delete' => 'CASCADE', 'update' => 'NO_ACTION']) + ->create(); + + // Tutti gli utenti esistenti appartengono a Seregno (1) + $this->execute(" + INSERT INTO school_user (user_id, idschool, school_role) + SELECT id, 1, 'student' FROM auth_users + "); + } + + public function down(): void + { + $this->table('school_user')->drop()->save(); + } +} diff --git a/db/migrations/CreateSchoolsTable.php b/db/migrations/CreateSchoolsTable.php new file mode 100644 index 0000000..ef1bbe4 --- /dev/null +++ b/db/migrations/CreateSchoolsTable.php @@ -0,0 +1,52 @@ +table('schools', [ + 'id' => false, + 'primary_key' => ['idschool'], + 'engine' => 'InnoDB', + 'collation' => 'utf8mb4_unicode_ci', + ]) + ->addColumn('idschool', 'integer', ['signed' => false, 'identity' => true]) + ->addColumn('name', 'string', ['limit' => 191]) + ->addColumn('slug', 'string', ['limit' => 100, 'comment' => 'usato per URL/sottodominio/widget']) + ->addColumn('email', 'string', ['limit' => 191, 'null' => true]) + ->addColumn('phone', 'string', ['limit' => 40, 'null' => true]) + ->addColumn('address', 'string', ['limit' => 255, 'null' => true]) + ->addColumn('city', 'string', ['limit' => 100, 'null' => true]) + ->addColumn('postcode', 'string', ['limit' => 20, 'null' => true]) + ->addColumn('country', 'string', ['limit' => 2, 'default' => 'IT']) + ->addColumn('vat_number', 'string', ['limit' => 30, 'null' => true]) + ->addColumn('logo', 'string', ['limit' => 191, 'null' => true]) + ->addColumn('timezone', 'string', ['limit' => 50, 'default' => 'Europe/Rome']) + ->addColumn('locale', 'string', ['limit' => 5, 'default' => 'it']) + ->addColumn('currency', 'string', ['limit' => 3, 'default' => 'EUR']) + ->addColumn('status', 'string', ['limit' => 20, 'default' => 'active', 'comment' => 'active|suspended|trial']) + ->addColumn('created_at', 'timestamp', ['null' => true, 'default' => 'CURRENT_TIMESTAMP']) + ->addColumn('updated_at', 'timestamp', ['null' => true, 'default' => null, 'update' => 'CURRENT_TIMESTAMP']) + ->addIndex(['slug'], ['unique' => true, 'name' => 'schools_slug_unique']) + ->addIndex(['status'], ['name' => 'schools_status_index']) + ->create(); + + // Seregno = scuola 1 (dati da completare poi dal pannello) + $this->table('schools')->insert([ + 'idschool' => 1, + 'name' => 'Scuola Yoga Seregno', + 'slug' => 'seregno', + 'city' => 'Seregno', + 'country' => 'IT', + ])->saveData(); + } + + public function down(): void + { + $this->table('schools')->drop()->save(); + } +}