Skip to content

Commit 9a9e405

Browse files
CopilotSupraSummus
andcommitted
Add SQLAlchemy, Flask-Migrate, and Flask-Admin to Flask init scripts
New scripts: - flask/31-environ.sh: python-dotenv + example.env - flask/32-sqlalchemy.sh: Flask-SQLAlchemy + psycopg - flask/33-alembic.sh: Flask-Migrate (Alembic wrapper) - flask/34-admin.sh: Flask-Admin Updated: - flask/30-flask.sh: app.py now loads config from .env, sets up SQLAlchemy - flask/everything.sh: sources all new scripts - flask/test.sh: copies example.env before running pytest - .github/workflows/test.yaml: passes DATABASE_URL for Flask tests - flask/example/: regenerated - AGENTS.md: clarify rule to prevent plan-only sessions Co-authored-by: SupraSummus <15822143+SupraSummus@users.noreply.github.com>
1 parent 1bebdfb commit 9a9e405

17 files changed

Lines changed: 536 additions & 18 deletions

.github/workflows/test.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ jobs:
4343
DATABASE_URL: postgresql://postgres:postgrespass@localhost:5432/testdb
4444
- run: ./test.sh
4545
working-directory: flask
46+
env:
47+
DATABASE_URL: postgresql://postgres:postgrespass@localhost:5432/testdb
4648
- run: ./fastapi/test.sh
4749
env:
4850
DATABASE_URL: postgresql+asyncpg://postgres:postgrespass@localhost:5432/postgres

AGENTS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
## Rules
44

55
- We aim to generate a bare usable minimum. Do not include example models, placeholder data, or unnecessary boilerplate that the user would have to delete.
6+
- When asked to add a feature, implement actual code changes. Planning without implementation is not acceptable.

flask/30-flask.sh

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,24 @@ poetry add 'flask==*'
55

66
# Create the main application file
77
cat > "app.py" <<EOF
8+
import os
9+
10+
from dotenv import load_dotenv
811
from flask import Flask
12+
from flask_sqlalchemy import SQLAlchemy
913
14+
load_dotenv()
1015
1116
app = Flask(__name__)
17+
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'changeme')
18+
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ['DATABASE_URL']
19+
20+
db = SQLAlchemy(app)
1221
1322
1423
@app.route('/')
1524
def index():
1625
return 'Hello, World!'
17-
18-
19-
if __name__ == '__main__':
20-
app.run()
2126
EOF
2227

2328
# Create test file in root

flask/31-environ.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
3+
# Install python-dotenv for environment configuration
4+
poetry add 'python-dotenv==*'
5+
6+
# Create example.env file
7+
cat <<EOF > example.env
8+
DEBUG=True
9+
DATABASE_URL=postgresql://localhost/$POETRY_PROJECT_NAME
10+
SECRET_KEY=your_secret_key_here
11+
EOF
12+
13+
# Add .env to .gitignore
14+
echo ".env" >> .gitignore
15+
16+
# Add to README
17+
cat <<EOF >> "README.md"
18+
19+
## Environment Variables
20+
21+
Configure quick-start env vars:
22+
23+
cp example.env .env
24+
EOF
25+
26+
git add --all
27+
git commit -m "Install python-dotenv and configure environment"

flask/32-sqlalchemy.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/bin/bash
2+
3+
# Install Flask-SQLAlchemy and psycopg
4+
poetry add 'flask-sqlalchemy==*'
5+
poetry add 'psycopg==*'
6+
7+
# Create models module
8+
cat > "models.py" <<EOF
9+
from app import db # noqa: F401
10+
EOF
11+
12+
poetry run isort .
13+
git add --all
14+
git commit -m "Install and configure Flask-SQLAlchemy"

flask/33-alembic.sh

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#!/bin/bash
2+
3+
# Install Flask-Migrate (Alembic wrapper for Flask-SQLAlchemy)
4+
poetry add 'flask-migrate==*'
5+
6+
# Add Flask-Migrate initialization to app.py
7+
sed -i '/^from flask_sqlalchemy import SQLAlchemy/a from flask_migrate import Migrate' app.py
8+
sed -i '/^db = SQLAlchemy(app)/a migrate = Migrate(app, db)' app.py
9+
10+
# Add to README
11+
cat <<EOF >> "README.md"
12+
13+
## Database Migrations
14+
15+
Create a new migration:
16+
17+
poetry run flask db migrate -m "Description"
18+
19+
Apply migrations:
20+
21+
poetry run flask db upgrade
22+
EOF
23+
24+
poetry run isort .
25+
git add --all
26+
git commit -m "Install and configure Flask-Migrate for database migrations"

flask/34-admin.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
3+
# Install Flask-Admin
4+
poetry add 'flask-admin==*'
5+
6+
# Add Flask-Admin initialization to app.py
7+
sed -i '/^from flask_migrate import Migrate/a from flask_admin import Admin' app.py
8+
sed -i '/^migrate = Migrate(app, db)/a admin = Admin(app)' app.py
9+
10+
poetry run isort .
11+
git add --all
12+
git commit -m "Install and configure Flask-Admin"

flask/everything.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,8 @@ source "$script_dir/../20-poetry.sh"
1111
source "$script_dir/../21-flake.sh"
1212
source "$script_dir/../22-isort.sh"
1313
source "$script_dir/../23-pytest.sh"
14+
source "$script_dir/31-environ.sh"
1415
source "$script_dir/30-flask.sh"
16+
source "$script_dir/32-sqlalchemy.sh"
17+
source "$script_dir/33-alembic.sh"
18+
source "$script_dir/34-admin.sh"

flask/example/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
__pycache__/
2+
.env

flask/example/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,19 @@ This project uses Poetry for dependency management. To install the dependencies,
99
To activate the virtual environment, run:
1010

1111
poetry shell
12+
13+
## Environment Variables
14+
15+
Configure quick-start env vars:
16+
17+
cp example.env .env
18+
19+
## Database Migrations
20+
21+
Create a new migration:
22+
23+
poetry run flask db migrate -m "Description"
24+
25+
Apply migrations:
26+
27+
poetry run flask db upgrade

0 commit comments

Comments
 (0)