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/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
- // console.log('Checking if this is the main module');
105
- // console.log(require.main === module)
106
- // console.log(require.main);
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 indexRegex = /(PRIMARY|UNIQUE)?\s*KEY\s*`([^`]+)`\s*\(([^)]+)\)|PRIMARY KEY\s*\(([^)]+)\)/;
19
-
20
- // Regular expression to match table options (ENGINE, CHARSET, COLLATE)
21
- const optionsRegex = /ENGINE=(\w+)\s+DEFAULT\s+CHARSET=(\w+)(\s+COLLATE=(\w+))?/;
22
-
23
-
24
- // Process each line
25
- for (const line of lines) {
26
- if (line.includes('CREATE TABLE')) {
27
- continue;
28
- }
29
- // Check if the line contains a column definition
30
- const columnMatch = columnRegex.exec(line);
31
- const indexMatch = indexRegex.exec(line);
32
- const constraintMatch = parseConstraint(line);
33
- if (columnMatch && !indexMatch && !constraintMatch) {
34
- const columnType = parseColumnType(line);
35
- const column = {
36
- Field: columnMatch[1],
37
- Type: columnType
38
- };
39
-
40
- // Set Null property based on NOT NULL presence
41
- if (columnMatch[3] && columnMatch[3].includes('NOT NULL')) {
42
- column.NotNull = true; // Set to false if NOT NULL is present
43
- }
44
-
45
- // Set Default property if it exists
46
- if (columnMatch[5]) {
47
- // console.log('columnMatch[5]', columnMatch[5], columnMatch[5].replace(/'/g, ''))
48
- column.Default = columnMatch[5].replace(/'/g, '');
49
- }
50
-
51
- // Set AutoIncrement property if it exists
52
- if (columnMatch[6]) {
53
- column.AutoIncrement = true; // Set to true if AUTO_INCREMENT is present
54
- }
55
-
56
- columns.push(column);
57
- } else if (indexMatch && !constraintMatch) {
58
-
59
- const isPrimary = line.includes("PRIMARY KEY"); // Check for PRIMARY in both patterns
60
- const isUnique = line.includes("UNIQUE KEY"); // Only set Unique if it matches
61
-
62
- const keyName = isPrimary ? null : indexMatch[2]; // Set KeyName to null if it's a primary key
63
- const columnNames = indexMatch[3]
64
- ? indexMatch[3].split(',').map(col => col.trim().replace(/`/g, '')) // Remove backticks
65
- : indexMatch[4].split(',').map(col => col.trim().replace(/`/g, '')); // Remove backticks
66
- const index = {
67
- ColumnName: columnNames
68
- };
69
- if (isPrimary) index.Primary = true, index.Name = "PRIMARY"
70
- if (isUnique) index.Unique = true;
71
- if (keyName) index.Name = keyName;
72
- indexes.push(index);
73
-
74
- } else if (constraintMatch) {
75
- constraints.push(constraintMatch);
76
- }
77
-
78
- }
79
-
80
- // Handle table options
81
- const optionsMatch = optionsRegex.exec(createTableSQL);
82
- if (optionsMatch) {
83
- engine = optionsMatch[1];
84
- charset = optionsMatch[2];
85
- collate = optionsMatch[4] || null; // Collate may not be present
86
- }
87
- return { name, columns, indexes, constraints, engine, charset, collate };
88
- }
89
-
90
- export const parseConstraint = (sql) => {
91
- if(!sql) return null;
92
- const regex = /FOREIGN KEY \((`[^`]+`)\) REFERENCES (`[^`]+`)\s?\((`[^`]+`)\)(?:\sON DELETE (NO|CASCADE|SET NULL|SET DEFAULT))?(?:\sON UPDATE (NO ACTION|CASCADE|SET NULL|SET DEFAULT))?/i;
93
- const match = sql.match(regex);
94
-
95
- if (!match) {
96
- return null;
97
- }
98
- let onDelete = null;
99
- let onUpdate = null;
100
- if(sql.includes('ON DELETE CASCADE')){
101
- onDelete = 'CASCADE';
102
- }else if(sql.includes('ON DELETE SET NULL')){
103
- onDelete = 'SET NULL';
104
- }else if(sql.includes('ON DELETE SET DEFAULT')){
105
- onDelete = 'SET DEFAULT';
106
- }else{
107
- onDelete = 'NO ACTION';
108
- }
109
-
110
- if(sql.includes('ON UPDATE CASCADE')){
111
- onUpdate = 'CASCADE';
112
- }else if(sql.includes('ON UPDATE SET NULL')){
113
- onUpdate = 'SET NULL';
114
- }else if(sql.includes('ON UPDATE SET DEFAULT')){
115
- onUpdate = 'SET DEFAULT';
116
- }else{
117
- onUpdate = 'NO ACTION';
118
- }
119
- let columnName = match[1] || ''
120
- columnName = columnName.replace(/`/g, '')
121
- let table = match[2] || ''
122
- table = table.replace(/`/g, '')
123
- let column = match[3] || ''
124
- column = column.replace(/`/g, '')
125
- return {
126
- Name: "AlertRule", // Replace with actual constraint name if needed
127
- Type: "FOREIGN KEY",
128
- ColumnName: [columnName], // Column in the current table
129
- References: {
130
- Table: table, // Referenced table
131
- Column: column, // Referenced column
132
- },
133
- OnDelete: onDelete,
134
- OnUpdate: onUpdate,
135
- };
136
- };
137
-
138
- const parseColumnType = (line) => {
139
- // Match type pattern including parameters, handling both single and double parameter types
140
- const typeRegex = /`[^`]+`\s+([a-zA-Z]+(?:\([0-9]+(?:,[0-9]+)?\))?)/i;
141
- const match = line.match(typeRegex);
142
-
143
- if (!match) return null;
144
- return match[1]; // Returns the full type including parameters
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
+ };