Skip to content

Adding queue name and change the database schema #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions Sources/JobsPostgres/Migrations/CreateJobDelay.swift

This file was deleted.

48 changes: 0 additions & 48 deletions Sources/JobsPostgres/Migrations/CreateJobQueue.swift

This file was deleted.

41 changes: 0 additions & 41 deletions Sources/JobsPostgres/Migrations/CreateJobQueueMetadata.swift

This file was deleted.

55 changes: 0 additions & 55 deletions Sources/JobsPostgres/Migrations/CreateJobs.swift

This file was deleted.

89 changes: 89 additions & 0 deletions Sources/JobsPostgres/Migrations/CreateSwiftJobsMigrations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import Logging
import PostgresMigrations
import PostgresNIO

struct CreateSwiftJobsMigrations: DatabaseMigration {

func apply(connection: PostgresNIO.PostgresConnection, logger: Logging.Logger) async throws {

try await connection.query("CREATE SCHEMA IF NOT EXISTS swift_jobs;", logger: logger)

try await connection.query(
"""
CREATE TABLE IF NOT EXISTS swift_jobs.jobs (
id uuid PRIMARY KEY,
job bytea NOT NULL,
last_modified TIMESTAMPTZ NOT NULL DEFAULT now(),
queue_name TEXT NOT NULL DEFAULT 'default',
status smallint NOT NULL
);
""",
logger: logger
)

try await connection.query(
"""
CREATE TABLE IF NOT EXISTS swift_jobs.queues(
job_id uuid PRIMARY KEY,
created_at TIMESTAMPTZ NOT NULL,
delayed_until TIMESTAMPTZ NOT NULL DEFAULT now(),
queue_name TEXT NOT NULL DEFAULT 'default'
);
""",
logger: logger
)

try await connection.query(
"""
CREATE INDEX CONCURRENTLY IF NOT EXISTS queues_delayed_until_queue_name_idx
ON swift_jobs.queues(delayed_until, queue_name)
""",
logger: logger
)

try await connection.query(
"""
CREATE TABLE IF NOT EXISTS swift_jobs.queues_metadata(
key text PRIMARY KEY,
value bytea NOT NULL,
queue_name TEXT NOT NULL DEFAULT 'default'
)
""",
logger: logger
)

try await connection.query(
"""
CREATE INDEX CONCURRENTLY IF NOT EXISTS queues_metadata_key_queue_name_idx
ON swift_jobs.queues_metadata(key, queue_name)
""",
logger: logger
)
}

func revert(connection: PostgresNIO.PostgresConnection, logger: Logging.Logger) async throws {
try await connection.query(
"""
DROP SCHEMA swift_jobs CASCADE;
""",
logger: logger
)
}

var description: String { "__CreateSwiftJobsMigrations__" }
var group: DatabaseMigrationGroup { .jobQueue }
}
20 changes: 20 additions & 0 deletions Sources/JobsPostgres/Migrations/Database+Migrations.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//===----------------------------------------------------------------------===//
//
// This source file is part of the Hummingbird server framework project
//
// Copyright (c) 2024 the Hummingbird authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See hummingbird/CONTRIBUTORS.txt for the list of Hummingbird authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import PostgresMigrations

extension DatabaseMigrationGroup {
/// JobQueue migration group
public static var jobQueue: Self { .init("_hb_jobqueue") }
}
41 changes: 0 additions & 41 deletions Sources/JobsPostgres/Migrations/UpdateJobDelay.swift

This file was deleted.

Loading
Loading