Explorer

definy.ensure-seeded

description
Create the tables if there are none and put the seed in the store if its fingerprint differs (docs/database.md). The host remembers it per isolate (read_seed_status / mark_seeded)
type
Fn(then: Fn(seeded: Boolean) -> @definy.service-io) -> @definy.service-io
expression
fn(then$0) =>
  #read_seed_status<@definy.service-io>({
    then: fn(status$1) =>
      match $1.seeded {
        true =>
          $0(seeded: true),
        false =>
          #sql<@definy.service-io>({
            statements: [
              {
                sql: "SELECT COUNT(*) AS tables FROM sqlite_master WHERE type = 'table' AND name IN ('events', 'cas_contents')",
                params: []
              }
            ],
            then: fn(results$2) =>
              @definy.sql-run-chunks(
                statements: match @core.equal(left: @definy.sql-first-text(results: $2, name: "tables"), right: "2") {
                  true =>
                    [],
                  false =>
                    @definy.store-schema-statements
                },
                start: 0,
                then: fn(done$3) =>
                  #sql<@definy.service-io>({
                    statements: [
                      {
                        sql: "SELECT content_bytes FROM cas_contents WHERE content_hash = ?",
                        params: [#text<@definy.sql-value>(@definy.seed-fingerprint-key)]
                      }
                    ],
                    then: fn(results$4) =>
                      let stored$5 = @definy.sql-first-text(results: $4, name: "content_bytes");
                      match @core.equal(left: $5, right: $1.expected_fingerprint) {
                        true =>
                          @definy.mark-seeded(
                            then: fn(seeded$21) =>
                              $0(seeded: $21)
                          ),
                        false =>
                          #read_seed_bundle<@definy.service-io>({
                            then: fn(bundle$6) =>
                              match $6 {
                                #none(_) =>
                                  @definy.mark-seeded(
                                    then: fn(seeded$22) =>
                                      $0(seeded: $22)
                                  ),
                                #some(bundle$7) =>
                                  match @core.equal(left: $5, right: $7.fingerprint) {
                                    true =>
                                      @definy.mark-seeded(
                                        then: fn(seeded$23) =>
                                          $0(seeded: $23)
                                      ),
                                    false =>
                                      @definy.sql-run-chunks(
                                        statements: @definy.store-seed-statements(bundle: $7),
                                        start: 0,
                                        then: fn(done$8) =>
                                          @definy.mark-seeded(
                                            then: fn(seeded$24) =>
                                              $0(seeded: $24)
                                          )
                                      )
                                  }
                              }
                          })
                      }
                  })
              )
          })
      }
  })
visibility
public
definy.ensure-seeded (project definy, branch main, snapshot l0VFjJZP174d8CbZWFoujAGTYjgsQu4uTq5uWCZaXgo)
id: 2GJW9Dh7WKkUg4Z9EIaLQEJTTgMQEAkUaNnMGzIq-d0
content hash: APNvy2L0DAdSRjHQFBV1W0J0372rAT3FNUuxEX8HJMU