Skip to content

Commit

Permalink
feat: schema change for inventory
Browse files Browse the repository at this point in the history
  • Loading branch information
DonWick32 committed Feb 7, 2024
1 parent 29cd4f1 commit bd18e14
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 2 deletions.
30 changes: 30 additions & 0 deletions migrations/2024-02-07-201037_feat_inventory/down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
-- This file should undo anything in `up.sql`

--remove hp from building_type
ALTER TABLE public.building_type
DROP COLUMN hp;

--remove cost name and level from emp
ALTER TABLE public.emp_type
DROP cost,
DROP "name",
DROP "level";

--remove name from attacker
ALTER TABLE public.attacker_type
DROP "name";

--remove attacker and emp from available_blocks
ALTER TABLE public.available_blocks

DROP CONSTRAINT attacker_id_fk,
DROP attacker_type_id,
DROP CONSTRAINT emp_id_fk,
DROP emp_type_id,
DROP category,
DROP CONSTRAINT available_blocks_id_primary,
DROP id,
ALTER COLUMN block_type_id SET NOT NULL,
ADD CONSTRAINT available_blocks_id_primary PRIMARY KEY(user_id, block_type_id);

DROP TYPE item_category;
36 changes: 36 additions & 0 deletions migrations/2024-02-07-201037_feat_inventory/up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Your SQL goes here

--add hp to building_type
ALTER TABLE public.building_type
ADD COLUMN hp INTEGER NOT NULL;

--add cost name and level to emp
ALTER TABLE public.emp_type
ADD cost INTEGER NOT NULL,
ADD "name" VARCHAR(255) NOT NULL,
ADD "level" INTEGER NOT NULL;

--add name to attacker
ALTER TABLE public.attacker_type
ADD "name" VARCHAR(255) NOT NULL;

--add attacker and emp to available_blocks
CREATE TYPE item_category AS ENUM ('attacker', 'emp', 'block');


ALTER TABLE public.available_blocks

ADD attacker_type_id INTEGER,
ADD CONSTRAINT attacker_id_fk FOREIGN KEY (attacker_type_id) REFERENCES public.attacker_type(id),

Add emp_type_id INTEGER,
ADD CONSTRAINT emp_id_fk FOREIGN KEY (emp_type_id) REFERENCES public.emp_type(id),

ADD category item_category NOT NULL,

ADD id INTEGER NOT NULL,

DROP CONSTRAINT available_blocks_id_primary,
ADD CONSTRAINT available_blocks_id_primary PRIMARY KEY(id),

ALTER COLUMN block_type_id DROP NOT NULL;
1 change: 1 addition & 0 deletions src/api/attack/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,7 @@ pub fn get_attacker_types(conn: &mut PgConnection) -> Result<HashMap<i32, Attack
attacker.id,
AttackerType {
id: attacker.id,
name: attacker.name.clone(),
max_health: attacker.max_health,
speed: attacker.speed,
amt_of_emps: attacker.amt_of_emps,
Expand Down
2 changes: 2 additions & 0 deletions src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub struct BuildingType {
pub capacity: i32,
pub level: i32,
pub cost: i32,
pub hp: i32,
}

#[derive(Insertable)]
Expand Down Expand Up @@ -305,4 +306,5 @@ pub struct AttackerType {
pub amt_of_emps: i32,
pub level: i32,
pub cost: i32,
pub name: String,
}
22 changes: 20 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ pub mod sql_types {
#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "block_category"))]
pub struct BlockCategory;

#[derive(diesel::sql_types::SqlType)]
#[diesel(postgres_type(name = "item_category"))]
pub struct ItemCategory;
}

diesel::table! {
Expand All @@ -30,13 +34,21 @@ diesel::table! {
amt_of_emps -> Int4,
level -> Int4,
cost -> Int4,
name -> Varchar,
}
}

diesel::table! {
available_blocks (user_id, block_type_id) {
block_type_id -> Int4,
use diesel::sql_types::*;
use super::sql_types::ItemCategory;

available_blocks (id) {
block_type_id -> Nullable<Int4>,
user_id -> Int4,
attacker_type_id -> Nullable<Int4>,
emp_type_id -> Nullable<Int4>,
category -> ItemCategory,
id -> Int4,
}
}

Expand All @@ -62,6 +74,7 @@ diesel::table! {
capacity -> Int4,
level -> Int4,
cost -> Int4,
hp -> Int4,
}
}

Expand All @@ -82,6 +95,9 @@ diesel::table! {
att_type -> Varchar,
attack_radius -> Int4,
attack_damage -> Int4,
cost -> Int4,
name -> Varchar,
level -> Int4,
}
}

Expand Down Expand Up @@ -182,7 +198,9 @@ diesel::table! {
}

diesel::joinable!(artifact -> map_spaces (map_space_id));
diesel::joinable!(available_blocks -> attacker_type (attacker_type_id));
diesel::joinable!(available_blocks -> block_type (block_type_id));
diesel::joinable!(available_blocks -> emp_type (emp_type_id));
diesel::joinable!(available_blocks -> user (user_id));
diesel::joinable!(block_type -> building_type (building_type));
diesel::joinable!(block_type -> defender_type (defender_type));
Expand Down

0 comments on commit bd18e14

Please sign in to comment.