Skip to content

[sqlite3] SQL script files only execute the first command #312

@atopala

Description

@atopala

First of all, thank you for this tool.
I've been successfully using postgrator-cli to manage migrations for both postgres and mssql, and recently encountered an issue with migrations for sqlite3.

Using a migration script like this in sqlite3, postgrator-cli will only execute CREATE TABLE "order"...

CREATE TABLE "order" (
    order_id TEXT PRIMARY KEY DEFAULT (lower(hex(randomblob(4))) || '-' || lower(hex(randomblob(2))) || '-4' || substr(lower(hex(randomblob(2))),2) || '-' || substr('89ab',abs(random()) % 4 + 1, 1) || substr(lower(hex(randomblob(2))),2) || '-' || lower(hex(randomblob(6)))),
    status TEXT NOT NULL DEFAULT 'created' CHECK (status IN ('created', 'paid', 'delivered', 'received')),
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    modified_at TEXT NOT NULL DEFAULT (datetime('now')),
    account_id TEXT NOT NULL,
    FOREIGN KEY (account_id) REFERENCES account (account_id)
);

CREATE TABLE order_item (
    order_id TEXT NOT NULL,
    product_id TEXT NOT NULL,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    modified_at TEXT NOT NULL DEFAULT (datetime('now')),
    product_price REAL NOT NULL,
    discount_price REAL,
    quantity INTEGER NOT NULL,
    metadata TEXT,
    PRIMARY KEY (order_id, product_id),
    FOREIGN KEY (order_id) REFERENCES "order" (order_id),
    FOREIGN KEY (product_id) REFERENCES product (product_id),
    CONSTRAINT order_item_metadata_is_json CHECK (json_valid(metadata))
);

The issue seems to be that sqlite3.js doesn't support actual script execution, and relies on sqlite3 all() - and sqlite3 driver only runs the first command.

I'd be happy to help with this fix.

Probably it's also a good idea to switch to better-sqlite3 in long term.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions