Skip to content

Commit d6a90fc

Browse files
Adding queue name and change the database schema (#20)
* Conforming to JobQueueDriver * Conforming to JobQueueDriver with queueName implementation * Migrate the Postgres driver to `swift_jobs` schema * Swift format * use the queue name from config instead * Removed breaking changes notes from README, keep track of which queue a job belongs to * Update Sources/JobsPostgres/PostgresJobsQueue.swift Co-authored-by: Adam Fowler <adamfowler71@gmail.com> * make select queue_name implicit and updated the test multiple job queue to split tasks * Deleting unused migrations * adding migration group * PR feedback --------- Co-authored-by: Adam Fowler <adamfowler71@gmail.com>
1 parent 59f992e commit d6a90fc

9 files changed

+179
-260
lines changed

Sources/JobsPostgres/Migrations/CreateJobDelay.swift

Lines changed: 0 additions & 38 deletions
This file was deleted.

Sources/JobsPostgres/Migrations/CreateJobQueue.swift

Lines changed: 0 additions & 48 deletions
This file was deleted.

Sources/JobsPostgres/Migrations/CreateJobQueueMetadata.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

Sources/JobsPostgres/Migrations/CreateJobs.swift

Lines changed: 0 additions & 55 deletions
This file was deleted.
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Hummingbird server framework project
4+
//
5+
// Copyright (c) 2024 the Hummingbird authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import Logging
16+
import PostgresMigrations
17+
import PostgresNIO
18+
19+
struct CreateSwiftJobsMigrations: DatabaseMigration {
20+
21+
func apply(connection: PostgresNIO.PostgresConnection, logger: Logging.Logger) async throws {
22+
23+
try await connection.query("CREATE SCHEMA IF NOT EXISTS swift_jobs;", logger: logger)
24+
25+
try await connection.query(
26+
"""
27+
CREATE TABLE IF NOT EXISTS swift_jobs.jobs (
28+
id uuid PRIMARY KEY,
29+
job bytea NOT NULL,
30+
last_modified TIMESTAMPTZ NOT NULL DEFAULT now(),
31+
queue_name TEXT NOT NULL DEFAULT 'default',
32+
status smallint NOT NULL
33+
);
34+
""",
35+
logger: logger
36+
)
37+
38+
try await connection.query(
39+
"""
40+
CREATE TABLE IF NOT EXISTS swift_jobs.queues(
41+
job_id uuid PRIMARY KEY,
42+
created_at TIMESTAMPTZ NOT NULL,
43+
delayed_until TIMESTAMPTZ NOT NULL DEFAULT now(),
44+
queue_name TEXT NOT NULL DEFAULT 'default'
45+
);
46+
""",
47+
logger: logger
48+
)
49+
50+
try await connection.query(
51+
"""
52+
CREATE INDEX CONCURRENTLY IF NOT EXISTS queues_delayed_until_queue_name_idx
53+
ON swift_jobs.queues(delayed_until, queue_name)
54+
""",
55+
logger: logger
56+
)
57+
58+
try await connection.query(
59+
"""
60+
CREATE TABLE IF NOT EXISTS swift_jobs.queues_metadata(
61+
key text PRIMARY KEY,
62+
value bytea NOT NULL,
63+
queue_name TEXT NOT NULL DEFAULT 'default'
64+
)
65+
""",
66+
logger: logger
67+
)
68+
69+
try await connection.query(
70+
"""
71+
CREATE INDEX CONCURRENTLY IF NOT EXISTS queues_metadata_key_queue_name_idx
72+
ON swift_jobs.queues_metadata(key, queue_name)
73+
""",
74+
logger: logger
75+
)
76+
}
77+
78+
func revert(connection: PostgresNIO.PostgresConnection, logger: Logging.Logger) async throws {
79+
try await connection.query(
80+
"""
81+
DROP SCHEMA swift_jobs CASCADE;
82+
""",
83+
logger: logger
84+
)
85+
}
86+
87+
var description: String { "__CreateSwiftJobsMigrations__" }
88+
var group: DatabaseMigrationGroup { .jobQueue }
89+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===----------------------------------------------------------------------===//
2+
//
3+
// This source file is part of the Hummingbird server framework project
4+
//
5+
// Copyright (c) 2024 the Hummingbird authors
6+
// Licensed under Apache License v2.0
7+
//
8+
// See LICENSE.txt for license information
9+
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
10+
//
11+
// SPDX-License-Identifier: Apache-2.0
12+
//
13+
//===----------------------------------------------------------------------===//
14+
15+
import PostgresMigrations
16+
17+
extension DatabaseMigrationGroup {
18+
/// JobQueue migration group
19+
public static var jobQueue: Self { .init("_hb_jobqueue") }
20+
}

Sources/JobsPostgres/Migrations/UpdateJobDelay.swift

Lines changed: 0 additions & 41 deletions
This file was deleted.

0 commit comments

Comments
 (0)