Skip to content

Commit

Permalink
[Add] 起動時に自動的にマイグレーションを動かすように設定
Browse files Browse the repository at this point in the history
  • Loading branch information
yukiho authored and get-me-power committed May 4, 2021
1 parent f1480ce commit 98c1b0d
Show file tree
Hide file tree
Showing 9 changed files with 81 additions and 7 deletions.
33 changes: 33 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,36 @@

# mysql data
docker/mysql/data/

# Created by https://www.toptal.com/developers/gitignore/api/macos
# Edit at https://www.toptal.com/developers/gitignore?templates=macos

### macOS ###
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk

# End of https://www.toptal.com/developers/gitignore/api/macos
Expand Down
2 changes: 1 addition & 1 deletion db/migration/000002_create_password_user.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ CREATE TABLE IF NOT EXISTS `password_user` (
`is_lock` TINYINT NOT NULL DEFAULT 0,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` INT NOT NULL,
`updated_at` DATETIME NOT NULL DEFAULT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_by` INT NOT NULL,
INDEX `fk_password_user_user_idx` (`user_id` ASC) VISIBLE,
PRIMARY KEY (`user_id`),
Expand Down
2 changes: 1 addition & 1 deletion db/migration/000003_create_ldap_user.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS `ldap_user` (
`ldap_id` VARCHAR(512) NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` INT NOT NULL,
`updated_at` DATETIME NOT NULL DEFAULT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_by` INT NOT NULL,
INDEX `fk_table1_user1_idx` (`user_id` ASC) VISIBLE,
PRIMARY KEY (`user_id`),
Expand Down
4 changes: 2 additions & 2 deletions db/migration/000005_create_item.up.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
CREATE TABLE IF NOT EXISTS `item` (
`id` INT NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL,
`overview` TEXT NOT NULL DEFAULT '',
`overview` TEXT NOT NULL,
`icon` BLOB NOT NULL,
`type` INT NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` INT NOT NULL,
`updated_at` DATETIME NOT NULL DEFAULT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_by` INT NOT NULL,
PRIMARY KEY (`id`))
ENGINE = InnoDB;
2 changes: 1 addition & 1 deletion db/migration/000006_create_tag.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS `tag` (
`name` VARCHAR(100) NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` INT NOT NULL,
`updated_at` DATETIME NOT NULL DEFAULT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_by` INT NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `name_UNIQUE` (`name` ASC) VISIBLE)
Expand Down
2 changes: 1 addition & 1 deletion db/migration/000008_create_lending.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ CREATE TABLE IF NOT EXISTS `lending` (
`returned_at` DATE NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` INT NOT NULL,
`updated_at` DATETIME NOT NULL DEFAULT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_by` INT NOT NULL,
INDEX `fk_lending_user1_idx` (`user_id` ASC) VISIBLE,
INDEX `fk_lending_item1_idx` (`item_id` ASC) VISIBLE,
Expand Down
2 changes: 1 addition & 1 deletion db/migration/000009_create_reservation.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ CREATE TABLE IF NOT EXISTS `reservation` (
`item_id` INT NOT NULL,
`created_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`created_by` INT NOT NULL,
`updated_at` DATETIME NOT NULL DEFAULT DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_at` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updated_by` INT NOT NULL,
INDEX `fk_reservation_user1_idx` (`user_id` ASC) VISIBLE,
INDEX `fk_reservation_item1_idx` (`item_id` ASC) VISIBLE,
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20200222043503-6f7a984d4dc4/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
github.com/go-sql-driver/mysql v1.4.0/go.mod h1:zAC/RDZ24gD3HViQzih4MyKcchzm+sOG5ZlKdlhCg5w=
github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs=
github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/gobuffalo/here v0.6.0/go.mod h1:wAG085dHOYqUpf+Ap+WOdrPTp5IYcDAs/x7PLa8Y5fM=
Expand Down
40 changes: 40 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package main

import (
"database/sql"
"fmt"

"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/database/mysql"
_ "github.com/golang-migrate/migrate/v4/source/file"
)

func main() {
db, err := sql.Open("mysql", "kiritan:kiritan@tcp(localhost:3306)/kiritan")

if err != nil {
fmt.Println("DBの接続に失敗しました")
fmt.Printf("%s\n", err)
return
}

driver, err := mysql.WithInstance(db, &mysql.Config{})

if err != nil {
fmt.Println("Hoge")
fmt.Printf("%s\n", err)
return
}

m, err := migrate.NewWithDatabaseInstance("file://db/migration", "mysql", driver)
if err != nil {
fmt.Printf("%s\n", err)
return
}

err = m.Up()
if err != nil && err != migrate.ErrNoChange {
fmt.Printf("%s\n", err)
return
}
}

0 comments on commit 98c1b0d

Please sign in to comment.