Add database changes

This commit is contained in:
Nikolay Kochulin 2019-11-16 15:19:05 +00:00
parent 29af7c9183
commit afc6306c9b
5 changed files with 22 additions and 19 deletions

View File

@ -10,7 +10,7 @@ namespace JetKarmaBot.Models
public long ChatId { get; set; } public long ChatId { get; set; }
public int FromId { get; set; } public int FromId { get; set; }
public int ToId { get; set; } public int ToId { get; set; }
public sbyte AwardTypeId { get; set; } public sbyte? AwardTypeId { get; set; }
public sbyte Amount { get; set; } public sbyte Amount { get; set; }
public DateTime Date { get; set; } public DateTime Date { get; set; }
[ForeignKey("AwardTypeId")] [ForeignKey("AwardTypeId")]

View File

@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
namespace JetKarmaBot.Models namespace JetKarmaBot.Models
{ {
@ -12,10 +13,13 @@ namespace JetKarmaBot.Models
public sbyte AwardTypeId { get; set; } public sbyte AwardTypeId { get; set; }
public string CommandName { get; set; } public string CommandName { get; set; }
public long ChatId { get; set; }
public string Name { get; set; } public string Name { get; set; }
public string Symbol { get; set; } public string Symbol { get; set; }
public string Description { get; set; } public string Description { get; set; }
public virtual ICollection<Award> Awards { get; set; } public virtual ICollection<Award> Awards { get; set; }
[ForeignKey("ChatId")]
public virtual Chat Chat { get; set; }
} }
} }

View File

@ -5,15 +5,11 @@ namespace JetKarmaBot.Models
{ {
public partial class Chat public partial class Chat
{ {
public Chat()
{
Awards = new HashSet<Award>();
}
public long ChatId { get; set; } public long ChatId { get; set; }
public string Locale { get; set; } public string Locale { get; set; }
public bool IsAdministrator { get; set; } public bool IsAdministrator { get; set; }
public virtual ICollection<Award> Awards { get; set; } public virtual ICollection<Award> Awards { get; set; }
public virtual ICollection<AwardType> AwardTypes { get; set; }
} }
} }

View File

@ -112,8 +112,8 @@ namespace JetKarmaBot.Models
.HasName("awardtypeid_UNIQUE") .HasName("awardtypeid_UNIQUE")
.IsUnique(); .IsUnique();
entity.HasIndex(e => e.CommandName) entity.HasIndex(e => new { e.CommandName, e.ChatId })
.HasName("commandname_UNIQUE") .HasName("un_cnandchat")
.IsUnique(); .IsUnique();
entity.Property(e => e.AwardTypeId) entity.Property(e => e.AwardTypeId)
@ -139,6 +139,12 @@ namespace JetKarmaBot.Models
.IsRequired() .IsRequired()
.HasColumnName("symbol") .HasColumnName("symbol")
.HasColumnType("varchar(16)"); .HasColumnType("varchar(16)");
entity.HasOne(d => d.Chat)
.WithMany(p => p.AwardTypes)
.HasForeignKey(d => d.ChatId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("fk_chat");
}); });
modelBuilder.Entity<Chat>(entity => modelBuilder.Entity<Chat>(entity =>

View File

@ -3,17 +3,15 @@
DROP TABLE IF EXISTS `awardtype`; DROP TABLE IF EXISTS `awardtype`;
CREATE TABLE `awardtype` ( CREATE TABLE `awardtype` (
`awardtypeid` tinyint(3) NOT NULL PRIMARY KEY, `awardtypeid` tinyint(3) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`commandname` varchar(35) NOT NULL UNIQUE, `commandname` varchar(35) NOT NULL,
`chatid` bigint(20) NOT NULL REFERENCES `chat` (`chatid`),
`name` varchar(32) NOT NULL, `name` varchar(32) NOT NULL,
`symbol` varchar(16) NOT NULL, `symbol` varchar(16) NOT NULL,
`description` text NOT NULL `description` text NOT NULL,
UNIQUE KEY `un_cnandchat` (`commandname`, `chatid`)
); );
LOCK TABLES `awardtype` WRITE;
INSERT INTO `awardtype` VALUES (1,'example','Example','Examples','An example');
UNLOCK TABLES;
DROP TABLE IF EXISTS `chat`; DROP TABLE IF EXISTS `chat`;
CREATE TABLE `chat` ( CREATE TABLE `chat` (
`chatid` bigint(20) NOT NULL PRIMARY KEY, `chatid` bigint(20) NOT NULL PRIMARY KEY,
@ -23,9 +21,8 @@ CREATE TABLE `chat` (
DROP TABLE IF EXISTS `user`; DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` ( CREATE TABLE `user` (
`userid` bigint(20) NOT NULL, `userid` bigint(20) NOT NULL PRIMARY KEY,
`username` varchar(45) DEFAULT NULL, `username` varchar(45) DEFAULT NULL
PRIMARY KEY (`userid`)
); );
DROP TABLE IF EXISTS `award`; DROP TABLE IF EXISTS `award`;
@ -34,7 +31,7 @@ CREATE TABLE `award` (
`chatid` bigint(20) NOT NULL REFERENCES `chat` (`chatid`), `chatid` bigint(20) NOT NULL REFERENCES `chat` (`chatid`),
`fromid` bigint(20) NOT NULL REFERENCES `user` (`userid`), `fromid` bigint(20) NOT NULL REFERENCES `user` (`userid`),
`toid` bigint(20) NOT NULL REFERENCES `user` (`userid`), `toid` bigint(20) NOT NULL REFERENCES `user` (`userid`),
`awardtypeid` tinyint(3) NOT NULL REFERENCES `awardtype` (`awardtypeid`), `awardtypeid` tinyint(3) REFERENCES `awardtype` (`awardtypeid`),
`amount` tinyint(3) NOT NULL DEFAULT 1, `amount` tinyint(3) NOT NULL DEFAULT 1,
`date` datetime NOT NULL DEFAULT current_timestamp() `date` datetime NOT NULL DEFAULT current_timestamp()
); );