Skip to content

Commit

Permalink
doc: ok
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed May 24, 2024
1 parent 3462c26 commit db2ce9a
Show file tree
Hide file tree
Showing 9 changed files with 114 additions and 54 deletions.
4 changes: 3 additions & 1 deletion docs/docs/02-quick-start/02-initialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ You can also create a context to the repository to avoid passing flags each time
amigo context --dsn "postgres://user:password@localhost:5432/dbname"
```

A config.yml file will be created in the `.mig` folder. You can edit it to add more configurations.
A config.yml file will be created in the `.amigo` folder. You can edit it to add more configurations.

It contains the following fields:
```yaml
Expand All @@ -30,4 +30,6 @@ pg-dump-path: pg_dump
schema-version-table: public.mig_schema_versions
shell-path: /bin/bash
debug: false
show-sql: false
show-sql-highlighting: true
```
12 changes: 11 additions & 1 deletion docs/docs/02-quick-start/03-running-your-first-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,17 @@

Since you have initialized mig, you can now run your first migration.

To run the migration, execute the following command:
Before that make sure to import the driver that amigo have added in the imports of the main file in `.amigo/main.go`.

By default for postgres it imports `github.com/jackc/pgx/v5/stdlib` but you can change it and it will works.

Amigo is driver agnostic and works with the `database/sql` package.
`pgx` provide a support for the `database/sql` package and is a good choice for postgres, but you can use any driver that support the `database/sql` package.




When you have installed the driver on you project, run the migration, execute the following command:

```sh
amigo migrate
Expand Down
25 changes: 19 additions & 6 deletions docs/docs/04-api/100-migrating-in-go.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,31 @@ package main

import (
"database/sql"
migrations "package/where/your/migrations/are"
"example/pg/migrations"
"github.com/alexisvisco/amigo/pkg/amigo"
_ "github.com/lib/pq"
"github.com/alexisvisco/amigo/pkg/amigoctx"
"github.com/alexisvisco/amigo/pkg/types"
_ "github.com/jackc/pgx/v5/stdlib"
"os"
)


// this is an example to run migration in a codebase
func main() {
db, _ := sql.Open("postgres", "postgres://user:password@localhost:5432/dbname?sslmode=disable")
ok, err := amigo.RunPostgresMigrations(&amigo.RunMigrationOptions{
Connection: db,
dsn := "postgres://postgres:postgres@localhost:6666/postgres"
db, err := sql.Open("pgx", dsn)
if err != nil {
panic(err)
}

err = amigo.NewAmigo(amigoctx.NewContext().WithDSN(dsn)).RunMigrations(amigo.RunMigrationParams{
DB: db,
Direction: types.MigrationDirectionDown,
Migrations: migrations.Migrations,
LogOutput: os.Stdout,
})
if err != nil {
panic(err)
}
}
```

Expand Down
42 changes: 25 additions & 17 deletions docs/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This library offer to you a new way to create migrations in Go with a powerful A
- **Go Language**: The library allows you to write migrations in Go, making it easy to define schema changes in a programming language you are already familiar with.
- **Type Safety**: Writing migrations in Go provides you with all the language's benefits, including type safety, simplicity, and strong tooling support.
- **Version Control**: Migrations are version controlled.
- **Auto Down Migration**: The library generates down migrations when it's possible.
- **Compatibility**: The library supports working with already migrated databases and allows you to seamlessly integrate it into existing projects.

## Installation
Expand All @@ -56,29 +57,36 @@ mit migrate # apply the migration
```go
package migrations

import (
"github.com/alexisvisco/amigo/pkg/schema/pg"
"github.com/alexisvisco/amigo/pkg/schema"
"time"
)
/* ... */

type Migration20240502155033SchemaVersion struct {}

func (m Migration20240502155033SchemaVersion) Change(s *pg.Schema) {
s.CreateTable("public.mig_schema_versions", func(s *pg.PostgresTableDef) {
s.String("id")
func (m Migration20240524090434CreateUserTable) Change(s *pg.Schema) {
s.CreateTable("users", func(def *pg.PostgresTableDef) {
def.Serial("id")
def.String("name")
def.String("email")
def.Timestamps()
def.Index([]string{"name"})
})
}
```

func (m Migration20240502155033SchemaVersion) Name() string {
return "schema_version"
}
Running up and down against this migration :

func (m Migration20240502155033SchemaVersion) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-05-02T17:50:33+02:00")
return t
}
```
$ amigo migrate
------> migrating: create_user_table version: 20240524110434
-- create_table(table: users, {columns: id, name, email, created_at, updated_at}, {pk: id})
-- add_index(table: users, name: idx_users_name, columns: [name])
------> version migrated: 20240524090434
$ amigo rollback
------> rollback: create_user_table version: 20240524110434
-- drop_table(table: users)
------> version rolled back: 20240524090434
```

Note that you did not have to write the down migration, the library generates it for you when it's possible.



## Supported databases
Expand Down
10 changes: 9 additions & 1 deletion example/pg/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,23 @@ module example/pg

go 1.22

require github.com/alexisvisco/amigo v0.0.2-alpha
require (
github.com/alexisvisco/amigo v0.0.2-alpha
github.com/jackc/pgx/v5 v5.5.5
)

require (
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/georgysavva/scany/v2 v2.1.3 // indirect
github.com/gobuffalo/flect v1.0.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/text v0.14.0 // indirect
)

replace github.com/alexisvisco/amigo => ../../
2 changes: 2 additions & 0 deletions example/pg/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
Expand Down
12 changes: 3 additions & 9 deletions example/pg/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,14 @@ import (
// this is an example to run migration in a codebase
func main() {
dsn := "postgres://postgres:postgres@localhost:6666/postgres"
db, err := sql.Open("pgx", "")
db, err := sql.Open("pgx", dsn)
if err != nil {
panic(err)
}

err = amigo.NewAmigo(amigoctx.MergeContext(amigoctx.Context{
Root: &amigoctx.Root{
DSN: dsn,
ShowSQL: true,
JSON: true,
},
})).RunMigrations(amigo.RunMigrationParams{
err = amigo.NewAmigo(amigoctx.NewContext().WithDSN(dsn)).RunMigrations(amigo.RunMigrationParams{
DB: db,
Direction: types.MigrationDirectionUp,
Direction: types.MigrationDirectionDown,
Migrations: migrations.Migrations,
LogOutput: os.Stdout,
})
Expand Down
15 changes: 15 additions & 0 deletions pkg/amigoctx/ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,21 @@ type Root struct {
Debug bool
}

func (a *Context) WithDSN(dsn string) *Context {
a.Root.DSN = dsn
return a
}

func (a *Context) WithVersion(version string) *Context {
a.Migration.Version = version
return a
}

func (a *Context) WithSteps(steps int) *Context {
a.Migration.Steps = steps
return a
}

func (r *Root) ValidateDSN() error {
if r.DSN == "" {
return ErrDSNEmpty
Expand Down
46 changes: 27 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ General documentation: [https://amigo.alexisvis.co](https://amigo.alexisvis.co)

## Why another migration library?

First thing, I don't have anything against SQL migrations file library. I appreciate them but somethimes with SQL files you are limited to do complex migrations that imply your models and business logic.
First thing, I don't have anything against SQL migrations file library. I appreciate them but somethimes with SQL files you are limited to do complex migrations that imply your models and business logic.

I just like the way activerecord (rails) migration system and I think it's powerful to combine migration and code.

Expand All @@ -32,6 +32,7 @@ This library offer to you a new way to create migrations in Go with a powerful A
- **Go Language**: The library allows you to write migrations in Go, making it easy to define schema changes in a programming language you are already familiar with.
- **Type Safety**: Writing migrations in Go provides you with all the language's benefits, including type safety, simplicity, and strong tooling support.
- **Version Control**: Migrations are version controlled.
- **Auto Down Migration**: The library generates down migrations when it's possible.
- **Compatibility**: The library supports working with already migrated databases and allows you to seamlessly integrate it into existing projects.

## Installation
Expand All @@ -55,29 +56,36 @@ mit migrate # apply the migration
```go
package migrations

import (
"github.com/alexisvisco/amigo/pkg/schema/pg"
"github.com/alexisvisco/amigo/pkg/schema"
"time"
)
/* ... */

type Migration20240502155033SchemaVersion struct {}

func (m Migration20240502155033SchemaVersion) Change(s *pg.Schema) {
s.CreateTable("public.mig_schema_versions", func(s *pg.PostgresTableDef) {
s.String("id")
})
func (m Migration20240524090434CreateUserTable) Change(s *pg.Schema) {
s.CreateTable("users", func(def *pg.PostgresTableDef) {
def.Serial("id")
def.String("name")
def.String("email")
def.Timestamps()
def.Index([]string{"name"})
})
}
```

func (m Migration20240502155033SchemaVersion) Name() string {
return "schema_version"
}
Running up and down against this migration :

func (m Migration20240502155033SchemaVersion) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-05-02T17:50:33+02:00")
return t
}
```
$ amigo migrate
------> migrating: create_user_table version: 20240524110434
-- create_table(table: users, {columns: id, name, email, created_at, updated_at}, {pk: id})
-- add_index(table: users, name: idx_users_name, columns: [name])
------> version migrated: 20240524090434
$ amigo rollback
------> rollback: create_user_table version: 20240524110434
-- drop_table(table: users)
------> version rolled back: 20240524090434
```

Note that you did not have to write the down migration, the library generates it for you when it's possible.



## Supported databases
Expand Down

0 comments on commit db2ce9a

Please sign in to comment.