Skip to content

Commit

Permalink
global: initial alembic recipes
Browse files Browse the repository at this point in the history
* Adds alembic recipe (closes #19).

* Updates dependencies in order to have correct alembic upgrade.

Signed-off-by: Nicolas Harraudeau <nicolas.harraudeau@cern.ch>
  • Loading branch information
Nicolas Harraudeau authored and nharraud committed Nov 2, 2017
1 parent b1abeaa commit 3a6982f
Show file tree
Hide file tree
Showing 6 changed files with 184 additions and 16 deletions.
39 changes: 32 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,33 @@ matrix:
cache:
- pip

services:
- mysql
- postgresql

# Note: dist/sudo/api is required for MySQL 5.6 which adds support for
# fractional seconds in datetime columns.
dist: trusty
sudo: required
addons:
postgresql: "9.4"
apt:
packages:
- mysql-server-5.6
- mysql-client-core-5.6
- mysql-client-5.6


env:
- REQUIREMENTS=lowest
- REQUIREMENTS=release DEPLOY=true
- REQUIREMENTS=devel
- REQUIREMENTS=lowest EXTRAS=mysql SQLALCHEMY_DATABASE_URI="mysql+pymysql://travis@localhost:3306/invenio"
- REQUIREMENTS=lowest EXTRAS=postgresql SQLALCHEMY_DATABASE_URI="postgresql+psycopg2://postgres@localhost:5432/invenio"
- REQUIREMENTS=lowest EXTRAS=sqlite SQLALCHEMY_DATABASE_URI="sqlite:///test.db"
- REQUIREMENTS=release EXTRAS=mysql SQLALCHEMY_DATABASE_URI="mysql+pymysql://travis@localhost:3306/invenio"
- REQUIREMENTS=release EXTRAS=postgresql SQLALCHEMY_DATABASE_URI="postgresql+psycopg2://postgres@localhost:5432/invenio" DEPLOY=true
- REQUIREMENTS=release EXTRAS=sqlite SQLALCHEMY_DATABASE_URI="sqlite:///test.db"
- REQUIREMENTS=devel EXTRAS=mysql SQLALCHEMY_DATABASE_URI="mysql+pymysql://travis@localhost:3306/invenio"
- REQUIREMENTS=devel EXTRAS=postgresql SQLALCHEMY_DATABASE_URI="postgresql+psycopg2://postgres@localhost:5432/invenio"
- REQUIREMENTS=devel EXTRAS=sqlite SQLALCHEMY_DATABASE_URI="sqlite:///test.db"

python:
- "2.7"
Expand All @@ -47,13 +70,15 @@ python:
before_install:
- "travis_retry pip install --upgrade pip setuptools py"
- "travis_retry pip install twine wheel coveralls requirements-builder"
- "requirements-builder --level=min setup.py > .travis-lowest-requirements.txt"
- "requirements-builder --level=pypi setup.py > .travis-release-requirements.txt"
- "requirements-builder --level=dev --req requirements-devel.txt setup.py > .travis-devel-requirements.txt"
- "requirements-builder -e records,indexer,${EXTRAS} --level=min setup.py > .travis-lowest-requirements.txt"
- "requirements-builder -e records,indexer,${EXTRAS} --level=pypi setup.py > .travis-release-requirements.txt"
- "requirements-builder -e records,indexer,${EXTRAS} --level=dev --req requirements-devel.txt setup.py > .travis-devel-requirements.txt"
- "mysql -e 'CREATE DATABASE IF NOT EXISTS invenio;' -uroot"
- "psql -c 'CREATE DATABASE invenio;' -U postgres"

install:
- "travis_retry pip install -r .travis-${REQUIREMENTS}-requirements.txt"
- "travis_retry pip install -e .[all]"
- "travis_retry pip install -e .[all,${EXTRAS}]"

script:
- "./run-tests.sh"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#
# This file is part of Invenio.
# Copyright (C) 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Create pidrelations tables."""

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = '1d4e361b7586'
down_revision = '2216c32d7059' # Create branch
branch_labels = ()
depends_on = None


def upgrade():
"""Upgrade database."""
op.create_table(
'pidrelations_pidrelation',
sa.Column('created', sa.DateTime(), nullable=False),
sa.Column('updated', sa.DateTime(), nullable=False),
sa.Column('parent_id', sa.Integer(), nullable=False),
sa.Column('child_id', sa.Integer(), nullable=False),
sa.Column('relation_type', sa.SmallInteger(), nullable=False),
sa.Column('index', sa.Integer(), nullable=True),
sa.ForeignKeyConstraint(
['child_id'], ['pidstore_pid.id'],
name=op.f('fk_pidrelations_pidrelation_child_id_pidstore_pid'),
onupdate='CASCADE', ondelete='CASCADE'
),
sa.ForeignKeyConstraint(
['parent_id'], ['pidstore_pid.id'],
name=op.f('fk_pidrelations_pidrelation_parent_id_pidstore_pid'),
onupdate='CASCADE', ondelete='CASCADE'),
sa.PrimaryKeyConstraint(
'parent_id', 'child_id', name=op.f('pk_pidrelations_pidrelation')
)
)


def downgrade():
"""Downgrade database."""
op.drop_table('pidrelations_pidrelation')
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#
# This file is part of Invenio.
# Copyright (C) 2017 CERN.
#
# Invenio is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# Invenio is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Invenio; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307, USA.
#
# In applying this license, CERN does not
# waive the privileges and immunities granted to it by virtue of its status
# as an Intergovernmental Organization or submit itself to any jurisdiction.

"""Create pidrelations branch."""

import sqlalchemy as sa
from alembic import op

revision = '2216c32d7059'
down_revision = None
branch_labels = (u'invenio_pidrelations',)
depends_on = '999c62899c20' # invenio-pidstore create tables


def upgrade():
"""Upgrade database."""


def downgrade():
"""Downgrade database."""
24 changes: 19 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,37 @@
],
'tests': tests_require,
'mysql': [
'invenio-db[mysql]>=1.0.0b1',
'invenio-db[mysql,versioning]>=1.0.0b3',
],
'postgresql': [
'invenio-db[postgresql]>=1.0.0b1',
'invenio-db[postgresql,versioning]>=1.0.0b3',
],
'sqlite': [
'invenio-db>=1.0.0b1',
'invenio-db[versioning]>=1.0.0b3',
],
'records': [
'invenio-deposit>=1.0.0a7',
'invenio-records-files>=1.0.0a8',
'invenio-records>=1.0.0b1',
# FIXME: Added because requirements-builder does not search
# recursively lowest dependencies.
'invenio-records-ui>=1.0.0a8',
'invenio-records-rest>=1.0.0a17',
'invenio-accounts>=1.0.0b5',
'Flask-WTF>=0.13.1',
# Needed in order to have correct alembic upgrade.
'invenio-oauth2server>=1.0.0a14',
'invenio-records-files>=1.0.0a9',
'invenio-files-rest>=1.0.0a15',
],
'indexer': [
'invenio-indexer>=1.0.0a9',
],
}

extras_require['all'] = []
for reqs in extras_require.values():
for name, reqs in extras_require.items():
if name in ('mysql', 'postgresql', 'sqlite'):
continue
extras_require['all'].extend(reqs)

setup_requires = [
Expand Down Expand Up @@ -114,6 +125,9 @@
'invenio_base.api_apps': [
'invenio_pidrelations = invenio_pidrelations:InvenioPIDRelations',
],
'invenio_db.alembic': [
'invenio_pidrelations = invenio_pidrelations:alembic',
],
'invenio_db.models': [
'invenio_pidrelations = invenio_pidrelations.models',
],
Expand Down
14 changes: 10 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
from invenio_records import InvenioRecords, Record
from invenio_search import InvenioSearch, current_search, current_search_client
from marshmallow import fields
from sqlalchemy_utils.functions import create_database, database_exists
from sqlalchemy_utils.functions import create_database, database_exists, \
drop_database

from invenio_pidrelations import InvenioPIDRelations
from invenio_pidrelations.config import RelationType
Expand Down Expand Up @@ -105,12 +106,17 @@ def es(app):
@pytest.yield_fixture()
def db(app):
"""Database fixture."""
if not database_exists(str(db_.engine.url)):
create_database(str(db_.engine.url))
if database_exists(str(db_.engine.url)):
db_.session.remove()
drop_database(str(db_.engine.url))
create_database(str(db_.engine.url))
db_.create_all()
yield db_
db_.session.remove()
db_.drop_all()
drop_database(db_.engine.url)
# Dispose the engine in order to close all connections. This is
# needed for sqlite in memory databases.
db_.engine.dispose()


@pytest.fixture()
Expand Down
20 changes: 20 additions & 0 deletions tests/test_invenio_pidrelations.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

from __future__ import absolute_import, print_function

import pytest
from flask import Flask

from invenio_pidrelations import InvenioPIDRelations
Expand All @@ -48,3 +49,22 @@ def test_init():
assert 'invenio-pidrelations' not in app.extensions
ext.init_app(app)
assert 'invenio-pidrelations' in app.extensions


def test_alembic(app, db):
"""Test alembic recipes."""
ext = app.extensions['invenio-db']

if db.engine.name == 'sqlite':
raise pytest.skip('Upgrades are not supported on SQLite.')

assert not ext.alembic.compare_metadata()
db.drop_all()
ext.alembic.upgrade()

assert not ext.alembic.compare_metadata()
ext.alembic.stamp()
ext.alembic.downgrade(target='96e796392533')
ext.alembic.upgrade()

assert not ext.alembic.compare_metadata()

0 comments on commit 3a6982f

Please sign in to comment.