Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Adds support for multiple different helper roles #88

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
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
14 changes: 5 additions & 9 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@

ENV = os.getenv('OH_QUEUE_ENV', 'dev')

if ENV in ('dev', 'staging'):
DEBUG = True
elif ENV == 'prod':
DEBUG = False
DEBUG = ENV in ('dev', 'staging')

SECRET_KEY = os.getenv('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL').replace('mysql://', 'mysql+pymysql://')
if ENV == 'dev':
SECRET_KEY = 'dev'
SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
else:
SECRET_KEY = os.getenv('SECRET_KEY')
SQLALCHEMY_DATABASE_URI = os.getenv('DATABASE_URL').replace('mysql://', 'mysql+pymysql://')
SECRET_KEY = SECRET_KEY or 'dev'
SQLALCHEMY_DATABASE_URI = SQLALCHEMY_DATABASE_URI or 'sqlite:///' + os.path.join(basedir, 'app.db')

SQLALCHEMY_TRACK_MODIFICATIONS = False
DATABASE_CONNECT_OPTIONS = {}
Expand Down
21 changes: 18 additions & 3 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
#!/usr/bin/env python3
import datetime
import functools
import random
import sys

from flask_migrate import Migrate, MigrateCommand
from flask_script import Manager
import names

from oh_queue import app, socketio
from oh_queue.models import db, Ticket, User, TicketStatus

migrate = Migrate(app, db)

manager = Manager(app)
manager.add_command('db', MigrateCommand)

def not_in_production(f):
@functools.wraps(f)
def wrapper(*args, **kwargs):
if app.config.get('ENV') == 'prod':
print('this commend should not be run in production. Aborting')
sys.exit(1)
return f(*args, **kwargs)
return wrapper

@manager.command
@not_in_production
def seed():
print('Seeding...')
for i in range(20):
Expand Down Expand Up @@ -41,6 +57,7 @@ def seed():


@manager.command
@not_in_production
def resetdb():
print('Dropping tables...')
db.drop_all(app=app)
Expand All @@ -49,11 +66,9 @@ def resetdb():
seed()

@manager.command
@not_in_production
def server():
socketio.run(app)

if __name__ == '__main__':
if app.config.get('ENV') == 'prod':
print('manage.py should not be run in production. Aborting')
sys.exit(1)
manager.run()
106 changes: 106 additions & 0 deletions migrations/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
# Generating Migrations

To generate migrations, you'll need to be running MySQL locally.

## Installing MySQL

### OSX

```
brew install mysql
```

Reccomended SQL Client for local exploration: [Sequel Pro](https://sequelpro.com/)

## Configuring MySQL
Start MySQL and connect
```
mysql.server start
mysql -u root
```
The password for root is blank by default. Just hit enter and you will enter the mysql console.
See the [MySQL docs](http://dev.mysql.com/doc/mysql-getting-started/en/) for more info.

Run the following commands to create a user and a table for OK

```
create database ohdevel;
CREATE USER 'ohdev'@'localhost';
GRANT ALL PRIVILEGES ON ohdevel.* TO 'ohdev'@'localhost';
```

## Running a migration

If the `master` branch is the current state of the production branch - start by checking out the master branch.

`git checkout master`

Make sure OK is using MySQL and not sqlite by setting the `DATABASE_URL`
environment variable. For Bash,
```
export DATABASE_URL=mysql://ohdev:@127.0.0.1:3306/ohdevel?charset=utf8mb4
```

```
# Rebuild the database
./manage.py resetdb
./manage.py db upgrade # Make sure this does not crash. If it does: see Common Errors below
```

Now you can checkout the branch with changes:

`git checkout feature-branch`

```
./manage.py db migrate -m "Added new feature"
```
That should produce the output along the lines of
```
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
INFO [alembic.autogenerate.compare] Detected added column 'assignment.published_scores'
Generating /dev/ok/server/migrations/versions/6ce2cf5c4534_publish_scores_for_assignments.py ... done
```

## Inspecting the output

Check the file contents before running the migrations to make sure the changes make sense. There should not be unncessary changes. It's ok to manually make changes to the script (or manually write a migration file) if needed.

## Deploying the migration.
Make sure the deploy the app. After it's running, run
```
dokku run officehours-web ./manage.py db upgrade
```
to upgrade.

## Common errors

If you are getting long tracebacks that end in somethng like this
```
raise errorclass(errno, errorvalue)
sqlalchemy.exc.ProgrammingError: (pymysql.err.ProgrammingError) (1146, "Table 'ok-dev.client' doesn't exist") [SQL: 'ALTER TABLE client ADD COLUMN created DATETIME NOT NULL DEFAULT now()']
```
or
```
raise util.CommandError("Target database is not up to date.")
alembic.util.exc.CommandError: Target database is not up to date.
```

It's likely that Alembic is trying to rerun migrations (or has not run all of the migrations in the folder)

If you are _sure_ that the DB is up to date - find the ID most recent migration run (the most recent commit in them migrations folder should reveal the file most recently created) and then run

```
./manage.py db stamp f7f27412d13f
```

This tells alembic that the DB is at revision `f7f27412d13f`. Now running `./manage.py db upgrade` should result in this output

```
$ ./manage.py db upgrade
INFO [alembic.runtime.migration] Context impl MySQLImpl.
INFO [alembic.runtime.migration] Will assume non-transactional DDL.
$
```

If that doesn't resolve it - make sure there a not unrun migrations in the branch.
45 changes: 45 additions & 0 deletions migrations/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# A generic, single database configuration.

[alembic]
# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
87 changes: 87 additions & 0 deletions migrations/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
import logging

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
logger = logging.getLogger('alembic.env')

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option('sqlalchemy.url',
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.


def run_migrations_offline():
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""
url = config.get_main_option("sqlalchemy.url")
context.configure(url=url)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""

# this callback is used to prevent an auto-migration from being generated
# when there are no changes to the schema
# reference: http://alembic.readthedocs.org/en/latest/cookbook.html
def process_revision_directives(context, revision, directives):
if getattr(config.cmd_opts, 'autogenerate', False):
script = directives[0]
if script.upgrade_ops.is_empty():
directives[:] = []
logger.info('No changes in schema detected.')

engine = engine_from_config(config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)

connection = engine.connect()
context.configure(connection=connection,
target_metadata=target_metadata,
process_revision_directives=process_revision_directives,
**current_app.extensions['migrate'].configure_args)

try:
with context.begin_transaction():
context.run_migrations()
finally:
connection.close()

if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
23 changes: 23 additions & 0 deletions migrations/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision}
Create Date: ${create_date}

"""

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}

from alembic import op
import sqlalchemy as sa
from oh_queue.models import *
${imports if imports else ""}

def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
31 changes: 31 additions & 0 deletions migrations/versions/826d994e7e11_multiple_user_roles.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""Multiple user roles

Revision ID: 826d994e7e11
Revises: None
Create Date: 2016-10-26 14:55:36.981449

"""

# revision identifiers, used by Alembic.
revision = '826d994e7e11'
down_revision = None

from alembic import op
import sqlalchemy as sa
from oh_queue.models import *
from sqlalchemy.dialects import mysql

def upgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('role', EnumType(Role), default=Role.student, nullable=False))
op.create_index(op.f('ix_user_role'), 'user', ['role'], unique=False)
op.drop_column('user', 'is_staff')
### end Alembic commands ###


def downgrade():
### commands auto generated by Alembic - please adjust! ###
op.add_column('user', sa.Column('is_staff', mysql.TINYINT(display_width=1), autoincrement=False, nullable=True))
op.drop_index(op.f('ix_user_role'), table_name='user')
op.drop_column('user', 'role')
### end Alembic commands ###
Loading