Skip to content

Commit

Permalink
chore: refine the replica-setup-pg and its doc
Browse files Browse the repository at this point in the history
  • Loading branch information
NoyException committed Nov 19, 2024
1 parent 81cb2ab commit c1a31a9
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 13 deletions.
4 changes: 2 additions & 2 deletions devtools/replica-setup-pg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ bash replica_setup.sh --pg_dump /path/to/pg_dump
## Example

```bash
bash replica_setup.sh --pg_dump ../../backup.sql
bash replica_setup.sh --pg_dump pg_dump.sql
```

This command sets up MyDuck Server as a replica of the PostgreSQL instance running at `192.168.1.100` on port `3306` with the user `root` and password `mypassword`.
This command sets up MyDuck Server as a replica of the PostgreSQL instance running at `192.168.1.100` on port `5432` with the user `root` and password `mypassword`.
3 changes: 0 additions & 3 deletions devtools/replica-setup-pg/install_psql.sh

This file was deleted.

11 changes: 4 additions & 7 deletions devtools/replica-setup-pg/replica_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,10 @@ if [[ -z $MYDUCK_DB ]]; then
usage
fi

# Step 1: Check if psql exists, if not, install it
# Step 1: Check if psql exists
if ! command -v psql &> /dev/null; then
echo "psql not found, attempting to install..."
bash install_psql.sh
check_command "psql installation"
else
echo "psql is already installed."
echo "Error: psql is not installed."
exit 1
fi

# Step 2: Check if replication has already been started
Expand All @@ -92,7 +89,7 @@ fi
# Step 5: Load the existing data from pg_dump file
if [[ -n "$PG_DUMP" ]]; then
echo "Loading the snapshot from pg_dump to MyDuck Server..."
source snapshot.sh
source snapshot_dump.sh
check_command "loading a snapshot from pg_dump"
else
echo "No pg_dump file specified. Skipping snapshot."
Expand Down
File renamed without changes.
6 changes: 5 additions & 1 deletion pgserver/connection_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,11 @@ func (h *ConnectionHandler) sendError(err error) {
}

// convertQuery takes the given Postgres query, and converts it as an ast.ConvertedQuery that will work with the handler.
func (h *ConnectionHandler) convertQuery(query string) (ConvertedQuery, error) {
func (h *ConnectionHandler) convertQuery(query string, modifiers ...QueryModifier) (ConvertedQuery, error) {
for _, modifier := range modifiers {
query = modifier(query)
}

parsable := true
stmts, err := parser.Parse(query)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions pgserver/duck_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,9 @@ func resultForOkIter(ctx *sql.Context, iter sql.RowIter) (*Result, error) {
// resultForEmptyIter ensures that an expected empty iterator returns no rows.
func resultForEmptyIter(ctx *sql.Context, iter sql.RowIter) (*Result, error) {
defer trace.StartRegion(ctx, "DuckHandler.resultForEmptyIter").End()
if iter == nil {
return &Result{Fields: nil}, nil
}
if _, err := iter.Next(ctx); err != io.EOF {
return nil, fmt.Errorf("result schema iterator returned more than zero rows")
}
Expand Down
22 changes: 22 additions & 0 deletions pgserver/query_modifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package pgserver

import "regexp"

type QueryModifier func(string) string

func NewQueryReplacer(regex string, replacement string) QueryModifier {
r := regexp.MustCompile(regex)
return func(query string) string {
return r.ReplaceAllString(query, replacement)
}

}

func NewQueryRemover(regex string) QueryModifier {
return NewQueryReplacer(regex, "")
}

var (
removeLocaleProvider = NewQueryRemover(`(?i)LOCALE_PROVIDER = [^ ;]*`)
removeLocale = NewQueryRemover(`(?i)LOCALE = [^ ;]*`)
)

0 comments on commit c1a31a9

Please sign in to comment.