feat: sync portal runtime fixes for release

This commit is contained in:
john
2026-06-27 22:28:41 +08:00
parent 8264e71f9e
commit 4a9bc710f1
14 changed files with 795 additions and 130 deletions
+25 -88
View File
@@ -46,84 +46,6 @@ export function createDbPool() {
});
}
export function splitSqlStatements(sql) {
const statements = [];
let current = '';
let quote = null;
let lineComment = false;
let blockComment = false;
for (let i = 0; i < sql.length; i += 1) {
const char = sql[i];
const next = sql[i + 1];
if (lineComment) {
if (char === '\n') {
lineComment = false;
current += '\n';
}
continue;
}
if (blockComment) {
if (char === '*' && next === '/') {
blockComment = false;
i += 1;
}
continue;
}
if (quote) {
current += char;
if (char === '\\' && (quote === '\'' || quote === '"') && next) {
current += next;
i += 1;
continue;
}
if (char === quote) {
if (next === quote && quote !== '`') {
current += next;
i += 1;
} else {
quote = null;
}
}
continue;
}
if (char === '-' && next === '-') {
lineComment = true;
i += 1;
continue;
}
if (char === '/' && next === '*') {
blockComment = true;
i += 1;
continue;
}
if (char === '\'' || char === '"' || char === '`') {
quote = char;
current += char;
continue;
}
if (char === ';') {
const trimmed = current.trim();
if (trimmed) statements.push(trimmed);
current = '';
continue;
}
current += char;
}
const trimmed = current.trim();
if (trimmed) statements.push(trimmed);
return statements;
}
async function columnExists(pool, table, column) {
const [rows] = await pool.query(
`SELECT 1 FROM information_schema.COLUMNS
@@ -203,13 +125,6 @@ export async function migrateSchema(pool) {
}
}
if (!(await columnExists(pool, 'h5_session_snapshots', 'display_title'))) {
await pool.query(
`ALTER TABLE h5_session_snapshots
ADD COLUMN display_title VARCHAR(512) NOT NULL DEFAULT '' AFTER name`,
);
}
await pool.query(`UPDATE h5_users SET slug = username WHERE slug IS NULL OR slug = ''`);
if (!(await indexExists(pool, 'h5_users', 'uq_h5_users_slug'))) {
await pool.query(`ALTER TABLE h5_users ADD UNIQUE KEY uq_h5_users_slug (slug)`);
@@ -318,6 +233,7 @@ export async function migrateSchema(pool) {
`);
const publishColumns = [
['user_confirmed_at', 'BIGINT NULL AFTER expires_at'],
['plaza_view_count', 'BIGINT NOT NULL DEFAULT 0'],
['plaza_like_count', 'BIGINT NOT NULL DEFAULT 0'],
];
@@ -327,6 +243,13 @@ export async function migrateSchema(pool) {
}
}
if (!(await indexExists(pool, 'h5_publish_records', 'idx_h5_publish_auto_private'))) {
await pool.query(
`ALTER TABLE h5_publish_records
ADD KEY idx_h5_publish_auto_private (access_mode, status, user_confirmed_at, expires_at)`,
);
}
const plazaUserColumns = [
['plaza_post_count', 'INT UNSIGNED NOT NULL DEFAULT 0'],
['plaza_follower_count', 'INT UNSIGNED NOT NULL DEFAULT 0'],
@@ -367,6 +290,12 @@ export async function migrateSchema(pool) {
);
}
if (!(await columnExists(pool, 'h5_session_snapshots', 'display_title'))) {
await pool.query(
`ALTER TABLE h5_session_snapshots ADD COLUMN display_title VARCHAR(512) NOT NULL DEFAULT '' AFTER name`,
);
}
const oauthStateColumns = [
['intent', "VARCHAR(16) NOT NULL DEFAULT 'login' AFTER utm_campaign"],
['bind_user_id', 'CHAR(36) NULL AFTER intent'],
@@ -602,6 +531,7 @@ export async function migrateSchema(pool) {
agent_session_id VARCHAR(128) NOT NULL PRIMARY KEY,
user_id CHAR(36) NOT NULL,
name VARCHAR(512) NOT NULL DEFAULT '',
display_title VARCHAR(512) NOT NULL DEFAULT '',
working_dir VARCHAR(1024) NOT NULL DEFAULT '',
created_at_str VARCHAR(64) NOT NULL DEFAULT '',
updated_at_str VARCHAR(64) NOT NULL DEFAULT '',
@@ -673,9 +603,16 @@ export async function migrateSchema(pool) {
export async function initSchema(pool) {
const schemaPath = path.join(__dirname, 'schema.sql');
const sql = fs.readFileSync(schemaPath, 'utf8');
for (const statement of splitSqlStatements(sql)) {
await pool.query(statement);
const sql = fs
.readFileSync(schemaPath, 'utf8')
.split('\n')
.filter((line) => !line.trim().startsWith('--'))
.join('\n');
for (const statement of sql.split(';')) {
const trimmed = statement.trim();
if (trimmed) {
await pool.query(trimmed);
}
}
await migrateSchema(pool);
await ensureDefaultSpaces(pool, {