Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit 8a3e2bd

Browse files
Merge pull request #36 from strapi/feature/migrateCoreContServ
Add migrations for factory files, config, and various other files
2 parents 16d2c84 + 5a5272b commit 8a3e2bd

31 files changed

+630
-56
lines changed

.github/workflows/tests.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
name: 'Tests'
2-
on: [push]
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
pull_request:
8+
paths-ignore:
9+
- '**.mdx?'
10+
- '**.md?'
11+
312
jobs:
413
unit_tests:
514
name: 'unit_tests (node: ${{ matrix.node }})'
615
runs-on: ubuntu-latest
716
strategy:
817
matrix:
9-
node: [12, 14, 16]
18+
node: [14, 16]
1019
steps:
1120
- uses: actions/checkout@v2
1221
with:

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,6 @@ dist
120120
.node_modules
121121
.DS_Store
122122
.vscode
123+
124+
# migration related
125+
v3/*

lib/v4/migration-helpers/__mocks__/mock-package-json-v3.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,9 @@
1010
"strapi-plugin-upload": "3.6.8",
1111
"strapi-plugin-i18n": "3.6.8",
1212
"strapi-connector-bookshelf": "3.6.8"
13+
},
14+
"engines": {
15+
"node": ">=10.16.0 <=14.x.x",
16+
"npm": ">=10.16.0 <=14.x.x"
1317
}
1418
}

lib/v4/migration-helpers/__tests__/update-api-folder-structure.test.js

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ const fs = require('fs-extra');
1515

1616
const updateApiFolderStructure = require('../update-api-folder-structure');
1717
const updateContentTypes = require('../convert-models-to-content-types');
18-
const updateRoutes = require('../update-routes');
1918
const updatePolicies = require('../update-api-policies');
2019
const runJscodeshift = require('../../utils/run-jscodeshift');
2120

@@ -64,18 +63,6 @@ describe('update api folder structure', () => {
6463
]);
6564
});
6665

67-
it('updates routes', async () => {
68-
const appPath = 'test-dir';
69-
70-
await updateApiFolderStructure(appPath);
71-
72-
const expectedV4Path = join(resolve(appPath), 'src', 'api');
73-
expect(updateRoutes.mock.calls).toEqual([
74-
[join(expectedV4Path, 'test'), 'test', 'test'],
75-
[join(expectedV4Path, 'test-two'), 'test-two', 'test-two'],
76-
]);
77-
});
78-
7966
it('updates policies', async () => {
8067
const appPath = 'test-dir';
8168

lib/v4/migration-helpers/__tests__/update-package-dependencies.test.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,13 @@ describe('update api folder structure', () => {
4646
const v4PackageJSON = {
4747
dependencies: {
4848
'@strapi/strapi': '4.0.0',
49-
'@strapi/admin': '4.0.0',
50-
'@strapi/utils': '4.0.0',
51-
'@strapi/plugin-content-type-builder': '4.0.0',
52-
'@strapi/plugin-content-manager': '4.0.0',
5349
'@strapi/plugin-users-permissions': '4.0.0',
54-
'@strapi/plugin-email': '4.0.0',
55-
'@strapi/plugin-upload': '4.0.0',
5650
'@strapi/plugin-i18n': '4.0.0',
5751
},
52+
engines: {
53+
node: '>=14.19.1 <=16.x.x',
54+
npm: '>=6.0.0',
55+
},
5856
};
5957

6058
await updatePackageDependencies(testDir);

lib/v4/migration-helpers/convert-models-to-content-types.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,20 @@ const convertModelToContentType = async (apiPath, contentTypeName) => {
5959
required: attribute.required,
6060
private: attribute.private,
6161
};
62+
} else if (
63+
attribute.plugin === 'admin' &&
64+
(attribute.model === 'user' || attribute.collection === 'user')
65+
) {
66+
// Handle admin user relation
67+
attribute = {
68+
type: 'relation',
69+
target: 'admin::user',
70+
relation: _.has(attribute, 'collection') ? 'oneToMany' : 'oneToOne',
71+
};
72+
73+
if (attribute.private) {
74+
attribute.private = true;
75+
}
6276
} else if (attribute.via && attribute.model && isSingular(attribute.via)) {
6377
// One-To-One
6478
attribute = getRelationObject('oneToOne', { ...attribute, inversed: true });
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const { join } = require('path');
2+
const fs = require('fs-extra');
3+
4+
const logger = require('../../global/utils/logger');
5+
6+
/**
7+
* @description Migrates v3 config structure to v4 structure
8+
*
9+
* @param {string} configPath Path to the config folder
10+
* @param {string} database Database Type
11+
* @param {function} liquidEngine Liquid engine to use for templating
12+
*/
13+
module.exports = async (configPath, database, liquidEngine) => {
14+
const files = ['admin', 'api', 'database', 'middlewares', 'plugins', 'server'];
15+
16+
let paths = {};
17+
18+
files.forEach((file) => {
19+
paths[file] = join(configPath, file);
20+
});
21+
22+
try {
23+
// create config path
24+
await fs.ensureDir(configPath);
25+
} catch (error) {
26+
logger.error(`an error occurred when creating ${configPath} folder`);
27+
console.log(error);
28+
}
29+
30+
for (const jsFile in files) {
31+
let template;
32+
33+
const fileName = files[jsFile];
34+
35+
try {
36+
// Compile the template
37+
if (fileName !== 'database') {
38+
template = await liquidEngine.renderFile(`config-${fileName}`);
39+
} else {
40+
template = await liquidEngine.renderFile(`config-${fileName}-${database}`);
41+
}
42+
43+
// Create the js file
44+
await fs.ensureFile(`${paths[fileName]}.js`);
45+
46+
// Create write stream for new js file
47+
const file = fs.createWriteStream(`${paths[fileName]}.js`);
48+
49+
// Export core controllers from liquid template file
50+
file.write(template);
51+
52+
// Close the write stream
53+
file.end();
54+
} catch (error) {
55+
logger.error(`an error occurred when migrating ${fileName} config file`);
56+
console.log(error);
57+
}
58+
}
59+
};
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
## Server Settings
2+
3+
HOST={{ host }}
4+
PORT={{ port }}
5+
APP_KEYS={{ appKeys }}
6+
7+
## Admin Settings
8+
9+
API_TOKEN_SALT={{ apiTokenSalt }}
10+
ADMIN_JWT_SECRET={{ adminJwtSecret }}
11+
12+
## Users-Permissions Plugin Settings
13+
14+
JWT_SECRET={{ jwtSecret }}
15+
16+
## Database Settings
17+
{% if databaseType == 'mysql' %}
18+
DATABASE_HOST={{ databaseHost }}
19+
DATABASE_PORT={{ databasePort }}
20+
DATABASE_NAME={{ databaseName }}
21+
DATABASE_USERNAME={{ databaseUsername }}
22+
DATABASE_PASSWORD={{ databasePassword }}
23+
DATABASE_SSL={{ databaseSsl }}
24+
{% elsif databaseType == 'postgres' %}
25+
DATABASE_HOST={{ databaseHost }}
26+
DATABASE_PORT={{ databasePort }}
27+
DATABASE_NAME={{ databaseName }}
28+
DATABASE_USERNAME={{ databaseUsername }}
29+
DATABASE_PASSWORD={{ databasePassword }}
30+
DATABASE_SSL={{ databaseSsl }}
31+
DATABASE_SCHEMA={{ databaseSchema }}
32+
{% else %}
33+
DATABASE_FILENAME={{ databaseFilename }}
34+
{% endif %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
module.exports = ({ env }) => ({
2+
auth: {
3+
secret: env('ADMIN_JWT_SECRET'),
4+
},
5+
apiToken: {
6+
salt: env('API_TOKEN_SALT'),
7+
},
8+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
rest: {
3+
defaultLimit: 25,
4+
maxLimit: 100,
5+
withCount: true,
6+
},
7+
};

0 commit comments

Comments
 (0)