mysqlsyncr 1.1.8 → 1.1.12
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.
- package/README.md +187 -186
- package/dist/index.cjs +218 -227
- package/dist/index.js +218 -227
- package/package.json +1 -1
- package/rollup.config.js +24 -24
- package/src/apply.js +362 -346
- package/src/index.js +3 -4
- package/src/parsers.js +151 -145
- package/src/socketServer.js +229 -229
package/src/index.js
CHANGED
|
@@ -101,10 +101,9 @@ export { DBStructureChecker, dumpAllDatabases, compare, startApiServer, apply, g
|
|
|
101
101
|
// Only run this if this is the main module
|
|
102
102
|
const isMain = (() => {
|
|
103
103
|
try {
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
return require.main === module;
|
|
104
|
+
const modulePath = fileURLToPath(import.meta.url);
|
|
105
|
+
return process.argv[1] === modulePath;
|
|
106
|
+
|
|
108
107
|
} catch (e) {
|
|
109
108
|
return false;
|
|
110
109
|
}
|
package/src/parsers.js
CHANGED
|
@@ -1,145 +1,151 @@
|
|
|
1
|
-
export const parseCreateTableSQL = (createTableSQL) => {
|
|
2
|
-
if(!createTableSQL) return null;
|
|
3
|
-
const name = createTableSQL.match(/CREATE TABLE\s+`([^`]+)`/)[1];
|
|
4
|
-
const columns = [];
|
|
5
|
-
const indexes = [];
|
|
6
|
-
const constraints = [];
|
|
7
|
-
let engine = null;
|
|
8
|
-
let charset = null;
|
|
9
|
-
let collate = null;
|
|
10
|
-
|
|
11
|
-
// Split the SQL statement into lines and filter out empty lines
|
|
12
|
-
const lines = createTableSQL.split('\n').map(line => line.trim()).filter(line => line.length > 0);
|
|
13
|
-
|
|
14
|
-
// Regular expression to match column definitions
|
|
15
|
-
const columnRegex = /`([^`]+)`\s+([^\s,]+)(\s+NOT NULL|\s+NULL)?(\s+DEFAULT\s+([^,\s]+))?(\s+AUTO_INCREMENT)?/;
|
|
16
|
-
|
|
17
|
-
// Regular expression to match index definitions
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
//
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
//
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
column.
|
|
49
|
-
}
|
|
50
|
-
|
|
51
|
-
// Set
|
|
52
|
-
if (columnMatch[
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
const
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
onDelete = '
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
if(sql.includes('ON
|
|
111
|
-
|
|
112
|
-
}else
|
|
113
|
-
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
onUpdate = '
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
1
|
+
export const parseCreateTableSQL = (createTableSQL) => {
|
|
2
|
+
if(!createTableSQL) return null;
|
|
3
|
+
const name = createTableSQL.match(/CREATE TABLE\s+`([^`]+)`/)[1];
|
|
4
|
+
const columns = [];
|
|
5
|
+
const indexes = [];
|
|
6
|
+
const constraints = [];
|
|
7
|
+
let engine = null;
|
|
8
|
+
let charset = null;
|
|
9
|
+
let collate = null;
|
|
10
|
+
|
|
11
|
+
// Split the SQL statement into lines and filter out empty lines
|
|
12
|
+
const lines = createTableSQL.split('\n').map(line => line.trim()).filter(line => line.length > 0);
|
|
13
|
+
|
|
14
|
+
// Regular expression to match column definitions
|
|
15
|
+
const columnRegex = /`([^`]+)`\s+([^\s,]+)(\s+NOT NULL|\s+NULL)?(\s+DEFAULT\s+([^,\s]+))?(\s+AUTO_INCREMENT)?/;
|
|
16
|
+
|
|
17
|
+
// Regular expression to match index definitions
|
|
18
|
+
const indexRegex_Old = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(([^)]+)\)|PRIMARY KEY\s*\(([^)]+)\)/;
|
|
19
|
+
|
|
20
|
+
// Updated to support indexes for text fields
|
|
21
|
+
// 'KEY `idx_e2alarms_alarmstring` (`alarmString`(255))' -- would cut off the final bracket and wouldnt work.
|
|
22
|
+
const indexRegex = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(((?:`[^`]+`(?:\(\d+\))?(?:\s*,\s*)?)+)\)|PRIMARY KEY\s*\(((?:`[^`]+`(?:\(\d+\))?(?:\s*,\s*)?)+)\)/;
|
|
23
|
+
|
|
24
|
+
// Regular expression to match table options (ENGINE, CHARSET, COLLATE)
|
|
25
|
+
const optionsRegex = /ENGINE=(\w+)\s+DEFAULT\s+CHARSET=(\w+)(\s+COLLATE=(\w+))?/;
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
// Process each line
|
|
29
|
+
for (const line of lines) {
|
|
30
|
+
if (line.includes('CREATE TABLE')) {
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
// Check if the line contains a column definition
|
|
34
|
+
const columnMatch = columnRegex.exec(line);
|
|
35
|
+
const indexMatch = indexRegex.exec(line);
|
|
36
|
+
const indexMatch_Old = indexRegex_Old.exec(line);
|
|
37
|
+
|
|
38
|
+
const constraintMatch = parseConstraint(line);
|
|
39
|
+
if (columnMatch && !indexMatch && !constraintMatch) {
|
|
40
|
+
const columnType = parseColumnType(line);
|
|
41
|
+
const column = {
|
|
42
|
+
Field: columnMatch[1],
|
|
43
|
+
Type: columnType
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
// Set Null property based on NOT NULL presence
|
|
47
|
+
if (columnMatch[3] && columnMatch[3].includes('NOT NULL')) {
|
|
48
|
+
column.NotNull = true; // Set to false if NOT NULL is present
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
// Set Default property if it exists
|
|
52
|
+
if (columnMatch[5]) {
|
|
53
|
+
// console.log('columnMatch[5]', columnMatch[5], columnMatch[5].replace(/'/g, ''))
|
|
54
|
+
column.Default = columnMatch[5].replace(/'/g, '');
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// Set AutoIncrement property if it exists
|
|
58
|
+
if (columnMatch[6]) {
|
|
59
|
+
column.AutoIncrement = true; // Set to true if AUTO_INCREMENT is present
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
columns.push(column);
|
|
63
|
+
} else if (indexMatch && !constraintMatch) {
|
|
64
|
+
|
|
65
|
+
const isPrimary = line.includes("PRIMARY KEY"); // Check for PRIMARY in both patterns
|
|
66
|
+
const isUnique = line.includes("UNIQUE KEY"); // Only set Unique if it matches
|
|
67
|
+
|
|
68
|
+
const keyName = isPrimary ? null : indexMatch[2]; // Set KeyName to null if it's a primary key
|
|
69
|
+
const columnNames = indexMatch[3]
|
|
70
|
+
? indexMatch[3].split(',').map(col => col.trim().replace(/`/g, '')) // Remove backticks
|
|
71
|
+
: indexMatch[4].split(',').map(col => col.trim().replace(/`/g, '')); // Remove backticks
|
|
72
|
+
const index = {
|
|
73
|
+
ColumnName: columnNames
|
|
74
|
+
};
|
|
75
|
+
if (isPrimary) index.Primary = true, index.Name = "PRIMARY"
|
|
76
|
+
if (isUnique) index.Unique = true;
|
|
77
|
+
if (keyName) index.Name = keyName;
|
|
78
|
+
indexes.push(index);
|
|
79
|
+
|
|
80
|
+
} else if (constraintMatch) {
|
|
81
|
+
constraints.push(constraintMatch);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Handle table options
|
|
87
|
+
const optionsMatch = optionsRegex.exec(createTableSQL);
|
|
88
|
+
if (optionsMatch) {
|
|
89
|
+
engine = optionsMatch[1];
|
|
90
|
+
charset = optionsMatch[2];
|
|
91
|
+
collate = optionsMatch[4] || null; // Collate may not be present
|
|
92
|
+
}
|
|
93
|
+
return { name, columns, indexes, constraints, engine, charset, collate };
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
export const parseConstraint = (sql) => {
|
|
97
|
+
if(!sql) return null;
|
|
98
|
+
const regex = /FOREIGN KEY \((`[^`]+`)\) REFERENCES (`[^`]+`)\s?\((`[^`]+`)\)(?:\sON DELETE (NO|CASCADE|SET NULL|SET DEFAULT))?(?:\sON UPDATE (NO ACTION|CASCADE|SET NULL|SET DEFAULT))?/i;
|
|
99
|
+
const match = sql.match(regex);
|
|
100
|
+
|
|
101
|
+
if (!match) {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
let onDelete = null;
|
|
105
|
+
let onUpdate = null;
|
|
106
|
+
if(sql.includes('ON DELETE CASCADE')){
|
|
107
|
+
onDelete = 'CASCADE';
|
|
108
|
+
}else if(sql.includes('ON DELETE SET NULL')){
|
|
109
|
+
onDelete = 'SET NULL';
|
|
110
|
+
}else if(sql.includes('ON DELETE SET DEFAULT')){
|
|
111
|
+
onDelete = 'SET DEFAULT';
|
|
112
|
+
}else{
|
|
113
|
+
onDelete = 'NO ACTION';
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if(sql.includes('ON UPDATE CASCADE')){
|
|
117
|
+
onUpdate = 'CASCADE';
|
|
118
|
+
}else if(sql.includes('ON UPDATE SET NULL')){
|
|
119
|
+
onUpdate = 'SET NULL';
|
|
120
|
+
}else if(sql.includes('ON UPDATE SET DEFAULT')){
|
|
121
|
+
onUpdate = 'SET DEFAULT';
|
|
122
|
+
}else{
|
|
123
|
+
onUpdate = 'NO ACTION';
|
|
124
|
+
}
|
|
125
|
+
let columnName = match[1] || ''
|
|
126
|
+
columnName = columnName.replace(/`/g, '')
|
|
127
|
+
let table = match[2] || ''
|
|
128
|
+
table = table.replace(/`/g, '')
|
|
129
|
+
let column = match[3] || ''
|
|
130
|
+
column = column.replace(/`/g, '')
|
|
131
|
+
return {
|
|
132
|
+
Name: "AlertRule", // Replace with actual constraint name if needed
|
|
133
|
+
Type: "FOREIGN KEY",
|
|
134
|
+
ColumnName: [columnName], // Column in the current table
|
|
135
|
+
References: {
|
|
136
|
+
Table: table, // Referenced table
|
|
137
|
+
Column: column, // Referenced column
|
|
138
|
+
},
|
|
139
|
+
OnDelete: onDelete,
|
|
140
|
+
OnUpdate: onUpdate,
|
|
141
|
+
};
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
const parseColumnType = (line) => {
|
|
145
|
+
// Match type pattern including parameters, handling both single and double parameter types
|
|
146
|
+
const typeRegex = /`[^`]+`\s+([a-zA-Z]+(?:\([0-9]+(?:,[0-9]+)?\))?)/i;
|
|
147
|
+
const match = line.match(typeRegex);
|
|
148
|
+
|
|
149
|
+
if (!match) return null;
|
|
150
|
+
return match[1]; // Returns the full type including parameters
|
|
151
|
+
};
|