(feat): Changed from SQLite3 to PostgreSQL
This commit is contained in:
		
							parent
							
								
									036233f141
								
							
						
					
					
						commit
						502de48484
					
				
					 2 changed files with 31 additions and 19 deletions
				
			
		
							
								
								
									
										37
									
								
								database.py
									
										
									
									
									
								
							
							
						
						
									
										37
									
								
								database.py
									
										
									
									
									
								
							|  | @ -1,41 +1,52 @@ | ||||||
| import sqlite3 |  | ||||||
| import os | import os | ||||||
|  | import psycopg2 | ||||||
|  | from psycopg2 import IntegrityError | ||||||
| from dotenv import load_dotenv | from dotenv import load_dotenv | ||||||
| 
 | 
 | ||||||
| load_dotenv() | load_dotenv() | ||||||
| 
 | 
 | ||||||
| DATABASE_URL = os.getenv("DATABASE_FILE") | def get_connection(): | ||||||
|  |     """Return a database connection.""" | ||||||
|  |     return psycopg2.connect( | ||||||
|  |         host=os.getenv("PG_HOST"), | ||||||
|  |         port=os.getenv("PG_PORT"), | ||||||
|  |         dbname=os.getenv("PG_DATABASE"), | ||||||
|  |         user=os.getenv("PG_USER"), | ||||||
|  |         password=os.getenv("PG_PASSWORD"), | ||||||
|  |         connect_timeout=10 | ||||||
|  |     ) | ||||||
| 
 | 
 | ||||||
| def init_db(): | def init_db(): | ||||||
|     conn = sqlite3.connect(DATABASE_URL) |     conn = get_connection() | ||||||
|     cursor = conn.cursor() |     cursor = conn.cursor() | ||||||
|     cursor.execute(""" |     cursor.execute(""" | ||||||
|         CREATE TABLE IF NOT EXISTS subscribers ( |         CREATE TABLE IF NOT EXISTS subscribers ( | ||||||
|             id INTEGER PRIMARY KEY AUTOINCREMENT, |             id SERIAL PRIMARY KEY, | ||||||
|             email TEXT UNIQUE NOT NULL |             email TEXT UNIQUE NOT NULL | ||||||
|         ) |         ) | ||||||
|     """) |     """) | ||||||
|     conn.commit() |     conn.commit() | ||||||
|  |     cursor.close() | ||||||
|     conn.close() |     conn.close() | ||||||
| 
 | 
 | ||||||
| def add_email(email): | def add_email(email): | ||||||
|     try: |     try: | ||||||
|         with sqlite3.connect(DATABASE_URL, timeout=10) as conn: |         with get_connection() as conn: | ||||||
|             cursor = conn.cursor() |             with conn.cursor() as cursor: | ||||||
|             cursor.execute("INSERT INTO subscribers (email) VALUES (?)", (email,)) |                 cursor.execute("INSERT INTO subscribers (email) VALUES (%s)", (email,)) | ||||||
|                 conn.commit() |                 conn.commit() | ||||||
|         return True |         return True | ||||||
|     except sqlite3.IntegrityError: |     except IntegrityError: | ||||||
|         return False |         return False | ||||||
|     except sqlite3.OperationalError as e: |     except psycopg2.OperationalError as e: | ||||||
|         print(f"Operational Error: {e}") |         print(f"Error: {e}") | ||||||
|         return False |         return False | ||||||
| 
 | 
 | ||||||
| def remove_email(email): | def remove_email(email): | ||||||
|     try: |     try: | ||||||
|         with sqlite3.connect(DATABASE_URL, timeout=10) as conn: |         with get_connection() as conn: | ||||||
|             cursor = conn.cursor() |             with conn.cursor() as cursor: | ||||||
|             cursor.execute("DELETE FROM subscribers WHERE email = ?", (email,)) |                 cursor.execute("DELETE FROM subscribers WHERE email = %s", (email,)) | ||||||
|                 conn.commit() |                 conn.commit() | ||||||
|                 if cursor.rowcount > 0: |                 if cursor.rowcount > 0: | ||||||
|                     return True |                     return True | ||||||
|  |  | ||||||
|  | @ -1,2 +1,3 @@ | ||||||
| flask | flask | ||||||
| python-dotenv | python-dotenv | ||||||
|  | psycopg2-binary | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Blake Ridgway
						Blake Ridgway