-
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: (almost) support sqlite, add status command
- Loading branch information
1 parent
677e483
commit c2284ed
Showing
50 changed files
with
1,214 additions
and
145 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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
*.out.sql | ||
*.out.txt | ||
*.data | ||
*.iml | ||
.idea | ||
migrations | ||
|
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,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) | ||
} |
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
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 | ||
``` |
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 @@ | ||
# 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. |
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,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`. |
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,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. |
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,5 @@ | ||
# Transformative operations | ||
|
||
They are the operations that change the data in the database. | ||
|
||
|
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,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'") | ||
}, | ||
}) | ||
``` |
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
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 |
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,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) | ||
} |
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,5 @@ | ||
module sqlite | ||
|
||
go 1.22 | ||
|
||
require github.com/mattn/go-sqlite3 v1.14.22 // indirect |
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,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= |
25 changes: 25 additions & 0 deletions
25
example/sqlite/migrations/20240602080728_create_table_schema_version.go
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 @@ | ||
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 | ||
} |
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,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 | ||
} |
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,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 | ||
} |
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,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{}, | ||
} |
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.