Skip to content

Commit

Permalink
feat: (almost) support sqlite, add status command
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed Jun 2, 2024
1 parent 677e483 commit c2284ed
Show file tree
Hide file tree
Showing 50 changed files with 1,214 additions and 145 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
*.out.sql
*.out.txt
*.data
*.iml
.idea
migrations
Expand Down
3 changes: 3 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ First you need to create a main folder with amigo init:
Postgres:
$ amigo context --dsn "postgres://user:password@host:port/dbname?sslmode=disable"
SQLite:
$ amigo context --dsn "sqlite:/path/to/db.sqlite" --schema-version-table mig_schema_versions
Unknown Driver (Mysql in this case):
$ amigo context --dsn "user:password@tcp(host:port)/dbname"
Expand Down
22 changes: 22 additions & 0 deletions cmd/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cmd

import (
"github.com/alexisvisco/amigo/pkg/amigo"
"github.com/spf13/cobra"
)

var statusCmd = &cobra.Command{
Use: "status",
Short: "Status explain the current state of the database.",
Run: wrapCobraFunc(func(cmd *cobra.Command, am amigo.Amigo, args []string) error {
if err := cmdCtx.ValidateDSN(); err != nil {
return err
}

return am.ExecuteMain(amigo.MainArgStatus)
}),
}

func init() {
rootCmd.AddCommand(statusCmd)
}
11 changes: 11 additions & 0 deletions docs/docs/02-quick-start/02-initialize.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,21 @@ amigo init

You can also create a context to the repository to avoid passing flags each time you run a command. To do so, run the following command:


### Postgres:
```sh
amigo context --dsn "postgres://user:password@localhost:5432/dbname"
```

### SQLite:
```sh
amigo context --dsn "sqlite:/path/to/db.sqlite" --schema-version-table mig_schema_versions
```
Note: The `--schema-version-table` flag is optional and is used to specify the table where mig will store the migration versions. By default, mig uses `public.mig_schema_versions` but since SQLite does not support schemas, you can specify the table name.


### Configuration

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

It contains the following fields:
Expand Down
5 changes: 5 additions & 0 deletions docs/docs/03-cli/04-migrate.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ To apply the migrations, run the following command:

```sh
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
```

## Flags
Expand Down
4 changes: 4 additions & 0 deletions docs/docs/03-cli/05-rollback.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ To rollback the last migration, run the following command:

```sh
amigo rollback

------> rollback: create_user_table version: 20240524110434
-- drop_table(table: users)
------> version rolled back: 20240524090434
```

## Flags
Expand Down
15 changes: 15 additions & 0 deletions docs/docs/03-cli/06-status.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Status

The `status` command will show the current status of the migrations.

It show the 10 most recent migrations and their status between applied and not applied.

The last line of the output will be the most recent migration.


```sh
amigo status

(20240530063939) create_table_schema_version applied
(20240524090434) create_user_table not applied
```
2 changes: 1 addition & 1 deletion docs/docs/04-api/01-postgres/01-constructive.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ They are the operations that create, alter, or drop tables, columns, indexes, co

- [AddForeignKey(fromTable, toTable schema.TableName, opts ...schema.AddForeignKeyConstraintOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.AddForeignKeyConstraint)

- [AddIndexConstraint(table schema.TableName, columns []string, option ...schema.IndexOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.AddIndexConstraint)
- [AddIndex(table schema.TableName, columns []string, option ...schema.IndexOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.AddIndex)

- [AddPrimaryKeyConstraint(tableName schema.TableName, columns []string, opts ...schema.PrimaryKeyConstraintOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.AddPrimaryKeyConstraint)

Expand Down
9 changes: 9 additions & 0 deletions docs/docs/04-api/02-sqlite/01-constructive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Constructive operations

They are the operations that create, alter, or drop tables, columns, indexes, constraints, and so on.

- [AddIndex(table schema.TableName, columns []string, option ...schema.IndexOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/sqlite#Schema.AddIndex)


Each of this functions are reversible, it means that in a migration that implement the `change` function, when you
rollback the migration you don't have to write manually the rollback operation, the library will do it for you.
23 changes: 23 additions & 0 deletions docs/docs/04-api/02-sqlite/02-destructive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Destructive operations

They are the operations that drop tables, columns, indexes, constraints, and so on.

- [DropIndex(table schema.TableName, columns []string, opts ...schema.DropIndexOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/sqlite#Schema.DropIndex)

Usually you will use these functions in the `down` function of a migration, but you can use them in the `up` function too.
If you want to have the reverse operation of a destructive operation, you can use the `reversible` options.

Example:

```go
p.DropTable("users", schema.DropTableOptions{
Reversible: &TableOption{
schema.TableName: "users",
PostgresTableDefinition: Innerschema.Tablefunc(t *PostgresTableDef) {
t.Serial("id")
t.String("name")
}),
}})
```

In that case, if you are in a `change` function in your migration, the library will at the up operation drop the table `users` and at the down operation re-create the table `users` with the columns `id` and `name`.
7 changes: 7 additions & 0 deletions docs/docs/04-api/02-sqlite/03-informative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Informative operations

They are the operations that give you information about the database schema.

- [IndexExist(tableName schema.TableName, indexName string) bool](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/sqlite#Schema.IndexExist)

These functions are not reversible.
5 changes: 5 additions & 0 deletions docs/docs/04-api/02-sqlite/04-transformative.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Transformative operations

They are the operations that change the data in the database.


20 changes: 20 additions & 0 deletions docs/docs/04-api/02-sqlite/05-others.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Others operations

They are functions that are not in the constructive, destructive, or informative categories.

- [Exec(query string, args ...interface{})](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/sqlite#Schema.Exec)

If you want to reverse a query in a `change` function you should use the Reversible method.


```go
s.Reversible(schema.Directions{
Up: func() {
s.Exec("INSERT INTO public.mig_schema_versions (id) VALUES ('1')")
},

Down: func() {
s.Exec("DELETE FROM public.mig_schema_versions WHERE id = '1'")
},
})
```
2 changes: 1 addition & 1 deletion example/mysql/.amigo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func main() {
opts, arg := entrypoint.AmigoContextFromFlags()

db, err := sql.Open("mysql", opts.DSN)
db, err := sql.Open("mysql", opts.GetRealDSN())
if err != nil {
logger.Error(events.MessageEvent{Message: err.Error()})
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion example/pg/.amigo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func main() {
opts, arg := entrypoint.AmigoContextFromFlags()

db, err := sql.Open("pgx", opts.DSN)
db, err := sql.Open("pgx", opts.GetRealDSN())
if err != nil {
logger.Error(events.MessageEvent{Message: err.Error()})
os.Exit(1)
Expand Down
10 changes: 10 additions & 0 deletions example/sqlite/.amigo/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
amigo-folder: .amigo
debug: false
dsn: sqlite:data.db
folder: migrations
json: false
package: migrations
schema-version-table: mig_schema_versions
shell-path: /bin/bash
sql: false
sql-syntax-highlighting: true
23 changes: 23 additions & 0 deletions example/sqlite/.amigo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package main

import (
"database/sql"
"github.com/alexisvisco/amigo/pkg/entrypoint"
"github.com/alexisvisco/amigo/pkg/utils/events"
"github.com/alexisvisco/amigo/pkg/utils/logger"
_ "github.com/mattn/go-sqlite3"
"os"
migrations "sqlite/migrations"
)

func main() {
opts, arg := entrypoint.AmigoContextFromFlags()

db, err := sql.Open("sqlite3", opts.GetRealDSN())
if err != nil {
logger.Error(events.MessageEvent{Message: err.Error()})
os.Exit(1)
}

entrypoint.Main(db, arg, migrations.Migrations, opts)
}
5 changes: 5 additions & 0 deletions example/sqlite/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module sqlite

go 1.22

require github.com/mattn/go-sqlite3 v1.14.22 // indirect
2 changes: 2 additions & 0 deletions example/sqlite/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/mattn/go-sqlite3 v1.14.22 h1:2gZY6PC6kBnID23Tichd1K+Z0oS6nE/XwU+Vz/5o4kU=
github.com/mattn/go-sqlite3 v1.14.22/go.mod h1:Uh1q+B4BYcTPb+yiD3kU8Ct7aC0hY9fxUwlHK0RXw+Y=
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package migrations

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

type Migration20240602080728CreateTableSchemaVersion struct{}

func (m Migration20240602080728CreateTableSchemaVersion) Up(s *sqlite.Schema) {
s.Exec("CREATE TABLE IF NOT EXISTS mig_schema_versions ( id VARCHAR(255) NOT NULL PRIMARY KEY )")
}

func (m Migration20240602080728CreateTableSchemaVersion) Down(s *sqlite.Schema) {
// nothing to do to keep the schema version table
}

func (m Migration20240602080728CreateTableSchemaVersion) Name() string {
return "create_table_schema_version"
}

func (m Migration20240602080728CreateTableSchemaVersion) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-06-02T10:07:28+02:00")
return t
}
24 changes: 24 additions & 0 deletions example/sqlite/migrations/20240602081304_add_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations

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

type Migration20240602081304AddIndex struct{}

func (m Migration20240602081304AddIndex) Change(s *sqlite.Schema) {
s.AddIndex("mig_schema_versions", []string{"id"}, schema.IndexOptions{
IfNotExists: true,
})
}

func (m Migration20240602081304AddIndex) Name() string {
return "add_index"
}

func (m Migration20240602081304AddIndex) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-06-02T10:13:04+02:00")
return t
}
24 changes: 24 additions & 0 deletions example/sqlite/migrations/20240602081806_drop_index.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package migrations

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

type Migration20240602081806DropIndex struct{}

func (m Migration20240602081806DropIndex) Change(s *sqlite.Schema) {
s.DropIndex("mig_schema_versions", []string{"id"}, schema.DropIndexOptions{
Reversible: &schema.IndexOptions{IfNotExists: true},
})
}

func (m Migration20240602081806DropIndex) Name() string {
return "drop_index"
}

func (m Migration20240602081806DropIndex) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-06-02T10:18:06+02:00")
return t
}
13 changes: 13 additions & 0 deletions example/sqlite/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Package migrations
// /!\ File is auto-generated DO NOT EDIT.
package migrations

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

var Migrations = []schema.Migration{
&Migration20240602080728CreateTableSchemaVersion{},
&Migration20240602081304AddIndex{},
&Migration20240602081806DropIndex{},
}
18 changes: 9 additions & 9 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ require (

// user for the cli (only imported in cmd)
github.com/spf13/cobra v1.8.0
github.com/spf13/viper v1.18.2
github.com/spf13/viper v1.19.0
)

// todo: remove
Expand All @@ -20,6 +20,7 @@ require github.com/gobuffalo/flect v1.0.2
// Dependencies for testing purpose
require (
github.com/jackc/pgx/v5 v5.6.0
github.com/mattn/go-sqlite3 v1.14.22
github.com/sergi/go-diff v1.3.1
github.com/stretchr/testify v1.9.0
)
Expand All @@ -35,7 +36,7 @@ require (
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -44,13 +45,12 @@ require (
github.com/spf13/cast v1.6.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/sync v0.5.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/exp v0.0.0-20240531132922-fd00a4e0eefc // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
Loading

0 comments on commit c2284ed

Please sign in to comment.