Skip to content

Instantly share code, notes, and snippets.

@igorsantos07
Last active August 3, 2018 00:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igorsantos07/4fbfaa49225a1809f99d6ab7d01fc32f to your computer and use it in GitHub Desktop.
Save igorsantos07/4fbfaa49225a1809f99d6ab7d01fc32f to your computer and use it in GitHub Desktop.
Handy git hooks
#!/usr/bin/env php
<?php
define('ROOT', dirname(dirname(__DIR__)));
chdir(ROOT);
$pending_migrations = (int)trim(`./artisan migrate:status --no-ansi | grep -c '| N'`);
echo "===> Pending migrations: $pending_migrations";
if ($pending_migrations) {
$migrations = array_filter(explode("\n", `./artisan migrate:status --no-ansi | grep '| N'`));
$migrations = array_map(function($line) { return ' > '.trim($line, " |N\t"); }, $migrations);
echo "\n".join("\n", $migrations)."\n\n";
echo 'Do you want to run them? [y/N] ';
$input = @fopen('/dev/tty', 'r');
if (!$input) {
die("\nWhoops, non-interactive shell. Don't forget to run the migrations!\n");
}
while (!in_array($opt = strtolower(trim(fgets($input))), ['y','yes','n','no',''])) {
echo 'I said y/n! ';
}
switch ($opt) {
case 'y':
case 'yes':
passthru("./artisan migrate");
break;
case 'n':
case 'no':
default:
echo "Don't forget them!\n";
break;
}
} else {
echo "\n";
}
#!/usr/bin/env php
<?php
define('ROOT', dirname(dirname(__DIR__)));
$phinx = 'vendor/bin/phinx';
chdir(ROOT);
$pending_migrations = (int)trim(`$phinx status|grep -c down`);
echo "===> Pending migrations: $pending_migrations";
if ($pending_migrations) {
echo "\n".`$phinx status|grep down`;
echo 'Do you want to run them? [y/N] ';
$input = @fopen('/dev/tty', 'r');
if (!$input) {
die("\nWhoops, non-interactive shell. Don't forget to run the migrations!\n");
}
while (!in_array($opt = strtolower(trim(fgets($input))), ['y','yes','n','no',''])) {
echo 'I said y/n! ';
}
switch ($opt) {
case 'y':
case 'yes':
passthru("$phinx migrate");
break;
case 'n':
case 'no':
default:
echo "Don't forget them!\n";
break;
}
} else {
echo "\n";
}
#!/usr/bin/env bash
if [ $3 == 0 ] || [ $1 == $2 ]; then #file checkout or same refs: no need to update deps
exit 0
fi
FROM=$(echo $1 | cut -c1-7)
TO=$(echo $2 | cut -c1-7)
echo ""
echo "Checked from $FROM into $TO. Refreshing PHP dependencies..."
echo ""
# first, updating dependencies
docker-compose exec -T php composer install
# then, checking migrations waiting to be executed. You may want to comment this if you feel safe locally
exec < /dev/tty # enables interactive TTY for the rest of the script (needed for the line below)
docker-compose exec app docker/hooks/check-migrations
#!/usr/bin/env bash
docker-compose exec app docker/hooks/check-migrations
@igorsantos07
Copy link
Author

igorsantos07 commented Dec 1, 2016

Summary

post-checkout (shell)

Verifies if that's not a file ($3 == 0) but a branch ($3 == 1) checkout and calls docker-compose to run composer install on the container.
Then, runs the migrations checker.

check-migrations (php)

Verifies if there's any pending Phinx migrations, and asks if it can run those. Written in PHP as it's way more readable than bash-magic, I'm sorry if you don't have PHP on your dev machine :)
Ideally, this should be executed on post-merge and post-checkout, but it could be slow. Thus, if you feel safe locally (i.e. you know what you're doing and you're not checking out branches from other devs frequently), you can keep it only inside post-merge.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment