diff --git a/.env.example b/.env.example deleted file mode 100644 index e9cca7e82..000000000 --- a/.env.example +++ /dev/null @@ -1,6 +0,0 @@ -# if running on vscode Dev-Container, please look for your credentials on file .devcontainer/docker-compose.yml -DB_USER='gitpod' -DB_PASSWORD='postgres' -DB_PORT=3306 -DB_HOST='localhost' -DB_NAME='sample_db' \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json index 1b17b6d68..c019e8c4c 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -2,5 +2,8 @@ "workbench.startupEditor": "readme", "workbench.editorAssociations": { "*.md": "vscode.markdown.preview.editor" - } + }, + "githubPullRequests.ignoredPullRequestBranches": [ + "main" + ] } diff --git a/requirements.txt b/requirements.txt index 2f24fd415..146f58992 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,6 +1,6 @@ # Base ---------------------------------------- -sqlalchemy -pandas +sqlalchemy==1.4.0 +pandas==2.1.4 python-dotenv==0.20.0 psycopg2-binary==2.9.3 diff --git a/src/app.py b/src/app.py index 82e6d2866..c5bccca9b 100644 --- a/src/app.py +++ b/src/app.py @@ -7,13 +7,99 @@ load_dotenv() # 1) Connect to the database here using the SQLAlchemy's create_engine function -connection_string = f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}" -engine = create_engine(connection_string).execution_options(autocommit=True) +connection_string=f"postgresql://{os.getenv('DB_USER')}:{os.getenv('DB_PASSWORD')}@{os.getenv('DB_HOST')}/{os.getenv('DB_NAME')}" +engine=create_engine(connection_string).execution_options(autocommit=True) engine.connect() -# 2) Execute the SQL sentences to create your tables using the SQLAlchemy's execute function +def create_table(table_name: str, query: str) -> None: + '''Function to create SQL table if it + does not exist.''' + + if not engine.dialect.has_table(engine, table_name): + engine.execute(query) + print(f'Created {table_name} table') + + else: + print(f'Tabel {table_name} exists') + +tables={ +'publishers': """CREATE TABLE publishers( + publisher_id INT NOT NULL, + name VARCHAR(255) NOT NULL, + PRIMARY KEY(publisher_id) +);""", +'authors': """CREATE TABLE authors( + author_id INT NOT NULL, + first_name VARCHAR(100) NOT NULL, + middle_name VARCHAR(50) NULL, + last_name VARCHAR(100) NULL, + PRIMARY KEY(author_id) +);""", +'books': """CREATE TABLE books( + book_id INT NOT NULL, + title VARCHAR(255) NOT NULL, + total_pages INT NULL, + rating DECIMAL(4, 2) NULL, + isbn VARCHAR(13) NULL, + published_date DATE, + publisher_id INT NULL, + PRIMARY KEY(book_id), + CONSTRAINT fk_publisher FOREIGN KEY(publisher_id) REFERENCES publishers(publisher_id) +);""", +'book_authors':"""CREATE TABLE book_authors ( + book_id INT NOT NULL, + author_id INT NOT NULL, + PRIMARY KEY(book_id, author_id), + CONSTRAINT fk_book FOREIGN KEY(book_id) REFERENCES books(book_id) ON DELETE CASCADE, + CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES authors(author_id) ON DELETE CASCADE +);""" +} + +for table_name, query in tables.items(): + create_table(table_name, query) # 3) Execute the SQL sentences to insert your data using the SQLAlchemy's execute function +engine.execute(""" +INSERT INTO publishers(publisher_id, name) VALUES (1, 'O Reilly Media') ON CONFLICT (publisher_id) DO NOTHING; +INSERT INTO publishers(publisher_id, name) VALUES (2, 'A Book Apart') ON CONFLICT (publisher_id) DO NOTHING; +INSERT INTO publishers(publisher_id, name) VALUES (3, 'A K PETERS') ON CONFLICT (publisher_id) DO NOTHING; +INSERT INTO publishers(publisher_id, name) VALUES (4, 'Academic Press') ON CONFLICT (publisher_id) DO NOTHING; +INSERT INTO publishers(publisher_id, name) VALUES (5, 'Addison Wesley') ON CONFLICT (publisher_id) DO NOTHING; +INSERT INTO publishers(publisher_id, name) VALUES (6, 'Albert&Sweigart') ON CONFLICT (publisher_id) DO NOTHING; +INSERT INTO publishers(publisher_id, name) VALUES (7, 'Alfred A. Knopf') ON CONFLICT (publisher_id) DO NOTHING; + +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (1, 'Merritt', null, 'Eric') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (2, 'Linda', null, 'Mui') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (3, 'Alecos', null, 'Papadatos') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (4, 'Anthony', null, 'Molinaro') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (5, 'David', null, 'Cronin') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (6, 'Richard', null, 'Blum') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (7, 'Yuval', 'Noah', 'Harari') ON CONFLICT (author_id) DO NOTHING; +INSERT INTO authors (author_id, first_name, middle_name, last_name) VALUES (8, 'Paul', null, 'Albitz') ON CONFLICT (author_id) DO NOTHING; + +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (1, 'Lean Software Development: An Agile Toolkit', 240, 4.17, '9780320000000', '2003-05-18', 5) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (2, 'Facing the Intelligence Explosion', 91, 3.87, null, '2013-02-01', 7) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (3, 'Scala in Action', 419, 3.74, '9781940000000', '2013-04-10', 1) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (4, 'Patterns of Software: Tales from the Software Community', 256, 3.84, '9780200000000', '1996-08-15', 1) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (5, 'Anatomy Of LISP', 446, 4.43, '9780070000000', '1978-01-01', 3) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (6, 'Computing machinery and intelligence', 24, 4.17, null, '2009-03-22', 4) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (7, 'XML: Visual QuickStart Guide', 269, 3.66, '9780320000000', '2009-01-01', 5) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (8, 'SQL Cookbook', 595, 3.95, '9780600000000', '2005-12-01', 7) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (9, 'The Apollo Guidance Computer: Architecture And Operation (Springer Praxis Books / Space Exploration)', 439, 4.29, '9781440000000', '2010-07-01', 6) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO books (book_id, title, total_pages, rating, isbn, published_date, publisher_id) VALUES (10, 'Minds and Computers: An Introduction to the Philosophy of Artificial Intelligence', 222, 3.54, '9780750000000', '2007-02-13', 7) ON CONFLICT (book_id) DO NOTHING; +INSERT INTO book_authors (book_id, author_id) VALUES (1, 1); +INSERT INTO book_authors (book_id, author_id) VALUES (2, 8); +INSERT INTO book_authors (book_id, author_id) VALUES (3, 7); +INSERT INTO book_authors (book_id, author_id) VALUES (4, 6); +INSERT INTO book_authors (book_id, author_id) VALUES (5, 5); +INSERT INTO book_authors (book_id, author_id) VALUES (6, 4); +INSERT INTO book_authors (book_id, author_id) VALUES (7, 3); +INSERT INTO book_authors (book_id, author_id) VALUES (8, 2); +INSERT INTO book_authors (book_id, author_id) VALUES (9, 4); +INSERT INTO book_authors (book_id, author_id) VALUES (10, 1); +""") # 4) Use pandas to print one of the tables as dataframes using read_sql function +result_dataFrame=pd.read_sql("Select * from publishers;", engine) +print(result_dataFrame) \ No newline at end of file diff --git a/src/solution.py b/src/solution.py index ace889f70..636141c44 100644 --- a/src/solution.py +++ b/src/solution.py @@ -45,7 +45,7 @@ CONSTRAINT fk_book FOREIGN KEY(book_id) REFERENCES books(book_id) ON DELETE CASCADE, CONSTRAINT fk_author FOREIGN KEY(author_id) REFERENCES authors(author_id) ON DELETE CASCADE ); -""") +""" # 3) Execute the SQL sentences to insert your data using the SQLAlchemy's execute function engine.execute(""" diff --git a/src/start_db.sh b/src/start_db.sh new file mode 100755 index 000000000..4bfa35bd6 --- /dev/null +++ b/src/start_db.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +# Starts posgreSQL service, creates gitpod user and creates database 'sample_db' +sudo service postgresql start +psql -U postgres -c "CREATE USER gitpod;" +psql -U postgres -c "CREATE DATABASE sample_db OWNER gitpod;" \ No newline at end of file