Skip to content

Commit

Permalink
Release/v0.5.0 (#15)
Browse files Browse the repository at this point in the history
## Features:
- Create migration up/down to start the project from scratch.
- Ensure that all files are created and not replaced when using
generateFile. If the file doesn't exist, it creates a new folder + file;
if it exists, it opens the file and returns the fd file descriptor.
- Validate if installed drivers and create a query connection to run
migrations. New rules were added to ensure that all necessary drivers
like MySQL and SQLite3 are installed.

## Chore:
- Removed an unused file.
- Removed an unused method.
- Removed an unused type
- Created templates for migrations.
- Created version to v0.5.0.
  • Loading branch information
renanbastos93 authored Jul 20, 2023
2 parents 76e67b9 + 7aecfb2 commit 97e9ae0
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 21 deletions.
20 changes: 16 additions & 4 deletions internal/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,22 @@ type contentTpl struct {
}

const (
MySQL = "mysql"
SQL = "sql"
SQLlite3 = "sqlite3"
)

func (e *contentTpl) setDatabaseToUse(whichSql string) {
switch strings.ToLower(whichSql) {
case SQL:
case SQL, MySQL:
e.IsSQL = true
default:
e.IsSQLLite3 = true
}
}

func generateFile(filePath string) (fd *os.File) {
_, err := os.Stat(filePath)
info, err := os.Stat(filePath)
if err != nil {
if !os.IsNotExist(err) {
panic(err)
Expand All @@ -44,6 +45,15 @@ func generateFile(filePath string) (fd *os.File) {
}
}

// It will ensure that don't replace files in case you repeat `boneless new` command
if info != nil {
fd, err := os.Open(filePath)
if err != nil {
panic(err)
}
return fd
}

fd, err = os.Create(filePath)
if err != nil {
panic(err)
Expand Down Expand Up @@ -87,13 +97,15 @@ func Build(appName string, which string, whichSql string) {
if tf.kind == KindComponent {
filePath = fmt.Sprintf(filePath, appName)
}

fd := generateFile(filePath)
t, err := template.ParseFS(templates.CreateTemplateFS, tf.pattern)
if err != nil {
panic(err)
}
t.Execute(fd, ct)
_ = t.Execute(fd, ct)
_ = fd.Close()
fmt.Println("created file:", filePath)
}

println("Created files!")
}
11 changes: 7 additions & 4 deletions internal/files.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package internal

type tplKind string

type tplFiles struct {
pattern string
filePath string
Expand Down Expand Up @@ -47,8 +45,13 @@ var templateFiles = []tplFiles{
kind: KindComponent,
},
{
pattern: "files/schema.sql.tpl",
filePath: "/internal/%s/db/migrations/schema.sql",
pattern: "files/create_table.up.sql.tpl",
filePath: "/internal/%s/db/migrations/000001_create_table.up.sql",
kind: KindComponent,
},
{
pattern: "files/create_table.down.sql.tpl",
filePath: "/internal/%s/db/migrations/000001_create_table.down.sql",
kind: KindComponent,
},
{
Expand Down
29 changes: 24 additions & 5 deletions internal/internal.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package internal

import (
"bytes"
"io/fs"
"os"
"os/exec"
Expand Down Expand Up @@ -62,7 +63,7 @@ func WeaverGenerate() {

func RunMakeMigrate(componentName string, name string) {
dir := findComponentPath(componentName)
err := runCmd("migrate", "create", "-ext", "sql", "-dir", dir+"/db/migrations/", name)
err := runCmd("migrate", "create", "-seq", "-ext", "sql", "-dir", dir+"/db/migrations/", name)
if err != nil {
panic(err)
}
Expand All @@ -84,10 +85,28 @@ func runCmd(name string, args ...string) (err error) {
}

func RunMigrate(componentName, upDown string) {
dir := findComponentPath(componentName)
wasInstalledDriversDB()
var dir = findComponentPath(componentName)

queryConn := ReadToml(componentName)
err := runCmd("migrate", "-path", dir+"/db/migrations/", "-database", queryConn, "-verbose", upDown)
if err != nil {
panic(err)
_ = runCmd("migrate", "-path", dir+"/db/migrations/", "-database", queryConn, "-verbose", upDown)
}

func wasInstalledDriversDB() {
cmd := exec.Command("migrate", "-h")
var errb bytes.Buffer
cmd.Stderr = &errb
_ = cmd.Run()

out := errb.String()
if i := strings.LastIndex(out, "Database drivers: "); i > -1 {
out = out[i:]
out = out[:strings.LastIndex(out, "\n")]
if !strings.Contains(out, "mysql") || !strings.Contains(out, "sqlite3") {
msg := `not supported drivers: mysql or sqlite3.
Install go-migrate with tags: go install -tags 'sql mysql sqlite3' github.com/golang-migrate/migrate/v4/cmd/migrate@latest
Or read documentation from github.com/golang-migrate/migrate`
println(msg)
}
}
}
2 changes: 1 addition & 1 deletion internal/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"
)

const Version = "v0.4.1"
const Version = "v0.5.0"

func ValidateLatestVersion() {
cmd := exec.Command("go", "list", "-m", "github.com/renanbastos93/boneless@latest")
Expand Down
1 change: 1 addition & 0 deletions templates/files/create_table.down.sql.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE examples;
5 changes: 5 additions & 0 deletions templates/files/create_table.up.sql.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
CREATE TABLE examples (
id INTEGER PRIMARY KEY,
created_at BIGINT NOT NULL,
message TEXT NOT NULL
);
6 changes: 0 additions & 6 deletions templates/files/schema.sql.tpl

This file was deleted.

2 changes: 1 addition & 1 deletion templates/files/weaver.toml.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ Driver = "mysql"
Source = "root:@tcp(localhost:3306)/mydatabase"
{{- else -}}
Driver = "sqlite3"
Source = "sqlite3.db"
Source = "db.sqlite3"
{{- end}}

0 comments on commit 97e9ae0

Please sign in to comment.