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

feat: adding conditional unique indexes on nullable fields in Evse and VariableAttributes #317

Merged
merged 2 commits into from
Dec 17, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion 01_Data/src/layers/sequelize/model/DeviceModel/Evse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,17 @@
import { Namespace, OCPP2_0_1 } from '@citrineos/base';
import { AutoIncrement, Column, DataType, Model, PrimaryKey, Table } from 'sequelize-typescript';

@Table
@Table({
indexes: [
{
unique: true,
fields: ['id'],
where: {
connectorId: null,
},
},
],
})
export class Evse extends Model implements OCPP2_0_1.EVSEType {
static readonly MODEL_NAME: string = Namespace.EVSEType;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,64 @@ import { Boot } from '../Boot';
import { VariableStatus } from './VariableStatus';
import { ChargingStation } from '../Location';

@Table
@Table({
indexes: [
{
unique: true,
fields: ['stationId'],
where: {
type: null,
variableId: null,
componentId: null,
},
},
{
unique: true,
fields: ['stationId', 'type'],
where: {
variableId: null,
componentId: null,
},
},
{
unique: true,
fields: ['stationId', 'variableId'],
where: {
type: null,
componentId: null,
},
},
{
unique: true,
fields: ['stationId', 'componentId'],
where: {
type: null,
variableId: null,
},
},
{
unique: true,
fields: ['stationId', 'type', 'variableId'],
where: {
componentId: null,
},
},
{
unique: true,
fields: ['stationId', 'type', 'componentId'],
where: {
variableId: null,
},
},
{
unique: true,
fields: ['stationId', 'variableId', 'componentId'],
where: {
type: null,
},
},
],
})
export class VariableAttribute extends Model implements OCPP2_0_1.VariableAttributeType {
static readonly MODEL_NAME: string = Namespace.VariableAttributeType;

Expand All @@ -24,6 +81,7 @@ export class VariableAttribute extends Model implements OCPP2_0_1.VariableAttrib
@Index
@Column({
unique: 'stationId_type_variableId_componentId',
allowNull: false,
})
@ForeignKey(() => ChargingStation)
declare stationId: string;
Expand Down
Loading