Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions src/drivers/sqlite/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ interface ColumnDefinition {
[columnName: string]: any;
}

function versionAtLeast(version: string, minimum: string): boolean {
const actual = version.split('.').map(Number);
const required = minimum.split('.').map(Number);
for (let index = 0; index < required.length; index++) {
const difference = (actual[index] || 0) - required[index];
if (difference !== 0) return difference > 0;
}
return true;
}

class SqliteDriver extends AbstractDriver {
static Spellbook = Spellbook;
static Attribute = Attribute;
Expand Down Expand Up @@ -149,6 +159,12 @@ class SqliteDriver extends AbstractDriver {
await this.query(chunks.join(' '), [], opts);
}

private async supportsDropColumn(): Promise<boolean> {
const { rows = [] } = await this.query('SELECT sqlite_version() AS version');
const version = String((rows[0] as any)?.version || '0');
return versionAtLeast(version, '3.35.0');
}

async alterTable(table: string, changes: ColumnDefinition): Promise<void> {
const chunks = [`ALTER TABLE ${escapeId(table)}`];
const attributes = Object.keys(changes).map((name) => {
Expand All @@ -157,8 +173,14 @@ class SqliteDriver extends AbstractDriver {
return new (this.constructor as typeof SqliteDriver).Attribute(name, changes[name]);
});

// SQLite doesn't support altering column attributes with MODIFY COLUMN and adding a PRIMARY KEY column
if (attributes.some((entry: any) => entry.modify || entry.primaryKey)) {
// SQLite doesn't support altering column attributes with MODIFY COLUMN and adding a PRIMARY KEY column.
// DROP COLUMN was only added in SQLite 3.35.0; sqlite3@5.0.2, our minimum supported peer,
// bundles SQLite 3.34.0.
const removesColumn = attributes.some((entry: any) => entry.remove);
if (
attributes.some((entry: any) => entry.modify || entry.primaryKey)
|| (removesColumn && !(await this.supportsDropColumn()))
) {
await alterTableWithChangeColumn(this, table, attributes);
return;
}
Expand Down
41 changes: 41 additions & 0 deletions test/integration/sqlite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,47 @@ describe('=> Table definitions (sqlite)', () => {
public: { dataType: 'integer', primaryKey: false },
});
});

it('should detect DROP COLUMN support from the SQLite version', async () => {
const query = sinon.stub(Bone.driver, 'query');
query.onCall(0).resolves({ rows: [{ version: '3.34.0' }] });
query.onCall(1).resolves({ rows: [{ version: '3.35.0' }] });
query.onCall(2).resolves({ rows: [{ version: '4.0.0' }] });
query.onCall(3).resolves({});

try {
assert.equal(await Bone.driver.supportsDropColumn(), false);
assert.equal(await Bone.driver.supportsDropColumn(), true);
assert.equal(await Bone.driver.supportsDropColumn(), true);
assert.equal(await Bone.driver.supportsDropColumn(), false);
sinon.assert.alwaysCalledWithExactly(query, 'SELECT sqlite_version() AS version');
} finally {
query.restore();
}
});

it('should rebuild the table when DROP COLUMN is unavailable', async () => {
const { STRING } = Bone.DataTypes;
class Before extends Bone {}
Before.init({ title: STRING, obsolete: STRING }, { tableName: 'notes', timestamps: false });
await Before.sync();
await Before.create({ title: 'kept', obsolete: 'removed' });

class After extends Bone {}
After.init({ title: STRING }, { tableName: 'notes', timestamps: false });
const supportsDropColumn = sinon.stub(Bone.driver, 'supportsDropColumn').resolves(false);
try {
await After.sync({ alter: true });
} finally {
supportsDropColumn.restore();
}

await checkDefinitions('notes', {
title: { dataType: 'varchar' },
obsolete: null,
});
assert.equal((await After.first).title, 'kept');
});
});

describe('=> upsert (sqlite)', function () {
Expand Down