diff --git a/Services/FlexMigrator.php b/Services/FlexMigrator.php index 90dc5c7..920bb70 100644 --- a/Services/FlexMigrator.php +++ b/Services/FlexMigrator.php @@ -173,6 +173,7 @@ public function copyNewTemplateFiles(string $projectDir): void public function migrateEnvFile(string $projectDir): void { $envPath = $projectDir . '/.env'; + $envLocalPath = $projectDir . '/.env.local'; if (!is_file($envPath)) { file_put_contents($envPath, self::ENV_DEFAULT); @@ -180,7 +181,10 @@ public function migrateEnvFile(string $projectDir): void return; } - rename($envPath, $envPath . '.local'); + if (!is_file($envLocalPath)) { + rename($envPath, $envLocalPath); + } + file_put_contents($envPath, self::ENV_DEFAULT); } diff --git a/Tests/Services/FlexMigratorTest.php b/Tests/Services/FlexMigratorTest.php index 06bc74f..7dacf63 100644 --- a/Tests/Services/FlexMigratorTest.php +++ b/Tests/Services/FlexMigratorTest.php @@ -87,6 +87,27 @@ public function testMigrateEnvExistingEnv(): void $fs->remove($tmpDir); } + public function testMigrateEnvDoesNotOverwriteExistingEnvLocal(): void + { + $tmpDir = sys_get_temp_dir() . '/flex-migrator-test'; + $fs = new Filesystem(); + $fs->mkdir($tmpDir); + $fs->dumpFile($tmpDir . '/.env', 'DATABASE_URL=mysql://user:pass@localhost/real_db'); + $fs->dumpFile($tmpDir . '/.env.local', 'DATABASE_URL=mysql://user:pass@localhost/real_db'); + + $flexMigrator = new FlexMigrator(); + + $flexMigrator->migrateEnvFile($tmpDir); + + static::assertFileExists($tmpDir . '/.env'); + static::assertFileExists($tmpDir . '/.env.local'); + static::assertStringContainsString('###> symfony/lock ###', (string) file_get_contents($tmpDir . '/.env')); + // The existing .env.local must not be overwritten + static::assertSame('DATABASE_URL=mysql://user:pass@localhost/real_db', (string) file_get_contents($tmpDir . '/.env.local')); + + $fs->remove($tmpDir); + } + public static function composerCases(): \Generator { yield 'no repos' => [