Skip to content
Merged
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
27 changes: 27 additions & 0 deletions deploy/docker/fs/opt/appsmith/utils/bin/move-to-postgres.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ let mongoDbUrl;
let mongoDumpFile = null;
const EXPORT_ROOT = "/appsmith-stacks/mongo-data";

// The minimum version of the MongoDB changeset that must be present in the mongockChangeLog collection to run this script.
// This is to ensure we are migrating the data from the stable version of MongoDB.
const MINIMUM_MONGO_CHANGESET = "add_empty_policyMap_for_null_entries";
const MONGO_MIGRATION_COLLECTION = "mongockChangeLog";

for (let i = 2; i < process.argv.length; ++i) {
const arg = process.argv[i];
if (arg.startsWith("--mongodb-url=") && !mongoDbUrl) {
Expand Down Expand Up @@ -77,6 +82,15 @@ if (isBaselineMode) {
const collectionNames = await mongoDb.listCollections({}, { nameOnly: true }).toArray();
const sortedCollectionNames = collectionNames.map(collection => collection.name).sort();

// Verify that the MongoDB data has been migrated to a stable version i.e. v1.43 before we start migrating the data to Postgres.
if (!await isMongoDataMigratedToStableVersion(mongoDb)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you move this logic to the isMongoDataMigratedToStableVersion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason for this ask? As the method suggests it only checks if the mongo data is migrated to correct changeId before starting the migration. Also if we go other route then instead of passing only the mongo client we will have to pass mongoServer as well which is not necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool works. Just wanted to keep all the changes related to version in a single method.

console.error("MongoDB migration check failed: Try upgrading the Appsmith instance to latest before opting for data migration.");
console.error(`Could not find the valid migration execution entry for "${MINIMUM_MONGO_CHANGESET}" in the "${MONGO_MIGRATION_COLLECTION}" collection.`);
await mongoClient.close();
mongoServer?.kill();
process.exit(1);
}

for await (const collectionName of sortedCollectionNames) {

console.log("Collection:", collectionName);
Expand Down Expand Up @@ -183,3 +197,16 @@ function mapClassToType(_class) {
return null;
}
}

/**
* Method to check if MongoDB data has migrated to a stable version before we start migrating the data to Postgres.
* @param {*} mongoDb - The MongoDB client.
* @returns {Promise<boolean>} - A promise that resolves to true if the data has been migrated to a stable version, false otherwise.
*/
async function isMongoDataMigratedToStableVersion(mongoDb) {
const doc = await mongoDb.collection(MONGO_MIGRATION_COLLECTION).findOne({
changeId: MINIMUM_MONGO_CHANGESET,
state: "EXECUTED",
});
return doc !== null;
}