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

Commit e924f6e

Browse files
Merge pull request #37 from strapi/feat/mongoSQL
Add support for PG, MySQL, and MariaDB to Mongo migration script
2 parents 790f77e + e8bddc0 commit e924f6e

15 files changed

+400
-110
lines changed

.gitignore

+2-5
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ web_modules/
7777
.yarn-integrity
7878

7979
# dotenv environment variable files
80-
.env
81-
.env.development.local
82-
.env.test.local
83-
.env.production.local
84-
.env.local
80+
*.env*
81+
!*.env*.example
8582

8683
# parcel-bundler cache (https://parceljs.org/)
8784
.cache

.prettierrc.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
endOfLine: 'lf',
3+
semi: true,
4+
singleQuote: true,
5+
tabWidth: 2,
6+
trailingComma: 'es5',
7+
printWidth: 100,
8+
arrowParens: 'always',
9+
};

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Some scripts may be in various states, *you should pay careful attention to what
2424

2525
## Scripts
2626

27-
- [Migration from v3 MongoDB to v3 SQL](./v3-mongodb-v3-sql/README.md) - **Currently in Alpha Testing**
27+
- [Migration from v3 MongoDB to v3 SQL](./v3-mongodb-v3-sql/README.md) - **Currently in Beta Testing**
2828
- [Migration from v3 SQL to v4 SQL](./v3-sql-v4-sql/README.md) - **Currently in Beta Testing**
2929

3030
## Callouts

v3-mongodb-v3-sql/.env.mysql.example

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# MongoDB Settings
2+
MONGO_USER=root
3+
MONGO_PASSWORD=strapi
4+
MONGO_HOST=127.0.0.1
5+
MONGO_PORT=27017
6+
MONGO_DB=admin
7+
8+
# SQL Settings
9+
SQL_CLIENT=mysql
10+
DATABASE_HOST=127.0.0.1
11+
DATABASE_PORT=3306
12+
DATABASE_USER=strapiUser
13+
DATABASE_PASSWORD=strapiPassword
14+
DATABASE_DATABASE=strapi

v3-mongodb-v3-sql/.env.pg.example

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# MongoDB Settings
2+
MONGO_USER=root
3+
MONGO_PASSWORD=strapi
4+
MONGO_HOST=127.0.0.1
5+
MONGO_PORT=27017
6+
MONGO_DB=admin
7+
8+
# SQL Settings
9+
SQL_CLIENT=postgres
10+
DATABASE_HOST=127.0.0.1
11+
DATABASE_PORT=5432
12+
DATABASE_USER=strapiUser
13+
DATABASE_PASSWORD=strapiPassword
14+
DATABASE_DATABASE=strapi
15+
DATABASE_SCHEMA=public

v3-mongodb-v3-sql/README.md

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Migration script from Strapi v3 on MongoDB to Strapi v3 on SQL
22

3-
> For now this works for sqlite only and can then be used to dump into pg or mysql (will automate the other two soon)
4-
53
## Preparation steps
64

7-
1. Preform a backup of your database you wish to migrate and store that backup somewhere secure
5+
- Preform a backup of your database you wish to migrate and store that backup somewhere secure
6+
- Create a new database on your choice of database engine (mysql or pg, sqlite will be created automatically)
87

98
## Install
109

@@ -21,11 +20,14 @@ yarn
2120

2221
## Configuration
2322

24-
WIP
23+
1. Choose which database you are migrating to (sqlite, mysql, pg)
24+
2. Copy the corresponding `.env.DBTYPE.example` file to `.env` using something like `cp .env.pg.example .env`
25+
3. Modify the configuration in the `.env` to match your MongoDB source and your SQL destination
2526

2627
## Migration
2728

2829
1. Start by following the ["prepare the migration locally"](https://docs.strapi.io/developer-docs/latest/update-migration-guides/migration-guides/v4/data/mongo.html#prepare-the-migration-locally) guide on our documentation to do any needed code changes
2930
2. Run your SQL Strapi v3 in `develop` mode with an empty DB to generate the DB structure
30-
3. Turn off / kill the running Strapi v4 server
31+
3. Turn off / kill the running SQL Strapi v3 server
3132
4. Run migration script using `yarn start`
33+
5. Run your SQL Strapi v3 using `yarn develop` mode with the migrated DB to test the migration

v3-mongodb-v3-sql/dialects/mysql.js

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module.exports = (knex, inspector) => ({
2+
async delAllTables() {
3+
const tableList = await inspector.tables();
4+
5+
// clear all tables
6+
for (const table of tableList) {
7+
await knex(table).del();
8+
9+
await knex.raw(`
10+
ALTER table
11+
\`${table}\`
12+
AUTO_INCREMENT = 1;
13+
`);
14+
}
15+
16+
return tableList;
17+
},
18+
19+
async beforeMigration() {
20+
await knex.raw(`SET foreign_key_checks = 0;`);
21+
},
22+
23+
async afterMigration() {
24+
await knex.raw(`SET foreign_key_checks = 1;`);
25+
},
26+
});
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
module.exports = (knex, inspector) => ({
2+
async delAllTables() {
3+
const tableList = await inspector.tables();
4+
5+
// clear all tables
6+
for (const table of tableList) {
7+
await knex(table).del();
8+
}
9+
10+
return tableList;
11+
},
12+
13+
async beforeMigration() {
14+
// do nothing for postgres
15+
},
16+
17+
async afterMigration() {
18+
const tableList = await inspector.tables();
19+
20+
// restart sequence for tables
21+
for (const table of tableList) {
22+
let result = await knex.raw('select max(id) from ??', [table]);
23+
const max = result.rows[0].max;
24+
25+
if (max) {
26+
await knex.raw(`ALTER SEQUENCE ?? RESTART WITH ??;`, [table + '_id_seq', max + 1]);
27+
}
28+
}
29+
},
30+
});

v3-mongodb-v3-sql/dialects/sqlite3.js

+11-13
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,14 @@
1-
module.exports = (knex) => ({
1+
module.exports = (knex, inspector) => ({
22
async clearSequences(tableList) {
33
const hasTable = await knex.schema.hasTable(`sqlite_sequence`);
44

55
if (hasTable) {
6-
await knex("sqlite_sequence").del().whereIn("name", tableList);
6+
await knex('sqlite_sequence').del().whereIn('name', tableList);
77
}
88
},
99

1010
async delAllTables() {
11-
const res = await knex.raw(`
12-
SELECT
13-
name
14-
FROM
15-
sqlite_schema
16-
WHERE
17-
type ='table' AND
18-
name NOT LIKE 'sqlite_%';
19-
`);
20-
21-
const tableList = res.map((r) => r.name);
11+
const tableList = await inspector.tables();
2212

2313
// clear all tables
2414
for (const table of tableList) {
@@ -27,4 +17,12 @@ module.exports = (knex) => ({
2717

2818
return this.clearSequences(tableList);
2919
},
20+
21+
async beforeMigration() {
22+
// do nothing because sqlite3 doesn't have foreign key checks
23+
},
24+
25+
async afterMigration() {
26+
// do nothing because sqlite3 doesn't have foreign key checks
27+
},
3028
});

0 commit comments

Comments
 (0)