diff --git a/freight/models/app.py b/freight/models/app.py index 720eeee5..f2f9222f 100644 --- a/freight/models/app.py +++ b/freight/models/app.py @@ -49,6 +49,14 @@ def deploy_config(self): TaskConfig.type == TaskConfigType.deploy, ).first() + @property + def build_config(self): + from freight.models import TaskConfig, TaskConfigType + return TaskConfig.query.filter( + TaskConfig.app_id == self.id, + TaskConfig.type == TaskConfigType.build, + ).first() + def get_default_ref(self, env): data = self.environments.get(env) if not data: diff --git a/freight/models/build.py b/freight/models/build.py new file mode 100644 index 00000000..c21b6a81 --- /dev/null +++ b/freight/models/build.py @@ -0,0 +1,22 @@ +from __future__ import absolute_import + +from sqlalchemy import Column, ForeignKey, Integer +from sqlalchemy.schema import Index, UniqueConstraint + +from freight.config import db + + +class Build(db.Model): + __tablename__ = 'build' + __table_args__ = ( + Index('idx_build_task_id', 'task_id'), + Index('idx_build_app_id', 'app_id'), + UniqueConstraint('task_id', 'app_id', 'number', name='unq_build_number'), + ) + + id = Column(Integer, primary_key=True) + task_id = Column(Integer, ForeignKey('task.id', ondelete='CASCADE'), + nullable=False) + app_id = Column(Integer, ForeignKey('app.id', ondelete='CASCADE'), + nullable=False) + number = Column(Integer, nullable=False) diff --git a/freight/models/buildsequence.py b/freight/models/buildsequence.py new file mode 100644 index 00000000..93b285fa --- /dev/null +++ b/freight/models/buildsequence.py @@ -0,0 +1,18 @@ +from __future__ import absolute_import + +from sqlalchemy import Column, Integer +from sqlalchemy.sql import func, select + +from freight.config import db + + +class BuildSequence(db.Model): + __tablename__ = 'buildsequence' + + app_id = Column(Integer, nullable=False, primary_key=True) + value = Column(Integer, default=0, server_default='0', nullable=False, + primary_key=True) + + @classmethod + def get_clause(self, app_id): + return select([func.next_build_number(app_id)]) diff --git a/freight/models/taskconfig.py b/freight/models/taskconfig.py index 84beaa00..f14d72f0 100644 --- a/freight/models/taskconfig.py +++ b/freight/models/taskconfig.py @@ -9,6 +9,7 @@ class TaskConfigType(object): deploy = 0 + build = 1 @classmethod def get_label(cls, status): @@ -21,6 +22,7 @@ def label_to_id(cls, label): TYPE_LABELS = { TaskConfigType.deploy: 'deploy', + TaskConfigType.build: 'build', } TYPE_LABELS_REV = { v: k for k, v in TYPE_LABELS.items() diff --git a/migrations/versions/18ff76b912af_create_buildsequence_table.py b/migrations/versions/18ff76b912af_create_buildsequence_table.py new file mode 100644 index 00000000..557ac958 --- /dev/null +++ b/migrations/versions/18ff76b912af_create_buildsequence_table.py @@ -0,0 +1,26 @@ +""" +Create buildsequence table. + +Revision ID: 18ff76b912af +Revises: 493c6c33fa27 +Create Date: 2016-05-05 14:47:50.243970 +""" + +# revision identifiers, used by Alembic. +revision = '18ff76b912af' +down_revision = '493c6c33fa27' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.create_table('buildsequence', + sa.Column('app_id', sa.Integer(), nullable=False), + sa.Column('value', sa.Integer(), server_default='0', nullable=False), + sa.PrimaryKeyConstraint('app_id', 'value') + ) + + +def downgrade(): + op.drop_table('buildsequence') diff --git a/migrations/versions/493c6c33fa27_create_build_table.py b/migrations/versions/493c6c33fa27_create_build_table.py new file mode 100644 index 00000000..54eb615d --- /dev/null +++ b/migrations/versions/493c6c33fa27_create_build_table.py @@ -0,0 +1,35 @@ +""" +Create build table. + +Revision ID: 493c6c33fa27 +Revises: 205fd513c96 +Create Date: 2016-05-05 14:31:38.491777 +""" + +# revision identifiers, used by Alembic. +revision = '493c6c33fa27' +down_revision = '205fd513c96' + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + op.create_table('build', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('task_id', sa.Integer(), nullable=False), + sa.Column('app_id', sa.Integer(), nullable=False), + sa.Column('number', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['app_id'], ['app.id'], ondelete='CASCADE'), + sa.ForeignKeyConstraint(['task_id'], ['task.id'], ondelete='CASCADE'), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('task_id', 'app_id', 'number', name='unq_build_number') + ) + op.create_index('idx_build_app_id', 'build', ['app_id'], unique=False) + op.create_index('idx_build_task_id', 'build', ['task_id'], unique=False) + + +def downgrade(): + op.drop_index('idx_build_task_id', table_name='build') + op.drop_index('idx_build_app_id', table_name='build') + op.drop_table('build')