|
| 1 | +""" |
| 2 | +This script creates and fills the schema for the database with data. |
| 3 | +If the schema already exists, it will be overwritten (dropped and |
| 4 | +recreated). Any data in the tables will be lost. |
| 5 | +
|
| 6 | +The schema is described in the file schema.sql. The data is loaded |
| 7 | +from the cleaned CSV files in the data folder. |
| 8 | +""" |
| 9 | + |
| 10 | +import pandas as pd |
| 11 | +from sqlalchemy.sql import text |
| 12 | + |
| 13 | +from mangoleaf.connection import Connection |
| 14 | + |
| 15 | + |
| 16 | +def main(): |
| 17 | + # Establish a connection to the database |
| 18 | + |
| 19 | + db_engine = Connection().get() |
| 20 | + |
| 21 | + # Create schema from SQL file |
| 22 | + with open("schema.sql") as f: |
| 23 | + sql_commands = f.read() |
| 24 | + |
| 25 | + with db_engine.connect() as connection: |
| 26 | + for command in sql_commands.split(";"): |
| 27 | + if command.strip(): |
| 28 | + connection.execute(text(command)) |
| 29 | + connection.commit() |
| 30 | + |
| 31 | + # Load cleaned data locally |
| 32 | + books = pd.read_csv("data/books/clean/books.csv", dtype="object") |
| 33 | + mangas = pd.read_csv("data/mangas/clean/mangas.csv", dtype="object") |
| 34 | + books_ratings = pd.read_csv("data/books/clean/ratings.csv", dtype="object") |
| 35 | + mangas_ratings = pd.read_csv("data/mangas/clean/ratings.csv", dtype="object") |
| 36 | + |
| 37 | + # Fill the static data: Books |
| 38 | + df = books[["ISBN", "Book-Title", "Book-Author", "Image-URL-M"]] |
| 39 | + df = df.rename( |
| 40 | + columns={ |
| 41 | + "ISBN": "item_id", |
| 42 | + "Book-Title": "title", |
| 43 | + "Book-Author": "author", |
| 44 | + "Image-URL-M": "image", |
| 45 | + } |
| 46 | + ) |
| 47 | + df.to_sql("books", db_engine, if_exists="append", index=False) |
| 48 | + |
| 49 | + # Fill the static data: Mangas |
| 50 | + df = mangas[["anime_id", "English name", "Other name", "Image URL"]] |
| 51 | + df = df.rename( |
| 52 | + columns={ |
| 53 | + "anime_id": "item_id", |
| 54 | + "English name": "title", |
| 55 | + "Other name": "other_title", |
| 56 | + "Image URL": "image", |
| 57 | + } |
| 58 | + ) |
| 59 | + df.to_sql("mangas", db_engine, if_exists="append", index=False) |
| 60 | + |
| 61 | + # Create users |
| 62 | + user_id = set(books_ratings["User-ID"].unique()) |
| 63 | + user_id |= set(mangas_ratings["user_id"].unique()) |
| 64 | + user_id = list(user_id) |
| 65 | + usernames = [f"User {i}" for i in user_id] |
| 66 | + passwords = [f"password{i}" for i in user_id] |
| 67 | + df = pd.DataFrame(dict(user_id=user_id, username=usernames, password=passwords)) |
| 68 | + df.to_sql("users", db_engine, if_exists="append", index=False) |
| 69 | + |
| 70 | + # Fill the ratings: Books |
| 71 | + df = books_ratings.rename( |
| 72 | + columns={"User-ID": "user_id", "ISBN": "item_id", "Book-Rating": "rating"} |
| 73 | + ) |
| 74 | + df.to_sql("books_ratings", db_engine, if_exists="append", index=False) |
| 75 | + |
| 76 | + # Fill the ratings: Mangas |
| 77 | + df = mangas_ratings.rename(columns={"anime_id": "item_id"}) |
| 78 | + df.to_sql("mangas_ratings", db_engine, if_exists="append", index=False) |
| 79 | + |
| 80 | + # Close the connection |
| 81 | + db_engine.dispose() |
| 82 | + |
| 83 | + |
| 84 | +if __name__ == "__main__": |
| 85 | + main() |
0 commit comments