Skip to content

Environment patches #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions .env.example

This file was deleted.

5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
"workbench.startupEditor": "readme",
"workbench.editorAssociations": {
"*.md": "vscode.markdown.preview.editor"
}
},
"githubPullRequests.ignoredPullRequestBranches": [
"main"
]
}
4 changes: 2 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -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

Expand Down
92 changes: 89 additions & 3 deletions src/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion src/solution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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("""
Expand Down
6 changes: 6 additions & 0 deletions src/start_db.sh
Original file line number Diff line number Diff line change
@@ -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;"