Skip to content
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

update postgres.dev dependency to postgresql_16.dev #1259

Merged
merged 5 commits into from
Jan 13, 2025
Merged
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
30 changes: 30 additions & 0 deletions examples/python-psycopg2/database.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import os
import psycopg2
from psycopg2 import Error
import time

def get_db_connection():
max_retries = 3
retry_delay = 2 # seconds

for attempt in range(max_retries):
try:
connection = psycopg2.connect(
host=os.getenv('PGHOST'),
database=os.getenv('PGDATABASE'),
user=os.getenv('PGUSER'),
password=os.getenv('PGPASSWORD'),
port=os.getenv('PGPORT')
)
return connection
except Error as e:
if "starting up" in str(e).lower():
if attempt < max_retries - 1: # Don't sleep on last attempt
time.sleep(retry_delay)
continue
print(f"Error connecting to PostgreSQL: {e}")
return None

def close_connection(connection):
if connection:
connection.close()
83 changes: 83 additions & 0 deletions examples/python-psycopg2/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from database import get_db_connection, close_connection

def create_table():
connection = get_db_connection()
if connection:
try:
cursor = connection.cursor()

# Create a sample table
create_table_query = '''
CREATE TABLE IF NOT EXISTS users (
id SERIAL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) UNIQUE NOT NULL
)
'''
cursor.execute(create_table_query)

# Commit the changes
connection.commit()
print("Table created successfully")

except Exception as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)

def insert_user(name, email):
connection = get_db_connection()
if connection:
try:
cursor = connection.cursor()

# Insert a new user
insert_query = '''
INSERT INTO users (name, email)
VALUES (%s, %s)
RETURNING id
'''
cursor.execute(insert_query, (name, email))
user_id = cursor.fetchone()[0]

# Commit the changes
connection.commit()
print(f"User inserted successfully with ID: {user_id}")

except Exception as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)

def get_all_users():
connection = get_db_connection()
if connection:
try:
cursor = connection.cursor()

# Select all users
select_query = "SELECT * FROM users"
cursor.execute(select_query)
users = cursor.fetchall()

for user in users:
print(f"ID: {user[0]}, Name: {user[1]}, Email: {user[2]}")

except Exception as e:
print(f"Error: {e}")
finally:
cursor.close()
close_connection(connection)

if __name__ == "__main__":
# Create the table
create_table()

# Insert some sample users
insert_user("John Doe", "[email protected]")
insert_user("Jane Smith", "[email protected]")

# Retrieve and display all users
get_all_users()
1 change: 1 addition & 0 deletions examples/python-psycopg2/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
psycopg2-binary>=2.9.10
11 changes: 4 additions & 7 deletions src/providers/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,18 +160,15 @@ enum EntryPoint {

impl PythonProvider {
fn setup(&self, app: &App, env: &Environment) -> Result<Option<Phase>> {
let mut setup = Phase::setup(None);

let mut pkgs: Vec<Pkg> = vec![];
let (python_base_package, nix_archive) = PythonProvider::get_nix_python_package(app, env)?;

pkgs.append(&mut vec![python_base_package]);

if PythonProvider::is_using_postgres(app, env)? {
// Postgres requires postgresql and gcc on top of the original python packages

// .dev variant is required in order for pg_config to be available, which is needed by psycopg2
// the .dev variant requirement is caused by this change in nix pkgs:
// https://github.com/NixOS/nixpkgs/blob/43eac3c9e618c4114a3441b52949609ea2104670/pkgs/servers/sql/postgresql/pg_config.sh
pkgs.append(&mut vec![Pkg::new("postgresql.dev")]);
pkgs.append(&mut vec![Pkg::new("postgresql_16.dev")]);
}

if PythonProvider::is_django(app, env)? && PythonProvider::is_using_mysql(app, env)? {
Expand All @@ -183,7 +180,7 @@ impl PythonProvider {
pkgs.append(&mut vec![Pkg::new("pipenv")]);
}

let mut setup = Phase::setup(Some(pkgs));
setup.add_nix_pkgs(&pkgs);
setup.set_nix_archive(nix_archive);

if PythonProvider::uses_dep(app, "cairo")? {
Expand Down
42 changes: 42 additions & 0 deletions tests/docker_run_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,6 +940,48 @@ async fn test_python_asdf_poetry() {
assert!(output.contains("Poetry (version 1.8.2)"), "{}", output);
}

#[tokio::test]
async fn test_python_psycopg2() -> Result<()> {
// Create the network
let n = create_network();
let network_name = n.name.clone();

// Create the postgres instance
let c = run_postgres();
let container_name = c.name.clone();

// Attach the postgres instance to the network
attach_container_to_network(n.name, container_name.clone());

let name = match simple_build("./examples/python-psycopg2").await {
Ok(name) => name,
Err(err) => {
// Cleanup containers and networks, and then error
stop_and_remove_container(container_name);
remove_network(network_name);
return Err(err);
}
};

let output = run_image(
&name,
Some(Config {
environment_variables: c.config.unwrap().environment_variables,
network: Some(network_name.clone()),
}),
)
.await;

println!("OUTPUT = {}", output);

// Cleanup containers and networks
stop_and_remove_container(container_name);
remove_network(network_name);

assert!(output.contains("User inserted successfully with ID"));
Ok(())
}

#[tokio::test]
async fn test_django() -> Result<()> {
// Create the network
Expand Down
3 changes: 1 addition & 2 deletions tests/snapshots/generate_plan_tests__python_django.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: tests/generate_plan_tests.rs
expression: plan
snapshot_kind: text
---
{
"providers": [],
Expand Down Expand Up @@ -36,7 +35,7 @@ snapshot_kind: text
"name": "setup",
"nixPkgs": [
"python3",
"postgresql.dev",
"postgresql_16.dev",
"gcc"
],
"nixLibs": [
Expand Down
3 changes: 1 addition & 2 deletions tests/snapshots/generate_plan_tests__python_postgres.snap
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
source: tests/generate_plan_tests.rs
expression: plan
snapshot_kind: text
---
{
"providers": [],
Expand Down Expand Up @@ -36,7 +35,7 @@ snapshot_kind: text
"name": "setup",
"nixPkgs": [
"python3",
"postgresql.dev",
"postgresql_16.dev",
"gcc"
],
"nixLibs": [
Expand Down
52 changes: 52 additions & 0 deletions tests/snapshots/generate_plan_tests__python_psycopg2.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
source: tests/generate_plan_tests.rs
expression: plan
---
{
"providers": [],
"buildImage": "[build_image]",
"variables": {
"NIXPACKS_METADATA": "python,postgres",
"PIP_DEFAULT_TIMEOUT": "100",
"PIP_DISABLE_PIP_VERSION_CHECK": "1",
"PIP_NO_CACHE_DIR": "1",
"PYTHONDONTWRITEBYTECODE": "1",
"PYTHONFAULTHANDLER": "1",
"PYTHONHASHSEED": "random",
"PYTHONUNBUFFERED": "1"
},
"phases": {
"install": {
"name": "install",
"dependsOn": [
"setup"
],
"cmds": [
"python -m venv --copies /opt/venv && . /opt/venv/bin/activate && pip install -r requirements.txt"
],
"cacheDirectories": [
"/root/.cache/pip"
],
"paths": [
"/opt/venv/bin"
]
},
"setup": {
"name": "setup",
"nixPkgs": [
"python3",
"postgresql_16.dev",
"gcc"
],
"nixLibs": [
"zlib",
"stdenv.cc.cc.lib"
],
"nixOverlays": [],
"nixpkgsArchive": "[archive]"
}
},
"start": {
"cmd": "python main.py"
}
}
Loading