• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

knex / knex / 3980053368

pending completion
3980053368

Pull #5459

github

GitHub
Merge 6d89bfd2f into 5caf526c2
Pull Request #5459: Additional lint checks before publishing

4186 of 4984 branches covered (83.99%)

Branch coverage included in aggregate %.

11 of 11 new or added lines in 7 files covered. (100.0%)

19749 of 20922 relevant lines covered (94.39%)

1379.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.86
/lib/schema/builder.js
1
const { EventEmitter } = require('events');
63✔
2
const toArray = require('lodash/toArray');
63✔
3
const assign = require('lodash/assign');
63✔
4
const { addQueryContext } = require('../util/helpers');
63✔
5
const saveAsyncStack = require('../util/save-async-stack');
63✔
6
const {
7
  augmentWithBuilderInterface,
8
} = require('../builder-interface-augmenter');
63✔
9

10
// Constructor for the builder instance, typically called from
11
// `knex.builder`, accepting the current `knex` instance,
12
// and pulling out the `client` and `grammar` from the current
13
// knex instance.
14
class SchemaBuilder extends EventEmitter {
15
  constructor(client) {
16
    super();
7,765✔
17
    this.client = client;
7,765✔
18
    this._sequence = [];
7,765✔
19

20
    if (client.config) {
7,765!
21
      this._debug = client.config.debug;
7,765✔
22
      saveAsyncStack(this, 4);
7,765✔
23
    }
24
  }
25

26
  withSchema(schemaName) {
27
    this._schema = schemaName;
31✔
28
    return this;
31✔
29
  }
30

31
  toString() {
32
    return this.toQuery();
28✔
33
  }
34

35
  toSQL() {
36
    return this.client.schemaCompiler(this).toSQL();
7,907✔
37
  }
38

39
  async generateDdlCommands() {
40
    return await this.client.schemaCompiler(this).generateDdlCommands();
15✔
41
  }
42
}
43

44
// Each of the schema builder methods just add to the
45
// "_sequence" array for consistency.
46
[
63✔
47
  'createTable',
48
  'createTableIfNotExists',
49
  'createTableLike',
50
  'createView',
51
  'createViewOrReplace',
52
  'createMaterializedView',
53
  'refreshMaterializedView',
54
  'dropView',
55
  'dropViewIfExists',
56
  'dropMaterializedView',
57
  'dropMaterializedViewIfExists',
58
  'createSchema',
59
  'createSchemaIfNotExists',
60
  'dropSchema',
61
  'dropSchemaIfExists',
62
  'createExtension',
63
  'createExtensionIfNotExists',
64
  'dropExtension',
65
  'dropExtensionIfExists',
66
  'table',
67
  'alterTable',
68
  'view',
69
  'alterView',
70
  'hasTable',
71
  'hasColumn',
72
  'dropTable',
73
  'renameTable',
74
  'renameView',
75
  'dropTableIfExists',
76
  'raw',
77
].forEach(function (method) {
78
  SchemaBuilder.prototype[method] = function () {
1,890✔
79
    if (method === 'createTableIfNotExists') {
8,187✔
80
      this.client.logger.warn(
4✔
81
        [
82
          'Use async .hasTable to check if table exists and then use plain .createTable. Since ',
83
          '.createTableIfNotExists actually just generates plain "CREATE TABLE IF NOT EXIST..." ',
84
          'query it will not work correctly if there are any alter table queries generated for ',
85
          'columns afterwards. To not break old migrations this function is left untouched for now',
86
          ', but it should not be used when writing new code and it is removed from documentation.',
87
        ].join('')
88
      );
89
    }
90
    if (method === 'table') method = 'alterTable';
8,187✔
91
    if (method === 'view') method = 'alterView';
8,187✔
92
    this._sequence.push({
8,187✔
93
      method,
94
      args: toArray(arguments),
95
    });
96
    return this;
8,187✔
97
  };
98
});
99

100
SchemaBuilder.extend = (methodName, fn) => {
63✔
101
  if (
1!
102
    Object.prototype.hasOwnProperty.call(SchemaBuilder.prototype, methodName)
103
  ) {
104
    throw new Error(
×
105
      `Can't extend SchemaBuilder with existing method ('${methodName}').`
106
    );
107
  }
108

109
  assign(SchemaBuilder.prototype, { [methodName]: fn });
1✔
110
};
111

112
augmentWithBuilderInterface(SchemaBuilder);
63✔
113
addQueryContext(SchemaBuilder);
63✔
114

115
module.exports = SchemaBuilder;
63✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc