-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into redshift-string-precision
- Loading branch information
Showing
12 changed files
with
204 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package databricks | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/artie-labs/transfer/lib/destination/ddl" | ||
"github.com/artie-labs/transfer/lib/typing" | ||
) | ||
|
||
type Volume struct { | ||
name string | ||
path string | ||
} | ||
|
||
func NewVolume(volumeRow map[string]any) (Volume, error) { | ||
_volName, isOk := volumeRow["name"] | ||
if !isOk { | ||
return Volume{}, fmt.Errorf("volume name is missing") | ||
} | ||
|
||
volName, err := typing.AssertType[string](_volName) | ||
if err != nil { | ||
return Volume{}, fmt.Errorf("volume name is not a string") | ||
} | ||
|
||
_path, isOk := volumeRow["path"] | ||
if !isOk { | ||
return Volume{}, fmt.Errorf("volume path is missing") | ||
} | ||
|
||
path, err := typing.AssertType[string](_path) | ||
if err != nil { | ||
return Volume{}, fmt.Errorf("volume path is not a string") | ||
} | ||
|
||
return Volume{name: volName, path: path}, nil | ||
} | ||
|
||
func (v Volume) ShouldDelete() bool { | ||
return ddl.ShouldDeleteFromName(strings.TrimSuffix(v.name, ".csv")) | ||
} | ||
|
||
func (v Volume) Path() string { | ||
return v.path | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package databricks | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/artie-labs/transfer/lib/config/constants" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewVolume(t *testing.T) { | ||
{ | ||
// Invalid | ||
{ | ||
// Missing name | ||
_, err := NewVolume(map[string]any{"path": "path"}) | ||
assert.ErrorContains(t, err, "volume name is missing") | ||
} | ||
{ | ||
// Name isn't string | ||
_, err := NewVolume(map[string]any{"name": 1, "path": "path"}) | ||
assert.ErrorContains(t, err, "volume name is not a string") | ||
} | ||
{ | ||
// Missing path | ||
_, err := NewVolume(map[string]any{"name": "name"}) | ||
assert.ErrorContains(t, err, "volume path is missing") | ||
} | ||
{ | ||
// Path isn't string | ||
_, err := NewVolume(map[string]any{"name": "name", "path": 1}) | ||
assert.ErrorContains(t, err, "volume path is not a string") | ||
} | ||
} | ||
{ | ||
// Valid | ||
volume, err := NewVolume(map[string]any{"name": "name", "path": "path"}) | ||
assert.Nil(t, err) | ||
assert.Equal(t, "name", volume.name) | ||
assert.Equal(t, "path", volume.path) | ||
} | ||
} | ||
|
||
func newVolume(volName string) Volume { | ||
return Volume{ | ||
name: volName, | ||
} | ||
} | ||
|
||
func TestVolume_ShouldDelete(t *testing.T) { | ||
{ | ||
// Should delete | ||
volume := Volume{name: "name.csv"} | ||
assert.False(t, volume.ShouldDelete()) | ||
} | ||
{ | ||
// Tables that are eligible to be dropped | ||
tablesToDrop := []string{ | ||
"transactions___ARTIE_48GJC_1723663043", | ||
fmt.Sprintf("expired_tbl_%s_suffix_%d", constants.ArtiePrefix, time.Now().Add(-1*constants.TemporaryTableTTL).Unix()), | ||
fmt.Sprintf("artie_%s_suffix_%d", constants.ArtiePrefix, time.Now().Add(-1*constants.TemporaryTableTTL).Unix()), | ||
} | ||
|
||
for _, tblToDelete := range tablesToDrop { | ||
assert.True(t, newVolume(strings.ToLower(tblToDelete)).ShouldDelete(), tblToDelete) | ||
assert.True(t, newVolume(strings.ToUpper(tblToDelete)).ShouldDelete(), tblToDelete) | ||
assert.True(t, newVolume(tblToDelete).ShouldDelete(), tblToDelete) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package sql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
) | ||
|
||
func RowsToObjects(rows *sql.Rows) ([]map[string]any, error) { | ||
defer rows.Close() | ||
|
||
columns, err := rows.Columns() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var objects []map[string]any | ||
for rows.Next() { | ||
row := make([]any, len(columns)) | ||
rowPointers := make([]any, len(columns)) | ||
for i := range row { | ||
rowPointers[i] = &row[i] | ||
} | ||
|
||
if err = rows.Scan(rowPointers...); err != nil { | ||
return nil, err | ||
} | ||
|
||
object := make(map[string]any) | ||
for i, column := range columns { | ||
object[column] = row[i] | ||
} | ||
|
||
objects = append(objects, object) | ||
} | ||
|
||
if err = rows.Err(); err != nil { | ||
return nil, fmt.Errorf("failed to iterate over rows: %w", err) | ||
} | ||
|
||
return objects, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters