mirror of
https://github.com/harness/drone.git
synced 2025-05-05 19:53:13 +08:00

This change is adding the concept of Paths. A repository and space always have a Primary Path which always is represents the ancestry to the root space. All access history / resource visibility / child listings / UI traversal / etc. is done via that path. Additionally, repos and spaces can have Alias Paths, which as the name states are aliases. via the primary path. They sole impact is that a space or repo can be reached via different paths from the UI / rest apis / git apis. This fulfills two major purposes: - Customers can rename or move projects and spaces without breaking any existing references from CI pipeliens / code bases / local repos / ... - Customer can create shorter aliases for important repos when in harness embeded mode! (acc/org/proj/repo can be shortened to acc/repo, or acc/repo' Apart from the path changes, this PR adds: Improved User facing errors Improved internal error handling and wrapping update / rename operation for repo and space path list / delete / create operation for repo and space
68 lines
1.8 KiB
Go
68 lines
1.8 KiB
Go
// Copyright 2021 Harness Inc. All rights reserved.
|
|
// Use of this source code is governed by the Polyform Free Trial License
|
|
// that can be found in the LICENSE.md file for this repository.
|
|
|
|
// Package database provides persistent data storage using
|
|
// a postgres or sqlite3 database.
|
|
package database
|
|
|
|
import (
|
|
"database/sql"
|
|
"time"
|
|
|
|
"github.com/harness/gitness/internal/store/database/migrate"
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/Masterminds/squirrel"
|
|
"github.com/jmoiron/sqlx"
|
|
)
|
|
|
|
// build is a global instance of the sql builder. we are able to
|
|
// hardcode to postgres since sqlite3 is compatible with postgres.
|
|
var builder = squirrel.StatementBuilder.PlaceholderFormat(squirrel.Dollar)
|
|
|
|
// Connect to a database and verify with a ping.
|
|
func Connect(driver, datasource string) (*sqlx.DB, error) {
|
|
db, err := sql.Open(driver, datasource)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "Failed to open the db")
|
|
}
|
|
dbx := sqlx.NewDb(db, driver)
|
|
if err := pingDatabase(dbx); err != nil {
|
|
return nil, errors.Wrap(err, "Failed to ping the db")
|
|
}
|
|
if err := setupDatabase(dbx); err != nil {
|
|
return nil, errors.Wrap(err, "Failed to setup the db")
|
|
}
|
|
return dbx, nil
|
|
}
|
|
|
|
// Must is a helper function that wraps a call to Connect
|
|
// and panics if the error is non-nil.
|
|
func Must(db *sqlx.DB, err error) *sqlx.DB {
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return db
|
|
}
|
|
|
|
// helper function to ping the database with backoff to ensure
|
|
// a connection can be established before we proceed with the
|
|
// database setup and migration.
|
|
func pingDatabase(db *sqlx.DB) (err error) {
|
|
for i := 0; i < 30; i++ {
|
|
err = db.Ping()
|
|
if err == nil {
|
|
return
|
|
}
|
|
time.Sleep(time.Second)
|
|
}
|
|
return
|
|
}
|
|
|
|
// helper function to setup the databsae by performing automated
|
|
// database migration steps.
|
|
func setupDatabase(db *sqlx.DB) error {
|
|
return migrate.Migrate(db)
|
|
}
|