Skip to content

Commit

Permalink
feat: Add migration test (#36)
Browse files Browse the repository at this point in the history
* feat: Add migration test

* rename

* fix tests

* update test

* update test

* update test
  • Loading branch information
hwbrzzl authored Dec 15, 2024
1 parent 14e08df commit c1283a9
Show file tree
Hide file tree
Showing 16 changed files with 438 additions and 312 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ Welcome to star, PR and issues!

[About air]: https://www.goravel.dev/getting-started/installation.html#live-reload

### GraphQL

```bash
# download and install gqlgen locally, only need to run it once
go get -d github.com/99designs/gqlgen
# regenerate code
go run github.com/99designs/gqlgen generate
```

### DB

[app/http/controllers/db_controller.go](https://github.com/goravel/example/blob/master/app/http/controllers/db_controller.go)
Expand Down Expand Up @@ -79,6 +70,15 @@ go run github.com/99designs/gqlgen generate

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L81)

### GraphQL

```bash
# download and install gqlgen locally, only need to run it once
go get -d github.com/99designs/gqlgen
# regenerate code
go run github.com/99designs/gqlgen generate
```

## Documentation

Online documentation [https://www.goravel.dev](https://www.goravel.dev)
Expand Down
9 changes: 9 additions & 0 deletions README_zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ Laravel!

[routes/web.go](https://github.com/goravel/example/blob/master/routes/web.go#L81)

### GraphQL

```bash
# download and install gqlgen locally, only need to run it once
go get -d github.com/99designs/gqlgen
# regenerate code
go run github.com/99designs/gqlgen generate
```

## 文档

在线文档 [https://www.goravel.dev/zh](https://www.goravel.dev/zh)
Expand Down
2 changes: 1 addition & 1 deletion app/http/controllers/validation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (r *ValidationController) Request(ctx http.Context) http.Response {

func (r *ValidationController) Form(ctx http.Context) http.Response {
validator, err := facades.Validation().Make(map[string]any{
"name": ctx.Request().Input("name", ""),
"name": ctx.Request().Input("name"),
}, map[string]string{
"name": "required",
})
Expand Down
6 changes: 5 additions & 1 deletion app/http/middleware/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import (

func Lang() httpcontract.Middleware {
return func(ctx httpcontract.Context) {
facades.App().SetLocale(ctx, ctx.Request().Input("lang", facades.Config().GetString("app.locale")))
lang := ctx.Request().Input("lang")
if lang == "" {
lang = facades.Config().GetString("app.locale")
}
facades.App().SetLocale(ctx, lang)

ctx.Request().Next()
}
Expand Down
4 changes: 4 additions & 0 deletions app/http/requests/user_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,7 @@ func (r *UserCreate) PrepareForValidation(ctx http.Context, data validation.Data

return nil
}

func (r *UserCreate) Filters(ctx http.Context) map[string]string {
return map[string]string{}
}
9 changes: 4 additions & 5 deletions app/providers/database_service_provider.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package providers

import (
"github.com/goravel/framework/contracts/database/seeder"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/facades"

"goravel/database/seeders"
"goravel/database"
)

type DatabaseServiceProvider struct {
Expand All @@ -16,7 +15,7 @@ func (receiver *DatabaseServiceProvider) Register(app foundation.Application) {
}

func (receiver *DatabaseServiceProvider) Boot(app foundation.Application) {
facades.Seeder().Register([]seeder.Seeder{
&seeders.DatabaseSeeder{},
})
kernel := database.Kernel{}
facades.Schema().Register(kernel.Migrations())
facades.Seeder().Register(kernel.Seeders())
}
6 changes: 5 additions & 1 deletion config/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ func init() {
// This table keeps track of all the migrations that have already run for
// your application. Using this information, we can determine which of
// the migrations on disk haven't actually been run in the database.
"migrations": "migrations",
// Available Drivers: "default", "sql"
"migrations": map[string]any{
"driver": "default",
"table": "migrations",
},

// Redis Databases
//
Expand Down
24 changes: 24 additions & 0 deletions database/kernel.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package database

import (
"github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/contracts/database/seeder"

"goravel/database/migrations"
"goravel/database/seeders"
)

type Kernel struct {
}

func (kernel Kernel) Migrations() []schema.Migration {
return []schema.Migration{
&migrations.M20241207095921CreateUsersTable{},
}
}

func (kernel Kernel) Seeders() []seeder.Seeder {
return []seeder.Seeder{
&seeders.DatabaseSeeder{},
}
}
34 changes: 34 additions & 0 deletions database/migrations/20241207095921_create_users_table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package migrations

import (
"github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/facades"
)

type M20241207095921CreateUsersTable struct {
}

// Signature The unique signature for the migration.
func (r *M20241207095921CreateUsersTable) Signature() string {
return "20241207095921_create_users_table"
}

// Up Run the migrations.
func (r *M20241207095921CreateUsersTable) Up() error {
if !facades.Schema().HasTable("users") {
return facades.Schema().Create("users", func(table schema.Blueprint) {
table.BigIncrements("id")
table.String("name").Default("")
table.String("avatar").Default("")
table.Timestamps()
table.SoftDeletes()
})
}

return nil
}

// Down Reverse the migrations.
func (r *M20241207095921CreateUsersTable) Down() error {
return facades.Schema().DropIfExists("users")
}
Loading

0 comments on commit c1283a9

Please sign in to comment.