feat: add chapter eight call records

This commit is contained in:
richjohnrambo
2026-08-13 16:14:57 +08:00
parent 92d0066a02
commit dfcccbb761
9 changed files with 711 additions and 20 deletions
+124
View File
@@ -57,6 +57,29 @@ func main() {
CONSTRAINT fk_im_tenant_config_tenant FOREIGN KEY(tenant_id) REFERENCES hotgo.im_tenant(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci`,
"CREATE DATABASE IF NOT EXISTS tinode CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci",
`CREATE TABLE IF NOT EXISTS tinode.im_call_records(
id BIGINT NOT NULL AUTO_INCREMENT,
call_id VARCHAR(64) NOT NULL,
topic CHAR(25) NOT NULL,
seq_id INT NOT NULL,
caller_id BIGINT NOT NULL,
callee_id BIGINT NOT NULL,
call_type VARCHAR(16) NOT NULL,
status VARCHAR(32) NOT NULL,
end_reason VARCHAR(64) NOT NULL DEFAULT '',
duration_ms BIGINT NOT NULL DEFAULT 0,
started_at DATETIME(3) NOT NULL,
accepted_at DATETIME(3),
finished_at DATETIME(3),
created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
updated_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3) ON UPDATE CURRENT_TIMESTAMP(3),
PRIMARY KEY(id),
UNIQUE INDEX uk_im_call_records_call(call_id),
UNIQUE INDEX uk_im_call_records_topic_seq(topic, seq_id),
INDEX idx_im_call_records_caller(caller_id, id DESC),
INDEX idx_im_call_records_callee(callee_id, id DESC),
INDEX idx_im_call_records_status(status, updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci`,
}
for _, stmt := range statements {
if _, err = db.Exec(stmt); err != nil {
@@ -88,6 +111,9 @@ func main() {
if _, err = db.Exec("UPDATE tinode.im_tenant SET db_name='tinode' WHERE code='default' AND (db_name='' OR db_name IS NULL)"); err != nil {
fatal(err)
}
if err = dropTenantIDColumns(db, "tinode"); err != nil {
fatal(err)
}
var code, dbName string
var state int
@@ -101,9 +127,107 @@ func main() {
fatal(err)
}
fmt.Printf("tinode.im_tenant routing columns: %d/3\n", columnCount)
rows, err := db.Query(`SELECT table_name FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA='tinode' AND COLUMN_NAME='tenant_id'
ORDER BY table_name`)
if err != nil {
fatal(err)
}
defer rows.Close()
var tables []string
for rows.Next() {
var table string
if err = rows.Scan(&table); err != nil {
fatal(err)
}
tables = append(tables, table)
}
if err = rows.Err(); err != nil {
fatal(err)
}
if len(tables) == 0 {
fmt.Println("tinode tenant_id columns: none")
} else {
fmt.Printf("tinode tenant_id columns: %s\n", strings.Join(tables, ","))
}
}
func fatal(v any) {
fmt.Fprintln(os.Stderr, v)
os.Exit(1)
}
func dropTenantIDColumns(db *sql.DB, schema string) error {
fks, err := queryPairs(db, `SELECT DISTINCT table_name, constraint_name
FROM information_schema.KEY_COLUMN_USAGE
WHERE TABLE_SCHEMA=? AND COLUMN_NAME='tenant_id' AND REFERENCED_TABLE_NAME IS NOT NULL`,
schema)
if err != nil {
return err
}
for _, fk := range fks {
if _, err = db.Exec("ALTER TABLE " + quoteIdent(schema) + "." + quoteIdent(fk[0]) + " DROP FOREIGN KEY " + quoteIdent(fk[1])); err != nil {
return fmt.Errorf("drop foreign key %s.%s: %w", fk[0], fk[1], err)
}
}
indexes, err := queryPairs(db, `SELECT DISTINCT table_name, index_name
FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA=? AND COLUMN_NAME='tenant_id' AND INDEX_NAME<>'PRIMARY'`,
schema)
if err != nil {
return err
}
for _, idx := range indexes {
if _, err = db.Exec("ALTER TABLE " + quoteIdent(schema) + "." + quoteIdent(idx[0]) + " DROP INDEX " + quoteIdent(idx[1])); err != nil {
return fmt.Errorf("drop index %s.%s: %w", idx[0], idx[1], err)
}
}
rows, err := db.Query(`SELECT table_name FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA=? AND COLUMN_NAME='tenant_id'
ORDER BY table_name`, schema)
if err != nil {
return err
}
defer rows.Close()
var tables []string
for rows.Next() {
var table string
if err = rows.Scan(&table); err != nil {
return err
}
tables = append(tables, table)
}
if err = rows.Err(); err != nil {
return err
}
for _, table := range tables {
if _, err = db.Exec("ALTER TABLE " + quoteIdent(schema) + "." + quoteIdent(table) + " DROP COLUMN tenant_id"); err != nil {
return fmt.Errorf("drop column %s.tenant_id: %w", table, err)
}
}
return nil
}
func queryPairs(db *sql.DB, query string, args ...any) ([][2]string, error) {
rows, err := db.Query(query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var pairs [][2]string
for rows.Next() {
var first, second string
if err = rows.Scan(&first, &second); err != nil {
return nil, err
}
pairs = append(pairs, [2]string{first, second})
}
return pairs, rows.Err()
}
func quoteIdent(name string) string {
return "`" + strings.ReplaceAll(name, "`", "``") + "`"
}