Skip to content

Commit

Permalink
feat: add fk
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Nov 10, 2022
1 parent 1a07118 commit 006555b
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/RomiChan/gocq-sqlite3-migrate
go 1.19

require (
github.com/FloatTech/sqlite v0.5.0
github.com/FloatTech/sqlite v0.5.1
github.com/Mrs4s/MiraiGo v0.0.0-20220828090150-a3c348100dfe
github.com/Mrs4s/go-cqhttp v1.0.0-rc3.0.20221109121432-fa267b6a2d47
github.com/pkg/errors v0.9.1
Expand Down
4 changes: 2 additions & 2 deletions sqlite3/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type StoredMessageAttribute struct {
ID int64 // ID is the crc64 of 字段s below
MessageSeq int32
InternalID int32
SenderUin int64
SenderUin int64 // SenderUin is fk to UinInfo
Timestamp int64
}

Expand All @@ -28,7 +28,7 @@ type StoredGuildMessageAttribute struct {
ID int64 // ID is the crc64 of 字段s below
MessageSeq int64
InternalID int64
SenderTinyID int64
SenderTinyID int64 // SenderTinyID is fk to TinyInfo
Timestamp int64
}

Expand Down
53 changes: 45 additions & 8 deletions sqlite3/sqlite3.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,72 @@ func Open(dbpath string) (s *Database, err error) {
if err != nil {
return nil, errors.Wrap(err, "open sqlite3 error")
}
err = s.db.Create(Sqlite3GroupMessageTableName, &StoredGroupMessage{})
_, err = s.db.DB.Exec("PRAGMA foreign_keys = ON;")
if err != nil {
return nil, errors.Wrap(err, "enable foreign_keys error")
}
err = s.db.Create(Sqlite3UinInfoTableName, &UinInfo{})
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3MessageAttributeTableName, &StoredMessageAttribute{})
err = s.db.Insert(Sqlite3UinInfoTableName, &UinInfo{Name: "null"})
if err != nil {
return nil, errors.Wrap(err, "insert into sqlite3 table "+Sqlite3UinInfoTableName+" error")
}
err = s.db.Create(Sqlite3TinyInfoTableName, &TinyInfo{})
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3GuildMessageAttributeTableName, &StoredGuildMessageAttribute{})
err = s.db.Insert(Sqlite3TinyInfoTableName, &TinyInfo{Name: "null"})
if err != nil {
return nil, errors.Wrap(err, "insert into sqlite3 table "+Sqlite3TinyInfoTableName+" error")
}
err = s.db.Create(Sqlite3MessageAttributeTableName, &StoredMessageAttribute{},
"FOREIGN KEY(SenderUin) REFERENCES "+Sqlite3UinInfoTableName+"(Uin)",
)
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3QuotedInfoTableName, &QuotedInfo{})
err = s.db.Insert(Sqlite3MessageAttributeTableName, &StoredMessageAttribute{})
if err != nil {
return nil, errors.Wrap(err, "insert into sqlite3 table "+Sqlite3MessageAttributeTableName+" error")
}
err = s.db.Create(Sqlite3GuildMessageAttributeTableName, &StoredGuildMessageAttribute{},
"FOREIGN KEY(SenderTinyID) REFERENCES "+Sqlite3TinyInfoTableName+"(ID)",
)
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3PrivateMessageTableName, &StoredPrivateMessage{})
err = s.db.Insert(Sqlite3GuildMessageAttributeTableName, &StoredGuildMessageAttribute{})
if err != nil {
return nil, errors.Wrap(err, "insert into sqlite3 table "+Sqlite3GuildMessageAttributeTableName+" error")
}
err = s.db.Create(Sqlite3QuotedInfoTableName, &QuotedInfo{})
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3GuildChannelMessageTableName, &StoredGuildChannelMessage{})
err = s.db.Insert(Sqlite3QuotedInfoTableName, &QuotedInfo{QuotedContent: "null"})
if err != nil {
return nil, errors.Wrap(err, "insert into sqlite3 table "+Sqlite3QuotedInfoTableName+" error")
}
err = s.db.Create(Sqlite3GroupMessageTableName, &StoredGroupMessage{},
"FOREIGN KEY(AttributeID) REFERENCES "+Sqlite3MessageAttributeTableName+"(ID)",
"FOREIGN KEY(QuotedInfoID) REFERENCES "+Sqlite3QuotedInfoTableName+"(ID)",
)
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3UinInfoTableName, &UinInfo{})
err = s.db.Create(Sqlite3PrivateMessageTableName, &StoredPrivateMessage{},
"FOREIGN KEY(AttributeID) REFERENCES "+Sqlite3MessageAttributeTableName+"(ID)",
"FOREIGN KEY(QuotedInfoID) REFERENCES "+Sqlite3QuotedInfoTableName+"(ID)",
)
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
err = s.db.Create(Sqlite3TinyInfoTableName, &TinyInfo{})
err = s.db.Create(Sqlite3GuildChannelMessageTableName, &StoredGuildChannelMessage{},
"FOREIGN KEY(AttributeID) REFERENCES "+Sqlite3MessageAttributeTableName+"(ID)",
"FOREIGN KEY(QuotedInfoID) REFERENCES "+Sqlite3QuotedInfoTableName+"(ID)",
)
if err != nil {
return nil, errors.Wrap(err, "create sqlite3 table error")
}
Expand Down

0 comments on commit 006555b

Please sign in to comment.