reduce api | version control
This commit is contained in:
@@ -48,11 +48,76 @@ db.prepare(`
|
||||
)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE TABLE IF NOT EXISTS sections (
|
||||
id TEXT PRIMARY KEY,
|
||||
title TEXT NOT NULL,
|
||||
description TEXT,
|
||||
user_id TEXT,
|
||||
created_by TEXT,
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE TABLE IF NOT EXISTS section_states (
|
||||
section_id TEXT PRIMARY KEY,
|
||||
status TEXT NOT NULL DEFAULT 'editing',
|
||||
head_commit_id TEXT,
|
||||
version INTEGER NOT NULL DEFAULT 0,
|
||||
locked_by TEXT,
|
||||
locked_at TEXT,
|
||||
lock_expires_at TEXT,
|
||||
updated_at TEXT NOT NULL,
|
||||
FOREIGN KEY (section_id) REFERENCES sections(id) ON DELETE CASCADE
|
||||
)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE TABLE IF NOT EXISTS section_commits (
|
||||
id TEXT PRIMARY KEY,
|
||||
section_id TEXT NOT NULL,
|
||||
parent_commit_id TEXT,
|
||||
commit_no INTEGER NOT NULL,
|
||||
kind TEXT NOT NULL DEFAULT 'manual',
|
||||
restored_from_commit_id TEXT,
|
||||
created_by TEXT NOT NULL,
|
||||
created_at TEXT NOT NULL,
|
||||
title TEXT,
|
||||
note TEXT,
|
||||
snapshot_json TEXT NOT NULL,
|
||||
snapshot_hash TEXT,
|
||||
FOREIGN KEY (section_id) REFERENCES sections(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (parent_commit_id) REFERENCES section_commits(id),
|
||||
FOREIGN KEY (restored_from_commit_id) REFERENCES section_commits(id)
|
||||
)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE TABLE IF NOT EXISTS section_submissions (
|
||||
id TEXT PRIMARY KEY,
|
||||
section_id TEXT NOT NULL,
|
||||
commit_id TEXT NOT NULL,
|
||||
submitted_by TEXT NOT NULL,
|
||||
submitted_at TEXT NOT NULL,
|
||||
status TEXT NOT NULL DEFAULT 'pending',
|
||||
reviewed_by TEXT,
|
||||
reviewed_at TEXT,
|
||||
review_note TEXT,
|
||||
snapshot_json TEXT NOT NULL,
|
||||
snapshot_hash TEXT,
|
||||
FOREIGN KEY (section_id) REFERENCES sections(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY (commit_id) REFERENCES section_commits(id)
|
||||
)
|
||||
`).run();
|
||||
|
||||
ensureColumn("entities", "status", "INTEGER DEFAULT 1");
|
||||
ensureColumn("entities", "is_deleted", "INTEGER NOT NULL DEFAULT 0");
|
||||
ensureColumn("entities", "type_id", "TEXT NOT NULL DEFAULT 'country'");
|
||||
ensureColumn("geometries", "is_deleted", "INTEGER NOT NULL DEFAULT 0");
|
||||
ensureColumn("geometries", "binding", "TEXT");
|
||||
ensureColumn("sections", "user_id", "TEXT");
|
||||
dropEntityDeprecatedColumnsIfExists();
|
||||
dropGeometryDeprecatedColumnsIfExists();
|
||||
migrateLegacyGeometryTypeTokens();
|
||||
@@ -79,6 +144,26 @@ db.prepare(`
|
||||
ON entity_geometries(entity_id)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE INDEX IF NOT EXISTS idx_section_states_status
|
||||
ON section_states(status, updated_at)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_section_commits_no
|
||||
ON section_commits(section_id, commit_no)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE INDEX IF NOT EXISTS idx_section_commits_section_time
|
||||
ON section_commits(section_id, created_at)
|
||||
`).run();
|
||||
|
||||
db.prepare(`
|
||||
CREATE INDEX IF NOT EXISTS idx_section_submissions_section_status
|
||||
ON section_submissions(section_id, status, submitted_at)
|
||||
`).run();
|
||||
|
||||
module.exports = db;
|
||||
|
||||
function ensureColumn(tableName, columnName, columnDefinition) {
|
||||
|
||||
Reference in New Issue
Block a user