Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: better logging for diff issues #34

Merged
merged 7 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -437,3 +437,19 @@ what makes `directus-sync` a robust tool for managing Directus instances in a te
By following these mechanisms, `directus-sync` streamlines the development workflow, allowing for local changes to be
efficiently deployed to various environments, all while keeping the Directus instances synchronized and
version-controlled.

# Troubleshooting

## Synchronization Failures Due to Firewall Configurations

Some requests made by `directus-sync`, particularly during the **diff** process, can be blocked by certain firewall
configurations.
You should see this log message if this is the case:

```text
[12:40:43.095] ERROR (54159): [snapshot] Could not get the diff from the Directus instance
```

Check and adjust your firewall settings to ensure they don't block or interfere with directus-sync operations.
This may involve whitelisting IP addresses or modifying rules to allow the necessary request patterns.
More information in this [issue](https://github.com/tractr/directus-sync/issues/33).
11 changes: 2 additions & 9 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions packages/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# 1.0.0 (2024-02-26)

**Note:** Version bump only for package directus-extension-sync

# 0.4.0 (2024-01-24)

### Features
Expand Down
2 changes: 1 addition & 1 deletion packages/api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "directus-extension-sync",
"description": "This extension exposes routes to manage id mapping for Directus sync.",
"icon": "extension",
"version": "0.4.0",
"version": "1.0.0",
"author": "Edouard Demotes-Mainard <edouard@tractr.net>",
"repository": {
"type": "git",
Expand Down
6 changes: 6 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [1.0.0](https://github.com/tractr/directus-sync/compare/directus-sync@0.6.0...directus-sync@1.0.0) (2024-02-26)

### Features

- add presets sync support ([#32](https://github.com/tractr/directus-sync/issues/32)) ([451f711](https://github.com/tractr/directus-sync/commit/451f711a610a0bebc1bb1e2e05da9bcb1151f5c3))

# [0.6.0](https://github.com/tractr/directus-sync/compare/directus-sync@0.5.0...directus-sync@0.6.0) (2024-01-31)

### Features
Expand Down
4 changes: 2 additions & 2 deletions packages/cli/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions packages/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "directus-sync",
"version": "0.6.0",
"version": "1.0.0",
"description": "This is a CLI tool to sync schema and configuration from one Directus instance to another.",
"main": "index.js",
"bin": {
Expand All @@ -20,7 +20,6 @@
},
"license": "MIT",
"devDependencies": {
"@types/app-root-path": "^1.2.7",
"@types/fs-extra": "^11.0.4",
"@types/http-errors": "^2.0.4",
"@types/jest": "^29.5.5",
Expand Down
15 changes: 14 additions & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dotenv/config';
import 'reflect-metadata';
import { Option, program } from 'commander';
import { resolve } from 'path';
import { readJSONSync } from 'fs-extra';
import {
DefaultConfig,
DefaultConfigPaths,
Expand Down Expand Up @@ -65,7 +67,7 @@ const forceOption = new Option(
);

program
.version(process.env.npm_package_version ?? 'unknown')
.version(getVersion())
.addOption(debugOption)
.addOption(directusUrlOption)
.addOption(directusTokenOption)
Expand Down Expand Up @@ -145,3 +147,14 @@ function wrapAction(action: () => Promise<void>) {
.then(logEndAndClose);
};
}

function getVersion(): string {
try {
const { version } = readJSONSync(
resolve(__dirname, '..', 'package.json'),
) as { version?: string };
return version ?? 'undefined';
} catch (e) {
return 'error';
}
}
17 changes: 17 additions & 0 deletions packages/cli/src/lib/services/snapshot/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/* eslint-disable @typescript-eslint/no-explicit-any */
export interface RawSchemaDiffOutput {
hash: string;
diff: Record<string, any>;
}

export interface SchemaDiffOutput {
hash: string;
diff: SnapshotDiffDiff | null | undefined;
}

export interface SnapshotDiffDiff {
collections: unknown[];
fields: unknown[];
relations: unknown[];
}

export interface Snapshot {
version: number;
directus: string;
Expand Down
33 changes: 20 additions & 13 deletions packages/cli/src/lib/services/snapshot/snapshot-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ import { Inject, Service } from 'typedi';
import { MigrationClient } from '../migration-client';
import { schemaApply, schemaDiff, schemaSnapshot } from '@directus/sdk';
import path from 'path';
import { Collection, Field, Relation, Snapshot } from './interfaces';
import {
Collection,
Field,
RawSchemaDiffOutput,
Relation,
SchemaDiffOutput,
Snapshot,
} from './interfaces';
import { mkdirpSync, readJsonSync, removeSync, writeJsonSync } from 'fs-extra';
import { LOGGER } from '../../constants';
import pino from 'pino';
import { getChildLogger, loadJsonFilesRecursively } from '../../helpers';
import { ConfigService } from '../config';

interface SnapshotDiffDiff {
collections: unknown[];
fields: unknown[];
relations: unknown[];
}

const SNAPSHOT_JSON = 'snapshot.json';
const INFO_JSON = 'info.json';
const COLLECTIONS_DIR = 'collections';
Expand Down Expand Up @@ -61,11 +62,13 @@ export class SnapshotClient {
*/
async push() {
const diff = await this.diffSnapshot();
if (!diff?.diff) {
if (!diff) {
this.logger.error('Could not get the diff from the Directus instance');
} else if (!diff.diff) {
this.logger.info('No changes to apply');
} else {
const directus = await this.migrationClient.get();
await directus.request(schemaApply(diff));
await directus.request(schemaApply(diff as RawSchemaDiffOutput));
this.logger.info('Changes applied');
}
}
Expand All @@ -75,10 +78,12 @@ export class SnapshotClient {
*/
async diff() {
const diff = await this.diffSnapshot();
if (!diff?.diff) {
if (!diff) {
this.logger.error('Could not get the diff from the Directus instance');
} else if (!diff.diff) {
this.logger.info('No changes to apply');
} else {
const { collections, fields, relations } = diff.diff as SnapshotDiffDiff;
const { collections, fields, relations } = diff.diff;
if (collections) {
this.logger.info(
`Found ${collections.length} change${
Expand Down Expand Up @@ -183,10 +188,12 @@ export class SnapshotClient {
/**
* Get the diff from Directus instance
*/
protected async diffSnapshot() {
protected async diffSnapshot(): Promise<SchemaDiffOutput | undefined> {
const directus = await this.migrationClient.get();
const snapshot = this.loadData();
return await directus.request(schemaDiff(snapshot, this.force));
return (await directus.request(schemaDiff(snapshot, this.force))) as
| SchemaDiffOutput
| undefined;
}

/**
Expand Down
Loading