mysqlsyncr 1.0.40 → 1.0.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/index.js +55 -53
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mysqlsyncr",
3
- "version": "1.0.40",
3
+ "version": "1.0.42",
4
4
  "description": "",
5
5
  "main": "src/index.js",
6
6
  "type": "module",
package/src/index.js CHANGED
@@ -47,60 +47,62 @@ class DBStructureChecker {
47
47
  // Export the DBStructureChecker class
48
48
  export { DBStructureChecker, dumpAllDatabases, compareAllDatabases, startApiServer };
49
49
 
50
- // Start the server only if the 'ui' command is used
51
- (async () => {
52
- const { command, config, argv } = cli();
53
- console.log("MySQLsyncr started via command line")
54
- if (command === 'ui') {
55
- const PORT = argv.uiPort
56
- startApiServer(PORT, argv); // Start the Socket.IO server
57
- console.log('Server started on port', PORT);
58
- console.log('Open http://localhost:' + PORT + ' in your browser to use the UI');
59
- if(argv.openUI && argv.openUI === true || argv.openUI === 'true'){
60
- await open(`http://localhost:${PORT}`);
61
- }
62
- } else {
63
- // Handle other commands (dump, compare, update) as before
64
- const checker = new DBStructureChecker(config, argv.database);
65
- checker.connection.on('connect', async () => {
66
- console.log('Connected to the database');
67
- checker.targetDatabase = argv.database;
68
- checker.command = command;
69
- checker.options = argv;
70
- try {
71
- if (command === 'dump') {
72
- console.log('Dumping the database structure to files');
73
- await dumpAllDatabases(checker.connection, command, argv);
74
- } else if (command === 'compare') {
75
- console.log('Comparing the database structure with JSON dumps (dry run)');
76
- let diffs = await compareAllDatabases(checker.connection, argv.output, false, command, argv); // Dry run
77
- let totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
78
- console.log('Total differences count:', totalDiffs);
79
- fs.writeFileSync(path.join(argv.output, 'diffs.json'), JSON.stringify(diffs, null, 4));
80
- } else if (command === 'update') {
81
- console.log('Updating the database structure based on JSON dumps');
82
- let totalDiffs = Infinity; // Initialize with a large number
83
- let previousDiffs = totalDiffs; // Store previous differences
84
- let runCount = 0; // Counter for the number of runs
85
- let maxRuns = 6;
86
- while (runCount < maxRuns && totalDiffs > 0 && (totalDiffs < previousDiffs || previousDiffs === Infinity)) {
87
- console.log('Running update command. Run count:', runCount);
88
- let diffs = await compareAllDatabases(checker.connection, argv.output, true, command, argv); // Apply updates
89
- previousDiffs = totalDiffs; // Update previous differences
90
- totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
50
+ // Only run this if this is the main module
51
+ if (import.meta.url === `file://${process.argv[1]}`) {
52
+ (async () => {
53
+ const { command, config, argv } = cli();
54
+ console.log("MySQLsyncr started via command line")
55
+ if (command === 'ui') {
56
+ const PORT = argv.uiPort
57
+ startApiServer(PORT, argv); // Start the Socket.IO server
58
+ console.log('Server started on port', PORT);
59
+ console.log('Open http://localhost:' + PORT + ' in your browser to use the UI');
60
+ if(argv.openUI && argv.openUI === true || argv.openUI === 'true'){
61
+ await open(`http://localhost:${PORT}`);
62
+ }
63
+ } else {
64
+ // Handle other commands (dump, compare, update) as before
65
+ const checker = new DBStructureChecker(config, argv.database);
66
+ checker.connection.on('connect', async () => {
67
+ console.log('Connected to the database');
68
+ checker.targetDatabase = argv.database;
69
+ checker.command = command;
70
+ checker.options = argv;
71
+ try {
72
+ if (command === 'dump') {
73
+ console.log('Dumping the database structure to files');
74
+ await dumpAllDatabases(checker.connection, command, argv);
75
+ } else if (command === 'compare') {
76
+ console.log('Comparing the database structure with JSON dumps (dry run)');
77
+ let diffs = await compareAllDatabases(checker.connection, argv.output, false, command, argv); // Dry run
78
+ let totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
91
79
  console.log('Total differences count:', totalDiffs);
92
- runCount++;
93
- if(totalDiffs > 0 && runCount < maxRuns){
94
- console.log('Total differences count is still greater than 0. Waiting 1 second before next run. Run count:', runCount);
95
- await new Promise(resolve => setTimeout(resolve, 1000));
80
+ fs.writeFileSync(path.join(argv.output, 'diffs.json'), JSON.stringify(diffs, null, 4));
81
+ } else if (command === 'update') {
82
+ console.log('Updating the database structure based on JSON dumps');
83
+ let totalDiffs = Infinity; // Initialize with a large number
84
+ let previousDiffs = totalDiffs; // Store previous differences
85
+ let runCount = 0; // Counter for the number of runs
86
+ let maxRuns = 6;
87
+ while (runCount < maxRuns && totalDiffs > 0 && (totalDiffs < previousDiffs || previousDiffs === Infinity)) {
88
+ console.log('Running update command. Run count:', runCount);
89
+ let diffs = await compareAllDatabases(checker.connection, argv.output, true, command, argv); // Apply updates
90
+ previousDiffs = totalDiffs; // Update previous differences
91
+ totalDiffs = Object.values(diffs).reduce((acc, arr) => acc + arr.length, 0);
92
+ console.log('Total differences count:', totalDiffs);
93
+ runCount++;
94
+ if(totalDiffs > 0 && runCount < maxRuns){
95
+ console.log('Total differences count is still greater than 0. Waiting 1 second before next run. Run count:', runCount);
96
+ await new Promise(resolve => setTimeout(resolve, 1000));
97
+ }
96
98
  }
97
99
  }
100
+ } catch (err) {
101
+ console.error(err.message);
102
+ } finally {
103
+ await checker.closeConnection();
98
104
  }
99
- } catch (err) {
100
- console.error(err.message);
101
- } finally {
102
- await checker.closeConnection();
103
- }
104
- });
105
- }
106
- })();
105
+ });
106
+ }
107
+ })();
108
+ }