-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from eduardolat/feat/restore-backups
Feat/restore backups
- Loading branch information
Showing
29 changed files
with
1,045 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
internal/database/migrations/20240805000451_add_restorations_table.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
-- +goose Up | ||
-- +goose StatementBegin | ||
CREATE TABLE IF NOT EXISTS restorations ( | ||
id UUID NOT NULL DEFAULT uuid_generate_v4() PRIMARY KEY, | ||
execution_id UUID NOT NULL REFERENCES executions(id) ON DELETE CASCADE, | ||
database_id UUID REFERENCES databases(id) ON DELETE CASCADE, | ||
|
||
status TEXT NOT NULL CHECK ( | ||
status IN ('running', 'success', 'failed') | ||
) DEFAULT 'running', | ||
message TEXT, | ||
|
||
started_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
updated_at TIMESTAMPTZ, | ||
finished_at TIMESTAMPTZ | ||
); | ||
|
||
CREATE TRIGGER restorations_change_updated_at | ||
BEFORE UPDATE ON restorations FOR EACH ROW EXECUTE FUNCTION change_updated_at(); | ||
|
||
CREATE INDEX IF NOT EXISTS | ||
idx_restorations_execution_id ON restorations(execution_id); | ||
-- +goose StatementEnd | ||
|
||
-- +goose Down | ||
-- +goose StatementBegin | ||
DROP TABLE IF EXISTS restorations; | ||
-- +goose StatementEnd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
-- name: ExecutionsServiceGetExecution :one | ||
SELECT * FROM executions | ||
WHERE id = @id; | ||
SELECT | ||
executions.*, | ||
databases.id AS database_id, | ||
databases.pg_version AS database_pg_version | ||
FROM executions | ||
INNER JOIN backups ON backups.id = executions.backup_id | ||
INNER JOIN databases ON databases.id = backups.database_id | ||
WHERE executions.id = @id; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package restorations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/eduardolat/pgbackweb/internal/database/dbgen" | ||
) | ||
|
||
func (s *Service) CreateRestoration( | ||
ctx context.Context, params dbgen.RestorationsServiceCreateRestorationParams, | ||
) (dbgen.Restoration, error) { | ||
return s.dbgen.RestorationsServiceCreateRestoration(ctx, params) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
-- name: RestorationsServiceCreateRestoration :one | ||
INSERT INTO restorations (execution_id, database_id, status, message) | ||
VALUES (@execution_id, @database_id, @status, @message) | ||
RETURNING *; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package restorations | ||
|
||
import "context" | ||
|
||
func (s *Service) GetRestorationsQty(ctx context.Context) (int64, error) { | ||
return s.dbgen.RestorationsServiceGetRestorationsQty(ctx) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- name: RestorationsServiceGetRestorationsQty :one | ||
SELECT COUNT(*) FROM restorations; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package restorations | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/eduardolat/pgbackweb/internal/database/dbgen" | ||
"github.com/eduardolat/pgbackweb/internal/util/paginateutil" | ||
"github.com/google/uuid" | ||
) | ||
|
||
type PaginateRestorationsParams struct { | ||
Page int | ||
Limit int | ||
ExecutionFilter uuid.NullUUID | ||
DatabaseFilter uuid.NullUUID | ||
} | ||
|
||
func (s *Service) PaginateRestorations( | ||
ctx context.Context, params PaginateRestorationsParams, | ||
) (paginateutil.PaginateResponse, []dbgen.RestorationsServicePaginateRestorationsRow, error) { | ||
page := max(params.Page, 1) | ||
limit := min(max(params.Limit, 1), 100) | ||
|
||
count, err := s.dbgen.RestorationsServicePaginateRestorationsCount( | ||
ctx, dbgen.RestorationsServicePaginateRestorationsCountParams{ | ||
ExecutionID: params.ExecutionFilter, | ||
DatabaseID: params.DatabaseFilter, | ||
}, | ||
) | ||
if err != nil { | ||
return paginateutil.PaginateResponse{}, nil, err | ||
} | ||
|
||
paginateParams := paginateutil.PaginateParams{ | ||
Page: page, | ||
Limit: limit, | ||
} | ||
offset := paginateutil.CreateOffsetFromParams(paginateParams) | ||
paginateResponse := paginateutil.CreatePaginateResponse(paginateParams, int(count)) | ||
|
||
restorations, err := s.dbgen.RestorationsServicePaginateRestorations( | ||
ctx, dbgen.RestorationsServicePaginateRestorationsParams{ | ||
ExecutionID: params.ExecutionFilter, | ||
DatabaseID: params.DatabaseFilter, | ||
Limit: int32(params.Limit), | ||
Offset: int32(offset), | ||
}, | ||
) | ||
if err != nil { | ||
return paginateutil.PaginateResponse{}, nil, err | ||
} | ||
|
||
return paginateResponse, restorations, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-- name: RestorationsServicePaginateRestorationsCount :one | ||
SELECT COUNT(restorations.*) | ||
FROM restorations | ||
INNER JOIN executions ON executions.id = restorations.execution_id | ||
INNER JOIN backups ON backups.id = executions.backup_id | ||
LEFT JOIN databases ON databases.id = restorations.database_id | ||
WHERE | ||
( | ||
sqlc.narg('execution_id')::UUID IS NULL | ||
OR | ||
restorations.execution_id = sqlc.narg('execution_id')::UUID | ||
) | ||
AND | ||
( | ||
sqlc.narg('database_id')::UUID IS NULL | ||
OR | ||
restorations.database_id = sqlc.narg('database_id')::UUID | ||
); | ||
|
||
-- name: RestorationsServicePaginateRestorations :many | ||
SELECT | ||
restorations.*, | ||
databases.name AS database_name, | ||
backups.name AS backup_name | ||
FROM restorations | ||
INNER JOIN executions ON executions.id = restorations.execution_id | ||
INNER JOIN backups ON backups.id = executions.backup_id | ||
LEFT JOIN databases ON databases.id = restorations.database_id | ||
WHERE | ||
( | ||
sqlc.narg('execution_id')::UUID IS NULL | ||
OR | ||
restorations.execution_id = sqlc.narg('execution_id')::UUID | ||
) | ||
AND | ||
( | ||
sqlc.narg('database_id')::UUID IS NULL | ||
OR | ||
restorations.database_id = sqlc.narg('database_id')::UUID | ||
) | ||
ORDER BY restorations.started_at DESC | ||
LIMIT sqlc.arg('limit') OFFSET sqlc.arg('offset'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package restorations | ||
|
||
import ( | ||
"github.com/eduardolat/pgbackweb/internal/database/dbgen" | ||
"github.com/eduardolat/pgbackweb/internal/integration" | ||
"github.com/eduardolat/pgbackweb/internal/service/databases" | ||
"github.com/eduardolat/pgbackweb/internal/service/destinations" | ||
"github.com/eduardolat/pgbackweb/internal/service/executions" | ||
) | ||
|
||
type Service struct { | ||
dbgen *dbgen.Queries | ||
ints *integration.Integration | ||
executionsService *executions.Service | ||
databasesService *databases.Service | ||
destinationsService *destinations.Service | ||
} | ||
|
||
func New( | ||
dbgen *dbgen.Queries, ints *integration.Integration, | ||
executionsService *executions.Service, databasesService *databases.Service, | ||
destinationsService *destinations.Service, | ||
) *Service { | ||
return &Service{ | ||
dbgen: dbgen, | ||
ints: ints, | ||
executionsService: executionsService, | ||
databasesService: databasesService, | ||
destinationsService: destinationsService, | ||
} | ||
} |
Oops, something went wrong.