Skip to content

Instantly share code, notes, and snippets.

@jeremynikolic
Created October 14, 2020 16:29
Show Gist options
  • Save jeremynikolic/5051911f8de565139cc79a5de88a255c to your computer and use it in GitHub Desktop.
Save jeremynikolic/5051911f8de565139cc79a5de88a255c to your computer and use it in GitHub Desktop.
/**
* This is a function that helps naming migrations published by packages
* it will generate YYYY_mm_dd_######_migration_name.php
* if the migration has already been published, it will return the published migration name to prevent dupplicate
*
* @param \Illuminate\Filesystem\Filesystem $filesystem
* @param $migrationName the migration file base name without timestamp or .stub e.g. create_user_table.php
* @param $i allows for incrementing in a way that the migrations order is kept
*
* @return string
*/
private function getMigrationFileName(Filesystem $filesystem, $migrationName, $i): string
{
$pad = str_pad($i, 6, '0', STR_PAD_LEFT);
$timestamp = date('Y_m_d').'_'.$pad;
return Collection::make($this->app->databasePath().DIRECTORY_SEPARATOR.'migrations'.DIRECTORY_SEPARATOR)
->flatMap(function ($path) use ($filesystem, $migrationName) {
return $filesystem->glob($path."*_{$migrationName}.php");
})
->push($this->app->databasePath()."/migrations/{$timestamp}_{$migrationName}.php")
->first();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment