-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added user_behaviours table and updated pet_behaviours table
- Loading branch information
Showing
5 changed files
with
160 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const MIN_BEHAVIOUR_LEVEL = 1; | ||
export const MAX_BEHAVIOUR_LEVEL = 4; |
56 changes: 56 additions & 0 deletions
56
backend/typescript/migrations/2024.10.29T20.20.02.create-user-behaviour-table.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { DataType } from "sequelize-typescript"; | ||
|
||
import { Migration } from "../umzug"; | ||
import { MAX_BEHAVIOUR_LEVEL, MIN_BEHAVIOUR_LEVEL } from "../constants"; | ||
|
||
const TABLE_NAME = "user_behaviours"; | ||
const CONSTRAINT_NAME = "unique_user_behaviour_skill"; | ||
|
||
export const up: Migration = async ({ context: sequelize }) => { | ||
await sequelize.getQueryInterface().createTable(TABLE_NAME, { | ||
id: { | ||
type: DataType.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
user_id: { | ||
type: DataType.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: "users", | ||
key: "id", | ||
}, | ||
}, | ||
behaviour_id: { | ||
type: DataType.INTEGER, | ||
allowNull: false, | ||
references: { | ||
model: "behaviours", | ||
key: "id", | ||
}, | ||
}, | ||
max_level: { | ||
type: DataType.INTEGER, | ||
validate: { | ||
min: MIN_BEHAVIOUR_LEVEL, | ||
max: MAX_BEHAVIOUR_LEVEL, | ||
}, | ||
allowNull: false, | ||
}, | ||
}); | ||
|
||
await sequelize.getQueryInterface().addConstraint(TABLE_NAME, { | ||
fields: ["behaviour_id", "user_id"], | ||
type: "unique", | ||
name: CONSTRAINT_NAME, | ||
}); | ||
}; | ||
|
||
export const down: Migration = async ({ context: sequelize }) => { | ||
await sequelize | ||
.getQueryInterface() | ||
.removeConstraint(TABLE_NAME, CONSTRAINT_NAME); | ||
|
||
await sequelize.getQueryInterface().dropTable(TABLE_NAME); | ||
}; |
36 changes: 36 additions & 0 deletions
36
backend/typescript/migrations/2024.10.29T20.29.50.add-user-behaviour-columns copy.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { DataType } from "sequelize-typescript"; | ||
import { Migration } from "../umzug"; | ||
import { MAX_BEHAVIOUR_LEVEL, MIN_BEHAVIOUR_LEVEL } from "../constants"; | ||
|
||
const TABLE_NAME = "pet_behaviours"; | ||
const CONSTRAINT_NAME = "unique_pet_behaviour"; | ||
|
||
export const up: Migration = async ({ context: sequelize }) => { | ||
await sequelize.getQueryInterface().addColumn(TABLE_NAME, "level", { | ||
type: DataType.INTEGER, | ||
allowNull: false, | ||
validate: { | ||
min: MIN_BEHAVIOUR_LEVEL, | ||
max: MAX_BEHAVIOUR_LEVEL, | ||
}, | ||
}); | ||
|
||
await sequelize.getQueryInterface().addColumn(TABLE_NAME, "is_highlighted", { | ||
type: DataType.BOOLEAN, | ||
allowNull: false, | ||
}); | ||
|
||
await sequelize.getQueryInterface().addConstraint(TABLE_NAME, { | ||
fields: ["behaviour_id", "pet_id"], | ||
type: "unique", | ||
name: CONSTRAINT_NAME, | ||
}); | ||
}; | ||
|
||
export const down: Migration = async ({ context: sequelize }) => { | ||
await sequelize.getQueryInterface().removeColumn(TABLE_NAME, "level"); | ||
|
||
await sequelize | ||
.getQueryInterface() | ||
.removeColumn(TABLE_NAME, "is_highlighted"); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { | ||
Column, | ||
Model, | ||
Table, | ||
ForeignKey, | ||
BelongsTo, | ||
DataType, | ||
} from "sequelize-typescript"; | ||
import Behaviour from "./behaviour.model"; | ||
import Pet from "./pet.model"; | ||
|
||
@Table({ timestamps: false, tableName: "pet_behaviours" }) | ||
export default class PetBehaviour extends Model { | ||
@ForeignKey(() => Pet) | ||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
pet_id!: number; | ||
|
||
@BelongsTo(() => Pet) | ||
pet!: Pet; | ||
|
||
@ForeignKey(() => Behaviour) | ||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
behaviour_id!: number; | ||
|
||
@BelongsTo(() => Behaviour) | ||
behaviour!: Behaviour; | ||
|
||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
skill_level!: number; | ||
|
||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
level!: number; | ||
|
||
@Column({ type: DataType.BOOLEAN, allowNull: false }) | ||
is_highlighted!: boolean; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { | ||
Column, | ||
Model, | ||
Table, | ||
ForeignKey, | ||
BelongsTo, | ||
DataType, | ||
} from "sequelize-typescript"; | ||
import Behaviour from "./behaviour.model"; | ||
import User from "./user.model"; | ||
|
||
@Table({ timestamps: false, tableName: "user_behaviours" }) | ||
export default class UserBehaviour extends Model { | ||
@ForeignKey(() => User) | ||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
user_id!: number; | ||
|
||
@BelongsTo(() => User) | ||
user!: User; | ||
|
||
@ForeignKey(() => Behaviour) | ||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
behaviour_id!: number; | ||
|
||
@BelongsTo(() => Behaviour) | ||
behaviour!: Behaviour; | ||
|
||
@Column({ type: DataType.INTEGER, allowNull: false }) | ||
max_level!: number; | ||
} |