mirror of
https://github.com/spchcap/speech.capital.git
synced 2026-01-14 16:48:44 +00:00
Refactor: Update schema.js
This commit is contained in:
@@ -18,7 +18,6 @@ const schemaV1 = [
|
|||||||
id INTEGER PRIMARY KEY,
|
id INTEGER PRIMARY KEY,
|
||||||
sub_id INTEGER NOT NULL,
|
sub_id INTEGER NOT NULL,
|
||||||
user_id INTEGER NOT NULL,
|
user_id INTEGER NOT NULL,
|
||||||
post_type TEXT NOT NULL DEFAULT 'text' CHECK(post_type IN ('text', 'link')),
|
|
||||||
title TEXT NOT NULL,
|
title TEXT NOT NULL,
|
||||||
link TEXT,
|
link TEXT,
|
||||||
content TEXT,
|
content TEXT,
|
||||||
@@ -56,10 +55,6 @@ const schemaV1 = [
|
|||||||
`CREATE UNIQUE INDEX IF NOT EXISTS idx_user_post_vote ON votes(user_id, post_id) WHERE post_id IS NOT NULL;`,
|
`CREATE UNIQUE INDEX IF NOT EXISTS idx_user_post_vote ON votes(user_id, post_id) WHERE post_id IS NOT NULL;`,
|
||||||
`CREATE UNIQUE INDEX IF NOT EXISTS idx_user_comment_vote ON votes(user_id, comment_id) WHERE comment_id IS NOT NULL;`
|
`CREATE UNIQUE INDEX IF NOT EXISTS idx_user_comment_vote ON votes(user_id, comment_id) WHERE comment_id IS NOT NULL;`
|
||||||
];
|
];
|
||||||
const schemaV2Migration = [
|
|
||||||
`ALTER TABLE posts ADD COLUMN post_type TEXT NOT NULL DEFAULT 'text' CHECK(post_type IN ('text', 'link'));`,
|
|
||||||
`UPDATE posts SET post_type = 'link' WHERE link IS NOT NULL;`
|
|
||||||
];
|
|
||||||
const json = (d, o = {}) => {
|
const json = (d, o = {}) => {
|
||||||
const h = new Headers(o.headers);
|
const h = new Headers(o.headers);
|
||||||
h.set('Content-Type', 'application/json');
|
h.set('Content-Type', 'application/json');
|
||||||
@@ -81,10 +76,6 @@ export async function onRequestPost({ request, env }) {
|
|||||||
const results = await db.batch(schemaV1.map(q => db.prepare(q)));
|
const results = await db.batch(schemaV1.map(q => db.prepare(q)));
|
||||||
return json({ results });
|
return json({ results });
|
||||||
}
|
}
|
||||||
if (action === 'migrate_v2') {
|
|
||||||
const results = await db.batch(schemaV2Migration.map(q => db.prepare(q)));
|
|
||||||
return json({ success: true, results });
|
|
||||||
}
|
|
||||||
if (action === 'set_role') {
|
if (action === 'set_role') {
|
||||||
const { username, role } = payload;
|
const { username, role } = payload;
|
||||||
if (!username || !['user','mod','admin','owner'].includes(role)) return json({ error: 'Missing or invalid fields' }, { status: 400 });
|
if (!username || !['user','mod','admin','owner'].includes(role)) return json({ error: 'Missing or invalid fields' }, { status: 400 });
|
||||||
@@ -100,9 +91,7 @@ export async function onRequestPost({ request, env }) {
|
|||||||
|
|
||||||
return json({ error: 'Invalid action' }, { status: 400 });
|
return json({ error: 'Invalid action' }, { status: 400 });
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e.message?.includes('duplicate column name')) {
|
|
||||||
return json({ success: false, message: 'Column already exists.' }, { status: 409 });
|
|
||||||
}
|
|
||||||
return json({ error: { message: e.message, cause: e.cause } }, { status: 500 });
|
return json({ error: { message: e.message, cause: e.cause } }, { status: 500 });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user