Skip to content

Commit

Permalink
split orm migration (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus621 authored Jan 20, 2025
1 parent d798851 commit 38f0551
Show file tree
Hide file tree
Showing 19 changed files with 579 additions and 307 deletions.
22 changes: 14 additions & 8 deletions _example/database/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ env: dev
log:
file_path: /dev/stdout
format: string
level: 4
level: 3

http:
- tag: main
Expand All @@ -13,13 +13,17 @@ mysql:
- tags: mysql_master,mysql_slave
host: 127.0.0.1
port: 3306
schema: test
schema: test_database
user: test
password: test
timezone: UTC
mysql_migrate:
- tags: mysql_master
dir: ./migrations

pgsql:
- tags: pgsql_master,pgsql_slave
host: 127.0.0.1
port: 5432
schema: postgres
user: postgres
password: postgres

sqlite:
- tags: sqlite_master
Expand All @@ -29,6 +33,8 @@ sqlite:
journal: WAL
locking_mode: EXCLUSIVE
other_params: "auto_vacuum=incremental"
sqlite_migrate:
- tags: mysql_master

db_migrate:
- tags: sqlite_master
dialect: sqlite
dir: ./migrations
19 changes: 14 additions & 5 deletions _example/database/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ package main
import (
"fmt"

"go.osspkg.com/logx"

"go.osspkg.com/goppy/v2"
"go.osspkg.com/goppy/v2/orm"
"go.osspkg.com/goppy/v2/plugins"
"go.osspkg.com/goppy/v2/web"
"go.osspkg.com/logx"
)

func main() {

app := goppy.New("goppy_database", "v1.0.0", "")
app.Plugins(
web.WithServer(),
orm.WithMysql(),
orm.WithSqlite(),
orm.WithPGSql(),
orm.WithMysqlClient(),
orm.WithSqliteClient(),
orm.WithPgsqlClient(),
orm.WithORM(),
orm.WithMigration(orm.Migration{
Tags: []string{"mysql_master"},
Dialect: "mysql",
Data: map[string]string{
"0001_data.sql": "CREATE TABLE IF NOT EXISTS `demo` (\n `id` int NOT NULL AUTO_INCREMENT PRIMARY KEY\n);",
},
}),
orm.WithMigration(),
)
app.Plugins(
plugins.Plugin{
Expand Down Expand Up @@ -74,5 +83,5 @@ func (v *Controller) User(ctx web.Context) {

ctx.ErrorJSON(400, fmt.Errorf("user not found"), web.ErrCtx{"id": id})

logx.Info("user - %d", id)
logx.Info("user", "id", id)
}
4 changes: 4 additions & 0 deletions _example/database/migrations/0001_data.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE IF NOT EXISTS "demo"
(
"id" integer NOT NULL PRIMARY KEY AUTOINCREMENT
);
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ require (
go.osspkg.com/errors v0.3.1
go.osspkg.com/events v0.3.0
go.osspkg.com/grape v1.2.3
go.osspkg.com/ioutils v0.4.8
go.osspkg.com/ioutils v0.5.0
go.osspkg.com/logx v0.4.2
go.osspkg.com/network v0.5.2
go.osspkg.com/random v0.4.1
go.osspkg.com/routine v0.3.1
go.osspkg.com/static v1.4.0
go.osspkg.com/syncing v0.3.0
go.osspkg.com/syncing v0.3.1
go.osspkg.com/validate v0.1.0
go.osspkg.com/xc v0.4.0
golang.org/x/mod v0.22.0
Expand Down
12 changes: 4 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,8 @@ go.osspkg.com/events v0.3.0 h1:W2IngTsKs0BKYIglqhrETwtpo6uNSZXWRIt0/l7c6dY=
go.osspkg.com/events v0.3.0/go.mod h1:Cjpx+qNM1y2MIAygFyZWYagTuRiYirmKppZQdaZumd4=
go.osspkg.com/grape v1.2.3 h1:3umuC4AV8foY4rGz3xoUdtJ7iG8STTLjqSNZyDygc/o=
go.osspkg.com/grape v1.2.3/go.mod h1:lg0K0VqCQE1/o4c2xM4b/wL5ZKG2NkqqCCF16ZjEJSI=
go.osspkg.com/ioutils v0.4.8 h1:7o7n6eypWdu3EF8i/ocnuyqDtYXJUqds+Chd1XmZp5s=
go.osspkg.com/ioutils v0.4.8/go.mod h1:58HhG2NHf9JUtixAH3R2XISlUmJruwVIUZ3039QVjOY=
go.osspkg.com/ioutils v0.5.0 h1:oCNOJS5AN8pS9hzgi0gOGQRpUp9R6D+UW+5eDpqSdqg=
go.osspkg.com/ioutils v0.5.0/go.mod h1:58HhG2NHf9JUtixAH3R2XISlUmJruwVIUZ3039QVjOY=
go.osspkg.com/logx v0.4.2 h1:3kqG7EaaT/DxpHytQm4MfcrmDhYf8ha9/iRpVjpRt88=
go.osspkg.com/logx v0.4.2/go.mod h1:mGbH9hdkeC0h9Gw1uWgQfi9MmlANcqNLffB0wxIDpsQ=
go.osspkg.com/network v0.5.2 h1:/LIVpamxO+647fCUi6FIfpRi7Rpb53l0I+nKOgDLE58=
Expand All @@ -146,16 +146,14 @@ go.osspkg.com/routine v0.3.1 h1:R0o4P0Ml5eoeHc2DiHjRvHBo/XXrW5nJNqIj3ToRzjg=
go.osspkg.com/routine v0.3.1/go.mod h1:z5AvvTbB19/tt1E5JOb4POhK1tOPgmejajgao/IWn4s=
go.osspkg.com/static v1.4.0 h1:2uy4/11c0QP+QLMucKQZbAU+e6lhVHKw5dWJPTk/DBg=
go.osspkg.com/static v1.4.0/go.mod h1:94YydVU3qUtb1J534486lpm+qg6CviQjqtxKlkpSppM=
go.osspkg.com/syncing v0.3.0 h1:yBkCsDPEt12a+qagInFFt7+ZongfT+GjSQl7nBmcybI=
go.osspkg.com/syncing v0.3.0/go.mod h1:Dpe0ljlEG6cI2Y9PxEjKiYEX2sgs1eUjWNVjFu4/iB0=
go.osspkg.com/syncing v0.3.1 h1:zt5o/X5DQ/GE5OQTKkq1nNWJMg7EcYhw0YiwMGuA0f8=
go.osspkg.com/syncing v0.3.1/go.mod h1:Dpe0ljlEG6cI2Y9PxEjKiYEX2sgs1eUjWNVjFu4/iB0=
go.osspkg.com/validate v0.1.0 h1:pYBuChZbsaq01Z6Gxlp7zyq8VkhKXzsuAVFaSUWrcXc=
go.osspkg.com/validate v0.1.0/go.mod h1:Z9hKSBTOW041moaEkouNldHVmRq7B1t49xqs16eTnp0=
go.osspkg.com/xc v0.4.0 h1:MGntRGa3EPCpfrTbWEN7x475BAsAtRYGpYEYJ5mE0I8=
go.osspkg.com/xc v0.4.0/go.mod h1:HWDrUQOKMkQser1teXqnFNMB1WVD0YsyIuM1vIKny7U=
golang.org/x/mod v0.22.0 h1:D4nJWe9zXqHOmWqj4VMOJhvzj7bEZg4wEYa759z1pH4=
golang.org/x/mod v0.22.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.33.0 h1:74SYHlV8BIgHIFC/LrYkOGIwL19eTYXQ5wc6TBuO36I=
golang.org/x/net v0.33.0/go.mod h1:HXLR5J+9DxmrqMwG9qjGCxZ+zKXxBru04zlTvWlWuN4=
golang.org/x/net v0.34.0 h1:Mb7Mrk043xzHgnRM88suvJFwzVrRfHEHJEl5/71CKw0=
golang.org/x/net v0.34.0/go.mod h1:di0qlW3YNM5oh6GqDGQr92MyTozJPmybPK4Ev/Gm31k=
golang.org/x/oauth2 v0.25.0 h1:CY4y7XT9v0cRI9oupztF8AgiIu99L/ksR/Xp/6jrZ70=
Expand All @@ -167,8 +165,6 @@ golang.org/x/sys v0.29.0 h1:TPYlXGxvx1MGTn2GiZDhnjPA9wZzZeGKHHmKhHYvgaU=
golang.org/x/sys v0.29.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.21.0 h1:zyQAAkrwaneQ066sspRyJaG9VNi/YJ1NfzcGB3hZ/qo=
golang.org/x/text v0.21.0/go.mod h1:4IBbMaMmOPCJ8SecivzSH54+73PCFmPWxNTLm+vZkEQ=
golang.org/x/tools v0.28.0 h1:WuB6qZ4RPCQo5aP3WdKZS7i595EdWqWR8vqJTlwTVK8=
golang.org/x/tools v0.28.0/go.mod h1:dcIOrVd3mfQKTgrDVQHqCPMWy6lnhfhtX3hLXYVLfRw=
golang.org/x/tools v0.29.0 h1:Xx0h3TtM9rzQpQuR4dKLrdglAmCEN5Oi+P74JdhdzXE=
golang.org/x/tools v0.29.0/go.mod h1:KMQVMRsVxU6nHCFXrBPhDB8XncLNLM0lIy/F14RP588=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
22 changes: 3 additions & 19 deletions orm/metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,11 @@ package orm
import (
"time"

"go.osspkg.com/goppy/v2/metrics"
"go.osspkg.com/logx"
)

type (
metric struct {
name string
}
metricExecutor interface {
ExecutionTime(name string, call func())
}
)

func newMetric(name string) metricExecutor {
return &metric{
name: name,
}
}

// ExecutionTime calculating the execution time
func (m *metric) ExecutionTime(name string, call func()) {
func execTime(tag, name string, call func()) {
t := time.Now()
call()
metrics.HistogramVec(m.name, "query", name).Observe(time.Since(t).Seconds())
logx.Debug("query_time", "tag", tag, "query", name, "execTime", time.Since(t).String())
}
Loading

0 comments on commit 38f0551

Please sign in to comment.