Skip to content
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

fix: update correct schema in postgres #3585

Merged
merged 1 commit into from
Dec 1, 2024
Merged
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
30 changes: 25 additions & 5 deletions cmd/ftl-provisioner-cloudformation/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ func PostgresPostUpdate(ctx context.Context, secrets *secretsmanager.Client, byN
return fmt.Errorf("failed to get username and password from secret ARN: %w", err)
}

adminEndpoint := endpointToDSN(write.OutputValue, "postgres", 5432, username, password)

// Connect to postgres without a specific database to create the new one
if err := createPostgresDatabase(ctx, *write.OutputValue, resourceID, username, password); err != nil {
return fmt.Errorf("failed to create postgres database: %w", err)
}
adminEndpoint := endpointToDSN(write.OutputValue, resourceID, 5432, username, password)
db, err := sql.Open("pgx", adminEndpoint)
if err != nil {
return fmt.Errorf("failed to connect to postgres: %w", err)
Expand All @@ -96,8 +97,7 @@ func PostgresPostUpdate(ctx context.Context, secrets *secretsmanager.Client, byN
if _, err := db.ExecContext(ctx, fmt.Sprintf(`
GRANT CONNECT ON DATABASE %s TO ftluser;
GRANT USAGE ON SCHEMA public TO ftluser;
GRANT USAGE ON SCHEMA public TO ftluser;
GRANT CREATE ON SCHEMA public TO ftluser;
GRANT CREATE ON SCHEMA public TO ftluser;
GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA public TO ftluser;
GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA public TO ftluser;
ALTER DEFAULT PRIVILEGES IN SCHEMA public GRANT ALL ON TABLES TO ftluser;
Expand All @@ -110,6 +110,26 @@ func PostgresPostUpdate(ctx context.Context, secrets *secretsmanager.Client, byN
return nil
}

func createPostgresDatabase(ctx context.Context, endpoint, resourceID, username, password string) error {
adminEndpoint := endpointToDSN(&endpoint, "postgres", 5432, username, password)

// Connect to postgres without a specific database to create the new one
db, err := sql.Open("pgx", adminEndpoint)
if err != nil {
return fmt.Errorf("failed to connect to postgres: %w", err)
}
defer db.Close()

// Create the database if it doesn't exist
if _, err := db.ExecContext(ctx, "CREATE DATABASE "+resourceID); err != nil {
// Ignore if database already exists
if !strings.Contains(err.Error(), "already exists") {
return fmt.Errorf("failed to create database: %w", err)
}
}
return nil
}

func updatePostgresOutputs(_ context.Context, to *schemapb.DatabaseRuntime, resourceID string, outputs []types.Output) error {
byName, err := outputsByPropertyName(outputs)
if err != nil {
Expand Down
Loading