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

[Postgres] Primary key override #481

Merged
merged 2 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions config/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type PostgreSQLTable struct {

// Optional settings
BatchSize uint `yaml:"batchSize,omitempty"`
PrimaryKeysOverride []string `yaml:"primaryKeysOverride,omitempty"`
OptionalPrimaryKeyValStart string `yaml:"optionalPrimaryKeyValStart,omitempty"`
OptionalPrimaryKeyValEnd string `yaml:"optionalPrimaryKeyValEnd,omitempty"`
ExcludeColumns []string `yaml:"excludeColumns,omitempty"`
Expand Down
10 changes: 7 additions & 3 deletions lib/postgres/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Table struct {
PrimaryKeys []string
}

func LoadTable(db *sql.DB, _schema string, name string) (*Table, error) {
func LoadTable(db *sql.DB, _schema string, name string, primaryKeysOverride []string) (*Table, error) {
tbl := &Table{
Name: name,
Schema: _schema,
Expand All @@ -29,8 +29,12 @@ func LoadTable(db *sql.DB, _schema string, name string) (*Table, error) {
return nil, fmt.Errorf("failed to describe table %s.%s: %w", tbl.Schema, tbl.Name, err)
}

if tbl.PrimaryKeys, err = schema.FetchPrimaryKeys(db, tbl.Schema, tbl.Name); err != nil {
return nil, fmt.Errorf("failed to retrieve primary keys: %w", err)
if len(primaryKeysOverride) > 0 {
tbl.PrimaryKeys = primaryKeysOverride
} else {
if tbl.PrimaryKeys, err = schema.FetchPrimaryKeys(db, tbl.Schema, tbl.Name); err != nil {
return nil, fmt.Errorf("failed to retrieve primary keys: %w", err)
}
}

return tbl, nil
Expand Down
2 changes: 1 addition & 1 deletion sources/postgres/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type PostgresAdapter struct {

func NewPostgresAdapter(db *sql.DB, tableCfg config.PostgreSQLTable) (PostgresAdapter, error) {
slog.Info("Loading metadata for table")
table, err := postgres.LoadTable(db, tableCfg.Schema, tableCfg.Name)
table, err := postgres.LoadTable(db, tableCfg.Schema, tableCfg.Name, tableCfg.PrimaryKeysOverride)
if err != nil {
return PostgresAdapter{}, fmt.Errorf("failed to load metadata for table %s.%s: %w", tableCfg.Schema, tableCfg.Name, err)
}
Expand Down
Loading