@autofleet/sadot 0.8.4 → 0.8.5

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.
@@ -6,6 +6,9 @@ import ContextAwareTestModel from './tests/contextAwareModels/ContextAwareTestMo
6
6
  import ContextTestModel from './tests/contextAwareModels/ContextTestModel';
7
7
  import AssociatedTestModel from './tests/AssociatedTestModel';
8
8
  import type { CustomFieldOptions } from '../types';
9
- declare const initTables: (sequelize: Sequelize, getUser: CustomFieldOptions['getUser']) => Promise<void>;
9
+ declare const initTables: (sequelize: Sequelize, getUser: CustomFieldOptions['getUser'], { schemaPrefix, schemaVersion }?: {
10
+ schemaPrefix: string;
11
+ schemaVersion: string;
12
+ }) => Promise<void>;
10
13
  declare const initTestModels: (sequelize: Sequelize) => Promise<void>;
11
14
  export { CustomFieldValue, CustomFieldDefinition, TestModel, AssociatedTestModel, ContextAwareTestModel, ContextTestModel, initTables, initTestModels, };
@@ -23,8 +23,8 @@ const productionModels = [CustomFieldDefinition_1.default, CustomFieldValue_1.de
23
23
  const testModels = [TestModel_1.default, AssociatedTestModel_1.default, ContextAwareTestModel_1.default, ContextTestModel_1.default];
24
24
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
25
25
  const SCHEMA_VERSION = 'fb0fa867-1241-4816-b08d-5ed9060c7ae5';
26
- const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
27
- const initTables = async (sequelize, getUser) => {
26
+ const initTables = async (sequelize, getUser, { schemaPrefix, schemaVersion } = { schemaPrefix: SADOT_MIGRATION_PREFIX, schemaVersion: SCHEMA_VERSION }) => {
27
+ const CUSTOM_FIELDS_SCHEMA_VERSION = `${schemaPrefix}_${schemaVersion}`;
28
28
  logger_1.default.info('custom-fields: initialize custom-fields tables');
29
29
  // Detect models and import them to the orm
30
30
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
@@ -61,14 +61,22 @@ const initTables = async (sequelize, getUser) => {
61
61
  timestamps: false,
62
62
  schema: 'public',
63
63
  });
64
- const migrations = await SequelizeMeta.findAll({ raw: true });
65
- const currentSadotSchemaVersion = migrations.reverse().find((m) => m.name.includes(SADOT_MIGRATION_PREFIX));
66
- if (!currentSadotSchemaVersion
67
- || currentSadotSchemaVersion.name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
64
+ const migrations = await SequelizeMeta.findAll({ where: { name: { [sequelize_1.Op.like]: `${schemaPrefix}%` } }, raw: true });
65
+ const currentSadotSchemaVersion = migrations.at(-1);
66
+ const expectedSchemaVersionIndex = migrations.findIndex((m) => m.name === CUSTOM_FIELDS_SCHEMA_VERSION);
67
+ if (!currentSadotSchemaVersion || currentSadotSchemaVersion.name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
68
68
  await CustomFieldDefinition_1.default.sync({ alter: true });
69
69
  await CustomFieldValue_1.default.sync({ alter: true });
70
- await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
70
+ if (expectedSchemaVersionIndex === -1) {
71
+ await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
72
+ }
71
73
  logger_1.default.info('custom-fields: models synced');
74
+ if (migrations.length && expectedSchemaVersionIndex !== -1 && expectedSchemaVersionIndex < migrations.length - 1) {
75
+ // We have existing migrations, and we are calling `sync`.
76
+ // This means we are in a `down` migration, and hence we should delete newer migrations to ensure we can reapply them.
77
+ const migrationsToDelete = migrations.slice(expectedSchemaVersionIndex + 1);
78
+ await SequelizeMeta.destroy({ where: { name: { [sequelize_1.Op.in]: migrationsToDelete.map((m) => m.name) } } });
79
+ }
72
80
  }
73
81
  };
74
82
  exports.initTables = initTables;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autofleet/sadot",
3
- "version": "0.8.4",
3
+ "version": "0.8.5",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
@@ -1,5 +1,5 @@
1
1
  /* eslint-disable no-param-reassign */
2
- import { DataTypes } from 'sequelize';
2
+ import { DataTypes, Op } from 'sequelize';
3
3
  import type { Sequelize } from 'sequelize-typescript';
4
4
  import logger from '../utils/logger';
5
5
  import CustomFieldDefinition from './CustomFieldDefinition';
@@ -15,9 +15,13 @@ const testModels = [TestModel, AssociatedTestModel, ContextAwareTestModel, Conte
15
15
 
16
16
  const SADOT_MIGRATION_PREFIX = 'sadot-migration';
17
17
  const SCHEMA_VERSION = 'fb0fa867-1241-4816-b08d-5ed9060c7ae5';
18
- const CUSTOM_FIELDS_SCHEMA_VERSION = `${SADOT_MIGRATION_PREFIX}_${SCHEMA_VERSION}`;
19
18
 
20
- const initTables = async (sequelize: Sequelize, getUser: CustomFieldOptions['getUser']): Promise<void> => {
19
+ const initTables = async (
20
+ sequelize: Sequelize,
21
+ getUser: CustomFieldOptions['getUser'],
22
+ { schemaPrefix, schemaVersion } = { schemaPrefix: SADOT_MIGRATION_PREFIX, schemaVersion: SCHEMA_VERSION },
23
+ ): Promise<void> => {
24
+ const CUSTOM_FIELDS_SCHEMA_VERSION = `${schemaPrefix}_${schemaVersion}`;
21
25
  logger.info('custom-fields: initialize custom-fields tables');
22
26
  // Detect models and import them to the orm
23
27
  // eslint-disable-next-line @typescript-eslint/explicit-function-return-type
@@ -61,17 +65,24 @@ const initTables = async (sequelize: Sequelize, getUser: CustomFieldOptions['get
61
65
  schema: 'public',
62
66
  },
63
67
  );
64
- const migrations = await SequelizeMeta.findAll({ raw: true });
65
- const currentSadotSchemaVersion = migrations.reverse().find((m: any) => m.name.includes(SADOT_MIGRATION_PREFIX));
66
68
 
67
- if (
68
- !currentSadotSchemaVersion
69
- || (currentSadotSchemaVersion as any).name !== CUSTOM_FIELDS_SCHEMA_VERSION
70
- ) {
69
+ const migrations = await SequelizeMeta.findAll({ where: { name: { [Op.like]: `${schemaPrefix}%` } }, raw: true });
70
+ const currentSadotSchemaVersion = migrations.at(-1);
71
+ const expectedSchemaVersionIndex = migrations.findIndex((m) => (m as any).name === CUSTOM_FIELDS_SCHEMA_VERSION);
72
+
73
+ if (!currentSadotSchemaVersion || (currentSadotSchemaVersion as any).name !== CUSTOM_FIELDS_SCHEMA_VERSION) {
71
74
  await CustomFieldDefinition.sync({ alter: true });
72
75
  await CustomFieldValue.sync({ alter: true });
73
- await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
76
+ if (expectedSchemaVersionIndex === -1) {
77
+ await SequelizeMeta.create({ name: CUSTOM_FIELDS_SCHEMA_VERSION });
78
+ }
74
79
  logger.info('custom-fields: models synced');
80
+ if (migrations.length && expectedSchemaVersionIndex !== -1 && expectedSchemaVersionIndex < migrations.length - 1) {
81
+ // We have existing migrations, and we are calling `sync`.
82
+ // This means we are in a `down` migration, and hence we should delete newer migrations to ensure we can reapply them.
83
+ const migrationsToDelete = migrations.slice(expectedSchemaVersionIndex + 1);
84
+ await SequelizeMeta.destroy({ where: { name: { [Op.in]: migrationsToDelete.map((m) => (m as any).name) } } });
85
+ }
75
86
  }
76
87
  };
77
88