In other words, how to generate a migration for database creation, or is this an anti-pattern ?
I'm running
python manage.py db migrate -m "init"
getting
sqlalchemy.exc.OperationalError: (psycopg2.OperationalError) FATAL: database "auth" does not exist
manage.py
# manage.py
import os
import unittest
import coverage
from flask_script import Manager
from authentek.app import create_app as initialize_app
from authentek.logger import log
from authentek.extensions import db, migrate
from authentek.database.models import User, BlacklistToken # noqa
from flask_migrate import MigrateCommand
app = initialize_app(False, True)
manager = Manager(app)
migrate.init_app(app)
manager.add_command('db', MigrateCommand)
print(app.extensions)
COV = coverage.coverage(
branch=True,
include='authentek/*',
omit=[
'authentek/tests/*',
]
)
COV.start()
@manager.command
def test():
"""Runs the unit tests without test coverage."""
tests = unittest.TestLoader().discover('authentek/tests', pattern='test*.py')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
return 0
return 1
@manager.command
def cov():
"""Runs the unit tests with coverage."""
tests = unittest.TestLoader().discover('authentek/tests')
result = unittest.TextTestRunner(verbosity=2).run(tests)
if result.wasSuccessful():
COV.stop()
COV.save()
print('Coverage Summary:')
COV.report()
basedir = os.path.abspath(os.path.dirname(__file__))
covdir = os.path.join(basedir, 'tmp/coverage')
COV.html_report(directory=covdir)
print('HTML version: file://%s/index.html' % covdir)
COV.erase()
return 0
return 1
@manager.command
def drop_db():
"""Drops the db tables."""
db.drop_all()
if __name__ == '__main__':
manager.run()
For security purposes you do not want to give your application the power to create or destroy databases, all you want is that the application can have read and write access to a single database.
For this reason, you often have to create the database either manually, or through a separate script that has database administration rights.
In the case of unit tests I suppose there is no risk in configuring database admin credentials as long as you have a separate database server for tests, so there is no risk of affecting other databases outside of the test environment. In this case you can add functions that create and destroy your database using SQL.