Skip to content

Commit bc9ffd1

Browse files
committed
fixing init from csv
1 parent d9d620d commit bc9ffd1

6 files changed

Lines changed: 82 additions & 61 deletions

File tree

database/init-scripts/02-store-init.sql

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ CREATE TABLE IF NOT EXISTS purchases (
5050
id INTEGER PRIMARY KEY,
5151
owner_id INTEGER NOT NULL,
5252
buyer_id INTEGER NOT NULL,
53-
ts INTEGER, -- make it zero if game not buyed yet but in cart already
53+
ts INTEGER, -- make it NULL if game not buyed yet but in cart already
5454
game_id INTEGER NOT NULL,
5555

5656
FOREIGN KEY (owner_id) REFERENCES users (id),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
COPY users (id, password_hash, name, balance)
2+
FROM '/store-init-csvs/init-data-users.csv'
3+
WITH (FORMAT CSV, HEADER, DELIMITER ',');
4+
5+
COPY tags(id, name)
6+
FROM '/store-init-csvs/init-data-tags.csv'
7+
WITH (FORMAT CSV, HEADER, DELIMITER ',');
8+
9+
COPY studios(id, name)
10+
FROM '/store-init-csvs/init-data-studios.csv'
11+
WITH (FORMAT CSV, HEADER, DELIMITER ',');
12+
13+
COPY games(id, name, price, description, brief, studio_id)
14+
FROM '/store-init-csvs/init-data-games.csv'
15+
WITH (FORMAT CSV, HEADER, DELIMITER ',');
16+
17+
-- generator can generate duplicates for this categories
18+
-- COPY game_tags(game_id, tag_id)
19+
-- FROM '/store-init-csvs/init-data-game-tags.csv'
20+
-- WITH (FORMAT CSV, HEADER, DELIMITER ',');
21+
22+
-- COPY purchases(id, owner_id, buyer_id, ts, game_id)
23+
-- FROM '/store-init-csvs/init-data-purchases.csv'
24+
-- WITH (FORMAT CSV, HEADER, DELIMITER ',', NULL 'NULL');
25+
26+
-- create temporary table for games pictures to transform csv
27+
CREATE TEMP TABLE temp4gpictures (
28+
id INTEGER PRIMARY KEY,
29+
name TEXT,
30+
game_id INTEGER,
31+
img_type TEXT,
32+
img_fmt TEXT,
33+
img TEXT
34+
);
35+
36+
COPY temp4gpictures
37+
FROM '/store-init-csvs/init-games-pictures.csv'
38+
WITH (FORMAT CSV, HEADER);
39+
40+
INSERT INTO games_pictures (id, name, game_id, img_type, img_fmt)
41+
SELECT
42+
s.id,
43+
s.id::TEXT || 'g.' || s.img_fmt as name,
44+
s.game_id,
45+
s.img_type,
46+
s.img_fmt
47+
FROM
48+
temp4gpictures s
49+
ON CONFLICT (id) DO NOTHING;
50+
51+
DROP TABLE temp4gpictures;
52+
-- end of copy games pictures
53+
54+
-- create temporary table for profile pictures to transform csv
55+
CREATE TEMP TABLE temp4ppictures (
56+
id INTEGER PRIMARY KEY,
57+
name TEXT,
58+
user_id INTEGER,
59+
img_fmt TEXT,
60+
img TEXT
61+
);
62+
63+
COPY temp4ppictures
64+
FROM '/store-init-csvs/init-profile-pictures.csv'
65+
WITH (FORMAT CSV, HEADER);
66+
67+
INSERT INTO profiles_pictures (id, name, user_id, img_fmt)
68+
SELECT
69+
s.id,
70+
s.id::TEXT || 'p.' || s.img_fmt as name,
71+
s.user_id,
72+
s.img_fmt
73+
FROM
74+
temp4ppictures s
75+
ON CONFLICT (id) DO NOTHING;
76+
77+
DROP TABLE temp4ppictures;
78+
-- end of copy profile pictures

docker-compose.yaml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,10 @@ services:
2929
- "5000:5000"
3030
depends_on:
3131
- db
32-
- bank-service
3332
environment:
3433
BANK_SERVICE_URL: http://bank-service:5001
3534
volumes:
36-
- store-db-data:/var/store-db
35+
- ./database/store-init-csvs:/tmp/init-csvs/
3736
networks:
3837
- my-network
3938

store-service/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ flask
22
uwsgi
33
bleach
44
pillow
5+
psycopg

store-service/store/database/db_api.py

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10,32 +10,9 @@
1010
from store.utility.User import User
1111

1212
DB_PATH = os.environ.get('DB_PATH', '/var/store-db/store.sql3.db')
13-
SCHEMA_PATH = os.environ.get('SCHEME_PATH', '/store/store/database/store-schema.sql')
14-
INIT_CSV_PATH = os.environ.get('INIT_CSV_PATH', '/store/store/database/csv-init')
13+
INIT_CSV_PATH = os.environ.get('INIT_CSV_PATH', '/tmp/init-csvs')
1514
STATIC_PATH = os.environ.get('STATIC_PATH', '/store/store/static')
1615

17-
def init_from_csv(db_file:str, csv_file:str, table_name:str):
18-
conn = sqlite3.connect(db_file)
19-
cursor = conn.cursor()
20-
try:
21-
with open(csv_file, 'r', encoding='UTF-8') as file:
22-
reader = csv.reader(file)
23-
headers = next(reader)
24-
query = f"""
25-
INSERT OR IGNORE INTO {table_name} ({','.join(headers)})
26-
VALUES ({','.join(['?'] * len(headers))});
27-
"""
28-
for row in reader:
29-
data = [None if x == 'NULL' else x for x in row]
30-
cursor.execute(query, data)
31-
conn.commit()
32-
conn.close()
33-
except Exception as e:
34-
print(f'error:{e}')
35-
conn.rollback()
36-
conn.close()
37-
raise ValueError(str(e)) from e
38-
3916
def init_pics_from_csv(db_file:str, csv_file:str, table_name:str, save_to:str):
4017
conn = sqlite3.connect(db_file)
4118
cursor = conn.cursor()
@@ -79,20 +56,6 @@ def execute_sql_file(conn, sql_file):
7956
conn.rollback()
8057

8158
def init_database():
82-
schema_file = SCHEMA_PATH
83-
conn = sqlite3.connect(DB_PATH)
84-
if conn:
85-
execute_sql_file(conn, schema_file)
86-
conn.commit()
87-
conn.close()
88-
init_csvs = [
89-
{'file': f'{INIT_CSV_PATH}/init-data-users.csv', 'table':'users'},
90-
{'file': f'{INIT_CSV_PATH}/init-data-studios.csv', 'table':'studios'},
91-
{'file': f'{INIT_CSV_PATH}/init-data-games.csv', 'table':'games'},
92-
{'file': f'{INIT_CSV_PATH}/init-data-tags.csv', 'table':'tags'},
93-
{'file': f'{INIT_CSV_PATH}/init-data-game-tags.csv', 'table':'game_tags'},
94-
{'file': f'{INIT_CSV_PATH}/init-data-purchases.csv', 'table':'purchases'}
95-
]
9659
init_pics_csvs = [{
9760
'file': f'{INIT_CSV_PATH}/init-profile-pictures.csv',
9861
'table':'profiles_pictures',

0 commit comments

Comments
 (0)