Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for querying sqlite_master table #9645

Merged
merged 4 commits into from Jul 14, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/dialects/sqlite/query-interface.js
Expand Up @@ -3,6 +3,7 @@
const _ = require('lodash');
const Promise = require('../../promise');
const sequelizeErrors = require('../../errors');
const QueryTypes = require('../../query-types');

/**
Returns an object that treats SQLite's inabilities to do certain queries.
Expand Down Expand Up @@ -152,7 +153,7 @@ function addConstraint(tableName, options) {
const describeCreateTableSql = this.QueryGenerator.describeCreateTableQuery(tableName);
let createTableSql;

return this.sequelize.query(describeCreateTableSql, options)
return this.sequelize.query(describeCreateTableSql, _.assign({}, options, { type: QueryTypes.SELECT, raw: true }))
.then(constraints => {
const sql = constraints[0].sql;
const index = sql.length - 1;
Expand Down
14 changes: 6 additions & 8 deletions lib/dialects/sqlite/query.js
Expand Up @@ -144,14 +144,12 @@ class Query extends AbstractQuery {
}
}

if (query.sql.indexOf('sqlite_master') !== -1) {
if (query.sql.indexOf('SELECT sql FROM sqlite_master WHERE tbl_name') !== -1) {
result = results;
if (result && result[0] && result[0].sql.indexOf('CONSTRAINT') !== -1) {
result = query.parseConstraintsFromSql(results[0].sql);
}
} else {
result = results.map(resultSet => resultSet.name);
if (query.isShowTablesQuery()) {
result = results.map(row => row.name);
} else if (query.isShowConstraintsQuery()) {
result = results;
if (results && results[0] && results[0].sql) {
result = query.parseConstraintsFromSql(results[0].sql);
}
} else if (query.isSelectQuery()) {
if (!query.options.raw) {
Expand Down
5 changes: 4 additions & 1 deletion test/integration/data-types.test.js
Expand Up @@ -639,7 +639,10 @@ describe(Support.getTestDialectTeaser('DataTypes'), () => {
return record.reload();
}).then(record => {
expect(typeof record.stamp).to.be.eql('string');
expect(new Date(record.stamp)).to.equalDate(newDate);
const recordDate = new Date(record.stamp);
expect(recordDate.getUTCFullYear()).to.equal(newDate.getUTCFullYear());
expect(recordDate.getUTCDate()).to.equal(newDate.getUTCDate());
expect(recordDate.getUTCMonth()).to.equal(newDate.getUTCMonth());
});
});

Expand Down
61 changes: 61 additions & 0 deletions test/integration/dialects/sqlite/sqlite-master.test.js
@@ -0,0 +1,61 @@
'use strict';

const chai = require('chai'),
expect = chai.expect,
Support = require(__dirname + '/../../support'),
dialect = Support.getTestDialect(),
DataTypes = require(__dirname + '/../../../../lib/data-types');

if (dialect === 'sqlite') {
describe('[SQLITE Specific] sqlite_master raw queries', () => {
beforeEach(function() {
this.sequelize.define('SomeTable', {
someColumn: DataTypes.INTEGER
}, {
freezeTableName: true,
timestamps: false
});

return this.sequelize.sync({ force: true });
});

it('should be able to select with tbl_name filter', function() {
return this.sequelize.query('SELECT * FROM sqlite_master WHERE tbl_name=\'SomeTable\'')
.then(result => {
const rows = result[0];
expect(rows).to.have.length(1);
const row = rows[0];
expect(row).to.have.property('type', 'table');
expect(row).to.have.property('name', 'SomeTable');
expect(row).to.have.property('tbl_name', 'SomeTable');
expect(row).to.have.property('sql');
});
});

it('should be able to select *', function() {
return this.sequelize.query('SELECT * FROM sqlite_master')
.then(result => {
const rows = result[0];
expect(rows).to.have.length(2);
rows.forEach(row => {
expect(row).to.have.property('type');
expect(row).to.have.property('name');
expect(row).to.have.property('tbl_name');
expect(row).to.have.property('rootpage');
expect(row).to.have.property('sql');
});
});
});

it('should be able to select just "sql" column and get rows back', function() {
return this.sequelize.query('SELECT sql FROM sqlite_master WHERE tbl_name=\'SomeTable\'')
.then(result => {
const rows = result[0];
expect(rows).to.have.length(1);
const row = rows[0];
expect(row).to.have.property('sql',
'CREATE TABLE `SomeTable` (`id` INTEGER PRIMARY KEY AUTOINCREMENT, `someColumn` INTEGER)');
});
});
});
}