Add metadata column to coverage table

This commit adds metadata column to the coverage table. This should be
useful to filter results in the future.

Change-Id: I224d340b70ddd2c3cf7923f517281020470a9e45
This commit is contained in:
Masayuki Igawa 2019-04-18 12:42:19 +09:00
parent 4704d56363
commit 52c7cfde08
No known key found for this signature in database
GPG Key ID: 290F53EDC899BF89
4 changed files with 58 additions and 2 deletions

View File

@ -71,7 +71,8 @@ def get_session(autocommit=True, expire_on_commit=False):
def create_coverage(project_name, coverage_rate=0.0, rates=[],
report_time=None, test_type='py27', session=None):
report_time=None, test_type='py27',
coverage_metadata=None, session=None):
"""Create a new coverage record in the database.
This method is used to add a new coverage in the database.
@ -82,6 +83,7 @@ def create_coverage(project_name, coverage_rate=0.0, rates=[],
:param datetime.Datetime report_time: when the coverage was collected
defaults to None
:param str test_type: test_type like a task name of tox e.g. py27
:param str coverage_metadata: metadata for more additional data
:param session: optional session object if one isn't provided a new session
will be acquired for the duration of this operation
:return: The coverage object stored in the DB
@ -91,6 +93,7 @@ def create_coverage(project_name, coverage_rate=0.0, rates=[],
coverage.project_name = project_name
coverage.coverage_rate = coverage_rate
coverage.test_type = test_type
coverage.coverage_metadata = coverage_metadata
if report_time:
report_time = report_time.replace(tzinfo=None)
report_time_microsecond = report_time.microsecond
@ -98,6 +101,7 @@ def create_coverage(project_name, coverage_rate=0.0, rates=[],
report_time_microsecond = None
coverage.report_time = report_time
coverage.report_time_microsecond = report_time_microsecond
coverage.coverage_metadata = coverage_metadata
session = session or get_session()
with session.begin():

View File

@ -46,7 +46,8 @@ class CoverageBase(object):
class Coverage(BASE, CoverageBase):
__tablename__ = 'coverages'
__table_args__ = (sa.Index('ix_project_name', 'project_name'), )
__table_args__ = (sa.Index('ix_project_name', 'project_name'),
sa.Index('ix_coverage_metadata', 'coverage_metadata'))
id = sa.Column(sa.BigInteger, primary_key=True)
project_name = sa.Column(sa.String(256),
nullable=False)
@ -54,6 +55,7 @@ class Coverage(BASE, CoverageBase):
test_type = sa.Column(sa.String(256), nullable=False, default='py27')
report_time = sa.Column(sa.DateTime(), default=datetime.datetime.utcnow())
report_time_microsecond = sa.Column(sa.Integer(), default=0)
coverage_metadata = sa.Column(sa.String(256))
class File(BASE, CoverageBase):

View File

@ -0,0 +1,41 @@
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
# implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Add metadata column
Revision ID: 8857b04071a2
Revises: 79dead6f7c26
Create Date: 2019-04-18 12:12:34.776240
"""
# revision identifiers, used by Alembic.
revision = '8857b04071a2'
down_revision = '79dead6f7c26'
branch_labels = None
depends_on = None
from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('coverages', sa.Column('coverage_metadata', sa.String(256),
nullable=True))
op.create_index('ix_coverage_metadata', 'coverages',
['coverage_metadata'])
def downgrade():
op.drop_index('ix_coverage_metadata', 'coverage_metadata')
op.drop_column('coverages', 'coverage_metadata')

View File

@ -66,6 +66,15 @@ class TestDatabaseAPI(base.TestCase):
self.assertEqual(len(covs), 1)
self.assertEqual(covs[0].project_name, 'foo1_project')
def test_get_coverage_with_metadata(self):
api.create_coverage('foo1_project', coverage_metadata="foo,bar")
api.create_coverage('foo2_project', coverage_metadata="bar,foo")
covs = api.get_coverage(project_name='foo1_project')
self.assertTrue(covs is not None)
self.assertEqual(len(covs), 1)
self.assertEqual(covs[0].project_name, 'foo1_project')
self.assertEqual(covs[0].coverage_metadata, 'foo,bar')
def test_add_file_rates(self):
rates = []
rates.append({'filename': 'foo/bar0', 'line-rate': '0'})