Skip to content

Commit

Permalink
fix: Update table name validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
bartventer committed Jan 2, 2024
1 parent 2f7cbaf commit 34f4739
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions drivers/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,27 @@ func newMultitenancyConfig(models []interface{}) (*multitenancyConfig, error) {
var (
publicModels = make([]interface{}, 0, len(models))
privateModels = make([]interface{}, 0, len(models))
errors = make([]error, 0)
)
errors := make([]error, 0)
for _, m := range models {
tn, ok := m.(interface{ TableName() string })
if !ok {
errors = append(errors, fmt.Errorf("model %T does not implement TableName()", m))
continue
}
tt, ok := m.(multitenancy.TenantTabler)
parts := strings.Split(tn.TableName(), ".")
if ok && tt.IsTenantTable() {
// ensure that contains no fullstop
if strings.Contains(tn.TableName(), ".") {
// ensure that the private model does not contain a fullstop
if len(parts) > 1 {
errors = append(errors, fmt.Errorf("invalid table name for model %T labeled as tenant table, table name should not contain a fullstop, got '%s'", m, tn.TableName()))
continue
}
privateModels = append(privateModels, m)
} else {
// Ensure that the public model starts with the default schema (public.)
if !strings.HasPrefix(tn.TableName(), fmt.Sprintf("%s.", PublicSchemaName)) {
errors = append(errors, fmt.Errorf("invalid table name for model %T labeled as public table; table name should start with '%s.', got '%s'", m, PublicSchemaName, tn.TableName()))
// ensure that the public model starts with the default schema (public.)
if len(parts) != 2 || parts[0] != PublicSchemaName {
errors = append(errors, fmt.Errorf("invalid table name for model %T labeled as public table, table name should start with '%s.', got '%s'", m, PublicSchemaName, tn.TableName()))
continue
}
publicModels = append(publicModels, m)
Expand Down

0 comments on commit 34f4739

Please sign in to comment.