• Czas czytania ~2 min
  • 15.06.2023

Jednym z typowych wyzwań podczas migracji starszej aplikacji PHP do Laravel jest tworzenie migracji baz danych w oparciu o istniejącą bazę danych.

W zależności od wielkości bazy danych może to być wyczerpujące zadanie. Musiałem to zrobić kilka razy, ale ostatnio natknąłem się na bazę danych z ponad setką tabel.

Jako programista nie mamy cierpliwości, aby wykonać takie zadanie i nie powinniśmy. Pierwsza myśl brzmi: jak to zautomatyzować. Mając to na uwadze, szukałem istniejącego rozwiązania, znalazłem kilka pakietów i wybrałem jeden przez kitloong, pakiet generatora migracji Laravel.

Praktyczny przykład wykorzystania istniejącej struktury

bazy danych Tworzenie tabel

CREATE TABLE permissions
(
    id bigint unsigned auto_increment primary key,
    name varchar(255) not null,
    guard_name varchar(255) not null,
    created_at timestamp    null,
    updated_at timestamp    null,
    constraint permissions_name_guard_name_unique
    unique (name, guard_name)
)
collate = utf8_unicode_ci;
CREATE TABLE roles
(
    id bigint unsigned auto_increment primary key,
    team_id bigint unsigned null,
    name varchar(255) not null,
    guard_name varchar(255) not null,
    created_at timestamp null,
    updated_at timestamp null,
    constraint roles_team_id_name_guard_name_unique
    unique (team_id, name, guard_name)
)
collate = utf8_unicode_ci;
CREATE TABLE role_has_permissions
(
    permission_id bigint unsigned not null,
    role_id bigint unsigned not null,
    primary key (permission_id, role_id),
    constraint role_has_permissions_permission_id_foreign
    foreign key (permission_id) references permissions (id)
    on delete cascade,
    constraint role_has_permissions_role_id_foreign
    foreign key (role_id) references roles (id)
    on delete cascade
)
collate = utf8_unicode_ci;
CREATE INDEX roles_team_foreign_key_index on roles (team_id);

Instalowanie pakietu

composer require --dev kitloong/laravel-migrations-generator

Uruchamianie polecenia pakietu, które robi magię

Możesz określić lub zignorować tabele, które chcesz użyć --tables= lub --ignore= odpowiednio.

Poniżej znajduje się polecenie, które uruchomiłem dla tabel, które utworzyliśmy powyżej. Aby uruchomić wszystkie tabele, nie dodawaj żadnych dodatkowych filtrów.

php artisan migrate:generate --tables="roles,permissions,role_permissions"

Dane wyjściowe

Using connection: mysql
Generating migrations for: permissions,role_has_permissions,roles
Do you want to log these migrations in the migrations table? (yes/no) [yes]:
> yes
Setting up Tables and Index migrations.
Created: /var/www/html/database/migrations/2023_06_08_132125_create_permissions_table.php
Created: /var/www/html/database/migrations/2023_06_08_132125_create_role_has_permissions_table.php
Created: /var/www/html/database/migrations/2023_06_08_132125_create_roles_table.php
Setting up Views migrations.
Setting up Stored Procedures migrations.
Setting up Foreign Key migrations.
Created: /var/www/html/database/migrations/2023_06_08_132128_add_foreign_keys_to_role_has_permissions_table.php
Finished!

polecenia Sprawdzanie plików

migracji Tabela uprawnień: Tabela ról: Tabela przestawna: Dodaj klucz obcy do tabeli przestawnej: 2023_06_08_132125_create_roles_table.php

...
Schema::create('roles', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('team_id')
        ->nullable()
        ->index('roles_team_foreign_key_index');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    $table->unique(['team_id', 'name', 'guard_name']);
});
...

...
Schema::create('roles', function (Blueprint $table) {
    $table->bigIncrements('id');
    $table->unsignedBigInteger('team_id')
        ->nullable()
        ->index('roles_team_foreign_key_index');
    $table->string('name');
    $table->string('guard_name');
    $table->timestamps();
    $table->unique(['team_id', 'name', 'guard_name']);
});
...

2023_06_08_132125_create_permissions_table.php

...
Schema::create('role_has_permissions', function (Blueprint $table) {
    $table->unsignedBigInteger('permission_id');
    $table->unsignedBigInteger('role_id')
        ->index('role_has_permissions_role_id_foreign');
    $table->primary(['permission_id', 'role_id']);
});
...

...
Schema::table('role_has_permissions', function (Blueprint $table) {
    $table->foreign(['permission_id'])
        ->references(['id'])
        ->on('permissions')
        ->onUpdate('NO ACTION')
        ->onDelete('CASCADE');
    $table->foreign(['role_id'])
        ->references(['id'])
        ->on('roles')
        ->onUpdate('NO ACTION')
        ->onDelete('CASCADE');
});
...

2023_06_08_132125_create_role_has_permissions_table.php2023_06_08_132128_add_foreign_keys_to_role_has_permissions_table.phpTo tylko jedno z wyzwań podczas migracji starszej aplikacji PHP do Laravel.

Poniższy post będzie dotyczył niezgodności algorytmu haszującego hasła.

Dołącz do dyskusji na Twitterze.

Comments

No comments yet
Yurij Finiv

Yurij Finiv

Full stack

O

Professional Fullstack Developer with extensive experience in website and desktop application development. Proficient in a wide range of tools and technologies, including Bootstrap, Tailwind, HTML5, CSS3, PUG, JavaScript, Alpine.js, jQuery, PHP, MODX, and Node.js. Skilled in website development using Symfony, MODX, and Laravel. Experience: Contributed to the development and translation of MODX3 i...

O autorze CrazyBoy49z
WORK EXPERIENCE
Kontakt
Ukraine, Lutsk
+380979856297