Fix: Polish bootstrap flow
This commit is contained in:
9
Backend/db/migrations/000003_installation_name.sql
Normal file
9
Backend/db/migrations/000003_installation_name.sql
Normal file
@@ -0,0 +1,9 @@
|
||||
-- +goose Up
|
||||
|
||||
ALTER TABLE installations
|
||||
ADD COLUMN IF NOT EXISTS name TEXT NOT NULL DEFAULT '';
|
||||
|
||||
-- +goose Down
|
||||
|
||||
ALTER TABLE installations
|
||||
DROP COLUMN IF EXISTS name;
|
||||
@@ -52,6 +52,7 @@ type SaveInstanceInput struct {
|
||||
|
||||
type SaveModeInput struct {
|
||||
Mode string
|
||||
Name string
|
||||
}
|
||||
|
||||
type SaveAdminInput struct {
|
||||
@@ -69,6 +70,7 @@ type SaveStructureInput struct {
|
||||
|
||||
type InstallationRecord struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Mode string `json:"mode"`
|
||||
Access string `json:"access"`
|
||||
Protocol string `json:"protocol"`
|
||||
@@ -176,9 +178,10 @@ func NewService(db *database.DB) *Service {
|
||||
|
||||
func (service *Service) SaveInstance(ctx context.Context, input SaveInstanceInput) (InstallationRecord, error) {
|
||||
row := service.db.Pool.QueryRow(ctx, `
|
||||
INSERT INTO installations (singleton, mode, access, protocol, host)
|
||||
INSERT INTO installations (singleton, name, mode, access, protocol, host)
|
||||
VALUES (
|
||||
TRUE,
|
||||
COALESCE((SELECT name FROM installations WHERE singleton = TRUE LIMIT 1), ''),
|
||||
COALESCE((SELECT mode FROM installations WHERE singleton = TRUE LIMIT 1), 'personal'::instance_mode),
|
||||
$1::instance_access,
|
||||
$2::instance_protocol,
|
||||
@@ -190,7 +193,7 @@ func (service *Service) SaveInstance(ctx context.Context, input SaveInstanceInpu
|
||||
protocol = EXCLUDED.protocol,
|
||||
host = EXCLUDED.host,
|
||||
updated_at = NOW()
|
||||
RETURNING id::text, mode::text, access::text, protocol::text, host, is_bootstrapped;
|
||||
RETURNING id::text, name, mode::text, access::text, protocol::text, host, is_bootstrapped;
|
||||
`, input.Access, input.Protocol, input.Host)
|
||||
|
||||
return scanInstallationRecord(row)
|
||||
@@ -198,20 +201,22 @@ func (service *Service) SaveInstance(ctx context.Context, input SaveInstanceInpu
|
||||
|
||||
func (service *Service) SaveMode(ctx context.Context, input SaveModeInput) (InstallationRecord, error) {
|
||||
row := service.db.Pool.QueryRow(ctx, `
|
||||
INSERT INTO installations (singleton, mode, access, protocol, host)
|
||||
INSERT INTO installations (singleton, name, mode, access, protocol, host)
|
||||
VALUES (
|
||||
TRUE,
|
||||
$2,
|
||||
$1::instance_mode,
|
||||
COALESCE((SELECT access FROM installations WHERE singleton = TRUE LIMIT 1), 'local'::instance_access),
|
||||
COALESCE((SELECT protocol FROM installations WHERE singleton = TRUE LIMIT 1), 'http'::instance_protocol),
|
||||
COALESCE((SELECT host FROM installations WHERE singleton = TRUE LIMIT 1), $2)
|
||||
COALESCE((SELECT host FROM installations WHERE singleton = TRUE LIMIT 1), $3)
|
||||
)
|
||||
ON CONFLICT (singleton) DO UPDATE
|
||||
SET
|
||||
name = EXCLUDED.name,
|
||||
mode = EXCLUDED.mode,
|
||||
updated_at = NOW()
|
||||
RETURNING id::text, mode::text, access::text, protocol::text, host, is_bootstrapped;
|
||||
`, input.Mode, defaultInstallationHost)
|
||||
RETURNING id::text, name, mode::text, access::text, protocol::text, host, is_bootstrapped;
|
||||
`, input.Mode, input.Name, defaultInstallationHost)
|
||||
|
||||
return scanInstallationRecord(row)
|
||||
}
|
||||
@@ -301,7 +306,7 @@ func (service *Service) SaveStructure(ctx context.Context, input SaveStructureIn
|
||||
|
||||
organizationName := strings.TrimSpace(input.OrganizationName)
|
||||
if organizationName == "" {
|
||||
organizationName = defaultRootOrganizationName(installation.Mode, installation.Host, admin.DisplayName)
|
||||
organizationName = defaultRootOrganizationName(installation.Name, installation.Mode, installation.Host, admin.DisplayName)
|
||||
}
|
||||
|
||||
organization, err := upsertNamedRecord(ctx, tx, `
|
||||
@@ -442,7 +447,7 @@ func (service *Service) ResetDevelopmentState(ctx context.Context) error {
|
||||
|
||||
func (service *Service) GetInstallation(ctx context.Context) (*InstallationRecord, error) {
|
||||
record, err := scanInstallationRecord(service.db.Pool.QueryRow(ctx, `
|
||||
SELECT id::text, mode::text, access::text, protocol::text, host, is_bootstrapped
|
||||
SELECT id::text, name, mode::text, access::text, protocol::text, host, is_bootstrapped
|
||||
FROM installations
|
||||
WHERE singleton = TRUE
|
||||
LIMIT 1;
|
||||
@@ -597,7 +602,7 @@ func (service *Service) GetAppShellState(ctx context.Context) (AppShellState, er
|
||||
|
||||
func scanInstallationRecord(row pgx.Row) (InstallationRecord, error) {
|
||||
var record InstallationRecord
|
||||
if err := row.Scan(&record.ID, &record.Mode, &record.Access, &record.Protocol, &record.Host, &record.IsBootstrapped); err != nil {
|
||||
if err := row.Scan(&record.ID, &record.Name, &record.Mode, &record.Access, &record.Protocol, &record.Host, &record.IsBootstrapped); err != nil {
|
||||
return InstallationRecord{}, err
|
||||
}
|
||||
|
||||
@@ -802,7 +807,7 @@ func (service *Service) listWorkspaces(ctx context.Context) ([]WorkspaceRecord,
|
||||
|
||||
func loadInstallation(ctx context.Context, tx pgx.Tx) (InstallationRecord, error) {
|
||||
return scanInstallationRecord(tx.QueryRow(ctx, `
|
||||
SELECT id::text, mode::text, access::text, protocol::text, host, is_bootstrapped
|
||||
SELECT id::text, name, mode::text, access::text, protocol::text, host, is_bootstrapped
|
||||
FROM installations
|
||||
WHERE singleton = TRUE
|
||||
LIMIT 1;
|
||||
@@ -829,7 +834,7 @@ func updateBootstrappedInstallation(ctx context.Context, tx pgx.Tx) (Installatio
|
||||
UPDATE installations
|
||||
SET is_bootstrapped = TRUE, bootstrapped_at = COALESCE(bootstrapped_at, NOW()), updated_at = NOW()
|
||||
WHERE singleton = TRUE
|
||||
RETURNING id::text, mode::text, access::text, protocol::text, host, is_bootstrapped;
|
||||
RETURNING id::text, name, mode::text, access::text, protocol::text, host, is_bootstrapped;
|
||||
`))
|
||||
}
|
||||
|
||||
@@ -860,10 +865,15 @@ func upsertWorkspace(ctx context.Context, tx pgx.Tx, organizationID, name, slug,
|
||||
return err
|
||||
}
|
||||
|
||||
func defaultRootOrganizationName(mode, host, adminDisplayName string) string {
|
||||
func defaultRootOrganizationName(installationName, mode, host, adminDisplayName string) string {
|
||||
trimmedInstallationName := strings.TrimSpace(installationName)
|
||||
trimmedHost := strings.TrimSpace(host)
|
||||
trimmedAdminDisplayName := strings.TrimSpace(adminDisplayName)
|
||||
|
||||
if trimmedInstallationName != "" {
|
||||
return trimmedInstallationName
|
||||
}
|
||||
|
||||
if strings.EqualFold(mode, defaultInstallationMode) {
|
||||
if trimmedAdminDisplayName != "" {
|
||||
return fmt.Sprintf("%s %s", trimmedAdminDisplayName, defaultPersonalServerSuffix)
|
||||
|
||||
@@ -20,6 +20,7 @@ type bootstrapInstanceStepRequest struct {
|
||||
|
||||
type bootstrapModeStepRequest struct {
|
||||
Mode string `json:"mode"`
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type bootstrapAdminStepRequest struct {
|
||||
@@ -234,13 +235,19 @@ func (routes apiRoutes) handleBootstrapModeStep(w http.ResponseWriter, r *http.R
|
||||
return
|
||||
}
|
||||
payload.Mode = strings.ToLower(strings.TrimSpace(payload.Mode))
|
||||
payload.Name = strings.TrimSpace(payload.Name)
|
||||
|
||||
if payload.Mode != "personal" && payload.Mode != "organizational" {
|
||||
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Mode must be either 'personal' or 'organizational'.")
|
||||
return
|
||||
}
|
||||
|
||||
record, err := routes.bootstrapService().SaveMode(r.Context(), bootstrapservice.SaveModeInput{Mode: payload.Mode})
|
||||
if payload.Name == "" {
|
||||
WriteError(w, http.StatusBadRequest, RequestIDFromContext(r.Context()), "invalid_request", "Name is required.")
|
||||
return
|
||||
}
|
||||
|
||||
record, err := routes.bootstrapService().SaveMode(r.Context(), bootstrapservice.SaveModeInput{Mode: payload.Mode, Name: payload.Name})
|
||||
if err != nil {
|
||||
routes.writeBootstrapPersistenceError(w, r, err)
|
||||
return
|
||||
@@ -356,7 +363,11 @@ func (routes apiRoutes) writeBootstrapPersistenceError(w http.ResponseWriter, r
|
||||
WriteError(w, http.StatusConflict, RequestIDFromContext(r.Context()), "bootstrap_prerequisite_missing", err.Error())
|
||||
default:
|
||||
routes.cfg.Logger.Error("persist bootstrap step", "error", err, "path", r.URL.Path)
|
||||
WriteError(w, http.StatusInternalServerError, RequestIDFromContext(r.Context()), "bootstrap_persist_failed", "Failed to persist bootstrap data.")
|
||||
message := "Failed to persist bootstrap data."
|
||||
if routes.cfg.Config.IsDevelopment() {
|
||||
message = message + " " + err.Error()
|
||||
}
|
||||
WriteError(w, http.StatusInternalServerError, RequestIDFromContext(r.Context()), "bootstrap_persist_failed", message)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user