diff --git a/app/models.py b/app/models.py index d644289bf..bc67d9338 100644 --- a/app/models.py +++ b/app/models.py @@ -247,6 +247,8 @@ class Job(db.Model): index=False, unique=False, nullable=True) + created_by = db.relationship('User') + created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False) VERIFY_CODE_TYPES = ['email', 'sms'] diff --git a/migrations/versions/0009_created_by_for_jobs.py b/migrations/versions/0009_created_by_for_jobs.py new file mode 100644 index 000000000..edc9ee664 --- /dev/null +++ b/migrations/versions/0009_created_by_for_jobs.py @@ -0,0 +1,34 @@ +"""empty message + +Revision ID: 0009_created_by_for_jobs +Revises: 0008_archive_template +Create Date: 2016-04-26 14:54:56.852642 + +""" + +# revision identifiers, used by Alembic. +revision = '0009_created_by_for_jobs' +down_revision = '0008_archive_template' + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.add_column('jobs', sa.Column('created_by_id', postgresql.UUID(as_uuid=True), nullable=True)) + op.create_index(op.f('ix_jobs_created_by_id'), 'jobs', ['created_by_id'], unique=False) + op.create_foreign_key(None, 'jobs', 'users', ['created_by_id'], ['id']) + op.get_bind() + op.execute('UPDATE jobs SET created_by_id = \ + (SELECT user_id FROM user_to_service WHERE jobs.service_id = user_to_service.service_id LIMIT 1)') + op.alter_column('jobs', 'created_by_id', nullable=False) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_constraint(None, 'jobs', type_='foreignkey') + op.drop_index(op.f('ix_jobs_created_by_id'), table_name='jobs') + op.drop_column('jobs', 'created_by_id') + ### end Alembic commands ### diff --git a/tests/app/conftest.py b/tests/app/conftest.py index b356fba30..1885c049c 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -235,7 +235,8 @@ def sample_job(notify_db, 'template_id': template.id, 'original_file_name': 'some.csv', 'notification_count': notification_count, - 'created_at': created_at + 'created_at': created_at, + 'created_by': service.created_by } job = Job(**data) dao_create_job(job) @@ -275,7 +276,8 @@ def sample_email_job(notify_db, 'service': service, 'template_id': template.id, 'original_file_name': 'some.csv', - 'notification_count': 1 + 'notification_count': 1, + 'created_by': service.created_by } job = Job(**data) dao_create_job(job) diff --git a/tests/app/dao/test_jobs_dao.py b/tests/app/dao/test_jobs_dao.py index 636bc163f..b8b03c231 100644 --- a/tests/app/dao/test_jobs_dao.py +++ b/tests/app/dao/test_jobs_dao.py @@ -20,7 +20,8 @@ def test_create_job(sample_template): 'service_id': sample_template.service.id, 'template_id': sample_template.id, 'original_file_name': 'some.csv', - 'notification_count': 1 + 'notification_count': 1, + 'created_by': sample_template.created_by } job = Job(**data) diff --git a/tests/app/job/test_rest.py b/tests/app/job/test_rest.py index a604a346f..fffa384cd 100644 --- a/tests/app/job/test_rest.py +++ b/tests/app/job/test_rest.py @@ -97,7 +97,8 @@ def test_create_job(notify_api, sample_template, mocker, fake_uuid): 'service': str(sample_template.service.id), 'template': str(sample_template.id), 'original_file_name': 'thisisatest.csv', - 'notification_count': 1 + 'notification_count': 1, + 'created_by': str(sample_template.created_by.id) } path = '/service/{}/job'.format(sample_template.service.id) auth_header = create_authorization_header(