Skip to content

Commit

Permalink
Merge pull request #6 from ShkrutDenis/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
ShkrutDenis authored Apr 20, 2020
2 parents ac56703 + b02e958 commit 6c14617
Show file tree
Hide file tree
Showing 22 changed files with 1,174 additions and 81 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.idea
.env
.docker-compose
docker-compose.yml
19 changes: 17 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## Overview

Simple module for using migrations in your project
A simple module for using migrations in your project

`Now only for MySQL and Postgres!`

Expand All @@ -21,15 +21,30 @@ for update use flag `-u`:
Run this command for put to your project the template for usage go-migrations:
```
bash $GOPATH/src/github.com/ShkrutDenis/go-migrations/init.sh
or if you use vendor folder
bash vendor/github.com/ShkrutDenis/go-migrations/init.sh
```

Or you can copy sources from your dependencies path manually if you have trouble with command.
For example from:
```
.../github.com/ShkrutDenis/go-migrations/template
```

In `migrations/list` directory create your migrations like existed example

In `migrations/entry.go` in `getMigrationsList()` method put your migrations structures

For migrate:
```
go run migrations/entry.go
```

If you want to rollback, add `--rollback` flag.

#### Environment variables

Module use next variables for creating a connection with DB:
Module uses next variables for creating a connection with DB:

- DB_DRIVER
- DB_USER
Expand Down
53 changes: 53 additions & 0 deletions builder/builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package builder

import (
"github.com/ShkrutDenis/go-migrations/builder/contract"
mysql "github.com/ShkrutDenis/go-migrations/builder/mysql/table"
postgres "github.com/ShkrutDenis/go-migrations/builder/postgress/table"
"github.com/jmoiron/sqlx"
"os"
)

func NewTable(name string, con *sqlx.DB) contract.Table {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return mysql.NewTable(name, con)
case "postgres":
return postgres.NewTable(name, con)
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func DropTable(name string, con *sqlx.DB) contract.Table {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return mysql.DropTable(name, con)
case "postgres":
return postgres.DropTable(name, con)
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func ChangeTable(name string, con *sqlx.DB) contract.Table {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return mysql.ChangeTable(name, con)
case "postgres":
return postgres.ChangeTable(name, con)
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}

func RenameTable(oldName, newName string, con *sqlx.DB) contract.Table {
switch os.Getenv("DB_DRIVER") {
case "mysql":
return mysql.RenameTable(oldName, newName, con)
case "postgres":
return postgres.RenameTable(oldName, newName, con)
default:
panic("Not supported DB driver: " + os.Getenv("DB_DRIVER"))
}
}
29 changes: 29 additions & 0 deletions builder/contract/column.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package contract

type Column interface {
Type(string) Column
Nullable() Column
NotNull() Column
Autoincrement() Column
NotAutoincrement() Column
Default(string) Column
Primary() Column
Unique() Column
NotUnique() Column
Drop() Column
Change() Column
First() Column
After(string) Column
Rename(string) Column
GetSQL() string
GetName() string
GetUniqueKeyName() string
IsPrimary() bool
IsUnique() bool
HasUniqueKey() bool
NeedUniqueKey() bool
NeedDropUniqueKey() bool
IsWaitingDrop() bool
IsWaitingRename() bool
IsWaitingChange() bool
}
14 changes: 14 additions & 0 deletions builder/contract/foreign_kay.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package contract

type ForeignKey interface {
Reference(string) ForeignKey
On(string) ForeignKey
OnUpdate(string) ForeignKey
OnDelete(string) ForeignKey
Drop() ForeignKey
SetKeyName(string) ForeignKey
GenerateKeyName() ForeignKey
GetSQL() string
GetName() string
ForDrop() bool
}
16 changes: 16 additions & 0 deletions builder/contract/table.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package contract

type Table interface {
Column(string) Column
String(string, int) Column
Integer(string) Column
WithTimestamps() Table
RenameColumn(string, string) Column
DropColumn(string) Column
PrimaryKey(string) Column
ForeignKey(string) ForeignKey
DropForeignKey(string) Table
GetSQL() string
Exec() error
MustExec()
}
13 changes: 13 additions & 0 deletions builder/contract/unique_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package contract

import "github.com/jmoiron/sqlx"

type UniqueKey interface {
SetKeyName(string) UniqueKey
GenerateKeyName() UniqueKey
Drop() UniqueKey
GetSQL() string
Exec(*sqlx.DB) error
MustExec(*sqlx.DB)
GetName() string
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ package column

import (
"fmt"
"github.com/ShkrutDenis/go-migrations/query_builders/mysql/info"
"github.com/ShkrutDenis/go-migrations/builder/contract"
"github.com/ShkrutDenis/go-migrations/builder/mysql/info"
"github.com/jmoiron/sqlx"
)

Expand All @@ -24,6 +25,8 @@ type Column struct {
unique bool
hasUniqueKey bool

isPrimaryKey bool

drop bool
change bool
first bool
Expand All @@ -33,14 +36,14 @@ type Column struct {
info *info.ColumnInfo
}

func NewColumn(table, fieldName string, con *sqlx.DB) *Column {
func NewColumn(table, fieldName string, con *sqlx.DB) contract.Column {
ci := info.GetColumnInfo(table, fieldName, con)
c := &Column{name: fieldName, info: ci}
c.init()
return c
}

func (c *Column) init() *Column {
func (c *Column) init() contract.Column {
if c.info != nil {
c.fieldType = c.info.ColumnType
if c.info.Nullable() {
Expand All @@ -57,68 +60,73 @@ func (c *Column) init() *Column {
}

// Functions for modify table
func (c *Column) Type(fieldType string) *Column {
func (c *Column) Type(fieldType string) contract.Column {
c.fieldType = fieldType
return c
}

func (c *Column) Nullable() *Column {
func (c *Column) Nullable() contract.Column {
c.nullable = null
return c
}

func (c *Column) NotNull() *Column {
func (c *Column) NotNull() contract.Column {
c.nullable = notNull
return c
}

func (c *Column) Autoincrement() *Column {
func (c *Column) Autoincrement() contract.Column {
c.autoincrement = true
return c
}

func (c *Column) NotAutoincrement() *Column {
func (c *Column) NotAutoincrement() contract.Column {
c.autoincrement = false
return c
}

func (c *Column) Default(value string) *Column {
func (c *Column) Default(value string) contract.Column {
c.hasDefault = value != ""
c.defaultValue = value
return c
}

func (c *Column) Unique() *Column {
func (c *Column) Primary() contract.Column {
c.isPrimaryKey = true
return c
}

func (c *Column) Unique() contract.Column {
c.unique = true
return c
}

func (c *Column) NotUnique() *Column {
func (c *Column) NotUnique() contract.Column {
c.unique = false
return c
}

func (c *Column) Drop() *Column {
func (c *Column) Drop() contract.Column {
c.drop = true
return c
}

func (c *Column) Change() *Column {
func (c *Column) Change() contract.Column {
c.change = true
return c
}

func (c *Column) First() *Column {
func (c *Column) First() contract.Column {
c.first = true
return c
}

func (c *Column) After(name string) *Column {
func (c *Column) After(name string) contract.Column {
c.after = name
return c
}

func (c *Column) Rename(name string) *Column {
func (c *Column) Rename(name string) contract.Column {
c.rename = name
return c
}
Expand Down Expand Up @@ -149,20 +157,21 @@ func (c *Column) changeColumnSQL() string {
}

func (c *Column) renameColumnSQL() string {
sql := fmt.Sprintf("change column %v %v", c.name, c.rename)
sql += c.columnOptionsSQL()
sql += c.columnPositionSQL()
return sql + ";"
return fmt.Sprintf("change column %v %v;", c.name, c.rename)
}

func (c *Column) dropColumnSQL() string {
return fmt.Sprintf("drop column %v;", c.name)
return fmt.Sprintf("drop column %v,", c.name)
}

func (c *Column) columnOptionsSQL() string {
sql := " " + c.fieldType
if c.hasDefault {
sql += " default " + c.defaultValue
if c.fieldType != "bool" && c.fieldType != "boolean" {
sql += fmt.Sprintf(" default '%v'", c.defaultValue)
} else {
sql += fmt.Sprintf(" default %v", c.defaultValue)
}
}
if c.autoincrement {
sql += " auto_increment"
Expand Down Expand Up @@ -201,6 +210,10 @@ func (c *Column) GetUniqueKeyName() string {
return k.KeyName
}

func (c *Column) IsPrimary() bool {
return c.isPrimaryKey
}

func (c *Column) IsUnique() bool {
return c.unique
}
Expand Down
File renamed without changes.
Loading

0 comments on commit 6c14617

Please sign in to comment.