fix: Make participants field nullable and improve database error handling

main
brobert (aider) 3 months ago
parent ccd72146cd
commit e4bb4e5414

@ -33,8 +33,19 @@ export async function fetchGroups(communityId: string): Promise<Set<string>> {
// Save the group to the database
execute(
'INSERT OR REPLACE INTO groups (id, name, linked_parent) VALUES (?, ?, ?)',
[group.id, group.subject || 'Unnamed Group', group.linkedParent] // Provide a default name if missing
`INSERT INTO groups (id, name, participants, linked_parent, last_updated)
VALUES (?, ?, ?, ?, ?)
ON CONFLICT(id) DO UPDATE SET
name = excluded.name,
participants = excluded.participants,
last_updated = excluded.last_updated`,
[
group.id,
group.subject || 'Unnamed Group',
group.participants ? JSON.stringify(group.participants) : null,
group.linkedParent,
new Date().toISOString()
]
);
}
}

@ -12,9 +12,16 @@ if (!fs.existsSync(dbDir)) {
const dbPath = path.join(dbDir, 'bot.db');
export const db = new Database(dbPath);
// Run the schema script
// Run the schema script with error handling
try {
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8');
db.exec('PRAGMA foreign_keys=OFF;'); // Disable foreign keys during schema changes
db.exec(schema);
db.exec('PRAGMA foreign_keys=ON;'); // Re-enable foreign keys
} catch (error) {
console.error('Error initializing database schema:', error);
process.exit(1);
}
// Helper function to query the database
export function query(sql: string, params: any[] = []) {

@ -2,7 +2,7 @@
CREATE TABLE IF NOT EXISTS groups (
id TEXT PRIMARY KEY, -- WhatsApp group ID
name TEXT NOT NULL DEFAULT 'Unnamed group',
participants TEXT NOT NULL, -- JSON array of phone numbers
participants TEXT DEFAULT NULL, -- JSON array of phone numbers (nullable)
linked_parent TEXT, -- Parent community ID
last_updated TEXT -- ISO 8601 timestamp of last update
);

Loading…
Cancel
Save