From dff343f109f172c851e8373a11b73dfa1426e98c Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Feb 23 2018 06:08:39 +0000 Subject: [PATCH 1/2] README: don't assume any particular location for git checkout --- diff --git a/README.md b/README.md index 5ed08a5..0d65260 100644 --- a/README.md +++ b/README.md @@ -17,11 +17,11 @@ Install dependencies: Run the server:: $ cp conf/settings.py.example conf/settings.py - $ PYTHONPATH=~/waiverdb DEV=true python waiverdb/manage.py run -h localhost -p 5004 --debugger + $ PYTHONPATH=. DEV=true python waiverdb/manage.py run -h localhost -p 5004 --debugger Migrate the db:: - $ PYTHONPATH=~/waiverdb DEV=true python waiverdb/manage.py db upgrade + $ PYTHONPATH=. DEV=true python waiverdb/manage.py db upgrade The server is now running at and API calls can be sent to . All data is stored inside `/var/tmp/waiverdb_db.sqlite`. From 3e56879bbc781f776af3a4e138401f803e20df08 Mon Sep 17 00:00:00 2001 From: Dan Callaghan Date: Feb 23 2018 06:31:01 +0000 Subject: [PATCH 2/2] no more SQLite We run the dev server and test suite against a local Postgres. --- diff --git a/README.md b/README.md index 0d65260..9776405 100644 --- a/README.md +++ b/README.md @@ -14,18 +14,30 @@ Install dependencies: $ sudo dnf builddep waiverdb.spec -Run the server:: +Configure Postgres on the local machine, with a `waiverdb` database: + + $ sudo dnf install postgresql-server + $ sudo postgresql-setup --initdb + $ sudo systemctl enable --now postgresql + $ sudo -u postgres createuser --superuser $USER + $ createdb waiverdb + +Create a local configuration file: $ cp conf/settings.py.example conf/settings.py - $ PYTHONPATH=. DEV=true python waiverdb/manage.py run -h localhost -p 5004 --debugger -Migrate the db:: +Populate the database: $ PYTHONPATH=. DEV=true python waiverdb/manage.py db upgrade +Run the server: + + $ PYTHONPATH=. DEV=true python waiverdb/manage.py run -h localhost -p 5004 --debugger + The server is now running at and API calls can be sent to -. All data is stored inside `/var/tmp/waiverdb_db.sqlite`. -You can verify the server is running correctly by visiting . +. All data is stored in the `waiverdb` Postgres +database on the local machine. You can verify the server is running correctly +by visiting . ## Adjusting configuration @@ -40,6 +52,10 @@ You can run this test suite with the following command:: $ py.test tests/ +The test suite will drop and re-create a Postgres database named +`waiverdb_test`. By default, it expects to have superuser access to Postgres on +the local machine. + To test against all supported versions of Python, you can use tox:: $ sudo dnf install python3-tox diff --git a/conf/settings.py.example b/conf/settings.py.example index ec06621..3f4da46 100644 --- a/conf/settings.py.example +++ b/conf/settings.py.example @@ -2,7 +2,7 @@ # in `waiverdb/config.py`. SECRET_KEY = 'replace-me-with-something-random' #DATABASE_URI = 'postgresql+psycopg2://dbuser:dbpassword@dbhost:dbport/dbname' -DATABASE_URI = 'sqlite:////var/tmp/waiverdb_db.sqlite' +DATABASE_URI = 'postgresql+psycopg2:///waiverdb' JOURNAL_LOGGING = False #SHOW_DB_URI = False HOST= '0.0.0.0' diff --git a/tests/conftest.py b/tests/conftest.py index 8a04e6b..b058202 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -1,7 +1,9 @@ # SPDX-License-Identifier: GPL-2.0+ +from copy import copy import pytest -from waiverdb.app import create_app, init_db +from sqlalchemy import create_engine +from waiverdb.app import create_app @pytest.fixture(scope='session') @@ -21,7 +23,17 @@ def app(request): @pytest.fixture(scope='session') def db(app): """Session-wide test database.""" - db = init_db(app) + from waiverdb.models import db + dbname = db.engine.url.database + # In order to drop and re-create the database, we have to connect to + # template1 database in special AUTOCOMMIT isolation level. + dburl = copy(db.engine.url) + dburl.database = 'template1' + with create_engine(dburl).connect() as connection: + connection.execution_options(isolation_level='AUTOCOMMIT') + connection.execute('DROP DATABASE IF EXISTS {}'.format(dbname)) + connection.execute('CREATE DATABASE {}'.format(dbname)) + db.create_all() return db diff --git a/waiverdb/app.py b/waiverdb/app.py index 9588b78..b8abf78 100644 --- a/waiverdb/app.py +++ b/waiverdb/app.py @@ -88,12 +88,6 @@ def create_app(config_obj=None): return app -def init_db(app): - with app.app_context(): - db.create_all() - return db - - def healthcheck(): """ Request handler for performing an application-level health check. This is diff --git a/waiverdb/config.py b/waiverdb/config.py index 120672f..318ba57 100644 --- a/waiverdb/config.py +++ b/waiverdb/config.py @@ -12,7 +12,7 @@ class Config(object): fedmsg when new waivers are created. """ DEBUG = True - DATABASE_URI = 'sqlite://' + DATABASE_URI = 'postgresql+psycopg2:///waiverdb' JOURNAL_LOGGING = False HOST = '0.0.0.0' PORT = 5004 @@ -42,7 +42,6 @@ class ProductionConfig(Config): class DevelopmentConfig(Config): SQLALCHEMY_TRACK_MODIFICATIONS = True TRAP_BAD_REQUEST_ERRORS = True - DATABASE_URI = 'sqlite:////var/tmp/waiverdb_db.sqlite' SHOW_DB_URI = True # The location of the client_secrets.json file used for API authentication OIDC_CLIENT_SECRETS = os.path.join( @@ -56,6 +55,9 @@ class DevelopmentConfig(Config): class TestingConfig(Config): SQLALCHEMY_TRACK_MODIFICATIONS = True TRAP_BAD_REQUEST_ERRORS = True + # Beware that the tests constantly wipe and re-create this database! + # Do not configure this to point at any data you care about! + DATABASE_URI = 'postgresql+psycopg2:///waiverdb_test' TESTING = True OIDC_CLIENT_SECRETS = os.path.join( os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'tests',