KScript docs
EN

What changed

Ownable rows and file users

Two changes in 0.35 move data, so an app upgrading from 0.33 or 0.34 needs a migration for each.

Ownable keeps a user and a guest

A model that extends Ownable used to keep its owner as text in owner, such as 'user:12' or 'guest:7db08f23-…'. It has a user and a guest relation now, stored in user_id and guest_id. Run kscript db diff: it sees owner go and the two fields come, and writes a migration that copies each row's owner across in a Run block before it drops owner, with a Down block that puts it back:

<Migration>
  <AddField table="notes" name="user_id" type="int" optional="yes" references="users" on_delete="cascade" />
  <AddField table="notes" name="guest_id" type="uuid" optional="yes" references="guests" on_delete="cascade" />
  <Run>
    <for each="row" in="@db('notes')->get()">
      <let name="owner" value="$row.owner ?? ''" />
      <if $owner->starts_with('user:')>
        <do>@db('notes')->where(id: $row.id)->update(user_id: $owner->replace('user:', '')->to_int())</do>
        <continue />
      </if>
      <if $owner->starts_with('guest:')>
        <let name="guest" value="$owner->replace('guest:', '')" />
        <if @db('guests')->where(id: $guest)->count() == 0>
          <do>@db('guests')->insert({ id: $guest, created_at: @now(), updated_at: @now() })</do>
        </if>
        <do>@db('notes')->where(id: $row.id)->update(guest_id: $guest)</do>
      </if>
    </for>
  </Run>
  <DropField table="notes" name="owner" />
</Migration>
Migration {
  AddField(table: 'notes', name: 'user_id', type: int, optional, references: 'users', on_delete: cascade)
  AddField(table: 'notes', name: 'guest_id', type: uuid, optional, references: 'guests', on_delete: cascade)
  Run {
    for row in @db('notes')->get() {
      $owner = $row.owner ?? ''
      if $owner->starts_with('user:') {
        @db('notes')->where(id: $row.id)->update(user_id: $owner->replace('user:', '')->to_int())
        continue
      }
      if $owner->starts_with('guest:') {
        $guest = $owner->replace('guest:', '')
        if @db('guests')->where(id: $guest)->count() == 0 {
          @db('guests')->insert({ id: $guest, created_at: @now(), updated_at: @now() })
        }
        @db('notes')->where(id: $row.id)->update(guest_id: $guest)
      }
    }
  }
  DropField(table: 'notes', name: 'owner')
}

Each guest named in owner gets a row in guests, so their rows stay theirs. A user's id must be a row in users, so move file users first, as below.

Users move into the User model

Ownable needs "users": "User" in http.auth, which keeps users in the users table. Users who signed up before that are JSON files in the app's data folder, and @file_users() reads them, each with its id, password_hash and dates.

File users have uuid ids, and the built-in User has a number key. Publish it with kscript publish User and give it a uuid key, so sessions, API tokens and owners still find the same people:

<import package="KScript.Data" />

<model name="User" auto_create="yes" unique="[email]" hidden="[password]" with="Timestamps">
  <field name="id" type="uuid" default="@uuid()" />
  <field name="name" type="string" />
  <field name="email" type="email" />
  <field name="password" type="password" />
</model>
import KScript.Data

model User(auto_create, unique: [email], hidden: [password]) extends Timestamps {
  id: uuid = @uuid()
  name: string
  email: email
  password: password
}

Then kscript db diff makes the users table, and kscript make migration import_file_users gives you a migration to copy them across. The password hash is copied as it is, so everyone signs in with the password they had:

<KScript.Migration>
  <import package="KScript.Auth" />

  <Run>
    <for each="account" in="@file_users()">
      <do>@db('users')->insert({
          id: $account.id,
          name: $account.name,
          email: $account.email,
          password: $account.password_hash,
          created_at: $account.created_at,
          updated_at: $account.updated_at,
        })</do>
    </for>
  </Run>
  <Down>
    <Run>
      <do>@db('users')->delete()</do>
    </Run>
  </Down>
</KScript.Migration>
import KScript.Data
import KScript.Auth

Migration {
  Run {
    for account in @file_users() {
      @db('users')->insert({
        id: $account.id,
        name: $account.name,
        email: $account.email,
        password: $account.password_hash,
        created_at: $account.created_at,
        updated_at: $account.updated_at,
      })
    }
  }
  Down {
    Run {
      @db('users')->delete()
    }
  }
}

Give it a name that sorts before the Ownable migration, or write it first, so the users are there when owners point to them. Fields you added to your users go in the insert too, and the files stay where they were until you delete them.