Skip to content

Commit

Permalink
Possibility of generating a migration in order to simplify the implem…
Browse files Browse the repository at this point in the history
…entation
  • Loading branch information
forxer committed Jan 11, 2022
1 parent f374bba commit 4b76a34
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 19 deletions.
6 changes: 6 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
Changelog
=========

1.2.0 (2022-01-11)
------------------

- Possibility of generating a migration in order to simplify the implementation


1.1.0 (2022-01-10)
------------------

Expand Down
12 changes: 12 additions & 0 deletions database/migrations/transform_pk_int_to_bigint.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\Artisan;

return new class extends Migration
{
public function up()
{
Artisan::call('pk-int-to-bigint:transform');
}
};
33 changes: 23 additions & 10 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
Laravel convert primary keys INT to BIGINT
==========================================

Convert DB primary keys and related foreign keys type from INT to BIGINT in a Laravel project.
This package convert DB primary keys and related foreign keys type from INT to BIGINT in a Laravel project.

This package is particularly useful for old projects that need to be updated.
This is especially useful for old projects that need to be updated.

Indeed, since Laravel 5.8 the ID columns are by default of type BIGINT. If you install new packages that use this new "standard" you will have trouble creating the foreign keys.

As a result, this package will transform all the primary keys of type INT into BIGINT.
It will also update the relationships on these keys.
As a result, this package will be of great help to you to modernize an old application.

It proceeds in 4 steps:

- introspection of the database and verification of the integrity of foreign keys (if an integrity is not respected it stops and indicates it to you)
- droping all foreign key constraints on each table
- converting INT to BIGINT on primary and foreign key columns on each table
- restoring all foreign key constraints on each table
1. introspection of the database and verification of the integrity of foreign keys (if an integrity is not respected it stops and indicates it to you)
2. droping all foreign key constraints on each table
3. converting INT to BIGINT on primary and foreign key columns on each table
4. restoring all foreign key constraints on each table

Instalation
-----------
Expand All @@ -29,9 +28,23 @@ composer require axn/laravel-pk-int-to-bigint
Usage
-----

Create a dump of your data in case there is a problem.
First create a dump of your database in case there is a problem.

Run the command:
### With migration

Pusblish the migration:

```sh
php artisan vendor:publish --tag="pk-int-to-bigint-migration"

php artisan migrate
```

So you can incorporate it into your deployment workflow.

### Manualy

If you want to run the command directly:

```sh
php artisan pk-int-to-bigint:transform
Expand Down
27 changes: 18 additions & 9 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,31 @@

namespace Axn\PkIntToBigint;

use Axn\PkIntToBigint\Console\TransformCommand;
use Axn\PkIntToBigint\Transformer;
use Illuminate\Support\ServiceProvider as BaseServiceProvider;

class ServiceProvider extends BaseServiceProvider
{
public function register()
{
if ($this->app->runningInConsole()) {
$this->app->singleton('command.pk-int-to-bigint.transform', function($app) {
$transformer = new Transformer($app['db.connection']);
if (! $this->app->runningInConsole()) {
return;
}

return new Console\TransformCommand($transformer);
});
$this->app->singleton('command.pk-int-to-bigint.transform', function($app) {
$transformer = new Transformer($app['db.connection']);

$this->commands([
'command.pk-int-to-bigint.transform'
]);
}
return new TransformCommand($transformer);
});

$this->commands([
'command.pk-int-to-bigint.transform',
]);

$this->publishes([
__DIR__.'/../database/migrations/transform_pk_int_to_bigint.php.stub' =>
database_path('migrations/' . now()->format('Y_m_d_His') . '_transform_pk_int_to_bigint.php'),
], 'pk-int-to-bigint-migration');
}
}

0 comments on commit 4b76a34

Please sign in to comment.