Support table prefix for sql reporter

In some environments zuul operators may have to rely on external
database providers. In this case it can be cumbersome to get extra
databases for each test environment. Adding an optional prefix to the
table names makes it possible to gracefully run several zuul
deployments against the same database and ensure they're still
isolated against each other.

Change-Id: Ib9948d6d74f4dc2453738f5d441e233e39e7f944
This commit is contained in:
Tobias Henkel
2017-12-09 12:24:52 +01:00
parent 4da9c4b977
commit 94a1d08552
12 changed files with 102 additions and 34 deletions

View File

@@ -16,8 +16,8 @@ from alembic import op
import sqlalchemy as sa
def upgrade():
op.alter_column('zuul_buildset', 'score', nullable=True,
def upgrade(table_prefix=''):
op.alter_column(table_prefix + 'zuul_buildset', 'score', nullable=True,
existing_type=sa.Integer)

View File

@@ -32,24 +32,28 @@ BUILDSET_TABLE = 'zuul_buildset'
BUILD_TABLE = 'zuul_build'
def upgrade():
def upgrade(table_prefix=''):
prefixed_buildset = table_prefix + BUILDSET_TABLE
prefixed_build = table_prefix + BUILD_TABLE
# To allow a dashboard to show a per-project view, optionally filtered
# by pipeline.
op.create_index(
'project_pipeline_idx', BUILDSET_TABLE, ['project', 'pipeline'])
'project_pipeline_idx', prefixed_buildset, ['project', 'pipeline'])
# To allow a dashboard to show a per-project-change view
op.create_index(
'project_change_idx', BUILDSET_TABLE, ['project', 'change'])
'project_change_idx', prefixed_buildset, ['project', 'change'])
# To allow a dashboard to show a per-change view
op.create_index('change_idx', BUILDSET_TABLE, ['change'])
op.create_index('change_idx', prefixed_buildset, ['change'])
# To allow a dashboard to show a job lib view. buildset_id is included
# so that it's a covering index and can satisfy the join back to buildset
# without an additional lookup.
op.create_index(
'job_name_buildset_id_idx', BUILD_TABLE, ['job_name', 'buildset_id'])
'job_name_buildset_id_idx', prefixed_build,
['job_name', 'buildset_id'])
def downgrade():

View File

@@ -19,9 +19,9 @@ BUILDSET_TABLE = 'zuul_buildset'
BUILD_TABLE = 'zuul_build'
def upgrade():
def upgrade(table_prefix=''):
op.create_table(
BUILDSET_TABLE,
table_prefix + BUILDSET_TABLE,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('zuul_ref', sa.String(255)),
sa.Column('pipeline', sa.String(255)),
@@ -34,10 +34,10 @@ def upgrade():
)
op.create_table(
BUILD_TABLE,
table_prefix + BUILD_TABLE,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('buildset_id', sa.Integer,
sa.ForeignKey(BUILDSET_TABLE + ".id")),
sa.ForeignKey(table_prefix + BUILDSET_TABLE + ".id")),
sa.Column('uuid', sa.String(36)),
sa.Column('job_name', sa.String(255)),
sa.Column('result', sa.String(255)),

View File

@@ -30,8 +30,9 @@ from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('zuul_buildset', sa.Column('ref_url', sa.String(255)))
def upgrade(table_prefix=''):
op.add_column(
table_prefix + 'zuul_buildset', sa.Column('ref_url', sa.String(255)))
def downgrade():

View File

@@ -18,8 +18,9 @@ import sqlalchemy as sa
BUILDSET_TABLE = 'zuul_buildset'
def upgrade():
op.add_column(BUILDSET_TABLE, sa.Column('result', sa.String(255)))
def upgrade(table_prefix=''):
op.add_column(
table_prefix + BUILDSET_TABLE, sa.Column('result', sa.String(255)))
connection = op.get_bind()
connection.execute(
@@ -29,9 +30,9 @@ def upgrade():
SELECT CASE score
WHEN 1 THEN 'SUCCESS'
ELSE 'FAILURE' END)
""".format(buildset_table=BUILDSET_TABLE))
""".format(buildset_table=table_prefix + BUILDSET_TABLE))
op.drop_column(BUILDSET_TABLE, 'score')
op.drop_column(table_prefix + BUILDSET_TABLE, 'score')
def downgrade():

View File

@@ -16,9 +16,11 @@ from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('zuul_buildset', sa.Column('oldrev', sa.String(255)))
op.add_column('zuul_buildset', sa.Column('newrev', sa.String(255)))
def upgrade(table_prefix=''):
op.add_column(
table_prefix + 'zuul_buildset', sa.Column('oldrev', sa.String(255)))
op.add_column(
table_prefix + 'zuul_buildset', sa.Column('newrev', sa.String(255)))
def downgrade():

View File

@@ -30,8 +30,9 @@ from alembic import op
import sqlalchemy as sa
def upgrade():
op.add_column('zuul_buildset', sa.Column('tenant', sa.String(255)))
def upgrade(table_prefix=''):
op.add_column(
table_prefix + 'zuul_buildset', sa.Column('tenant', sa.String(255)))
def downgrade():