-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add rename table and change column type
- Loading branch information
1 parent
bba5080
commit db2b932
Showing
20 changed files
with
533 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Migrating in go | ||
|
||
Usually you will need to run migration in your Go application, to do so you can use the `amigo` package. | ||
|
||
```go | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
migrations "package/where/your/migrations/are" | ||
"github.com/alexisvisco/amigo/pkg/amigo" | ||
_ "github.com/lib/pq" | ||
) | ||
|
||
|
||
func main() { | ||
db, _ := sql.Open("postgres", "postgres://user:password@localhost:5432/dbname?sslmode=disable") | ||
ok, err := amigo.RunPostgresMigrations(&amigo.RunMigrationOptions{ | ||
Connection: db, | ||
Migrations: migrations.Migrations, | ||
}) | ||
} | ||
``` | ||
|
||
You can specify all the options the cli can take in the `RunMigrationOptions` struct (steps, version, dryrun ...) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Transformative operations | ||
|
||
They are the operations that change the data in the database. | ||
|
||
- [RenameColumn(tableName schema.TableName, oldColumnName, newColumnName string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.RenameColumn) | ||
|
||
- [RenameTable(tableName schema.TableName, newTableName string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.RenameTable) | ||
|
||
- [ChangeColumnType(tableName schema.TableName, columnName string, columnType schema.ColumnType, opts ...schema.ChangeColumnTypeOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.ChangeColumnType) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# Return error | ||
|
||
As you can see in a migration file the functions Up, Down or Change cannot return an error. | ||
If you want to raise an error you can use the `RaiseError` function from the context. | ||
|
||
```go | ||
package migrations | ||
|
||
import ( | ||
"github.com/alexisvisco/amigo/pkg/schema/pg" | ||
"...../repositories/userrepo" | ||
"time" | ||
) | ||
|
||
type Migration20240517135429Droptable struct{} | ||
|
||
func (m Migration20240517135429Droptable) Change(s *pg.Schema) { | ||
s.CreateTable("test", func(def *pg.PostgresTableDef) { | ||
def.String("name") | ||
def.JSON("data") | ||
}) | ||
|
||
_, err := userrepo.New(s.DB).GetUser(5) | ||
if err != nil { | ||
s.Context.RaiseError(fmt.Errorf("error: %w", err)) | ||
} | ||
} | ||
|
||
func (m Migration20240517135429Droptable) Name() string { | ||
return "droptable" | ||
} | ||
|
||
func (m Migration20240517135429Droptable) Date() time.Time { | ||
t, _ := time.Parse(time.RFC3339, "2024-05-17T15:54:29+02:00") | ||
return t | ||
} | ||
|
||
``` | ||
|
||
In this example, if the `GetUser` function returns an error, the migration will fail and the error will be displayed in the logs. | ||
|
||
|
||
The only way to not crash the migration when a RaiseError is called is to use the `--continue-on-error` flag. | ||
|
||
And the only way to crash when this flag is used is to use a `schema.ForceStopError` error. | ||
|
||
```go | ||
s.Context.RaiseError(schema.NewForceStopError(errors.New("force stop"))) | ||
``` | ||
|
||
This will crash the migration EVEN if the `--continue-on-error` flag is used. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.