Fix sync errors: string move_id, FK constraints on connection pool
API returns move_id as string ("Season-013-Week-11-1772073335"), not
i64. Also disable foreign_keys at pool level via SqliteConnectOptions
since transactions reference players/teams that may not exist locally.
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
a18c0431d1
commit
60b397b529
@ -185,7 +185,6 @@ pub async fn sync_transactions(
|
|||||||
continue;
|
continue;
|
||||||
};
|
};
|
||||||
|
|
||||||
let move_id_str = move_id.to_string();
|
|
||||||
let player_id = player.id;
|
let player_id = player.id;
|
||||||
// Schema has from_team_id/to_team_id as NOT NULL; use 0 as sentinel for missing teams
|
// Schema has from_team_id/to_team_id as NOT NULL; use 0 as sentinel for missing teams
|
||||||
let from_team_id = data.oldteam.map(|t| t.id).unwrap_or(0);
|
let from_team_id = data.oldteam.map(|t| t.id).unwrap_or(0);
|
||||||
@ -206,7 +205,7 @@ pub async fn sync_transactions(
|
|||||||
)
|
)
|
||||||
.bind(season)
|
.bind(season)
|
||||||
.bind(week)
|
.bind(week)
|
||||||
.bind(move_id_str)
|
.bind(&move_id)
|
||||||
.bind(player_id)
|
.bind(player_id)
|
||||||
.bind(from_team_id)
|
.bind(from_team_id)
|
||||||
.bind(to_team_id)
|
.bind(to_team_id)
|
||||||
|
|||||||
@ -147,9 +147,9 @@ pub struct TransactionsResponse {
|
|||||||
|
|
||||||
#[derive(Debug, Deserialize)]
|
#[derive(Debug, Deserialize)]
|
||||||
pub struct TransactionData {
|
pub struct TransactionData {
|
||||||
/// Transaction move ID — API field is "moveid".
|
/// Transaction move ID — API field is "moveid" (string like "Season-013-Week-11-1772073335").
|
||||||
#[serde(rename = "moveid", default)]
|
#[serde(rename = "moveid", default)]
|
||||||
pub move_id: Option<i64>,
|
pub move_id: Option<String>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub week: Option<i64>,
|
pub week: Option<i64>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
|||||||
@ -7,7 +7,8 @@ pub async fn init_pool(db_path: &Path) -> Result<SqlitePool> {
|
|||||||
let db_url = format!("sqlite://{}?mode=rwc", db_path.display());
|
let db_url = format!("sqlite://{}?mode=rwc", db_path.display());
|
||||||
let options = SqliteConnectOptions::from_str(&db_url)?
|
let options = SqliteConnectOptions::from_str(&db_url)?
|
||||||
.create_if_missing(true)
|
.create_if_missing(true)
|
||||||
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal);
|
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal)
|
||||||
|
.foreign_keys(false);
|
||||||
|
|
||||||
let pool = SqlitePoolOptions::new()
|
let pool = SqlitePoolOptions::new()
|
||||||
.max_connections(5)
|
.max_connections(5)
|
||||||
@ -18,10 +19,6 @@ pub async fn init_pool(db_path: &Path) -> Result<SqlitePool> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub async fn create_tables(pool: &SqlitePool) -> Result<()> {
|
pub async fn create_tables(pool: &SqlitePool) -> Result<()> {
|
||||||
sqlx::query("PRAGMA foreign_keys = ON")
|
|
||||||
.execute(pool)
|
|
||||||
.await?;
|
|
||||||
|
|
||||||
// 1. teams — API-provided PKs (no autoincrement)
|
// 1. teams — API-provided PKs (no autoincrement)
|
||||||
sqlx::query(
|
sqlx::query(
|
||||||
"CREATE TABLE IF NOT EXISTS teams (
|
"CREATE TABLE IF NOT EXISTS teams (
|
||||||
@ -175,9 +172,9 @@ pub async fn create_tables(pool: &SqlitePool) -> Result<()> {
|
|||||||
season INTEGER NOT NULL,
|
season INTEGER NOT NULL,
|
||||||
week INTEGER NOT NULL,
|
week INTEGER NOT NULL,
|
||||||
move_id TEXT NOT NULL,
|
move_id TEXT NOT NULL,
|
||||||
player_id INTEGER NOT NULL REFERENCES players(id),
|
player_id INTEGER NOT NULL,
|
||||||
from_team_id INTEGER NOT NULL REFERENCES teams(id),
|
from_team_id INTEGER NOT NULL,
|
||||||
to_team_id INTEGER NOT NULL REFERENCES teams(id),
|
to_team_id INTEGER NOT NULL,
|
||||||
cancelled INTEGER DEFAULT 0,
|
cancelled INTEGER DEFAULT 0,
|
||||||
frozen INTEGER DEFAULT 0,
|
frozen INTEGER DEFAULT 0,
|
||||||
synced_at TEXT,
|
synced_at TEXT,
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user