@vida-global/core 2.0.8 → 2.0.9

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -128,11 +128,12 @@ class Migrator {
128
128
  }
129
129
 
130
130
 
131
- async addIndex(tableName, fields, { concurrently, unique, name, where }) {
131
+ async addIndex(tableName, fields, { concurrently, unique, name, using, where }) {
132
132
  const options = {fields: fields};
133
133
  if (concurrently) options.concurrently = true;
134
134
  if (unique) options.unique = true;
135
135
  if (name) options.name = name;
136
+ if (using) options.using = using;
136
137
  if (where) options.where = where;
137
138
 
138
139
  await this.#sequelizeQueryInterface.addIndex(tableName, options);
@@ -81,6 +81,7 @@ function cleanSchema(schema, dialect) {
81
81
  const dataType = dataTypeForColumn(details, dialect);
82
82
  if (dataType) details.type = dataType;
83
83
  resolveAutoIncrementColumns(details);
84
+ resolveTimestampDefaults(details, dialect);
84
85
  resolveUUIDColumns(details);
85
86
  }
86
87
  }
@@ -97,6 +98,15 @@ function resolveAutoIncrementColumns(details) {
97
98
  }
98
99
 
99
100
 
101
+ function resolveTimestampDefaults(details, dialect) {
102
+ if (dialect != 'postgres') return;
103
+ if (details.type != Sequelize.DataTypes.DATE) return;
104
+ if (!/^(now\(\)|CURRENT_TIMESTAMP)$/i.test(details.defaultValue)) return;
105
+
106
+ details.defaultValue = Sequelize.NOW;
107
+ }
108
+
109
+
100
110
  function dataTypeForColumn(details, dialect) {
101
111
  const typeStr = details.type.replace(/\(.*\)/, '').toUpperCase();
102
112
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vida-global/core",
3
- "version": "2.0.8",
3
+ "version": "2.0.9",
4
4
  "description": "Core libraries for supporting Vida development",
5
5
  "author": "",
6
6
  "license": "ISC",
@@ -102,7 +102,7 @@ describe('Migrator', () => {
102
102
 
103
103
  describe('Migrator#addIndex', () => {
104
104
  it.each([
105
- ['all supported options', { concurrently: true, unique: true, where: {}, name: 'foo' }],
105
+ ['all supported options', { concurrently: true, unique: true, where: {}, name: 'foo', using: 'gin' }],
106
106
  ['empty options', {}],
107
107
  ['falsey options', { concurrently: false, unique: false, where: null, name: null }],
108
108
  ])('builds addIndex options for %s', async (_label, options) => {
@@ -114,6 +114,7 @@ describe('Migrator', () => {
114
114
  if (options.unique) expectedOptions.unique = true;
115
115
  if (options.where) expectedOptions.where = options.where;
116
116
  if (options.name) expectedOptions.name = options.name;
117
+ if (options.using) expectedOptions.using = options.using;
117
118
 
118
119
  await Helpers.migrator.addIndex(tableName, fields, options);
119
120
 
@@ -154,6 +154,32 @@ describe('activeRecord/db/schema', () => {
154
154
  expect(schema.id.defaultValue).toBeUndefined();
155
155
  });
156
156
  });
157
+
158
+
159
+ describe('timestamp default resolution', () => {
160
+ it.each(['now()', 'CURRENT_TIMESTAMP'])(
161
+ 'maps PostgreSQL %s defaults to Sequelize.NOW',
162
+ (defaultValue) => {
163
+ importSchema.mockImplementation(() => ({
164
+ created_at: { type: 'TIMESTAMP WITH TIME ZONE', defaultValue }
165
+ }));
166
+
167
+ const schema = getActiveRecordSchema('t', 'main', 'postgres');
168
+
169
+ expect(schema.created_at.defaultValue).toBe(Sequelize.NOW);
170
+ }
171
+ );
172
+
173
+ it ('does not rewrite SQLite timestamp defaults', () => {
174
+ importSchema.mockImplementation(() => ({
175
+ created_at: { type: 'DATETIME', defaultValue: 'CURRENT_TIMESTAMP' }
176
+ }));
177
+
178
+ const schema = getActiveRecordSchema('t', 'main', 'sqlite');
179
+
180
+ expect(schema.created_at.defaultValue).toBe('CURRENT_TIMESTAMP');
181
+ });
182
+ });
157
183
  });
158
184
 
159
185
 
@@ -9,6 +9,7 @@ const tableName2 = TestHelpers.Faker.Text.randomString();
9
9
  const mockQueryInterface = {
10
10
  showAllTables: jest.fn(() => [tableName1, tableName2]),
11
11
  describeTable: jest.fn(tableName => ({ name: tableName })),
12
+ showIndex: jest.fn(),
12
13
  };
13
14
 
14
15