Skip to content

Commit 06a0fb0

Browse files
Merge pull request #118 from move-elevator/feat/php-sync-tool
feat: use the php-sync-tool dump options in dev:dump and check the sync tool
2 parents 57d714f + cc49f77 commit 06a0fb0

11 files changed

Lines changed: 70 additions & 16 deletions

File tree

‎autoload.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
require_once(__DIR__ . '/deployer/requirements/task/requirements.php');
5151
require_once(__DIR__ . '/deployer/requirements/task/check_locales.php');
5252
require_once(__DIR__ . '/deployer/requirements/task/check_packages.php');
53+
require_once(__DIR__ . '/deployer/requirements/task/check_sync_tool.php');
5354
require_once(__DIR__ . '/deployer/requirements/task/check_image_processing.php');
5455
require_once(__DIR__ . '/deployer/requirements/task/check_media_support.php');
5556
require_once(__DIR__ . '/deployer/requirements/task/check_php_extensions.php');

‎deployer/dev/task/dump.php‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,9 @@
2020

2121
$dbSyncTool = requireSyncTool('dump');
2222

23-
// php-sync-tool has no -kd/-dn equivalent yet (konradmichalik/php-sync-tool#33); until it
24-
// does, the dump lands wherever the project's own sync-tool YAML config's "dump_dir" says,
25-
// not necessarily $dbDumpDir
26-
$dumpLocationOptions = usingPhpSyncTool($dbSyncTool) ? '' : "-kd $dbDumpDir -dn $dbDumpFilename";
23+
$dumpLocationOptions = usingPhpSyncTool($dbSyncTool)
24+
? '--keep-dump --target-dump-dir=' . escapeshellarg($dbDumpDir) . ' --dump-name=' . escapeshellarg($dbDumpFilename)
25+
: "-kd $dbDumpDir -dn $dbDumpFilename";
2726

2827
$dbSyncToolConfigPath = get('dev_db_sync_tool_config_path');
2928
runLocally(escapeshellarg($dbSyncTool) . " -f $dbSyncToolConfigPath/$dbSyncToolSync -y $dumpLocationOptions $additionalOptions", forceOutput: true);

‎deployer/dev/task/import.php‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33
namespace Deployer;
44

55
task('dev:import', function () {
6-
$dbDumpDir = get('dev_tr_db_dump_dir');
7-
$dbDumpFilename = getRecentDatabaseCacheDumpFilename();
8-
96
$dbSyncToolSync = get('dev_db_sync_tool_default_sync');
107

118
$dbSyncTool = requireSyncTool('import');
129

1310
$dbSyncToolConfigPath = get('dev_db_sync_tool_config_path');
14-
runLocally(escapeshellarg($dbSyncTool) . " -f $dbSyncToolConfigPath/$dbSyncToolSync -y -i $dbDumpDir/$dbDumpFilename.sql", forceOutput: true);
11+
runLocally(escapeshellarg($dbSyncTool) . " -f $dbSyncToolConfigPath/$dbSyncToolSync -y -i " . escapeshellarg(getRecentDatabaseCacheDumpPath()), forceOutput: true);
1512
})
1613
->desc('Import a database dump with db-sync-tool');

‎deployer/functions.php‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,17 @@ function runExtended(string $command, ?int $timeout = null, ?int $idle_timeout =
304304

305305
function getRecentDatabaseCacheDumpPath(): string
306306
{
307-
return get('dev_tr_db_dump_dir') . '/' . getRecentDatabaseCacheDumpFilename() . '.sql';
307+
return get('dev_tr_db_dump_dir') . '/' . getRecentDatabaseCacheDumpFilename() . getDatabaseCacheDumpSuffix();
308+
}
309+
310+
/**
311+
* php-sync-tool compresses every dump it keeps, db-sync-tool leaves a plain .sql file.
312+
*/
313+
function getDatabaseCacheDumpSuffix(): string
314+
{
315+
$dbSyncTool = get('db_sync_tool');
316+
317+
return false !== $dbSyncTool && usingPhpSyncTool($dbSyncTool) ? '.sql.gz' : '.sql';
308318
}
309319

310320
function getRecentDatabaseCacheDumpFilename(): string
@@ -323,7 +333,7 @@ function cleanUpDatabaseCacheDumps(int $days = 7): void
323333
run("mkdir -p $dbDumpDir");
324334

325335
// cleanup beforehand: delete all dump files with the above naming scheme older than 7 days
326-
run("find $dbDumpDir -name '*.sql' -mtime +$days -delete");
336+
run("find $dbDumpDir \\( -name '*.sql' -o -name '*.sql.gz' \\) -mtime +$days -delete");
327337
}
328338

329339
/**

‎deployer/requirements/config/set.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
// Enable/disable per check category
1111
set('requirements_check_locales_enabled', true);
1212
set('requirements_check_packages_enabled', true);
13+
set('requirements_check_sync_tool_enabled', true);
1314
set('requirements_check_image_processing_enabled', true);
1415
set('requirements_check_media_support_enabled', true);
1516
set('requirements_check_php_extensions_enabled', true);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Deployer;
6+
7+
// The sync tools run on the deploy machine, so unlike the other checks this one does not look at the host.
8+
task('requirements:check:sync_tool', function (): void {
9+
if (!get('requirements_check_sync_tool_enabled')) {
10+
return;
11+
}
12+
13+
$dbSyncTool = get('db_sync_tool');
14+
15+
if (false === $dbSyncTool) {
16+
addRequirementRow('Sync tool', REQUIREMENT_SKIP, 'Disabled (db_sync_tool is false)');
17+
18+
return;
19+
}
20+
21+
if (!syncToolAvailableLocally($dbSyncTool)) {
22+
addRequirementRow('Sync tool', REQUIREMENT_FAIL, "\"$dbSyncTool\" not available locally (preferred: composer require --dev konradmichalik/php-sync-tool)");
23+
24+
return;
25+
}
26+
27+
addRequirementRow('Sync tool', REQUIREMENT_OK, usingPhpSyncTool($dbSyncTool) ? "php-sync-tool ($dbSyncTool)" : "db-sync-tool ($dbSyncTool, legacy)");
28+
})->hidden();

‎deployer/requirements/task/list.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@
9696
writeln(' ' . implode(', ', $packageNames));
9797
}
9898

99+
// Sync Tool
100+
if (get('requirements_check_sync_tool_enabled')) {
101+
writeln('');
102+
writeln('<fg=yellow;options=bold>Sync Tool (deploy machine)</>');
103+
writeln(' php-sync-tool (vendor/bin/sync-tool) or the legacy db-sync-tool');
104+
}
105+
99106
// Locales
100107
if (get('requirements_check_locales_enabled')) {
101108
writeln('');

‎deployer/requirements/task/requirements.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
task('requirements:check', [
88
'requirements:check:locales',
99
'requirements:check:packages',
10+
'requirements:check:sync_tool',
1011
'requirements:check:image_processing',
1112
'requirements:check:media_support',
1213
'requirements:check:php_extensions',

‎docs/DEV.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,9 @@ Use `cache-db` to cache the database for 1 day after syncing it. Useful for larg
2929
<a name="dump"></a>
3030
## dev:dump
3131

32-
Dump the local database to an sql file. This task is used by `dev:tr` with `--cache-db` option set to create a once-per-day sql dump.
32+
Dump the database of the sync source (see `dev_db_sync_tool_default_sync`) to an sql file. This task is used by `dev:tr` with `--cache-db` option set to create a once-per-day sql dump. The dump is kept in `dev_tr_db_dump_dir` as `<date>.sql` with the legacy db-sync-tool and as `<date>.sql.gz` with php-sync-tool, which compresses it.
33+
34+
php-sync-tool also leaves its copy of the dump in the dump directory of the source (default `/tmp/`) and only removes old dumps with its own file name prefix. Clean up the directory there or point `dump_dir` in the sync configuration to a directory that is cleaned regularly.
3335
```bash
3436
$ dep dev:dump local
3537
```

‎docs/FEATURE.md‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,9 +165,9 @@ before('deploy:success', 'feature:notify');
165165
The MS Teams webhook comes from `msteams_webhook` or the environment variable `DEPLOYER_CONFIG_NOTIFICATION_MSTEAMS_WEBHOOK`. A missing webhook or a request that cannot be sent only logs a warning, the deployment is not failed.
166166

167167
### Synchronization
168-
The `feature:sync` command synchronizes the feature branch instance with data from a different instance using the [db-sync-tool](https://github.com/jackd248/db-sync-tool).
168+
The `feature:sync` command synchronizes the feature branch instance with data from a different instance using [php-sync-tool](https://github.com/konradmichalik/php-sync-tool) or the legacy [db-sync-tool](https://github.com/jackd248/db-sync-tool) (see the [prerequisites](#prerequirements)).
169169

170-
You need to configure the paths within your `deploy.php` to the db-sync-tool configuration files:
170+
You need to configure the paths within your `deploy.php` to the sync configuration files:
171171

172172
```php
173173
# general sync configuration file for feature branches
@@ -178,13 +178,13 @@ set('feature_sync_target_path', '{{deploy_path}}/shared/.env.local');
178178
set('feature_sync_target_path_files', '{{deploy_path}}/shared/public/upload/media/');
179179
```
180180

181-
Having an optionally entry `files` in your sync configuration (e.g. [sync-stage-to-feature.yaml](../deployer/feature/example/.deployment/db-sync-tool/sync-stage-to-feature.yaml#17)) enables the [file-sync-tool](https://github.com/jackd248/file-sync-tool) for a simple file synchronisation feature.
181+
Having an optionally entry `files` in your sync configuration (e.g. [sync-stage-to-feature.yaml](../deployer/feature/example/.deployment/db-sync-tool/sync-stage-to-feature.yaml#17)) enables a simple file synchronisation. php-sync-tool does it within the same call (`--with-files`), the legacy tools with the separate [file-sync-tool](https://github.com/jackd248/file-sync-tool).
182182

183183
It's possible to override db_sync or file_sync command (setting it to false disables the feature):
184184
```php
185185
# disables db_sync feature entirely
186186
set('db_sync_tool', false);
187-
# disables file_sync feature entirely
187+
# disables file_sync feature entirely (with php-sync-tool no --with-files)
188188
set('file_sync_tool', false);
189189
```
190190

0 commit comments

Comments
 (0)