[GH-ISSUE #73] Add ability to install dbdiff globally (via 1: composer global require, and 2. PHAR) #51

Closed
opened 2026-02-25 21:33:08 +03:00 by kerem · 11 comments
Owner

Originally created by @plashenkov on GitHub (Feb 20, 2018).
Original GitHub issue: https://github.com/DBDiff/DBDiff/issues/73

Hi!

It would be nice to have self-sufficient phar which we then can simply copy to our bin folder and use it from anywhere. And there will be no need to composer install, etc.

An example can be seen here: https://github.com/banago/PHPloy
(see dist folder)

As a bonus there will be no more issues like, for example, this: https://github.com/DBDiff/DBDiff/issues/72

Originally created by @plashenkov on GitHub (Feb 20, 2018). Original GitHub issue: https://github.com/DBDiff/DBDiff/issues/73 Hi! It would be nice to have self-sufficient phar which we then can simply copy to our `bin` folder and use it from anywhere. And there will be no need to `composer install`, etc. An example can be seen here: https://github.com/banago/PHPloy (see `dist` folder) As a bonus there will be no more issues like, for example, this: https://github.com/DBDiff/DBDiff/issues/72
kerem closed this issue 2026-02-25 21:33:08 +03:00
Author
Owner

@plashenkov commented on GitHub (Feb 23, 2018):

Let's add an ability to install dbdiff globally via:

  1. composer global require "dbdiff/dbdiff:@dev"
  2. PHAR executable

This will allow the user to use dbdiff as a "binary" from anywhere in the system. I can create .dbdiff config file under each project folder and then just run dbdiff as a standalone tool from command-line (without copying it to each project).

Installing globally via composer will allow us to easily update dbdiff (composer global update); installing globally as a phar self-sufficient executable also has its advantages: there will be no conflicts of dependencies if there are other libraries in the global composer space.

What do we need to update to achieve this?

  1. https://github.com/DBDiff/DBDiff/blob/master/src/Params/FSGetter.php#L40-L41
    Let's use getcwd() to get current working directory from which dbdiff is executed:
if (file_exists(getcwd() . '/.dbdiff')) {
    $configFile = getcwd() . '/.dbdiff';
}
  1. https://github.com/DBDiff/DBDiff/blob/master/dbdiff#L4
    When dbdiff is installed using composer, dbdiff is located inside the vendor directory, and we should look for autoload.php where it is really located. When dbdiff is installed by cloning/downloading Git repository and then using composer install, the vendor directory is located alongside dbdiff executable. We should consider both variants. Let's do this:
if (file_exists(__DIR__ . '/../../autoload.php')) {
    require __DIR__ . '/../../autoload.php';
} else {
    require __DIR__ . '/vendor/autoload.php';
}

It's always a good idea to use __DIR__ (or getcwd()) instead of ambiguous relative paths.

<!-- gh-comment-id:368167991 --> @plashenkov commented on GitHub (Feb 23, 2018): Let's add an ability to install dbdiff globally via: 1) `composer global require "dbdiff/dbdiff:@dev"` 2) PHAR executable This will allow the user to use dbdiff as a "binary" from anywhere in the system. I can create `.dbdiff` config file under each project folder and then just run dbdiff as a standalone tool from command-line (without copying it to each project). Installing globally via composer will allow us to easily update dbdiff (`composer global update`); installing globally as a phar self-sufficient executable also has its advantages: there will be no conflicts of dependencies if there are other libraries in the global composer space. What do we need to update to achieve this? 1. https://github.com/DBDiff/DBDiff/blob/master/src/Params/FSGetter.php#L40-L41 Let's use `getcwd()` to get current working directory from which dbdiff is executed: ```php if (file_exists(getcwd() . '/.dbdiff')) { $configFile = getcwd() . '/.dbdiff'; } ``` 2. https://github.com/DBDiff/DBDiff/blob/master/dbdiff#L4 When dbdiff is installed using composer, dbdiff is located inside the vendor directory, and we should look for `autoload.php` where it is really located. When dbdiff is installed by cloning/downloading Git repository and then using `composer install`, the `vendor` directory is located alongside `dbdiff` executable. We should consider both variants. Let's do this: ```php if (file_exists(__DIR__ . '/../../autoload.php')) { require __DIR__ . '/../../autoload.php'; } else { require __DIR__ . '/vendor/autoload.php'; } ``` It's always a good idea to use `__DIR__` (or `getcwd()`) instead of ambiguous relative paths.
Author
Owner

@plashenkov commented on GitHub (Feb 24, 2018):

  1. Let's create PHAR

We need to make a copy of dbdiff (in the repository root) and name it dbdiff.php. Then delete the first line (#!/usr/bin/env php) and use the following composer autoload:

require __DIR__ . '/vendor/autoload.php';

(this single line is enough here as we will use this dbdiff.php file only in PHAR).

Then create build executable in repo root with the following content:

#!/usr/bin/env php
<?php

$dir = __DIR__ . '/dist';
$file = $dir . '/dbdiff.phar';

@unlink($file);
@mkdir($dir, 0755, true);

$phar = new Phar($file);

$phar->startBuffering();

$phar->buildFromDirectory(__DIR__, '/src\/.*|vendor\/.*|dbdiff\.php/');

$stub = $phar->createDefaultStub('dbdiff.php');
$stub = "#!/usr/bin/env php \n" . $stub;
$phar->setStub($stub);

$phar->compressFiles(Phar::GZ);
$phar->stopBuffering();

chmod($file, 0755);

echo 'Build successful: dist/dbdiff.phar' . PHP_EOL;

Now, running ./build will create dbdiff.phar file inside dist folder.

<!-- gh-comment-id:368175597 --> @plashenkov commented on GitHub (Feb 24, 2018): 3. Let's create PHAR We need to make a copy of `dbdiff` (in the repository root) and name it `dbdiff.php`. Then delete the first line (`#!/usr/bin/env php`) and use the following composer autoload: ```php require __DIR__ . '/vendor/autoload.php'; ``` (this single line is enough here as we will use this `dbdiff.php` file only in PHAR). Then create `build` executable in repo root with the following content: ```php #!/usr/bin/env php <?php $dir = __DIR__ . '/dist'; $file = $dir . '/dbdiff.phar'; @unlink($file); @mkdir($dir, 0755, true); $phar = new Phar($file); $phar->startBuffering(); $phar->buildFromDirectory(__DIR__, '/src\/.*|vendor\/.*|dbdiff\.php/'); $stub = $phar->createDefaultStub('dbdiff.php'); $stub = "#!/usr/bin/env php \n" . $stub; $phar->setStub($stub); $phar->compressFiles(Phar::GZ); $phar->stopBuffering(); chmod($file, 0755); echo 'Build successful: dist/dbdiff.phar' . PHP_EOL; ``` Now, running `./build` will create `dbdiff.phar` file inside `dist` folder.
Author
Owner

@plashenkov commented on GitHub (Feb 24, 2018):

The previous actions will produce fully working PHAR as well as global dbdiff installation. I hope you will find them really useful. If you want, I can create a pull request. But I thought the detailed explanation will be relevant.

I believe the following step should be to add an ability to specify what to compare inside .dbdiff file. Often we need a simple complete databases comparison. It's annoying to type them as parameters every time (their names are different from project to project, their names are different on server1 and server2, etc., and I do not remember them, I need to open DB client and connect to databases to see their names). I want to simply run dbdiff inside project folder to get the migration.sql. And if I want to compare something special, I use parameters (which should override default settings from .dbdiff). But this is for a separate issue, I think.

<!-- gh-comment-id:368177930 --> @plashenkov commented on GitHub (Feb 24, 2018): The previous actions will produce fully working PHAR as well as global dbdiff installation. I hope you will find them really useful. If you want, I can create a pull request. But I thought the detailed explanation will be relevant. I believe the following step should be to add an ability to specify what to compare inside `.dbdiff` file. Often we need a simple complete databases comparison. It's annoying to type them as parameters every time (their names are different from project to project, their names are different on server1 and server2, etc., and I do not remember them, I need to open DB client and connect to databases to see their names). I want to simply run `dbdiff` inside project folder to get the `migration.sql`. And if I want to compare something special, I use parameters (which should override default settings from `.dbdiff`). But this is for a separate issue, I think.
Author
Owner

@jasdeepkhalsa commented on GitHub (Feb 24, 2018):

Thanks for the extremely detailed comments @plashenkov . We have a few things in the pipeline to address first but will get onto this ! In the meantime if you'd like to raise a PR for this, we can prioritise getting it merged in :)

<!-- gh-comment-id:368231593 --> @jasdeepkhalsa commented on GitHub (Feb 24, 2018): Thanks for the extremely detailed comments @plashenkov . We have a few things in the pipeline to address first but will get onto this ! In the meantime if you'd like to raise a PR for this, we can prioritise getting it merged in :)
Author
Owner

@tennox commented on GitHub (Sep 6, 2018):

Any updates on this?
It would be really amazing to be able to download a .phar executable - especially for CI environments.

If I find time I will try to create this PR myself, but at the moment I don't have any.

<!-- gh-comment-id:419151257 --> @tennox commented on GitHub (Sep 6, 2018): Any updates on this? It would be really amazing to be able to download a .phar executable - especially for CI environments. If I find time I will try to create this PR myself, but at the moment I don't have any.
Author
Owner

@plashenkov commented on GitHub (Sep 7, 2018):

Forked. I'll add a PR ASAP. Approx. 1–3 days

<!-- gh-comment-id:419285958 --> @plashenkov commented on GitHub (Sep 7, 2018): Forked. I'll add a PR ASAP. Approx. 1–3 days
Author
Owner

@plashenkov commented on GitHub (Sep 14, 2018):

I've added a PR. As described,

1. We will be able to install DBDiff globally using:

composer global require "dbdiff/dbdiff:@dev"

2. Or we can build and use a Phar:

  1. Clone or download the repository;
  2. composer install
  3. ./build
  4. Find a Phar in dist folder;
  5. You can rename "dbdiff.phar" to "dbdiff" and move it to /usr/local/bin, for example.
<!-- gh-comment-id:421194804 --> @plashenkov commented on GitHub (Sep 14, 2018): I've added a PR. As described, #### 1. We will be able to install DBDiff globally using: ``` composer global require "dbdiff/dbdiff:@dev" ``` #### 2. Or we can build and use a Phar: 1. Clone or download the repository; 2. `composer install` 3. `./build` 4. Find a Phar in dist folder; 5. You can rename "dbdiff.phar" to "dbdiff" and move it to `/usr/local/bin`, for example.
Author
Owner

@plashenkov commented on GitHub (Mar 18, 2019):

Hey @jhbsk! I am happy to see you are with us again :)
Any news on this? :)

<!-- gh-comment-id:473750283 --> @plashenkov commented on GitHub (Mar 18, 2019): Hey @jhbsk! I am happy to see you are with us again :) Any news on this? :)
Author
Owner

@jasdeepkhalsa commented on GitHub (Mar 19, 2019):

Thanks @plashenkov for all the hard work on the PR.

My only reservation to merging it in is that I never had a chance to complete some basic tests (using PHPUnit) around functionality, as I want to make sure nothing regresses. Have you already thoroughly tested this change?

Also...just need to add your steps into the documentation README file - which i can do no problem!

Thanks again @plashenkov

<!-- gh-comment-id:474533251 --> @jasdeepkhalsa commented on GitHub (Mar 19, 2019): Thanks @plashenkov for all the hard work on the PR. My only reservation to merging it in is that I never had a chance to complete some basic tests (using PHPUnit) around functionality, as I want to make sure nothing regresses. Have you already thoroughly tested this change? Also...just need to add your steps into the documentation README file - which i can do no problem! Thanks again @plashenkov
Author
Owner

@plashenkov commented on GitHub (Mar 20, 2019):

Hi @jhbsk!

Well, this PR adds two files which do not affect current functionality at all; and two other small edits are described in my second post of this thread. I am not too good at automatic tests like PHPUnit, etc., but all edits contained in this PR are carefully tested by me, of course. I use this new customized DBDiff build for a relatively long time already and it works very well and as expected.

All added and changed files: https://github.com/DBDiff/DBDiff/pull/86/files (for convenience)

<!-- gh-comment-id:474649648 --> @plashenkov commented on GitHub (Mar 20, 2019): Hi @jhbsk! Well, this PR adds two files which do not affect current functionality at all; and two other small edits are described in my second post of this thread. I am not too good at automatic tests like PHPUnit, etc., but all edits contained in this PR are carefully tested by me, of course. I use this new customized DBDiff build for a relatively long time already and it works very well and as expected. All added and changed files: https://github.com/DBDiff/DBDiff/pull/86/files (for convenience)
Author
Owner

@ThaDaVos commented on GitHub (Feb 3, 2020):

Any ETA on the merge/addition of this functionality? Would be so amazing!

<!-- gh-comment-id:581397751 --> @ThaDaVos commented on GitHub (Feb 3, 2020): Any ETA on the merge/addition of this functionality? Would be so amazing!
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/DBDiff#51
No description provided.