Skip to content

Commit

Permalink
Support migration from original get5-web #4 (#140)
Browse files Browse the repository at this point in the history
* added migration script and main.go

* fixed migration binary. Added new tables. (see description)

(Season settings are not done yet!)
Added new column "cvars","side_type","is_pug"for #134 #128 #127 .
Added new column "dem_path" for #106 .
Added new table "round_stats" for #93  #94 #96 .

* Added migration code for original get5-web(and current get5-web-go) users

* fixed commands, add "steamids" column for team table

* Added makefile for migration

* Update TravisCI settings
  • Loading branch information
Shugo Kawamura authored Feb 15, 2020
1 parent 0dd1534 commit 05de1de
Show file tree
Hide file tree
Showing 9 changed files with 413 additions and 11 deletions.
13 changes: 7 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ branches:
- master
notifications:
email: false
before_install:
- make deps-go
matrix:
include:
- language: go
go: 1.13.5
before_install:
- make deps-go

script:
- make build-linux-server-only
- language: go
go: 1.13.5
before_install:
- make deps-go
script:
- make build-mac-server-only
- language: go
go: 1.13.5
before_install:
- make deps-go
script:
- make build-windows-server-only
- language: go
go: 1.13.5
script:
- make build-migration-all
- language: node_js
node_js: 10.18.0
before_install:
Expand Down
23 changes: 18 additions & 5 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
GOGET=$(GOCMD) get
BINARY_NAME=get5
MIGRATION_BINARY_NAME=migration
DIST_DIR=build
SERVER_DIR=server
WEB_DIR=web
Expand Down Expand Up @@ -54,6 +55,7 @@ deps-web:
deps-go:
-@$(RM) $(GOPATHDIR)/src/github.com/FlowingSPDG/get5-web-go
@$(GOGET) -v -u \
github.com/rubenv/sql-migrate \
golang.org/x/sys/unix \
github.com/mitchellh/gox \
github.com/go-ini/ini \
Expand Down Expand Up @@ -135,8 +137,19 @@ build-mac-server-only: build-prepare
@$(CP) ./server/config.ini.template ./$(DIST_DIR)/$(BINARY_NAME)_$(OS_Mac)_$(ARCH_AMD64)/config.ini.template
build-web:
@cd ./web && yarn run build

# Source Mod compile
# TODO
# build-game:
# @cd ./game_plugin/scripting
build-migration-all: build-migration-linux build-migration-windows build-migration-mac
build-migration-linux:
@cd ./migration/migrate && gox \
-os="$(OS_Linux)" \
-arch="$(ARCH_386) $(ARCH_AMD64)" \
--output "../../$(DIST_DIR)/$(MIGRATION_BINARY_NAME)_{{.OS}}_{{.Arch}}/$(MIGRATION_BINARY_NAME)"
build-migration-windows:
@cd ./migration/migrate && gox \
-os="$(OS_Windows)" \
-arch="$(ARCH_386) $(ARCH_AMD64)" \
--output "../../$(DIST_DIR)/$(MIGRATION_BINARY_NAME)_{{.OS}}_{{.Arch}}/$(MIGRATION_BINARY_NAME)"
build-migration-mac:
@cd ./migration/migrate && gox \
-os="$(OS_Mac)" \
-arch="$(ARCH_386) $(ARCH_AMD64)" \
--output "../../$(DIST_DIR)/$(MIGRATION_BINARY_NAME)_{{.OS}}_{{.Arch}}/$(MIGRATION_BINARY_NAME)"
24 changes: 24 additions & 0 deletions migration/README.MD
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Migration from Original get5-web(and get5-web-go =< v0.1.2)

## Preparation
If you gonna migrate from original get5-web database, you'll need to execute preparation before start migration.
**This step is not required if you gonna create new database from empty MySQL database.**
- go to migration/migrate_from_original folder. Execute `migrate`(or ``go run main.go``) binary with following args :
- -db - Database name. e.g. `"get5"`
- -host - Database host destination. e.g. `"localhost"`.
- -port - Database password `3306` by default.
- -user Database user name e.g. `"get5_user"`.
- -password - Database user password e.g. `"MyGet5Password"`.
- Example: `./migrate -db get5 -host localhost -port 3306 -user get5_user -password MyGet5Password`

## Migration
**This step is required for all users!**
This will Add/Modiby Database tables to support new incoming features in get5-web-go.
- goto migration/migrate folder. Execute `migrate`(or ``go run main.go``) binary with following args :
- -direction - `"up"` or `"down"`. in this case, chose `"up"` or leave it by default value `"up"`.
- -db - Database name. e.g. `"get5"`
- -host - Database host destination. e.g. `"localhost"`.
- -port - Database password `3306` by default.
- -user Database user name e.g. `"get5_user"`.
- -password - Database user password e.g. `"MyGet5Password"`.
- Example: `./migrate -direction up -db get5 -host localhost -port 3306 -user get5_user -password MyGet5Password`
4 changes: 4 additions & 0 deletions migration/migrate/dbconfig.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
development:
dialect: mysql
dir: ./schema
datasource: user_name:password@tcp(127.0.0.1)/dbname?charset=utf8mb4&collation=utf8mb4_general_ci&parseTime=true
58 changes: 58 additions & 0 deletions migration/migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"database/sql"
"flag"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/rubenv/sql-migrate"
"log"
"os"
)

func main() {
log.Println("START Migration...")
direction := flag.String("direction", "up", "Migration direction. (up|down)")

MySQLHost := flag.String("host", "127.0.0.1", "MySQL Host destination")
MySQLPort := flag.Uint("port", 3306, "MySQL Database Port")
MySQLUser := flag.String("user", "user", "MySQL User ID")
MySQLPassword := flag.String("password", "password", "MySQL User password")
MySQLDB := flag.String("db", "get5", "MySQL Database name")

flag.Parse()

sqloption := fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8mb4&parseTime=True&loc=Local", *MySQLUser, *MySQLPassword, *MySQLHost, *MySQLPort, *MySQLDB)
db, err := sql.Open("mysql", sqloption)
if err != nil {
panic(err.Error())
}
defer db.Close()

switch *direction {
case "up":
log.Println("Upgrating...")
migrations := &migrate.FileMigrationSource{
Dir: "./schema",
}
n, err := migrate.Exec(db, "mysql", migrations, migrate.Up)
if err != nil {
log.Printf("Failed to migrate database! ERR : %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Applied %d migrations!\n", n)
case "down":
log.Println("Downgrating...")
migrations := &migrate.FileMigrationSource{
Dir: "./schema",
}
n, err := migrate.Exec(db, "mysql", migrations, migrate.Down)
if err != nil {
log.Printf("Failed to migrate database! ERR : %s\n", err.Error())
os.Exit(1)
}
fmt.Printf("Applied %d migrations!\n", n)
default:
log.Printf("Unsupported command. command : %s\n", *direction)
}
}
180 changes: 180 additions & 0 deletions migration/migrate/schema/20000101000000-create_tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@

-- +migrate Up
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;


CREATE TABLE `alembic_version` (
`version_num` varchar(32) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `game_server` (
`id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`in_use` tinyint(1) DEFAULT NULL,
`ip_string` varchar(32) DEFAULT NULL,
`port` int(11) DEFAULT NULL,
`rcon_password` varchar(32) DEFAULT NULL,
`display_name` varchar(32) DEFAULT NULL,
`public_server` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `map_stats` (
`id` int(11) NOT NULL,
`match_id` int(11) DEFAULT NULL,
`map_number` int(11) DEFAULT NULL,
`map_name` varchar(64) DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`winner` int(11) DEFAULT NULL,
`team1_score` int(11) DEFAULT NULL,
`team2_score` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `match` (
`id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`server_id` int(11) DEFAULT NULL,
`team1_id` int(11) DEFAULT NULL,
`team2_id` int(11) DEFAULT NULL,
`winner` int(11) DEFAULT NULL,
`cancelled` tinyint(1) DEFAULT NULL,
`start_time` datetime DEFAULT NULL,
`end_time` datetime DEFAULT NULL,
`max_maps` int(11) DEFAULT NULL,
`title` varchar(60) DEFAULT NULL,
`skip_veto` tinyint(1) DEFAULT NULL,
`api_key` varchar(32) DEFAULT NULL,
`veto_mappool` varchar(500) DEFAULT NULL,
`team1_score` int(11) DEFAULT NULL,
`team2_score` int(11) DEFAULT NULL,
`team1_string` varchar(32) DEFAULT NULL,
`team2_string` varchar(32) DEFAULT NULL,
`forfeit` tinyint(1) DEFAULT NULL,
`plugin_version` varchar(32) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `player_stats` (
`id` int(11) NOT NULL,
`match_id` int(11) DEFAULT NULL,
`map_id` int(11) DEFAULT NULL,
`team_id` int(11) DEFAULT NULL,
`steam_id` varchar(40) DEFAULT NULL,
`name` varchar(40) DEFAULT NULL,
`kills` int(11) DEFAULT NULL,
`deaths` int(11) DEFAULT NULL,
`roundsplayed` int(11) DEFAULT NULL,
`assists` int(11) DEFAULT NULL,
`flashbang_assists` int(11) DEFAULT NULL,
`teamkills` int(11) DEFAULT NULL,
`suicides` int(11) DEFAULT NULL,
`headshot_kills` int(11) DEFAULT NULL,
`damage` int(11) DEFAULT NULL,
`bomb_plants` int(11) DEFAULT NULL,
`bomb_defuses` int(11) DEFAULT NULL,
`v1` int(11) DEFAULT NULL,
`v2` int(11) DEFAULT NULL,
`v3` int(11) DEFAULT NULL,
`v4` int(11) DEFAULT NULL,
`v5` int(11) DEFAULT NULL,
`k1` int(11) DEFAULT NULL,
`k2` int(11) DEFAULT NULL,
`k3` int(11) DEFAULT NULL,
`k4` int(11) DEFAULT NULL,
`k5` int(11) DEFAULT NULL,
`firstdeath_Ct` int(11) DEFAULT NULL,
`firstdeath_t` int(11) DEFAULT NULL,
`firstkill_ct` int(11) DEFAULT NULL,
`firstkill_t` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `team` (
`id` int(11) NOT NULL,
`user_id` int(11) DEFAULT NULL,
`name` varchar(40) DEFAULT NULL,
`flag` varchar(4) DEFAULT NULL,
`logo` varchar(10) DEFAULT NULL,
`auths` blob,
`tag` varchar(40) DEFAULT NULL,
`public_team` tinyint(1) DEFAULT '1'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `user` (
`id` int(11) NOT NULL,
`steam_id` varchar(40) DEFAULT NULL,
`name` varchar(40) DEFAULT NULL,
`admin` tinyint(1) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


ALTER TABLE `game_server`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id` (`user_id`);

ALTER TABLE `map_stats`
ADD PRIMARY KEY (`id`),
ADD KEY `match_id` (`match_id`),
ADD KEY `winner` (`winner`);

ALTER TABLE `match`
ADD PRIMARY KEY (`id`),
ADD KEY `server_id` (`server_id`),
ADD KEY `team1_id` (`team1_id`),
ADD KEY `team2_id` (`team2_id`),
ADD KEY `user_id` (`user_id`),
ADD KEY `winner` (`winner`);

ALTER TABLE `player_stats`
ADD PRIMARY KEY (`id`),
ADD KEY `map_id` (`map_id`),
ADD KEY `match_id` (`match_id`),
ADD KEY `team_id` (`team_id`);

ALTER TABLE `team`
ADD PRIMARY KEY (`id`),
ADD KEY `user_id` (`user_id`),
ADD KEY `ix_team_public_team` (`public_team`);

ALTER TABLE `user`
ADD PRIMARY KEY (`id`),
ADD UNIQUE KEY `steam_id` (`steam_id`);


ALTER TABLE `game_server`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `map_stats`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `match`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `player_stats`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `team`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;

ALTER TABLE `user`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT;


ALTER TABLE `map_stats`
ADD CONSTRAINT `map_stats_ibfk_1` FOREIGN KEY (`match_id`) REFERENCES `match` (`id`),
ADD CONSTRAINT `map_stats_ibfk_2` FOREIGN KEY (`winner`) REFERENCES `team` (`id`);
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;


-- +migrate Down
DROP TABLE IF EXISTS `alembic_version`,`game_server`,`map_stats`,`match`,`player_stats`,`team`,`user`;
Loading

0 comments on commit 05de1de

Please sign in to comment.