Skip to content

Commit 44bf54b

Browse files
committed
Add data only option to import
1 parent bd490a3 commit 44bf54b

File tree

5 files changed

+51
-33
lines changed

5 files changed

+51
-33
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
# Version 3.0.5
1+
# Version 3.0.6
2+
3+
- **Added** Way to only import data and not the schema, for cases where you use something else to migrate your schema.
4+
- Set `SCHEMA_SYNC_DATA_ONLY=true` in your environment file.
5+
- Or when using `import` command, use `--data` option.
6+
7+
## Version 3.0.5
28

39
- **Added** `SCHEMA_SYNC_MERGE` option to environment variables
410
- Only insert and update items found in the import set (including duplicates). Does not remove items in the DB that are not in the import set.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ Besides auto importing and exporting, you can also run the commands manually.
138138
| Command | Description |
139139
| ------- | ----------- |
140140
| `export` | Export the schema and data from the Directus API |
141-
| `import` | Import the schema and data to the Directus API (options: `merge`) |
141+
| `import` | Import the schema and data to the Directus API (options: `merge`, `data`) |
142142
| `hash`| Recalculate the hash for all the data files (already happens after export) |
143143
| `install` | Install the extension's columns in the database and add the config folder (options: `force`) |
144144
| `export-schema` | Export only the schema (options: --split <boolean>) |

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "directus-extension-schema-sync",
33
"description": "Sync schema and data betwreen Directus instances",
44
"icon": "sync_alt",
5-
"version": "3.0.5",
5+
"version": "3.0.6",
66
"repository": {
77
"type": "git",
88
"url": "https://github.com/bcc-code/directus-schema-sync.git"

src/index.ts

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,39 +36,50 @@ const registerHook: HookConfig = async ({ action, init }, { env, services, datab
3636

3737
// We need to do this in async in order to load the config files
3838
let _exportManager: ExportManager;
39-
const exportManager = async () => {
40-
if (!_exportManager) {
41-
_exportManager = new ExportManager(logger);
4239

43-
if (env.SCHEMA_SYNC_DATA_ONLY !== true) {
44-
_exportManager.addExporter({
45-
watch: ['collections', 'fields', 'relations'],
46-
exporter: new SchemaExporter(getSchemaService, logger, schemaOptions),
47-
});
48-
}
40+
const createExportManager = async (dataOnly = false) => {
41+
const exportMng = new ExportManager(logger);
4942

50-
const { syncDirectusCollections } = (await nodeImport(ExportHelper.schemaDir, 'directus_config.js')) as {
51-
syncDirectusCollections: ExportCollectionConfig;
52-
};
53-
const { syncCustomCollections } = (await nodeImport(ExportHelper.schemaDir, 'config.js')) as {
43+
if (!dataOnly) {
44+
exportMng.addExporter({
45+
watch: ['collections', 'fields', 'relations'],
46+
exporter: new SchemaExporter(getSchemaService, logger, schemaOptions),
47+
});
48+
}
49+
50+
const { syncDirectusCollections } = (await nodeImport(ExportHelper.schemaDir, 'directus_config.js')) as {
51+
syncDirectusCollections: ExportCollectionConfig;
52+
};
53+
const { syncCustomCollections } = (await nodeImport(ExportHelper.schemaDir, 'config.js')) as {
54+
syncCustomCollections: ExportCollectionConfig;
55+
};
56+
exportMng.addCollectionExporter(syncDirectusCollections, getItemsService);
57+
exportMng.addCollectionExporter(syncCustomCollections, getItemsService);
58+
59+
// Additional config
60+
if (env.SCHEMA_SYNC_CONFIG) {
61+
const { syncCustomCollections } = (await nodeImport(ExportHelper.schemaDir, env.SCHEMA_SYNC_CONFIG)) as {
5462
syncCustomCollections: ExportCollectionConfig;
5563
};
56-
_exportManager.addCollectionExporter(syncDirectusCollections, getItemsService);
57-
_exportManager.addCollectionExporter(syncCustomCollections, getItemsService);
58-
59-
// Additional config
60-
if (env.SCHEMA_SYNC_CONFIG) {
61-
const { syncCustomCollections } = (await nodeImport(ExportHelper.schemaDir, env.SCHEMA_SYNC_CONFIG)) as {
62-
syncCustomCollections: ExportCollectionConfig;
63-
};
64-
if (syncCustomCollections) {
65-
_exportManager.addCollectionExporter(syncCustomCollections, getItemsService);
66-
} else {
67-
logger.warn(`Additonal config specified but not exporting "syncCustomCollections"`);
68-
}
64+
if (syncCustomCollections) {
65+
exportMng.addCollectionExporter(syncCustomCollections, getItemsService);
66+
} else {
67+
logger.warn(`Additonal config specified but not exporting "syncCustomCollections"`);
6968
}
7069
}
7170

71+
return exportMng;
72+
}
73+
74+
const exportManager = async (dataOnly = false) => {
75+
if (dataOnly && env.SCHEMA_SYNC_DATA_ONLY !== true) {
76+
return await createExportManager(true);
77+
}
78+
79+
if (!_exportManager) {
80+
_exportManager = await createExportManager(env.SCHEMA_SYNC_DATA_ONLY !== true);
81+
}
82+
7283
return _exportManager;
7384
};
7485

@@ -177,10 +188,11 @@ const registerHook: HookConfig = async ({ action, init }, { env, services, datab
177188
.command('import')
178189
.description('Import the schema and all available data from file to DB.')
179190
.option('--merge', 'Only upsert data and not delete')
180-
.action(async ({ merge }: { merge: boolean }) => {
191+
.option('--data', 'Only import data and not schema')
192+
.action(async ({ merge, data }: { merge: boolean; data: boolean }) => {
181193
try {
182194
logger.info(`Importing everything from: ${ExportHelper.dataDir}`);
183-
const expMng = await exportManager();
195+
const expMng = await exportManager(data);
184196
await expMng.loadAll(merge);
185197

186198
logger.info('Done!');

0 commit comments

Comments
 (0)