Skip to content

Commit

Permalink
[5.x] Implies only the new migrations behaviour on L11 (#305)
Browse files Browse the repository at this point in the history
* Implies only the new migrations behaviour on L11

* Improves method `usingLaravel11OrNewer`
  • Loading branch information
nunomaduro authored Jan 23, 2024
1 parent a269426 commit e5a3ce8
Show file tree
Hide file tree
Showing 5 changed files with 226 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/NewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ protected function defaultBranch()
protected function configureDefaultDatabaseConnection(string $directory, string $database, string $name, bool $migrate)
{
// MariaDB configuration only exists as of Laravel 11...
if ($database === 'mariadb' && ! $this->hasMariaDBConfig($directory)) {
if ($database === 'mariadb' && ! $this->usingLaravel11OrNewer($directory)) {
$database = 'mysql';
}

Expand Down Expand Up @@ -308,22 +308,18 @@ protected function configureDefaultDatabaseConnection(string $directory, string
}

/**
* Determine if the application has a MariaDB configuration entry.
* Determine if the application is using Laravel 11 or newer.
*
* @param string $directory
* @return bool
*/
protected function hasMariaDBConfig(string $directory): bool
public function usingLaravel11OrNewer(string $directory): bool
{
// Laravel 11+ has moved the configuration files into the framework...
if (! file_exists($directory.'/config/database.php')) {
return true;
}
$version = json_decode(file_get_contents($directory.'/composer.json'), true)['require']['laravel/framework'];
$version = str_replace('^', '', $version);
$version = explode('.', $version)[0];

return str_contains(
file_get_contents($directory.'/config/database.php'),
"'mariadb' =>"
);
return $version >= 11;
}

/**
Expand Down Expand Up @@ -450,7 +446,7 @@ protected function installJetstream(string $directory, InputInterface $input, Ou
protected function promptForDatabaseOptions(string $directory, InputInterface $input)
{
// Laravel 11.x appliations use SQLite as default...
$defaultDatabase = $this->hasMariaDBConfig($directory) ? 'sqlite' : 'mysql';
$defaultDatabase = $this->usingLaravel11OrNewer($directory) ? 'sqlite' : 'mysql';

if ($input->isInteractive()) {
$database = select(
Expand All @@ -465,7 +461,7 @@ protected function promptForDatabaseOptions(string $directory, InputInterface $i
default: $defaultDatabase
);

if ($database !== $defaultDatabase) {
if ($this->usingLaravel11OrNewer($directory) && $database !== $defaultDatabase) {
$migrate = confirm(label: 'Default database updated. Would you like to run the default database migrations?', default: true);
}
}
Expand Down
13 changes: 13 additions & 0 deletions tests/NewCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,17 @@ public function test_it_can_scaffold_a_new_laravel_app()
$this->assertDirectoryExists($scaffoldDirectory.'/vendor');
$this->assertFileExists($scaffoldDirectory.'/.env');
}

public function test_on_at_least_laravel_11()
{
$command = new NewCommand;

$onLaravel10 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel10');
$onLaravel11 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel11');
$onLaravel12 = $command->usingLaravel11OrNewer(__DIR__.'/fixtures/laravel12');

$this->assertFalse($onLaravel10);
$this->assertTrue($onLaravel11);
$this->assertTrue($onLaravel12);
}
}
66 changes: 66 additions & 0 deletions tests/fixtures/laravel10/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.1",
"guzzlehttp/guzzle": "^7.2",
"laravel/framework": "^10.10",
"laravel/sanctum": "^3.3",
"laravel/tinker": "^2.8"
},
"require-dev": {
"fakerphp/faker": "^1.9.1",
"laravel/pint": "^1.0",
"laravel/sail": "^1.18",
"mockery/mockery": "^1.4.4",
"nunomaduro/collision": "^7.0",
"phpunit/phpunit": "^10.1",
"spatie/laravel-ignition": "^2.0"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi"
]
},
"extra": {
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
"minimum-stability": "stable",
"prefer-stable": true
}
69 changes: 69 additions & 0 deletions tests/fixtures/laravel11/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.2",
"laravel/framework": "^11.0",
"laravel/tinker": "^2.9"
},
"require-dev": {
"fakerphp/faker": "^1.23",
"laravel/pint": "^1.13",
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^10.5",
"spatie/laravel-ignition": "^2.4"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi",
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
"@php artisan migrate --ansi"
]
},
"extra": {
"branch-alias": {
"dev-master": "11.x-dev"
},
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}
69 changes: 69 additions & 0 deletions tests/fixtures/laravel12/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{
"name": "laravel/laravel",
"type": "project",
"description": "The skeleton application for the Laravel framework.",
"keywords": ["laravel", "framework"],
"license": "MIT",
"require": {
"php": "^8.2",
"laravel/framework": "^12.0.1",
"laravel/tinker": "^2.9"
},
"require-dev": {
"fakerphp/faker": "^1.23",
"laravel/pint": "^1.13",
"laravel/sail": "^1.26",
"mockery/mockery": "^1.6",
"nunomaduro/collision": "^8.0",
"phpunit/phpunit": "^10.5",
"spatie/laravel-ignition": "^2.4"
},
"autoload": {
"psr-4": {
"App\\": "app/",
"Database\\Factories\\": "database/factories/",
"Database\\Seeders\\": "database/seeders/"
}
},
"autoload-dev": {
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-autoload-dump": [
"Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
"@php artisan package:discover --ansi"
],
"post-update-cmd": [
"@php artisan vendor:publish --tag=laravel-assets --ansi --force"
],
"post-root-package-install": [
"@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"@php artisan key:generate --ansi",
"@php -r \"file_exists('database/database.sqlite') || touch('database/database.sqlite');\"",
"@php artisan migrate --ansi"
]
},
"extra": {
"branch-alias": {
"dev-master": "11.x-dev"
},
"laravel": {
"dont-discover": []
}
},
"config": {
"optimize-autoloader": true,
"preferred-install": "dist",
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true,
"php-http/discovery": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

0 comments on commit e5a3ce8

Please sign in to comment.