Explorer

definy.store-seed-statements

description
The SQL that replaces the seed in the store: delete the events in the time range of the old seed (the first second of 2019-01-31T04:36:01Z) not in the bundle, insert the events and contents, and record the fingerprint last. Other events (also those of accounts in the bundle) are kept (docs/source-in-store.md)
type
Fn(bundle: @definy.seed-bundle) -> List<@definy.sql-statement>
expression
fn(bundle$0) =>
  let hashes$1 = @core.list-map(
      T: @definy.seed-event,
      U: String,
      f: fn(item$2) =>
        $2.event_hash,
      xs: $0.events
    );
  let not_in$5 = @core.string-concat(
      left: "DELETE FROM events WHERE time_ms < 1548909362000 AND event_hash NOT IN (",
      right: @core.string-concat(left: @definy.sql-placeholders(count: @core.list-length(value: $1)), right: ")")
    );
  let deletes$6 = match @core.equal(left: @core.list-length(value: $1), right: 0) {
      true =>
        [],
      false =>
        [
          {
            sql: $5,
            params: @core.list-map(
              T: String,
              U: @definy.sql-value,
              f: fn(item$8) =>
                #text<@definy.sql-value>($8),
              xs: $1
            )
          }
        ]
    };
  let events$9 = @core.list-map(
      T: @definy.seed-event,
      U: @definy.sql-statement,
      f: fn(item$10) =>
        {
          sql: "INSERT OR IGNORE INTO events (event_hash, signed_event_bytes, account_id, event_type, created_at_rfc3339, time_ms) VALUES (?, ?, ?, ?, ?, ?)",
          params: [
            #text<@definy.sql-value>($10.event_hash),
            #text<@definy.sql-value>($10.signed_event_bytes),
            #text<@definy.sql-value>($10.account_id),
            #text<@definy.sql-value>($10.event_type),
            #text<@definy.sql-value>($10.created_at_rfc3339),
            #integer<@definy.sql-value>($10.time_ms)
          ]
        },
      xs: $0.events
    );
  let contents$11 = @core.list-map(
      T: @definy.content-item,
      U: @definy.sql-statement,
      f: fn(item$12) =>
        {
          sql: "INSERT OR REPLACE INTO cas_contents (content_hash, content_bytes) VALUES (?, ?)",
          params: [#text<@definy.sql-value>($12.content_hash), #text<@definy.sql-value>($12.content_bytes)]
        },
      xs: $0.contents
    );
  @core.list-concat(
    left: $6,
    right: @core.list-concat(
      left: $9,
      right: @core.list-append(
        list: $11,
        item: {
          sql: "INSERT OR REPLACE INTO cas_contents (content_hash, content_bytes) VALUES (?, ?)",
          params: [#text<@definy.sql-value>(@definy.seed-fingerprint-key), #text<@definy.sql-value>($0.fingerprint)]
        }
      )
    )
  )
visibility
public
definy.store-seed-statements (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: CeB3zfqG_K14BeS3yG9BtrEXgmfPfOw-Fp9k0P_4Bak
content hash: aUuCua3Hh2j_Bjrr0ZqZHuMbWbgVrPrHp7n1Qbh1MdA