created basic DB structure and accessing db

This commit is contained in:
Joshua Ashton
2025-03-05 18:55:04 -07:00
parent 8d244e586d
commit 54228c4e8e
16 changed files with 151 additions and 16 deletions
+117 -3
View File
@@ -1,7 +1,121 @@
create_db() {
mariadb -u root -p -e "CREATE DATABASE vintagecodingdotnet;"
mariadb -u root -p -e "USE vintagecodingdotnet; CREATE TABLE articles (article_id INT PRIMARY KEY AUTO_INCREMENT, article_name VARCHAR(255) NOT NULL);"
mariadb -u root -p -e "USE vintagecodingdotnet; INSERT INTO articles (article_name) VALUES ('test'), ('another test'), ('a third test');"
/opt/lampp/bin/mariadb -u violet -p -e "DROP DATABASE IF EXISTS vintagecodingdotnet;";
/opt/lampp/bin/mariadb -u violet -p -e "CREATE DATABASE vintagecodingdotnet;";
# Create file_types table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE file_types (
file_type_id INT PRIMARY KEY AUTO_INCREMENT,
file_type VARCHAR(8)
);";
# Create the media table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE media (
media_id INT PRIMARY KEY,
file_name TINYTEXT NOT NULL,
file_path VARCHAR(2048) NOT NULL,
file_type INT NOT NULL,
FOREIGN KEY (file_type) REFERENCES file_types(file_type_id)
);";
# Create the roles table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE roles (
role_id INT PRIMARY KEY AUTO_INCREMENT,
role VARCHAR(32) NOT NULL
);";
# Create the accounts table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE accounts (
account_id INT PRIMARY KEY,
username VARCHAR(32) UNIQUE NOT NULL,
email VARCHAR(64) UNIQUE NOT NULL,
password_hash VARCHAR(64) NOT NULL,
role INT NOT NULL,
registration_date DATETIME,
bio TINYTEXT,
profile_picture INT,
FOREIGN KEY (role) REFERENCES roles(role_id),
FOREIGN KEY (profile_picture) REFERENCES media(media_id)
);";
# Create the article_status table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE article_status (
status_id INT PRIMARY KEY AUTO_INCREMENT,
status VARCHAR(16) NOT NULL
);";
# Create the articles table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
CREATE TABLE articles (
article_id INT PRIMARY KEY AUTO_INCREMENT,
account_id INT NOT NULL,
title VARCHAR(128) UNIQUE NOT NULL,
slug VARCHAR(128) UNIQUE NOT NULL,
content LONGTEXT NOT NULL,
publish_date DATETIME,
last_modified DATETIME NOT NULL,
status INT NOT NULL,
featured_image INT,
excerpt VARCHAR(255),
FOREIGN KEY (account_id) REFERENCES accounts(account_id),
FOREIGN KEY (status) REFERENCES article_status(status_id)
);";
}
# Insert data constants into the database.
# Acceptable file types: png, jpg, jpeg, svg.
# Acceptable roles: administrator, contributor, commenter, guest.
# Acceptable article status: published, draft, archived.
#
# Also creates administrator user
const_data() {
# Insert the data to the file_types table.
# Acceptable file types are png, jpg, jpeg, svg
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO file_types (file_type) VALUES ('png'), ('jpg'), ('jpeg'), ('svg');
";
# Insert the data to the roles table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO roles (role) VALUES ('administrator'), ('contributor'), ('commenter'), ('guest');
";
# Insert the data to the article_status table.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO article_status (status) VALUES ('published'), ('draft'), ('archived');
";
# Create an administrator user.
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO accounts
(account_id, username, email, password_hash, role, registration_date, bio, profile_picture) VALUES
(42, 'admin', 'me@joshashton.dev', 'e03b1a00caa6e8113a3bc3e7101ecbd0fea710f5e091e7b195bdba53bbeb004e', 1, CURDATE(), null, null);
";
}
insert_data() {
/opt/lampp/bin/mariadb -u violet -p -e "
USE vintagecodingdotnet;
INSERT INTO articles (article_name) VALUES
('test'),
('another test'),
('a third test');
";
}
create_db
const_data