Skip to content

Commit

Permalink
Merge branch 'main' into fix-where
Browse files Browse the repository at this point in the history
  • Loading branch information
liangfujian committed Sep 21, 2023
2 parents bce5639 + 728e1e4 commit b07678b
Show file tree
Hide file tree
Showing 14 changed files with 191 additions and 21 deletions.
101 changes: 99 additions & 2 deletions .github/workflows/migrate.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
name: migrate
on: push
jobs:
sqlite:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
- name: build
- name: Build
run: go build -o /usr/local/bin/queryx cmd/queryx/main.go
- uses: actions/upload-artifact@v3
with:
name: bin
path: /usr/local/bin/queryx
sqlite:
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
- uses: actions/download-artifact@v3
with:
name: bin
path: /usr/local/bin
- run: chmod a+x /usr/local/bin/queryx
- name: migrate
run: |
cd internal/migrate
Expand All @@ -24,3 +41,83 @@ jobs:
run: |
cd internal/migrate
sqlite3 test.sqlite3 "insert into users(name, email) values('test', 'test@example.com')"
postgresql:
needs: [build]
runs-on: ubuntu-latest
services:
postgres:
image: postgres:14.2
env:
POSTGRES_PASSWORD: postgres
POSTGRES_USER: postgres
POSTGRES_DB: queryx_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
- uses: actions/download-artifact@v3
with:
name: bin
path: /usr/local/bin
- run: chmod a+x /usr/local/bin/queryx
- name: migrate
run: |
cd internal/migrate
queryx db:migrate --schema postgresql1.hcl
- name: sleep
run: sleep 1
- name: migrate again
run: |
cd internal/migrate
queryx db:migrate --schema postgresql2.hcl
- name: test
run: |
PGPASSWORD=postgres psql -h localhost -U postgres -d queryx_test -tc "insert into users(name, email) values('test', 'test@example.com')"
mysql:
needs: [build]
runs-on: ubuntu-latest
services:
mysql:
image: mysql:8.0.32
env:
MYSQL_DATABASE: queryx_test
MYSQL_ALLOW_EMPTY_PASSWORD: true
ports:
- 3306:3306
options: >-
--health-cmd "mysqladmin ping -uuser_test -pqueryx_test"
--health-interval 10s
--health-start-period 10s
--health-timeout 5s
--health-retries 10
steps:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
- uses: actions/download-artifact@v3
with:
name: bin
path: /usr/local/bin
- run: chmod a+x /usr/local/bin/queryx
- name: migrate
run: |
cd internal/migrate
queryx db:migrate --schema mysql1.hcl
- name: sleep
run: sleep 1
- name: migrate again
run: |
cd internal/migrate
queryx db:migrate --schema mysql2.hcl
- name: test
run: |
mysql --protocol=tcp -h localhost -u root -D queryx_test -e "insert into users(name, email) values('test', 'test@example.com')"
2 changes: 1 addition & 1 deletion README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
> TypeScript 支持已发布!现在可以在项目中使用 TypeScript 来使用 queryx,[查看文档](https://github.com/swiftcarrot/queryx/blob/main/docs/generator-typescript-client.md)进行操作。
<img height="400" alt="image" src="https://github.com/swiftcarrot/queryx/assets/1039026/20f00a81-d920-4550-8f6a-6e094edb5d5b">
<img width="400" alt="image" src="https://github.com/swiftcarrot/queryx/assets/1039026/5813a0b6-cecf-4d09-ba9a-a0bd19a8c7f6">

Queryx 是一个 schema 优先、类型安全的 ORM。

Expand Down
2 changes: 1 addition & 1 deletion adapter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (c *Config) GoFormat() string {
u = fmt.Sprintf("postgres://%s:%s@%s:%s/%s", c.Username, c.Password, c.Host, c.Port, c.Database)
case "mysql":
c.Options.Set("parseTime", "true")
u = fmt.Sprintf("%s@tcp(%s:%s)/%s", c.Username, c.Host, c.Port, c.Database)
u = fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", c.Username, c.Password, c.Host, c.Port, c.Database)
case "sqlite":
return fmt.Sprintf("file:%s", c.Database)
default:
Expand Down
4 changes: 2 additions & 2 deletions adapter/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ func TestNewMySQLConfig(t *testing.T) {
require.Equal(t, "localhost", c.Host)
require.Equal(t, "3306", c.Port)
require.Equal(t, "queryx_test", c.Database)
require.Equal(t, "root@tcp(localhost:3306)/queryx_test?parseTime=true", c.URL)
require.Equal(t, "root@tcp(localhost:3306)/?parseTime=true", c.URL2)
require.Equal(t, "root:@tcp(localhost:3306)/queryx_test?parseTime=true", c.URL)
require.Equal(t, "root:@tcp(localhost:3306)/?parseTime=true", c.URL2)
require.Equal(t, "mysql://root:@localhost:3306/queryx_test?parseTime=true", c.TSFormat())

}
Expand Down
2 changes: 1 addition & 1 deletion cmd/queryx/action/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/spf13/cobra"
)

var Version = "0.2.4"
var Version = "0.2.5"

var versionCmd = &cobra.Command{
Use: "version",
Expand Down
16 changes: 8 additions & 8 deletions generator/client/golang/templates/[model].gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,23 @@ type {{ $.model.Name }} struct {

// String implements the stringer interface.
func ({{ $m }} *{{.model.Name}}) String() string {
var b strings.Builder
b.WriteString("({{ $.model.Name }} ")
var sb strings.Builder
sb.WriteString("({{ $.model.Name }} ")
{{- range $i, $c := $.model.Columns }}
{{- if $c.Null }}
b.WriteString(fmt.Sprintf("{{ $c.Name }}: %s", {{ $m }}.{{ pascal $c.Name }}))
sb.WriteString(fmt.Sprintf("{{ $c.Name }}: %s", {{ $m }}.{{ pascal $c.Name }}))
{{- else if eq $c.Type "string" }}
b.WriteString(fmt.Sprintf(`{{ $c.Name }}: "%s"`, {{ $m }}.{{ pascal $c.Name }}))
sb.WriteString(fmt.Sprintf(`{{ $c.Name }}: "%s"`, {{ $m }}.{{ pascal $c.Name }}))
{{- else }}
b.WriteString(fmt.Sprintf("{{ $c.Name }}: %v", {{ $m }}.{{ pascal $c.Name }}))
sb.WriteString(fmt.Sprintf("{{ $c.Name }}: %v", {{ $m }}.{{ pascal $c.Name }}))
{{- end }}
{{- $l := len $.model.Columns }}
{{- if ne $i (sub $l 1) }}
b.WriteString(", ")
sb.WriteString(", ")
{{- end }}
{{- end }}
b.WriteString(")")
return b.String()
sb.WriteString(")")
return sb.String()
}

func ({{ $m }} *{{.model.Name}}) applyChange(change *queryx.{{.model.Name}}Change) error {
Expand Down
10 changes: 5 additions & 5 deletions generator/client/golang/templates/queryx/date_column.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (c *DateColumn) EQ(v string) *Clause {
}
return &Clause{
fragment: fmt.Sprintf("%s.%s = ?", c.Table.Name, c.Name),
args: []interface{}{d.UTC()},
args: []interface{}{d},
err: err,
}
}
Expand All @@ -41,7 +41,7 @@ func (c *DateColumn) LE(v string) *Clause {
}
return &Clause{
fragment: fmt.Sprintf("%s.%s <= ?", c.Table.Name, c.Name),
args: []interface{}{d.UTC()},
args: []interface{}{d},
err: err,
}
}
Expand All @@ -55,7 +55,7 @@ func (c *DateColumn) LT(v string) *Clause {
}
return &Clause{
fragment: fmt.Sprintf("%s.%s <=?", c.Table.Name, c.Name),
args: []interface{}{d.UTC()},
args: []interface{}{d},
err: err,
}
}
Expand All @@ -69,7 +69,7 @@ func (c *DateColumn) GE(v string) *Clause {
}
return &Clause{
fragment: fmt.Sprintf("%s.%s >= ?", c.Table.Name, c.Name),
args: []interface{}{d.UTC()},
args: []interface{}{d},
err: err,
}
}
Expand All @@ -83,7 +83,7 @@ func (c *DateColumn) GT(v string) *Clause {
}
return &Clause{
fragment: fmt.Sprintf("%s.%s > ?", c.Table.Name, c.Name),
args: []interface{}{d.UTC()},
args: []interface{}{d},
err: err,
}
}
Expand Down
2 changes: 1 addition & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ start() {

pkg="github.com/swiftcarrot/queryx"
bin="queryx.tar.gz"
version="v0.2.4"
version="v0.2.5"

prefix=${PREFIX:-"/usr/local/bin"}
tmp="$(mktmpdir)"
Expand Down
3 changes: 3 additions & 0 deletions internal/integration/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ test("time", async () => {
test("date", async () => {
let user = await c.queryUser().create({ date: "2012-11-10" });
expect(format(user.date!, "yyyy-MM-dd")).toEqual("2012-11-10");

user = await c.queryUser().where(c.userDate.eq("2012-11-10")).first();
expect(format(user.date!, "yyyy-MM-dd")).toEqual("2012-11-10");
});

test("datetime", async () => {
Expand Down
4 changes: 4 additions & 0 deletions internal/integration/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,10 @@ func TestDate(t *testing.T) {
user, err := c.QueryUser().Create(c.ChangeUser().SetDate("2012-11-10"))
require.NoError(t, err)
require.Equal(t, "2012-11-10", user.Date.Val.Format("2006-01-02"))

user, err = c.QueryUser().Where(c.UserDate.EQ("2012-11-10")).First()
require.NoError(t, err)
require.Equal(t, "2012-11-10", user.Date.Val.Format("2006-01-02"))
}

func TestDatetime(t *testing.T) {
Expand Down
15 changes: 15 additions & 0 deletions internal/migrate/mysql1.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
database "db" {
adapter = "mysql"

config "development" {
url = "mysql://root:@127.0.0.1:3306/queryx_test"
}

model "User" {
timestamps = false

column "name" {
type = string
}
}
}
18 changes: 18 additions & 0 deletions internal/migrate/mysql2.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
database "db" {
adapter = "mysql"

config "development" {
url = "mysql://root:@127.0.0.1:3306/queryx_test"
}

model "User" {
timestamps = false

column "name" {
type = string
}
column "email" {
type = string
}
}
}
15 changes: 15 additions & 0 deletions internal/migrate/postgresql1.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
database "db" {
adapter = "postgresql"

config "development" {
url = "postgresql://postgres:postgres@localhost:5432/queryx_test?sslmode=disable"
}

model "User" {
timestamps = false

column "name" {
type = string
}
}
}
18 changes: 18 additions & 0 deletions internal/migrate/postgresql2.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
database "db" {
adapter = "postgresql"

config "development" {
url = "postgresql://postgres:postgres@localhost:5432/queryx_test?sslmode=disable"
}

model "User" {
timestamps = false

column "name" {
type = string
}
column "email" {
type = string
}
}
}

0 comments on commit b07678b

Please sign in to comment.