Files
inventory-middleware/scripts/table_template.sql
T

165 lines
7.1 KiB
SQL

-- Version 0.5.5.1 Removed character length restrictions as the docs for POSTGRESQL recommend not specifying a limit.
-- Version 0.6.0.0 Changed the numeric values to integers as Golang doesn't have a native decimal type.
-- Version 0.6.2.0 Added the first draft for cost of ownership. Added quantity to the item table.
-- Version 0.6.5.0 Added a documents table along with the tables to reference them as either an expense or as a document for an item.
DROP TABLE IF EXISTS property_value;
DROP TABLE IF EXISTS property;
DROP TABLE IF EXISTS property_data_type;
DROP TABLE IF EXISTS item_component;
DROP TABLE IF EXISTS coo_document;
DROP TABLE IF EXISTS item_document;
DROP TABLE IF EXISTS document_file;
DROP TABLE IF EXISTS coo_line_item;
DROP TABLE IF EXISTS cost_of_ownership;
DROP TABLE IF EXISTS expense_type;
DROP TABLE IF EXISTS item;
DROP TABLE IF EXISTS subcategory;
DROP TABLE IF EXISTS category;
DROP TABLE IF EXISTS document_type;
CREATE TABLE property_data_type (
property_data_type_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
property_data_type CHARACTER VARYING UNIQUE NOT NULL
);
INSERT INTO property_data_type (property_data_type) VALUES ('Money');
INSERT INTO property_data_type (property_data_type) VALUES ('Text');
INSERT INTO property_data_type (property_data_type) VALUES ('DateTime');
INSERT INTO property_data_type (property_data_type) VALUES ('Number');
CREATE TABLE category (
category_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
category_name CHARACTER VARYING UNIQUE NOT NULL
);
CREATE TABLE subcategory (
subcategory_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
subcategory_name CHARACTER VARYING UNIQUE NOT NULL,
category_id INT NOT NULL REFERENCES category(category_id)
);
CREATE TABLE item (
item_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
item_name CHARACTER VARYING NOT NULL,
value_cents INT DEFAULT 0,
purchase_cost_cents INT DEFAULT 0,
date_aquired TIMESTAMP WITH TIME ZONE,
description CHARACTER VARYING,
notes CHARACTER VARYING,
sold_for_cents INT DEFAULT 0,
sold_to CHARACTER VARYING, -- Could be a pointer TO a TABLE OF comtacts.
quantity INT DEFAULT 1,
category_id INT NOT NULL REFERENCES category(category_id),
subcategory_id INT REFERENCES subcategory(subcategory_id)
);
-- Example: property_name of "Author" which maps to property_data_type of "Text".
CREATE TABLE property (
property_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
property_name CHARACTER VARYING NOT NULL,
property_type_id INT NOT NULL REFERENCES property_data_type(property_data_type_id),
category_id INT NOT NULL REFERENCES category(category_id),
subcategory_id INT REFERENCES subcategory(subcategory_id)
);
-- Allows grouping items together, i.e. Alienware Desktop -> CPU, HDDs / SSDs, Motherboard, etc.
CREATE TABLE item_component (
item_component_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
item_id INT NOT NULL REFERENCES item(item_id),
item_subcomponent_id INT NOT NULL REFERENCES item(item_id)
);
CREATE TABLE property_value (
property_value_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
item_id INT NOT NULL REFERENCES item(item_id),
property_id INT NOT NULL REFERENCES property(property_id),
value CHARACTER VARYING NOT NULL
);
CREATE TABLE cost_of_ownership (
coo_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
coo_description CHARACTER VARYING,
coo_display_text CHARACTER VARYING NOT NULL, -- I.E. "Transmission Rebuild" etc.
coo_service_rendered_date TIMESTAMP WITH TIME ZONE
--service BIT Like was this a service or did you buy a replacement and do it yourself?
--coo type eg 'Regular Maintence' etc
);
-- An item or items associated with the cost of ownership, i.e. a replacement motor.
--CREATE TABLE coo_item (
-- coo_item_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
-- item_id INT NOT NULL REFERENCES item(item_id),
-- coo_id INT NOT NULL REFERENCES cost_of_ownership(coo_id),
--
--)
CREATE TABLE expense_type (
expense_type_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
expense_type_name CHARACTER VARYING UNIQUE NOT NULL --LIKE "Sales Tax" OR "Labour"
);
INSERT INTO expense_type (expense_type_name) VALUES ('Sales Tax');
INSERT INTO expense_type (expense_type_name) VALUES ('Labour');
INSERT INTO expense_type (expense_type_name) VALUES ('Grand Total');
-- An itemized expense, like cost of labour, or cost of parts for whoever is doing the repairs / replacement, taxes etc..
CREATE TABLE coo_line_item (
coo_line_item_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
cost_cents INT DEFAULT 0,
expense_type_id INT NOT NULL REFERENCES expense_type(expense_type_id),
coo_id INT NOT NULL REFERENCES cost_of_ownership(coo_id),
item_id INT REFERENCES item(item_id)
);
CREATE TABLE document_type (
document_type_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
document_type_name CHARACTER VARYING UNIQUE NOT NULL
);
INSERT INTO document_type (document_type_name) VALUES ('Certificate of Authenticity');
INSERT INTO document_type (document_type_name) VALUES ('Sales Contract');
INSERT INTO document_type (document_type_name) VALUES ('Invoice');
INSERT INTO document_type (document_type_name) VALUES ('Title');
INSERT INTO document_type (document_type_name) VALUES ('Deed');
INSERT INTO document_type (document_type_name) VALUES ('Receipt');
CREATE TABLE document_file (
document_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
original_filename CHARACTER VARYING NOT NULL,
display_name CHARACTER VARYING NOT NULL,
mime_type CHARACTER VARYING NOT NULL,
file_extension CHARACTER VARYING NOT NULL,
document_type_id INT NOT NULL REFERENCES document_type(document_type_id)
);
CREATE TABLE item_document (
item_document_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
item_id INT NOT NULL REFERENCES item(item_id),
document_file_id INT NOT NULL REFERENCES document_file(document_id)
);
CREATE TABLE coo_document (
coo_document_id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY,
coo_id INT NOT NULL REFERENCES cost_of_ownership(coo_id),
document_file_id INT NOT NULL REFERENCES document_file(document_id)
);
INSERT INTO category (category_name) VALUES ('Books');
INSERT INTO category (category_name) VALUES ('Electronics');
INSERT INTO category (category_name) VALUES ('Appliances');
INSERT INTO category (category_name) VALUES ('Kitchenware');
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('PCIe Cards', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Laptops', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Desktops', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('CPUs', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Hard Drives', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Switches', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Routers', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('CPU Coolers', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('USB Devices', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Oscilloscopes', 2);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Heaters', 3);
INSERT INTO subcategory(subcategory_name, category_id) VALUES ('Eletric Kettles', 4);