Add a column with a migration
Task: products need a stock count, and the database already exists.
yaml
# Recipe: change the schema with a new migration, never by editing an old one.
paths:
components: ./components
migrations: ./migrations
datasources:
db:
driver: sqlite
database: ./data/shop.dbThe first migration made the table:
sql
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL
);
INSERT INTO products (name) VALUES ('Mug'), ('Sticker');The change is a second file. Migrations run in order, each once, each in its own transaction; an applied migration is never edited:
sql
-- Rows that already exist get the default.
ALTER TABLE products ADD COLUMN stock INTEGER NOT NULL DEFAULT 0;
UPDATE products SET stock = 12 WHERE name = 'Mug';bash
quantum migrate upxml
<q:component name="Products">
<q:query name="products" datasource="db">
SELECT name, stock FROM products ORDER BY name
</q:query>
<ui:window title="Products">
<ui:table source="{products}">
<ui:column key="name" label="Name" />
<ui:column key="stock" label="In stock" />
</ui:table>
</ui:window>
</q:component>Each test starts from a database built by the migrations, so the test sees the schema a new install gets:
xml
<q:test name="both migrations ran, in order" page="/">
<test:visit />
<test:expect text="In stock" />
<test:expect table="products" count="1" where="name = 'Mug' AND stock = 12" />
<test:expect table="products" count="1" where="name = 'Sticker' AND stock = 0" />
</q:test>text
tests/stock.test.q
PASS both migrations ran, in order
1 passed, 0 failedRather write the schema you want and let Quantum write the migration? See quantum migrate plan in Project Structure. See DB-6 and DB-8.