Skip to content

Commit

Permalink
feat(pkg/show/spanner): add spanner experimental support (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent authored Jan 15, 2024
2 parents 9187119 + 165eb32 commit 347b97f
Show file tree
Hide file tree
Showing 71 changed files with 6,151 additions and 516 deletions.
4 changes: 3 additions & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ linters-settings:
- func errors.New(text string) error
- func fmt.Errorf(format string, a ...any) error
- func fmt.Errorf(format string, a ...interface{}) error
- func github.com/kunitsucom/util.go/errors.Errorf(format string, a ...interface{}) error
- func github.com/kunitsucom/util.go/apperr.Errorf(format string, a ...interface{}) error
- var github.com/kunitsucom/ddlctl/pkg/apperr.Errorf func(format string, a ...any) error
- var github.com/kunitsucom/ddlctl/pkg/apperr.Errorf func(format string, a ...interface{}) error


issues:
Expand Down
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# [ddlctl](https://github.com/kunitsucom/ddlctl)

> [!WARNING]
> This project is experimental. It is operational in the author's environment, but it is not known if it can be operated in other environments without trouble.
[![license](https://img.shields.io/github/license/kunitsucom/ddlctl)](LICENSE)
[![pkg](https://pkg.go.dev/badge/github.com/kunitsucom/ddlctl)](https://pkg.go.dev/github.com/kunitsucom/ddlctl)
[![goreportcard](https://goreportcard.com/badge/github.com/kunitsucom/ddlctl)](https://goreportcard.com/report/github.com/kunitsucom/ddlctl)
Expand Down Expand Up @@ -27,7 +30,7 @@
- `show` subcommand
- dialect
- [x] Support `mysql` (beta)
- [x] Support `postgres` (beta)
- [x] Support `postgres` (alpha)
- [x] Support `cockroachdb` (beta)
- [x] Support `spanner` (alpha)
- [ ] Support `sqlite3`
Expand All @@ -36,14 +39,14 @@
- [x] Support `mysql` (alpha)
- [x] Support `postgres` (alpha)
- [x] Support `cockroachdb` (alpha)
- [ ] Support `spanner`
- [x] Support `spanner` (alpha)
- [ ] Support `sqlite3`
- `apply` subcommand
- dialect
- [x] Support `mysql` (alpha)
- [x] Support `postgres` (alpha)
- [x] Support `cockroachdb` (alpha)
- [ ] Support `spanner`
- [x] Support `spanner` (alpha)
- [ ] Support `sqlite3`

## Example
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.21.5
require (
github.com/go-sql-driver/mysql v1.7.1
github.com/googleapis/go-sql-spanner v1.1.1
github.com/kunitsucom/util.go v0.0.61-0.20240112184211-8b1d5e248ad7
github.com/kunitsucom/util.go v0.0.62-rc.1
github.com/lib/pq v1.10.9
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ github.com/googleapis/gax-go/v2 v2.12.0 h1:A+gCJKdRfqXkr+BIRGtZLibNXf0m1f9E4HG56
github.com/googleapis/gax-go/v2 v2.12.0/go.mod h1:y+aIqrI5eb1YGMVJfuV3185Ts/D7qKpsEkdD5+I6QGU=
github.com/googleapis/go-sql-spanner v1.1.1 h1:Z5kRckvrSNokM/x21BBy23kg9b0e9ikkderuuLVXfGY=
github.com/googleapis/go-sql-spanner v1.1.1/go.mod h1:e12AKZmltQH/2XGqR/2SAPWPKshc5+WF4W7OGD9YcAw=
github.com/kunitsucom/util.go v0.0.61-0.20240112184211-8b1d5e248ad7 h1:u6fQAHydd8pzKpGNDsHrAwGrU/7MM0CoIS9RdN46+Wo=
github.com/kunitsucom/util.go v0.0.61-0.20240112184211-8b1d5e248ad7/go.mod h1:bYFf2JvRqVF1brBtpdt3xkkTGJBxmYBxZlItrc/lf7Y=
github.com/kunitsucom/util.go v0.0.62-rc.1 h1:IgyOfnSNrzj0K0bxjU3oJaUTBsIFkHPLFMEeM32IyE8=
github.com/kunitsucom/util.go v0.0.62-rc.1/go.mod h1:bYFf2JvRqVF1brBtpdt3xkkTGJBxmYBxZlItrc/lf7Y=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand Down
10 changes: 8 additions & 2 deletions pkg/errors/errors.go → pkg/apperr/errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package errors
package apperr

import "errors"
import (
"errors"
"fmt"
)

var (
ErrNotSupported = errors.New("not supported")
Expand All @@ -11,3 +14,6 @@ var (
ErrBothArgumentsIsDSN = errors.New("both arguments is dsn")
ErrBothArgumentsAreNotDSNOrSQLFile = errors.New("both arguments are not dsn or sql file")
)

//nolint:gochecknoglobals
var Errorf = fmt.Errorf
4 changes: 2 additions & 2 deletions pkg/ddl/cockroachdb/ddl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
)

const (
Dialect = "cockroachdb"
DriverName = "postgres" // cockroachdb's driver is postgres
Dialect = "cockroachdb" //diff:ignore-line-postgres-cockroach
DriverName = "postgres" // cockroachdb's driver is postgres //diff:ignore-line-postgres-cockroach
Indent = " "
CommentPrefix = "-- "
)
Expand Down
7 changes: 7 additions & 0 deletions pkg/ddl/cockroachdb/ddl_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type ForeignKeyConstraint struct {
Columns []*ColumnIdent
Ref *Ident
RefColumns []*ColumnIdent
OnAction string
}

var _ Constraint = (*ForeignKeyConstraint)(nil)
Expand All @@ -101,6 +102,9 @@ func (c *ForeignKeyConstraint) String() string {
str += " (" + stringz.JoinStringers(", ", c.Columns...) + ")"
str += " REFERENCES " + c.Ref.String()
str += " (" + stringz.JoinStringers(", ", c.RefColumns...) + ")"
if c.OnAction != "" {
str += " " + c.OnAction
}
return str
}

Expand All @@ -127,6 +131,9 @@ func (c *ForeignKeyConstraint) StringForDiff() string {
str += v.StringForDiff()
}
str += ")"
if c.OnAction != "" {
str += " " + c.OnAction
}
return str
}

Expand Down
11 changes: 8 additions & 3 deletions pkg/ddl/cockroachdb/ddl_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,16 @@ func TestForeignKeyConstraint(t *testing.T) {
Columns: []*ColumnIdent{{Ident: &Ident{Name: "group_id", QuotationMark: `"`, Raw: `"group_id"`}}},
Ref: &Ident{Name: "groups", QuotationMark: `"`, Raw: `"groups"`},
RefColumns: []*ColumnIdent{{Ident: &Ident{Name: "id", QuotationMark: `"`, Raw: `"id"`}}},
OnAction: "ON DELETE NO ACTION",
}

expected := `CONSTRAINT "fk_users_groups" FOREIGN KEY ("group_id") REFERENCES "groups" ("id")`
actual := foreignKeyConstraint.String()
require.Equal(t, expected, actual)
expectedString := `CONSTRAINT "fk_users_groups" FOREIGN KEY ("group_id") REFERENCES "groups" ("id") ON DELETE NO ACTION`
actualString := foreignKeyConstraint.String()
require.Equal(t, expectedString, actualString)

expectedStringForDiff := `CONSTRAINT fk_users_groups FOREIGN KEY (group_id ASC) REFERENCES groups (id ASC) ON DELETE NO ACTION`
actualStringForDiff := foreignKeyConstraint.StringForDiff()
require.Equal(t, expectedStringForDiff, actualStringForDiff)

t.Logf("✅: %s: foreignKeyConstraint: %#v", t.Name(), foreignKeyConstraint)
})
Expand Down
8 changes: 5 additions & 3 deletions pkg/ddl/cockroachdb/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
errorz "github.com/kunitsucom/util.go/errors"
"github.com/kunitsucom/util.go/exp/diff/simplediff"

apperr "github.com/kunitsucom/ddlctl/pkg/apperr"

"github.com/kunitsucom/ddlctl/pkg/ddl"
)

Expand All @@ -29,7 +31,7 @@ func Diff(before, after *DDL) (*DDL, error) {
Name: s.Name,
})
default:
return nil, errorz.Errorf("%s: %T: %w", s.GetNameForDiff(), s, ddl.ErrNotSupported)
return nil, apperr.Errorf("%s: %T: %w", s.GetNameForDiff(), s, ddl.ErrNotSupported)
}
}
return result, nil
Expand All @@ -49,7 +51,7 @@ func Diff(before, after *DDL) (*DDL, error) {
Name: beforeStmt.Name,
})
default:
return nil, errorz.Errorf("%s: %T: %w", beforeStmt.GetNameForDiff(), beforeStmt, ddl.ErrNotSupported)
return nil, apperr.Errorf("%s: %T: %w", beforeStmt.GetNameForDiff(), beforeStmt, ddl.ErrNotSupported)
}
}

Expand All @@ -61,7 +63,7 @@ func Diff(before, after *DDL) (*DDL, error) {
case *CreateIndexStmt:
result.Stmts = append(result.Stmts, afterStmt)
default:
return nil, errorz.Errorf("%s: %T: %w", afterStmt.GetNameForDiff(), afterStmt, ddl.ErrNotSupported)
return nil, apperr.Errorf("%s: %T: %w", afterStmt.GetNameForDiff(), afterStmt, ddl.ErrNotSupported)
}
}

Expand Down
7 changes: 4 additions & 3 deletions pkg/ddl/cockroachdb/diff_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ package cockroachdb
import (
"reflect"

errorz "github.com/kunitsucom/util.go/errors"
"github.com/kunitsucom/util.go/exp/diff/simplediff"

apperr "github.com/kunitsucom/ddlctl/pkg/apperr"

"github.com/kunitsucom/ddlctl/pkg/ddl"
)

Expand Down Expand Up @@ -53,7 +54,7 @@ func DiffCreateTable(before, after *CreateTableStmt, opts ...DiffCreateTableOpti
})
return result, nil
case (before == nil && after == nil) || reflect.DeepEqual(before, after) || before.String() == after.String():
return nil, errorz.Errorf("before: %s, after: %s: %w", before.GetNameForDiff(), after.GetNameForDiff(), ddl.ErrNoDifference)
return nil, apperr.Errorf("before: %s, after: %s: %w", before.GetNameForDiff(), after.GetNameForDiff(), ddl.ErrNoDifference)
}

if before.Name.StringForDiff() != after.Name.StringForDiff() {
Expand Down Expand Up @@ -181,7 +182,7 @@ func DiffCreateTable(before, after *CreateTableStmt, opts ...DiffCreateTableOpti
}

if len(result.Stmts) == 0 {
return nil, errorz.Errorf("before: %s, after: %s: %w", before.GetNameForDiff(), after.GetNameForDiff(), ddl.ErrNoDifference)
return nil, apperr.Errorf("before: %s, after: %s: %w", before.GetNameForDiff(), after.GetNameForDiff(), ddl.ErrNoDifference)
}

return result, nil
Expand Down
20 changes: 15 additions & 5 deletions pkg/ddl/cockroachdb/lexar.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ import (

// MEMO: https://www.postgresql.jp/docs/11/datatype.html

const (
QuotationChar = '"'
QuotationStr = string(QuotationChar)
)

// Token はSQL文のトークンを表す型です。
type Token struct {
Type TokenType
Expand Down Expand Up @@ -63,6 +58,8 @@ const (
TOKEN_DROP TokenType = "DROP"
TOKEN_RENAME TokenType = "RENAME"
TOKEN_TRUNCATE TokenType = "TRUNCATE"
TOKEN_DELETE TokenType = "DELETE"
TOKEN_UPDATE TokenType = "UPDATE"

// OBJECT.
TOKEN_TABLE TokenType = "TABLE"
Expand Down Expand Up @@ -108,6 +105,9 @@ const (
TOKEN_NOT TokenType = "NOT"
TOKEN_ASC TokenType = "ASC"
TOKEN_DESC TokenType = "DESC"
TOKEN_CASCADE TokenType = "CASCADE"
TOKEN_NO TokenType = "NO"
TOKEN_ACTION TokenType = "ACTION"

// CONSTRAINT.
TOKEN_CONSTRAINT TokenType = "CONSTRAINT"
Expand Down Expand Up @@ -155,6 +155,10 @@ func lookupIdent(ident string) TokenType {
return TOKEN_RENAME
case "TRUNCATE":
return TOKEN_TRUNCATE
case "DELETE":
return TOKEN_DELETE
case "UPDATE":
return TOKEN_UPDATE
case "TABLE":
return TOKEN_TABLE
case "INDEX":
Expand Down Expand Up @@ -225,6 +229,12 @@ func lookupIdent(ident string) TokenType {
return TOKEN_ASC
case "DESC":
return TOKEN_DESC
case "CASCADE":
return TOKEN_CASCADE
case "NO":
return TOKEN_NO
case "ACTION":
return TOKEN_ACTION
case "CONSTRAINT":
return TOKEN_CONSTRAINT
case "PRIMARY":
Expand Down
3 changes: 3 additions & 0 deletions pkg/ddl/cockroachdb/lexar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ func Test_lookupIdent(t *testing.T) {
{name: "success,DROP", input: "DROP", want: TOKEN_DROP},
{name: "success,RENAME", input: "RENAME", want: TOKEN_RENAME},
{name: "success,TRUNCATE", input: "TRUNCATE", want: TOKEN_TRUNCATE},
{name: "success,DELETE", input: "DELETE", want: TOKEN_DELETE},
{name: "success,UPDATE", input: "UPDATE", want: TOKEN_UPDATE},
{name: "success,TABLE", input: "TABLE", want: TOKEN_TABLE},
{name: "success,INDEX", input: "INDEX", want: TOKEN_INDEX},
{name: "success,VIEW", input: "VIEW", want: TOKEN_VIEW},
Expand Down Expand Up @@ -63,6 +65,7 @@ func Test_lookupIdent(t *testing.T) {
{name: "success,NULL", input: "NULL", want: TOKEN_NULL},
{name: "success,ASC", input: "ASC", want: TOKEN_ASC},
{name: "success,DESC", input: "DESC", want: TOKEN_DESC},
{name: "success,CASCADE", input: "CASCADE", want: TOKEN_CASCADE},
{name: "success,CONSTRAINT", input: "CONSTRAINT", want: TOKEN_CONSTRAINT},
{name: "success,PRIMARY", input: "PRIMARY", want: TOKEN_PRIMARY},
{name: "success,KEY", input: "KEY", want: TOKEN_KEY},
Expand Down
Loading

0 comments on commit 347b97f

Please sign in to comment.