Archived
1
This repository has been archived on 2022-11-04. You can view files and clone it, but cannot push or open issues or pull requests.
i2_bot/lib/migrations/8_create_relations.go

50 lines
1.8 KiB
Go
Raw Normal View History

// i2_bot Instinct PokememBro Bot
// Copyright (c) 2017-2018 Vladimir "fat0troll" Hodakov
package migrations
import (
2017-10-18 07:03:34 +04:00
"database/sql"
)
2017-11-14 03:44:21 +04:00
// CreateRelationsUp creates tables for pokememes-locations and pokememes-elements links
func CreateRelationsUp(tx *sql.Tx) error {
2017-11-14 03:44:21 +04:00
request := "CREATE TABLE `pokememes_locations` ("
request += "`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID связи',"
request += "`pokememe_id` int(11) NOT NULL COMMENT 'ID покемема',"
request += "`location_id` int(11) NOT NULL COMMENT 'ID локации',"
request += "`created_at` datetime NOT NULL COMMENT 'Добавлено в базу',"
request += "PRIMARY KEY (`id`),"
request += "UNIQUE KEY `id` (`id`),"
request += "KEY `pokememes_locations_created_at` (`created_at`)"
request += ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Связь Покемемы-Локации'"
2017-11-14 03:44:21 +04:00
_, err := tx.Exec(request)
2017-10-18 07:03:34 +04:00
if err != nil {
return err
}
2017-11-14 03:44:21 +04:00
request = "CREATE TABLE `pokememes_elements` ("
request += "`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID связи',"
request += "`pokememe_id` int(11) NOT NULL COMMENT 'ID покемема',"
request += "`element_id` int(11) NOT NULL COMMENT 'ID элемента',"
request += "`created_at` datetime NOT NULL COMMENT 'Добавлено в базу',"
request += "PRIMARY KEY (`id`),"
request += "UNIQUE KEY `id` (`id`),"
request += "KEY `pokememes_elements_created_at` (`created_at`)"
request += ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COMMENT='Связь Покемемы-Элементы'"
_, err = tx.Exec(request)
return err
}
2017-11-14 03:44:21 +04:00
// CreateRelationsDown drops pokememe-* relations tables
func CreateRelationsDown(tx *sql.Tx) error {
_, err := tx.Exec("DROP TABLE `pokememes_locations`")
2017-10-18 07:03:34 +04:00
if err != nil {
return err
}
_, err = tx.Exec("DROP TABLE `pokememes_elements`;")
return err
}