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

add IfNotExists to CreateTable to easily skip table creation #194

Open
guregu opened this issue Mar 23, 2022 · 1 comment
Open

add IfNotExists to CreateTable to easily skip table creation #194

guregu opened this issue Mar 23, 2022 · 1 comment
Labels

Comments

@guregu
Copy link
Owner

guregu commented Mar 23, 2022

or maybe a flag like SkipIfExists, UpdateIfExists to try to match the input

@guregu guregu added the feature label Mar 23, 2022
@pedrohba1
Copy link

pedrohba1 commented Mar 8, 2023

I have this small piece of code that does that, with the aws sdk, if it helps:

package database

import (
	"context"
	"log"

	"github.com/aws/aws-sdk-go-v2/aws"
	"github.com/aws/aws-sdk-go-v2/service/dynamodb"
	"github.com/aws/aws-sdk-go-v2/service/sns/types"
	"github.com/pkg/errors"
)

func TableExists(tableName string) (bool, error) {
	log.Printf("Validating table existence: %s", tableName)

	exists := true
	_, err := DbClient.DescribeTable(
		context.TODO(), &dynamodb.DescribeTableInput{TableName: aws.String(tableName)},
	)
	if err != nil {
		var notFoundEx *types.ResourceNotFoundException
		if errors.As(err, &notFoundEx) {
			log.Printf("Table %v does not exist.\n", tableName)
			err = nil
		} else {
			log.Printf("Couldn't determine existence of table %v. Here's why: %v\n", tableName, err)
		}
		exists = false
	}

	if exists {
		log.Printf("Table already exists. skipping...")
	}
	return exists, err
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants