Fix UNIQUE constraint: Use (service_name, version, username) instead of service_fqn

- Change UNIQUE constraint to composite key on separate columns
- Move upsert logic into D1Storage.createService() for atomic operation
- Delete existing service and its offers before inserting new one
- Remove redundant delete logic from app.ts endpoint
- Fixes 'UNIQUE constraint failed: services.service_fqn' error when republishing
This commit is contained in:
2025-12-10 19:42:03 +01:00
parent cfa58f1dfa
commit e3ede0033e
3 changed files with 22 additions and 7 deletions

View File

@@ -68,7 +68,7 @@ CREATE TABLE services (
created_at INTEGER NOT NULL,
expires_at INTEGER NOT NULL,
FOREIGN KEY (username) REFERENCES usernames(username) ON DELETE CASCADE,
UNIQUE(service_fqn)
UNIQUE(service_name, version, username)
);
CREATE INDEX idx_services_fqn ON services(service_fqn);

View File

@@ -337,11 +337,7 @@ export function createApp(storage: Storage, config: Config) {
return c.json({ error: 'Invalid signature for username' }, 403);
}
// Delete existing service if one exists (upsert behavior)
const existingService = await storage.getServiceByFqn(serviceFqn);
if (existingService) {
await storage.deleteService(existingService.id, username);
}
// Note: createService handles upsert behavior (deletes existing service if it exists)
// Validate all offers
for (const offer of offers) {

View File

@@ -399,7 +399,26 @@ export class D1Storage implements Storage {
const { serviceName, version, username } = parsed;
// Insert service with extracted fields
// Delete existing service with same (service_name, version, username) and its related offers (upsert behavior)
// First get the existing service
const existingService = await this.db.prepare(`
SELECT id FROM services
WHERE service_name = ? AND version = ? AND username = ?
`).bind(serviceName, version, username).first();
if (existingService) {
// Delete related offers first (no FK cascade from offers to services)
await this.db.prepare(`
DELETE FROM offers WHERE service_id = ?
`).bind(existingService.id).run();
// Delete the service
await this.db.prepare(`
DELETE FROM services WHERE id = ?
`).bind(existingService.id).run();
}
// Insert new service with extracted fields
await this.db.prepare(`
INSERT INTO services (id, service_fqn, service_name, version, username, created_at, expires_at)
VALUES (?, ?, ?, ?, ?, ?, ?)