-
-
Notifications
You must be signed in to change notification settings - Fork 207
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
98 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package cmd | ||
|
||
import ( | ||
"errors" | ||
|
||
"github.com/spf13/cobra" | ||
"github.com/supabase/cli/internal/resolve" | ||
) | ||
|
||
var ( | ||
appliedTimestamp *string | ||
rolledBackTimestamp *string | ||
|
||
resolveCmd = &cobra.Command{ | ||
Use: "resolve", | ||
Short: "Resolve issues with migration history on the deploy database.", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if appliedTimestamp != nil { | ||
return resolve.ResolveApplied(*appliedTimestamp) | ||
} else if rolledBackTimestamp != nil { | ||
return resolve.ResolveRolledBack(*rolledBackTimestamp) | ||
} else { | ||
return errors.New("Must set either --applied or --rolled-back.") | ||
} | ||
}, | ||
} | ||
) | ||
|
||
func init() { | ||
resolveCmd.Flags().StringVar(appliedTimestamp, "applied", "", "Migration timestamp to be recorded as applied.") | ||
resolveCmd.Flags().StringVar(rolledBackTimestamp, "rolled-back", "", "Migration timestamp to be recorded as rolled back.") | ||
|
||
rootCmd.AddCommand(resolveCmd) | ||
} |
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,62 @@ | ||
package resolve | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
pgx "github.com/jackc/pgx/v4" | ||
) | ||
|
||
var ctx = context.TODO() | ||
|
||
func ResolveApplied(timestamp string) error { | ||
url := os.Getenv("SUPABASE_DEPLOY_DB_URL") | ||
if url == "" { | ||
return errors.New("❌ SUPABASE_DEPLOY_DB_URL is not set.") | ||
} | ||
|
||
conn, err := pgx.Connect(ctx, url) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close(context.Background()) | ||
|
||
if _, err := conn.Query( | ||
ctx, | ||
"INSERT INTO supabase_migrations.schema_migrations(version) VALUES ($1)", | ||
timestamp, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Finished supabase resolve.") | ||
|
||
return nil | ||
} | ||
|
||
func ResolveRolledBack(timestamp string) error { | ||
url := os.Getenv("SUPABASE_DEPLOY_DB_URL") | ||
if url == "" { | ||
return errors.New("❌ SUPABASE_DEPLOY_DB_URL is not set.") | ||
} | ||
|
||
conn, err := pgx.Connect(ctx, url) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close(context.Background()) | ||
|
||
if _, err := conn.Query( | ||
ctx, | ||
"DELETE FROM supabase_migrations.schema_migrations WHERE version = $1", | ||
timestamp, | ||
); err != nil { | ||
return err | ||
} | ||
|
||
fmt.Println("Finished supabase resolve.") | ||
|
||
return nil | ||
} |