forked from evershopcommerce/evershop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysqlToPostgres.js
170 lines (157 loc) · 5.22 KB
/
mysqlToPostgres.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
108
109
110
111
112
113
114
115
116
117
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
const mysql = require("mysql2/promise");
const { Pool } = require("pg");
// MySQL database configuration
const mysqlConfig = {
host: "localhost",
user: "root",
password: "123456",
database: "evershop",
};
// PostgreSQL database configuration
const pgConfig = {
host: "localhost",
user: "postgres",
password: "123456",
database: "evershop",
port: 5432,
};
// List of tables to copy
const tables = [
"admin_user",
"attribute",
"attribute_group",
"attribute_group_link",
"attribute_option",
"cart",
"cart_address",
"cart_item",
"category",
"category_description",
"cms_page",
"cms_page_description",
"coupon",
"customer",
"customer_address",
"customer_group",
"order",
"order_activity",
"order_address",
"order_item",
"payment_transaction",
"product",
"product_attribute_value_index",
"product_category",
"product_custom_option",
"product_custom_option_value",
"product_description",
"product_image",
"setting",
"shipment",
"user_token_secret",
"variant_group",
];
async function copyData(mysqlConn, pgClient, table) {
try {
// Disabling foreign key constraints on PostgreSQL table
const disableFKQuery = `ALTER TABLE "${table}" DISABLE TRIGGER ALL`;
await pgClient.query(disableFKQuery);
// Fetching data from MySQL table
const mysqlQuery = `SELECT * FROM \`${table}\``;
let [rows, fields] = await mysqlConn.execute(mysqlQuery);
console.log(`Data fetched successfully for ${table}`, rows);
// Checking if the table has data
if (rows.length === 0) {
console.log(`Table ${table} has no data`);
return;
}
const outdatedColumns = ["uuid", "row_id", "attribute_six", "attribute_sevent", "attribute_eight", "attribute_nine", "attribute_ten" ];
// Inserting the data into the PostgreSQL database for each table
const placeholders = new Array(
Object.keys(rows[0]).filter((col) => !outdatedColumns.includes(col)).length
)
.fill("")
.map((_, index) => `$${index + 1}`)
.join(",");
const pgQuery = `INSERT INTO "${table}" (${Object.keys(rows[0])
.filter((col) => !outdatedColumns.includes(col))
.join(",")}) OVERRIDING SYSTEM VALUE VALUES (${placeholders})`;
// If the table is product_description, we need to make the url_key unique
if (table === "product_description") {
rows = rows.map((row) => {
row.url_key = row.url_key + "-" + row.product_description_id;
return row;
});
}
// Loop through each row of data and insert it into PostgreSQL table
for (const row of rows) {
// Check if the row exists in the PostgreSQL table
const check = await pgClient.query(
`SELECT * FROM "${table}" WHERE ${Object.keys(row)[0]} = $1`,
[row[Object.keys(row)[0]]]
);
if (check.rows.length > 0) {
// Update the row if it exists
const updateData = Object.assign({}, row);
delete updateData[Object.keys(row)[0]];
outdatedColumns.forEach((col) => delete updateData[col]);
const updateQuery = `UPDATE "${table}" SET ${Object.keys(updateData)
.map((col, index) => `${col} = $${index + 1}`)
.join(",")} WHERE ${Object.keys(row)[0]} = ${
row[Object.keys(row)[0]]
}`;
await pgClient.query(updateQuery, Object.values(updateData));
} else {
// Transforming the data into a PostgreSQL-compatible format
const newRow = Object.assign({}, row);
outdatedColumns.forEach((col) => delete newRow[col]);
const data = Object.values(newRow);
await pgClient.query(pgQuery, data);
}
}
// Set the sequence value to the max value of the id column
const maxId = await pgClient.query(
`SELECT MAX(${Object.keys(rows[0])[0]}) FROM "${table}"`
);
// Get the sequence name
const sequenceName = await pgClient.query(
`SELECT pg_get_serial_sequence('${table}', '${Object.keys(rows[0])[0]}')`
);
// Set the sequence value
await pgClient.query(
`ALTER SEQUENCE ${
sequenceName.rows[0].pg_get_serial_sequence
} RESTART WITH ${maxId.rows[0].max + 1}`
);
// Re-enabling foreign key constraints on PostgreSQL table
const enableFKQuery = `ALTER TABLE "${table}" ENABLE TRIGGER ALL`;
await pgClient.query(enableFKQuery);
console.log(`Data copied successfully for ${table}`);
} catch (err) {
// Rolling back the transaction if there is an error
await pgClient.query("ROLLBACK");
console.error(`Error copying data for ${table}: ${err.message}`);
throw err;
}
}
(async () => {
const mysqlConn = await mysql.createConnection(mysqlConfig);
const postgresPool = new Pool(pgConfig);
const client = await postgresPool.connect();
try {
// Start postgresql transaction
await client.query("BEGIN");
// Delete data form attributes table
await client.query("DELETE FROM attribute");
// Fetch data from mysql table
for (const table of tables) {
await copyData(mysqlConn, client, table);
}
// Committing the transaction on PostgreSQL database
await client.query("COMMIT");
process.exit(0);
} catch (err) {
// Rolling back the transaction if there is an error
await client.query("ROLLBACK");
process.exit(1);
}
})();