Created
October 3, 2022 15:20
-
-
Save scotthansonde/c7ce0f0b78316f8464619f1a68576449 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // using mysql2 so I can use promises | |
| async function main() { | |
| var mysql = require('mysql2/promise') | |
| const connection = await mysql.createConnection({ | |
| host: 'xxx', | |
| user: 'xxx', | |
| password: 'xxx', | |
| database: 'feedland', | |
| charset: 'utf8mb4', | |
| }) | |
| connection.connect() | |
| const tables = ['feeds', 'likes', 'subscriptions', 'users'] | |
| for (const table of tables) { | |
| console.log(table) | |
| // get names of datetime fields | |
| const datetimeFields = [] | |
| const [fields, x] = await connection.query(`DESCRIBE ${table}`) | |
| for (const field of fields) { | |
| if (field.Type === 'datetime') datetimeFields.push(field.Field) | |
| } | |
| const rows = require(`./backups/${table}.json`) | |
| for (const [index, row] of rows.entries()) { | |
| console.log(index) | |
| // Convert json timestampt to mysql format | |
| for (const i of datetimeFields) { | |
| if (row[i]) { | |
| row[i] = row[i].replace('T', ' ').replace('.000Z', '') | |
| } | |
| } | |
| const template = `REPLACE INTO ${table} SET ?` | |
| const sql = mysql.format(template, row) | |
| const [results, y] = await connection.query(sql) | |
| } | |
| } | |
| connection.end() | |
| } | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment