Skip to content
Closed
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 .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
.vscode
tmp
/ripoff
/ripoff-export

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just ignoring the other executable this builds.

.DS_Store
/export
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,15 @@ Currently, it attempts to export all data from all tables into a single ripoff f
ripoff-export --exclude users --exclude audit_logs /path/to/export
```

You can also use the `--ignore-on-update` flag to mark columns that should be ignored during subsequent imports (useful for timestamps that shouldn't change):

```bash
# Export all data but ignore created_at and updated_at columns during updates
ripoff-export --ignore-on-update created_at --ignore-on-update updated_at /path/to/export
```

This adds a `~ignore_on_update` metadata field to rows containing the specified columns. During import, these columns will be included for new rows but ignored when updating existing rows, preventing timestamp-only changes from triggering unnecessary updates.

In the future, additional flags may be added to allow you to include tables, add arbitrary `WHERE` conditions, modify the row id/key, export multiple files, or use existing templates.

## Installation
Expand Down
34 changes: 32 additions & 2 deletions cmd/ripoff-export/ripoff_export.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ func errAttr(err error) slog.Attr {
func main() {
// Define flags
var excludeTables stringSliceFlag
var ignoreOnUpdateColumns stringSliceFlag
flag.Var(&excludeTables, "exclude", "Exclude specific tables from export (can be specified multiple times)")
flag.Var(&ignoreOnUpdateColumns, "ignore-on-update", "Columns to ignore during updates but include in initial export (can be specified multiple times)")

// Parse flags
flag.Parse()
Expand Down Expand Up @@ -56,6 +58,33 @@ func main() {
os.Exit(1)
}

// Load existing data if ignore-on-update is specified and directory exists
var existingData *ripoff.RipoffFile
if len(ignoreOnUpdateColumns) > 0 && err == nil && !os.IsNotExist(err) {
// Directory exists and we have ignore-on-update columns, try to load existing data
slog.Info("Loading existing YAML data to preserve ignore-on-update column values")
// We need a temporary transaction to load enums
tempTx, tempErr := conn.Begin(ctx)
if tempErr != nil {
slog.Error("Could not create temporary transaction for loading existing data", errAttr(tempErr))
os.Exit(1)
}
enums, tempErr := ripoff.GetEnumValues(ctx, tempTx)
_ = tempTx.Rollback(ctx) // Clean up temp transaction, ignore error since it's temporary
if tempErr != nil {
slog.Error("Could not load enums for existing data", errAttr(tempErr))
os.Exit(1)
}
existingRipoff, tempErr := ripoff.RipoffFromDirectory(exportDirectory, enums)
if tempErr != nil {
slog.Warn("Could not load existing YAML data, will use fresh database values", errAttr(tempErr))
existingData = nil
} else {
existingData = &existingRipoff
slog.Info(fmt.Sprintf("Loaded existing data with %d rows", len(existingData.Rows)))
}
}

// Directory exists, delete it after verifying that it's safe to do so.
if err == nil && !os.IsNotExist(err) {
err = filepath.WalkDir(exportDirectory, func(path string, entry os.DirEntry, err error) error {
Expand Down Expand Up @@ -97,8 +126,9 @@ func main() {
}
}()

// Pass the excluded tables to the export function
ripoffFile, err := ripoff.ExportToRipoff(ctx, tx, excludeTables)
// Pass the excluded tables and ignore-on-update columns to the export function
// Use ExportToRipoffWithExisting to preserve ignore-on-update column values
ripoffFile, err := ripoff.ExportToRipoffWithExisting(ctx, tx, excludeTables, ignoreOnUpdateColumns, existingData)
if err != nil {
slog.Error("Could not assemble ripoff file from database", errAttr(err))
os.Exit(1)
Expand Down
31 changes: 29 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,23 @@ func buildQueryForRow(primaryKeys PrimaryKeysResult, rowId string, row Row, depe
columns := []string{}
values := []string{}
setStatements := []string{}

// Extract ignore-on-update columns if present
ignoreOnUpdateColumns := map[string]bool{}
if ignoreOnUpdateRaw, hasIgnoreOnUpdate := row["~ignore_on_update"]; hasIgnoreOnUpdate {
switch v := ignoreOnUpdateRaw.(type) {
// Coming from yaml
case []interface{}:
for _, curr := range v {
ignoreOnUpdateColumns[curr.(string)] = true
}
// Coming from Go, probably a test
case []string:
for _, curr := range v {
ignoreOnUpdateColumns[curr] = true
}
}
}

onConflictColumn := ""
if hasPrimaryKeysForTable {
Expand All @@ -187,6 +204,10 @@ func buildQueryForRow(primaryKeys PrimaryKeysResult, rowId string, row Row, depe
if column == "~conflict" {
continue
}
// Ignore-on-update metadata, not a database column.
if column == "~ignore_on_update" {
continue
}
// Explicit dependencies, for foreign keys to non-primary keys.
if column == "~dependencies" {
dependencies := []string{}
Expand All @@ -212,7 +233,10 @@ func buildQueryForRow(primaryKeys PrimaryKeysResult, rowId string, row Row, depe
if valueRaw == nil {
columns = append(columns, pq.QuoteIdentifier(column))
values = append(values, "NULL")
setStatements = append(setStatements, fmt.Sprintf("%s = %s", pq.QuoteIdentifier(column), "NULL"))
// Only add to setStatements if this column is not ignored on update
if !ignoreOnUpdateColumns[column] {
setStatements = append(setStatements, fmt.Sprintf("%s = %s", pq.QuoteIdentifier(column), "NULL"))
}
} else {
value := fmt.Sprint(valueRaw)

Expand All @@ -234,7 +258,10 @@ func buildQueryForRow(primaryKeys PrimaryKeysResult, rowId string, row Row, depe
onConflictColumn = pq.QuoteIdentifier(column)
}
values = append(values, pq.QuoteLiteral(valuePrepared))
setStatements = append(setStatements, fmt.Sprintf("%s = %s", pq.QuoteIdentifier(column), pq.QuoteLiteral(valuePrepared)))
// Only add to setStatements if this column is not ignored on update
if !ignoreOnUpdateColumns[column] {
setStatements = append(setStatements, fmt.Sprintf("%s = %s", pq.QuoteIdentifier(column), pq.QuoteLiteral(valuePrepared)))
}
}
}

Expand Down
38 changes: 37 additions & 1 deletion export.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,16 @@ type RowMissingDependency struct {

// Exports all rows in the database to a ripoff file.
// excludeTables is a list of table names to exclude from the export.
func ExportToRipoff(ctx context.Context, tx pgx.Tx, excludeTables []string) (RipoffFile, error) {
// ignoreOnUpdateColumns is a list of column names to mark as ignored during updates.
func ExportToRipoff(ctx context.Context, tx pgx.Tx, excludeTables []string, ignoreOnUpdateColumns []string) (RipoffFile, error) {
return ExportToRipoffWithExisting(ctx, tx, excludeTables, ignoreOnUpdateColumns, nil)
}

// ExportToRipoffWithExisting exports all rows in the database to a ripoff file, preserving existing values for ignore-on-update columns.
// excludeTables is a list of table names to exclude from the export.
// ignoreOnUpdateColumns is a list of column names to mark as ignored during updates.
// existingData is optional existing YAML data to preserve ignore-on-update column values from.
func ExportToRipoffWithExisting(ctx context.Context, tx pgx.Tx, excludeTables []string, ignoreOnUpdateColumns []string, existingData *RipoffFile) (RipoffFile, error) {
ripoffFile := RipoffFile{
Rows: map[string]Row{},
}
Expand Down Expand Up @@ -90,7 +99,17 @@ func ExportToRipoff(ctx context.Context, tx pgx.Tx, excludeTables []string) (Rip
// A map of fieldName -> tableName to convert values to literal:(...)
literalFields := map[string]string{}
ids := []string{}
// Track columns that should be ignored on update
var ignoreOnUpdateFields []string
for i, field := range fields {
// Check if this column should be ignored on update
for _, ignoreCol := range ignoreOnUpdateColumns {
if field.Name == ignoreCol {
ignoreOnUpdateFields = append(ignoreOnUpdateFields, field.Name)
break
}
}

// Null columns are still exported since we don't know if there is a default or not (at least not at time of writing).
if columns[i] == nil {
ripoffRow[field.Name] = nil
Expand Down Expand Up @@ -172,6 +191,23 @@ func ExportToRipoff(ctx context.Context, tx pgx.Tx, excludeTables []string) (Rip
for fieldName, toTable := range literalFields {
ripoffRow[fieldName] = fmt.Sprintf("%s:literal(%s)", toTable, ripoffRow[fieldName])
}
// Add ignore-on-update metadata if any columns should be ignored
if len(ignoreOnUpdateFields) > 0 {
ripoffRow["~ignore_on_update"] = ignoreOnUpdateFields
}

// Preserve existing values for ignore-on-update columns if existing data is provided
if existingData != nil {
if existingRow, exists := existingData.Rows[rowKey]; exists {
for _, ignoreCol := range ignoreOnUpdateColumns {
if existingValue, hasExistingValue := existingRow[ignoreCol]; hasExistingValue {
// Preserve the existing value instead of using the current database value
ripoffRow[ignoreCol] = existingValue
}
}
}
}

ripoffFile.Rows[rowKey] = ripoffRow
}
}
Expand Down
Loading