Skip to content

Commit

Permalink
[Sqlite3] Fix can not get table sql because of case sensitive (#5509)
Browse files Browse the repository at this point in the history
Co-authored-by: Olivier Cavadenti <olivier.cavadenti@gmail.com>
  • Loading branch information
zydmayday and OlivierCavadenti committed Mar 30, 2023
1 parent 13936f2 commit ab1f9ae
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ function renameTable(tableName, alteredName) {
}

function getTableSql(tableName) {
return `SELECT type, sql FROM sqlite_master WHERE (type='table' OR (type='index' AND sql IS NOT NULL)) AND tbl_name='${tableName}'`;
return `SELECT type, sql FROM sqlite_master WHERE (type='table' OR (type='index' AND sql IS NOT NULL)) AND lower(tbl_name)='${tableName.toLowerCase()}'`;
}

function isForeignCheckEnabled() {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,8 @@
],
"exclude": [
"lib/dialects/oracle",
"lib/dialects/oracledb"
"lib/dialects/oracledb",
"test/**/*.spec.js"
]
},
"tsd": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
exports.up = async (knex) => {
await knex.schema.createTableIfNotExists('some_table', (table) => {
table.increments();
});
};

exports.down = async (knex) => {
await knex.schema.dropTableIfExists('some_table');
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
exports.up = async (knex) => {
// Use the same table but different case.
await knex.schema.table('Some_Table', (table) => {
table.string('other_id').after('id');
table.string('name').after('other_id');
});
};

exports.down = async (knex) => {
await knex.schema.table('Some_Table', (table) => {
table.dropColumns('other_id', 'name');
});
};
25 changes: 25 additions & 0 deletions test/integration2/migrate/migration-integration.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,31 @@ describe('Migrations', function () {
await db.migrate.rollback();
await knexInstance.destroy();
});

it('should not fail alter-table-ignore-case-sensitive', async () => {
await knex.migrate.latest({
directory:
'test/integration2/migrate/alter-table-ignore-case-sensitive',
});
expect(await knex.schema.hasColumn('Some_Table', 'id')).to.be.true;
expect(await knex.schema.hasColumn('Some_Table', 'other_id')).to.be
.true;
expect(await knex.schema.hasColumn('Some_Table', 'name')).to.be
.true;
// try different case
expect(await knex.schema.hasColumn('some_table', 'id')).to.be.true;
expect(await knex.schema.hasColumn('some_table', 'other_id')).to.be
.true;
expect(await knex.schema.hasColumn('some_table', 'name')).to.be
.true;

await knex.migrate.rollback({
directory:
'test/integration2/migrate/alter-table-ignore-case-sensitive',
});
expect(await knex.schema.hasTable('Some_Table')).to.be.false;
expect(await knex.schema.hasTable('some_table')).to.be.false;
});
}

it('should not fail drop-and-recreate-column operation when using async/await', () => {
Expand Down

0 comments on commit ab1f9ae

Please sign in to comment.