Skip to content

Commit

Permalink
feat: support unknown DSN
Browse files Browse the repository at this point in the history
  • Loading branch information
alexisvisco committed May 30, 2024
1 parent 785ad7c commit 677e483
Show file tree
Hide file tree
Showing 39 changed files with 546 additions and 254 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
.idea
migrations
.amigo
!example/pg/.amigo
!example/pg/migrations
!example/*/.amigo
!example/*/migrations
9 changes: 6 additions & 3 deletions cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,21 @@ var initCmd = &cobra.Command{
return fmt.Errorf("unable to open migrations.go file: %w", err)
}

template, err := templates.GetInitCreateTableTemplate(templates.CreateTableData{Name: cmdCtx.SchemaVersionTable})
inUp, err := templates.GetInitCreateTableTemplate(templates.CreateTableData{Name: cmdCtx.SchemaVersionTable},
am.Driver == types.DriverUnknown)
if err != nil {
return err
}

err = am.GenerateMigrationFile(&amigo.GenerateMigrationFileParams{
Name: "create_table_schema_version",
Up: template,
Up: inUp,
Down: "// nothing to do to keep the schema version table",
Type: types.MigrationFileTypeClassic,
Now: now,
Writer: file,
UseSchemaImport: true,
UseSchemaImport: am.Driver != types.DriverUnknown,
UseFmtImport: am.Driver == types.DriverUnknown,
})
if err != nil {
return err
Expand Down
5 changes: 5 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,13 @@ var rootCmd = &cobra.Command{
First you need to create a main folder with amigo init:
will create a folder named .amigo with a context file inside to not have to pass the dsn every time.
Postgres:
$ amigo context --dsn "postgres://user:password@host:port/dbname?sslmode=disable"
Unknown Driver (Mysql in this case):
$ amigo context --dsn "user:password@tcp(host:port)/dbname"
$ amigo init
note: will create:
Expand Down
13 changes: 13 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,19 @@ services:
- 6666:5432
volumes:
- pg_data:/var/lib/postgresql/data
mysql_mig:
image: mysql:8
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: mysql
MYSQL_USER: mysql
MYSQL_PASSWORD: mysql
ports:
- 6668:3306
volumes:
- mysql_data:/var/lib/mysql
volumes:
pg_data:
name: pg_data
mysql_data:
name: mysql_data
5 changes: 5 additions & 0 deletions docs/docs/02-quick-start/05-working-with-not-supported-db.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Working with not supported database

If you want to use a database that is not supported by amigo, you can.

If the DSN is not recognized you will be using the base interface which is `base.Schema` (it only implement methods to manipulate the versions table) but you have access to the `*sql.DB`.
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 @@ -22,7 +22,7 @@ They are the operations that create, alter, or drop tables, columns, indexes, co

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

- [AddVersion(version string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.AddVersion)
- [AddVersion(version string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/base#Schema.AddVersion)

- [CreateEnum(name string, values []string, opts ...schema.CreateEnumOptions)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.CreateEnum)

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/04-api/01-postgres/02-destructive.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ They are the operations that drop tables, columns, indexes, constraints, and so

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

- [RemoveVersion(version string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.RemoveVersion)
- [RemoveVersion(version string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/base#Schema.RemoveVersion)

- [RenameColumn(tableName schema.TableName, oldColumnName, newColumnName string)](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.RenameColumn)

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/04-api/01-postgres/03-informative.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ They are the operations that give you information about the database schema.

- [PrimaryKeyExist(tableName schema.TableName) bool](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.PrimaryKeyExist)

- [FindAppliedVersions() []string](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.FindAppliedVersions)
- [FindAppliedVersions() []string](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/base#Schema.FindAppliedVersions)

- [FindEnumUsage(name string, schemaName *string) []schema.EnumUsage](https://pkg.go.dev/github.com/alexisvisco/amigo/pkg/schema/pg#Schema.FindEnumUsage)

Expand Down
10 changes: 10 additions & 0 deletions example/mysql/.amigo/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
amigo-folder: .amigo
debug: false
dsn: root:root@tcp(localhost:6668)/mysql
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/mysql/.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/go-sql-driver/mysql"
migrations "mysql/migrations"
"os"
)

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

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

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

go 1.22

replace github.com/alexisvisco/amigo => ../../

require (
github.com/alexisvisco/amigo v0.0.0-00010101000000-000000000000
github.com/go-sql-driver/mysql v1.8.1
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/alecthomas/chroma/v2 v2.14.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/gobuffalo/flect v1.0.2 // indirect
github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551 // indirect
)
54 changes: 54 additions & 0 deletions example/mysql/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
github.com/alecthomas/assert/v2 v2.7.0 h1:QtqSACNS3tF7oasA8CU6A6sXZSBDqnm7RfpLl9bZqbE=
github.com/alecthomas/assert/v2 v2.7.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
github.com/alecthomas/chroma/v2 v2.14.0 h1:R3+wzpnUArGcQz7fCETQBzO5n9IMNi13iIs46aU4V9E=
github.com/alecthomas/chroma/v2 v2.14.0/go.mod h1:QolEbTfmUHIMVpBqxeDnNBj2uoeI4EbYP4i6n68SG4I=
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dlclark/regexp2 v1.11.0 h1:G/nrcoOa7ZXlpoa/91N3X7mM3r8eIlMBBJZvsz/mxKI=
github.com/dlclark/regexp2 v1.11.0/go.mod h1:DHkYz0B9wPfa6wondMfaivmHpzrQ3v9q8cnmRbL6yW8=
github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y=
github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg=
github.com/gobuffalo/flect v1.0.2 h1:eqjPGSo2WmjgY2XlpGwo2NXgL3RucAKo4k4qQMNA5sA=
github.com/gobuffalo/flect v1.0.2/go.mod h1:A5msMlrHtLqh9umBSnvabjsMrCcCpAyzglnDvkbYKHs=
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8=
github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I=
github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551 h1:+EXKKt7RC4HyE/iE8zSeFL+7YBL8Z7vpBaEE3c7lCnk=
github.com/simukti/sqldb-logger v0.0.0-20230108155151-646c1a075551/go.mod h1:ztTX0ctjRZ1wn9OXrzhonvNmv43yjFUXJYJR95JQAJE=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
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.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=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
golang.org/x/crypto v0.17.0 h1:r8bRNjWL3GshPW3gkd+RpvzWrZAwPS49OmTGZ/uhM4k=
golang.org/x/crypto v0.17.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4=
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE=
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package migrations

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

type Migration20240529125357CreateTableSchemaVersion struct{}

func (m Migration20240529125357CreateTableSchemaVersion) Up(s *base.Schema) {
query := `CREATE TABLE IF NOT EXISTS mig_schema_versions ( id VARCHAR(255) NOT NULL PRIMARY KEY )`
_, err := s.TX.ExecContext(s.Context.Context, query)
if err != nil {
s.Context.RaiseError(fmt.Errorf("unable to create table schema version: %w", err))
}
}

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

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

func (m Migration20240529125357CreateTableSchemaVersion) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-05-29T14:53:57+02:00")
return t
}
11 changes: 11 additions & 0 deletions example/mysql/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Package migrations
// /!\ File is auto-generated DO NOT EDIT.
package migrations

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

var Migrations = []schema.Migration{
&Migration20240529125357CreateTableSchemaVersion{},
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package migrations

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

type Migration20240530063939CreateTableSchemaVersion struct{}

func (m Migration20240530063939CreateTableSchemaVersion) Up(s *pg.Schema) {
s.CreateTable("public.mig_schema_versions", func(s *pg.PostgresTableDef) {
s.String("id")
}, schema.TableOptions{IfNotExists: true})
}

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

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

func (m Migration20240530063939CreateTableSchemaVersion) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-05-30T08:39:39+02:00")
return t
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"time"
)

type Migration20240524090434CreateUserTable struct{}
type Migration20240530063940CreateUserTable struct{}

func (m Migration20240524090434CreateUserTable) Change(s *pg.Schema) {
func (m Migration20240530063940CreateUserTable) Change(s *pg.Schema) {
s.CreateTable("users", func(def *pg.PostgresTableDef) {
def.Column("id", "bigserial")
def.String("name")
Expand All @@ -17,11 +17,11 @@ func (m Migration20240524090434CreateUserTable) Change(s *pg.Schema) {
})
}

func (m Migration20240524090434CreateUserTable) Name() string {
func (m Migration20240530063940CreateUserTable) Name() string {
return "create_user_table"
}

func (m Migration20240524090434CreateUserTable) Date() time.Time {
func (m Migration20240530063940CreateUserTable) Date() time.Time {
t, _ := time.Parse(time.RFC3339, "2024-05-24T11:04:34+02:00")
return t
}
4 changes: 2 additions & 2 deletions example/pg/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ import (
)

var Migrations = []schema.Migration{
&Migration20240524090427CreateTableSchemaVersion{},
&Migration20240524090434CreateUserTable{},
&Migration20240530063939CreateTableSchemaVersion{},
&Migration20240530063940CreateUserTable{},
}
1 change: 1 addition & 0 deletions go.work
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ go 1.22

use (
example/pg
example/mysql
.
)
Loading

0 comments on commit 677e483

Please sign in to comment.