Files
umed/web/scripts/migrate-to-mysql.mjs
2026-07-09 14:35:32 +08:00

233 lines
6.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* 从 SQLite 导出数据并写入 MySQL(admin_roles / admin_users / page_seo)
* 用法: node scripts/migrate-to-mysql.mjs
*/
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import mysql from 'mysql2/promise'
const config = {
host: process.env.NUXT_MYSQL_HOST || '47.86.202.232',
port: Number(process.env.NUXT_MYSQL_PORT || 3306),
user: process.env.NUXT_MYSQL_USER || 'umedglobal',
password: process.env.NUXT_MYSQL_PASSWORD || 'mysql_6ZdKPT',
database: process.env.NUXT_MYSQL_DATABASE || 'umedglobal',
multipleStatements: true,
}
const scriptsDir = join(process.cwd(), 'scripts')
function loadJson(name) {
return JSON.parse(readFileSync(join(scriptsDir, name), 'utf8'))
}
function mapLegacyUserId(value) {
if (value == null) return null
if (value === 'user_1') return 1
const num = Number(value)
return Number.isFinite(num) ? num : null
}
async function createTables(conn) {
await conn.query(`
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS admin_users;
DROP TABLE IF EXISTS admin_roles;
DROP TABLE IF EXISTS page_seo;
DROP TABLE IF EXISTS assessment_submissions;
DROP TABLE IF EXISTS partner_submissions;
DROP TABLE IF EXISTS news_articles;
SET FOREIGN_KEY_CHECKS = 1;
`)
await conn.query(`
CREATE TABLE admin_roles (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
code VARCHAR(64) NOT NULL UNIQUE,
description TEXT,
is_built_in TINYINT NOT NULL DEFAULT 0,
status VARCHAR(32) NOT NULL DEFAULT 'active',
menu_ids TEXT NOT NULL,
create_time VARCHAR(32) NOT NULL,
update_time VARCHAR(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE admin_users (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
name VARCHAR(255),
avatar TEXT,
phone VARCHAR(64),
email VARCHAR(255),
role_id BIGINT,
status VARCHAR(32) NOT NULL DEFAULT 'active',
is_built_in TINYINT NOT NULL DEFAULT 0,
bio TEXT,
tags TEXT,
create_time VARCHAR(32) NOT NULL,
update_time VARCHAR(32) NOT NULL,
CONSTRAINT fk_admin_users_role FOREIGN KEY (role_id) REFERENCES admin_roles(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE page_seo (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
route_key VARCHAR(512) NOT NULL,
locale VARCHAR(8) NOT NULL,
page_label VARCHAR(255) NOT NULL,
title TEXT NOT NULL,
description TEXT NOT NULL,
keywords TEXT,
og_title TEXT,
og_description TEXT,
og_image TEXT,
robots VARCHAR(64) DEFAULT 'index,follow',
og_type VARCHAR(32) DEFAULT 'website',
updated_at VARCHAR(32) NOT NULL,
updated_by BIGINT,
UNIQUE KEY uk_page_seo_route_locale (route_key, locale)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE assessment_submissions (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
lang VARCHAR(8) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255),
email VARCHAR(255) NOT NULL,
condition_text TEXT NOT NULL,
reports TEXT,
message TEXT,
status VARCHAR(32) NOT NULL DEFAULT 'unread',
created_at VARCHAR(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE partner_submissions (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
lang VARCHAR(8) NOT NULL,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
company_name VARCHAR(255) NOT NULL,
country VARCHAR(255) NOT NULL,
email VARCHAR(255) NOT NULL,
category VARCHAR(255) NOT NULL,
collaborate TEXT NOT NULL,
status VARCHAR(32) NOT NULL DEFAULT 'unread',
created_at VARCHAR(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE news_articles (
id BIGINT PRIMARY KEY AUTO_INCREMENT,
locale VARCHAR(8) NOT NULL,
media_type VARCHAR(32) NOT NULL DEFAULT 'article',
title TEXT NOT NULL,
summary TEXT NOT NULL,
thumbnail TEXT NOT NULL,
thumbnail_alt TEXT,
url TEXT,
youtube_url TEXT,
tiktok_url TEXT,
sort_order INT NOT NULL DEFAULT 0,
status VARCHAR(32) NOT NULL DEFAULT 'published',
created_at VARCHAR(32) NOT NULL,
updated_at VARCHAR(32) NOT NULL,
INDEX idx_news_locale_sort (locale, sort_order, id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
`)
}
async function migrateData(conn) {
const roles = loadJson('admin_roles.json')
const users = loadJson('admin_users.json')
const pageSeo = loadJson('page_seo.json')
for (const role of roles) {
await conn.execute(
`INSERT INTO admin_roles (id, name, code, description, is_built_in, status, menu_ids, create_time, update_time)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
1,
role.name,
role.code,
role.description,
role.is_built_in,
role.status,
role.menu_ids,
role.create_time,
role.update_time,
]
)
}
for (const user of users) {
await conn.execute(
`INSERT INTO admin_users (
id, username, password, name, avatar, phone, email, role_id, status, is_built_in, bio, tags, create_time, update_time
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
1,
user.username,
user.password,
user.name,
user.avatar,
user.phone,
user.email,
1,
user.status,
user.is_built_in,
user.bio,
user.tags,
user.create_time,
user.update_time,
]
)
}
for (const row of pageSeo) {
await conn.execute(
`INSERT INTO page_seo (
route_key, locale, page_label, title, description,
keywords, og_title, og_description, og_image, robots, og_type,
updated_at, updated_by
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
[
row.route_key,
row.locale,
row.page_label,
row.title,
row.description,
row.keywords,
row.og_title,
row.og_description,
row.og_image,
row.robots,
row.og_type,
row.updated_at,
mapLegacyUserId(row.updated_by),
]
)
}
console.log(`迁移完成: admin_roles=${roles.length}, admin_users=${users.length}, page_seo=${pageSeo.length}`)
}
async function main() {
console.log(`连接 MySQL ${config.host}:${config.port}/${config.database} ...`)
const conn = await mysql.createConnection(config)
try {
await createTables(conn)
console.log('MySQL 表结构已创建')
await migrateData(conn)
console.log('数据迁移成功')
} finally {
await conn.end()
}
}
main().catch((err) => {
console.error('迁移失败:', err)
process.exit(1)
})