database insertions function properly from python script
This commit is contained in:
16
db/Seed.js
16
db/Seed.js
@@ -20,7 +20,7 @@ async function main() {
|
||||
const createCartTable = `
|
||||
CREATE TABLE IF NOT EXISTS cart (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
appUserId INT REFERENCES users(id),
|
||||
appUserId INT REFERENCES users(id)
|
||||
);
|
||||
`;
|
||||
|
||||
@@ -49,7 +49,7 @@ async function main() {
|
||||
CREATE TABLE IF NOT EXISTS category (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
description VARCHAR NOT NULL
|
||||
description VARCHAR
|
||||
);
|
||||
`;
|
||||
|
||||
@@ -58,10 +58,10 @@ async function main() {
|
||||
CREATE TABLE IF NOT EXISTS product (
|
||||
id INT PRIMARY KEY GENERATED ALWAYS AS IDENTITY NOT NULL,
|
||||
name VARCHAR NOT NULL,
|
||||
description VARCHAR NOT NULL,
|
||||
description VARCHAR,
|
||||
categoryId INT REFERENCES category(id),
|
||||
regionId INT REFERENCES region(id),
|
||||
price NUMERIC NOT NULL
|
||||
price NUMERIC
|
||||
);
|
||||
`;
|
||||
|
||||
@@ -87,8 +87,8 @@ async function main() {
|
||||
let status;
|
||||
|
||||
try {
|
||||
await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public;");
|
||||
|
||||
await client.query("DROP SCHEMA public CASCADE; CREATE SCHEMA public");
|
||||
|
||||
for (let q of allQueries) {
|
||||
await client.query(q);
|
||||
}
|
||||
@@ -97,6 +97,10 @@ async function main() {
|
||||
status = "Database setup successful!";
|
||||
} catch(e) {
|
||||
status = e;
|
||||
} finally {
|
||||
if (status !== "Database setup successful!") {
|
||||
throw new Error(status);
|
||||
}
|
||||
}
|
||||
|
||||
console.log(status);
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
from logging.config import IDENTIFIER
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
import csv
|
||||
import os
|
||||
|
||||
# read data from environment if present
|
||||
env_path = "../../.env"
|
||||
fd = os.open(env_path, os.O_RDONLY)
|
||||
n = 300
|
||||
|
||||
file_data = os.read(fd, n)
|
||||
parsed_string = ""
|
||||
|
||||
# convert each ASCII value to its corresponding character
|
||||
for c in file_data:
|
||||
parsed_string = parsed_string + chr(c)
|
||||
|
||||
start = parsed_string.find('postgres')
|
||||
end = parsed_string.find('EXPRESS')
|
||||
|
||||
# receive conn string from env
|
||||
constring = parsed_string[start:end]
|
||||
os.close(fd)
|
||||
|
||||
# connect to local database instance and open a cursor
|
||||
conn = psycopg2.connect("dbname=e-commerce-092122 user=mikayladobson")
|
||||
cur = conn.cursor()
|
||||
|
||||
# read and print data from selection
|
||||
cur.execute("SELECT * FROM users;")
|
||||
for i in cur.fetchall():
|
||||
print(i)
|
||||
|
||||
# function to read from a given csv file into postgres
|
||||
def insert_file_contents(file_path, table_name, column_count):
|
||||
with open(file_path, 'r') as f:
|
||||
reader = csv.reader(f)
|
||||
header_names = ""
|
||||
first_row_accessed = False
|
||||
|
||||
for row in reader:
|
||||
# get row values from first row of reader
|
||||
if not first_row_accessed:
|
||||
header_names = row
|
||||
first_row_accessed = True
|
||||
continue
|
||||
|
||||
# execute table insertion for each row of csv based on number of columns
|
||||
if column_count == 1:
|
||||
cur.execute(sql.SQL(
|
||||
"INSERT INTO {} (%s) VALUES (%s)").format(sql.Identifier(table_name)),
|
||||
[header_names, row]
|
||||
)
|
||||
elif column_count == 3:
|
||||
cur.execute(sql.SQL("INSERT INTO {} ({}) VALUES (%s, %s, %s)".format(sql.Identifier(table_name), sql.Identifier(header_names[0])), row))
|
||||
else:
|
||||
raise
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
insert_file_contents("./categories.csv", "category", 1)
|
||||
|
||||
cur.execute("SELECT * FROM category;")
|
||||
for i in cur.fetchall():
|
||||
print(i)
|
||||
@@ -1,9 +1,11 @@
|
||||
import psycopg2
|
||||
from psycopg2 import sql
|
||||
import csv
|
||||
import os
|
||||
|
||||
# read data from environment if present
|
||||
path = "../../.env"
|
||||
fd = os.open(path, os.O_RDONLY)
|
||||
env_path = "../../.env"
|
||||
fd = os.open(env_path, os.O_RDONLY)
|
||||
n = 300
|
||||
|
||||
file_data = os.read(fd, n)
|
||||
@@ -18,4 +20,51 @@ end = parsed_string.find('EXPRESS')
|
||||
|
||||
# receive conn string from env
|
||||
constring = parsed_string[start:end]
|
||||
os.close(fd)
|
||||
os.close(fd)
|
||||
|
||||
# connect to local database instance and open a cursor
|
||||
conn = psycopg2.connect("dbname=e-commerce-092122 user=mikayladobson")
|
||||
cur = conn.cursor()
|
||||
|
||||
# read and print data from selection
|
||||
cur.execute("SELECT * FROM users;")
|
||||
for i in cur.fetchall():
|
||||
print(i)
|
||||
|
||||
# function to read from a given csv file into postgres
|
||||
def insert_file_contents(file_path, table_name, column_count):
|
||||
with open(file_path, 'r', encoding='utf-8-sig') as f:
|
||||
reader = csv.reader(f)
|
||||
header_names = ""
|
||||
first_row_accessed = False
|
||||
|
||||
for row in reader:
|
||||
# get row values from first row of reader
|
||||
if not first_row_accessed:
|
||||
header_names = [item for item in row]
|
||||
first_row_accessed = True
|
||||
continue
|
||||
|
||||
# execute table insertion for each row of csv based on number of columns
|
||||
if column_count == 3:
|
||||
cur.execute(sql.SQL("INSERT INTO {table} ({h1}, {h2}, {h3}) VALUES (%s, %s, %s)".format(
|
||||
table=table_name,
|
||||
h1=header_names[0],
|
||||
h2=header_names[1],
|
||||
h3=header_names[2]
|
||||
)),
|
||||
row
|
||||
)
|
||||
elif column_count == 1:
|
||||
cur.execute(sql.SQL("INSERT INTO {table} ({field}) VALUES (%s)".format(table=table_name, field=header_names[0])), row)
|
||||
else:
|
||||
raise
|
||||
|
||||
conn.commit()
|
||||
|
||||
|
||||
insert_file_contents("./categories.csv", 'category', 1)
|
||||
insert_file_contents("./regions.csv", 'region', 1)
|
||||
insert_file_contents("./products.csv", 'product', 3)
|
||||
|
||||
print("Database insertions executed successfully.")
|
||||
@@ -1,51 +1,51 @@
|
||||
id,name,regionId,categoryId
|
||||
1,Allspice - Whole,8,1
|
||||
2,Cocoa Powder,1,1
|
||||
3,Cardamom - Green,3,1
|
||||
4,Cardamom - Black,4,1
|
||||
5,Cinnamon,2,1
|
||||
6,Cloves,1,1
|
||||
7,Fenugreek Seed,4,1
|
||||
8,Lavender Flowers,3,2
|
||||
9,Nutmeg,8,1
|
||||
10,Star Anise,2,1
|
||||
11,Vanilla Bean - Madagascar,1,1
|
||||
12,Vanilla Bean - Mexico,5,1
|
||||
13,Vanilla Paste,8,1
|
||||
14,Ancho Chile,5,3
|
||||
15,Cayenne - African,1,3
|
||||
16,Cayenne - Indian,4,3
|
||||
17,Chipotle Chile,5,3
|
||||
18,Ghost Chile,4,3
|
||||
19,Guajillo Chile,5,3
|
||||
20,Paprika - Hungarian,3,3
|
||||
21,Paprika - Spain,3,3
|
||||
22,Piment d'Esplette,3,3
|
||||
23,Thai Chile,2,3
|
||||
24,Basil,1,2
|
||||
25,Bay Leaf - California,7,2
|
||||
26,Bay Leaf - Turkish,3,2
|
||||
27,Chives,7,2
|
||||
28,Dill Weed,7,2
|
||||
29,Fenugreek Leaf,4,2
|
||||
30,Herbes de Provence,3,2
|
||||
31,Lemongrass,2,2
|
||||
32,Marjoram,1,2
|
||||
33,Oregano - Mexican,5,2
|
||||
34,Oregano - Turkish,3,2
|
||||
35,Parsley,7,2
|
||||
36,Peppermint,7,2
|
||||
37,Rosemary,3,2
|
||||
38,Sage,3,2
|
||||
39,Tarragon,3,2
|
||||
40,Fleur de Sel,3,4
|
||||
41,Grains of Paradise,1,4
|
||||
42,Himalayan Pink Salt,2,4
|
||||
43,Long Pepper,2,4
|
||||
44,Maldon Flake Sea Salt,3,4
|
||||
45,Peppercorn - Black,4,4
|
||||
46,Peppercorn - Green,4,4
|
||||
47,Peppercorn - Pink,8,4
|
||||
48,Peppercorn - Red,2,4
|
||||
49,Peppercorn - White,2,4
|
||||
50,Pepper - Szechuan,2,4
|
||||
name,regionId,categoryId
|
||||
Allspice - Whole,8,1
|
||||
Cocoa Powder,1,1
|
||||
Cardamom - Green,3,1
|
||||
Cardamom - Black,4,1
|
||||
Cinnamon,2,1
|
||||
Cloves,1,1
|
||||
Fenugreek Seed,4,1
|
||||
Lavender Flowers,3,2
|
||||
Nutmeg,8,1
|
||||
Star Anise,2,1
|
||||
Vanilla Bean - Madagascar,1,1
|
||||
Vanilla Bean - Mexico,5,1
|
||||
Vanilla Paste,8,1
|
||||
Ancho Chile,5,3
|
||||
Cayenne - African,1,3
|
||||
Cayenne - Indian,4,3
|
||||
Chipotle Chile,5,3
|
||||
Ghost Chile,4,3
|
||||
Guajillo Chile,5,3
|
||||
Paprika - Hungarian,3,3
|
||||
Paprika - Spain,3,3
|
||||
Piment d'Esplette,3,3
|
||||
Thai Chile,2,3
|
||||
Basil,1,2
|
||||
Bay Leaf - California,7,2
|
||||
Bay Leaf - Turkish,3,2
|
||||
Chives,7,2
|
||||
Dill Weed,7,2
|
||||
Fenugreek Leaf,4,2
|
||||
Herbes de Provence,3,2
|
||||
Lemongrass,2,2
|
||||
Marjoram,1,2
|
||||
Oregano - Mexican,5,2
|
||||
Oregano - Turkish,3,2
|
||||
Parsley,7,2
|
||||
Peppermint,7,2
|
||||
Rosemary,3,2
|
||||
Sage,3,2
|
||||
Tarragon,3,2
|
||||
Fleur de Sel,3,4
|
||||
Grains of Paradise,1,4
|
||||
Himalayan Pink Salt,2,4
|
||||
Long Pepper,2,4
|
||||
Maldon Flake Sea Salt,3,4
|
||||
Peppercorn - Black,4,4
|
||||
Peppercorn - Green,4,4
|
||||
Peppercorn - Pink,8,4
|
||||
Peppercorn - Red,2,4
|
||||
Peppercorn - White,2,4
|
||||
Pepper - Szechuan,2,4
|
||||
|
@@ -1,9 +1,9 @@
|
||||
id,name
|
||||
1,Africa
|
||||
2,Asia
|
||||
3,Europe
|
||||
4,India
|
||||
5,Latin America
|
||||
6,Middle East
|
||||
7,North America
|
||||
8,Carribean
|
||||
name
|
||||
Africa
|
||||
Asia
|
||||
Europe
|
||||
India
|
||||
Latin America
|
||||
Middle East
|
||||
North America
|
||||
Carribean
|
||||
|
Reference in New Issue
Block a user