-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from romanchyla/limits
Added limits to the persistence layer
- Loading branch information
Showing
10 changed files
with
296 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Generic single-database configuration. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
from __future__ import with_statement | ||
from alembic import context | ||
from sqlalchemy import engine_from_config, pool | ||
from logging.config import fileConfig | ||
import os | ||
import sys | ||
|
||
# this is the Alembic Config object, which provides | ||
# access to the values within the .ini file in use. | ||
config = context.config | ||
|
||
# Interpret the config file for Python logging. | ||
# This line sets up loggers basically. | ||
fileConfig(config.config_file_name) | ||
|
||
# add your model's MetaData object here | ||
# for 'autogenerate' support | ||
# from myapp import mymodel | ||
# target_metadata = mymodel.Base.metadata | ||
target_metadata = None | ||
|
||
# other values from the config, defined by the needs of env.py, | ||
# can be acquired: | ||
# my_important_option = config.get_main_option("my_important_option") | ||
# ... etc. | ||
|
||
def run_migrations_offline(): | ||
"""Run migrations in 'offline' mode. | ||
This configures the context with just a URL | ||
and not an Engine, though an Engine is acceptable | ||
here as well. By skipping the Engine creation | ||
we don't even need a DBAPI to be available. | ||
Calls to context.execute() here emit the given string to the | ||
script output. | ||
""" | ||
url = config.get_main_option("sqlalchemy.url") | ||
context.configure(url=url, target_metadata=target_metadata) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
def get_app_config(key): | ||
opath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..')) | ||
if opath not in sys.path: | ||
sys.path.insert(0, opath) | ||
|
||
from orcid_service import app as application | ||
app = application.create_app() | ||
|
||
with app.app_context() as c: | ||
print 'Getting actual config for', key, app.config.get(key) | ||
return app.config.get(key) | ||
|
||
def run_migrations_online(): | ||
"""Run migrations in 'online' mode. | ||
In this scenario we need to create an Engine | ||
and associate a connection with the context. | ||
""" | ||
cfg = config.get_section(config.config_ini_section) | ||
if 'use_flask_db_url' in cfg and cfg['use_flask_db_url'] == 'true': | ||
cfg['sqlalchemy.url'] = get_app_config('SQLALCHEMY_BINDS')['orcid'] | ||
|
||
|
||
engine = engine_from_config( | ||
cfg, | ||
prefix='sqlalchemy.', | ||
poolclass=pool.NullPool) | ||
|
||
connection = engine.connect() | ||
context.configure( | ||
connection=connection, | ||
target_metadata=target_metadata | ||
) | ||
|
||
try: | ||
with context.begin_transaction(): | ||
context.run_migrations() | ||
finally: | ||
connection.close() | ||
|
||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = ${repr(up_revision)} | ||
down_revision = ${repr(down_revision)} | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
${imports if imports else ""} | ||
|
||
def upgrade(): | ||
#with app.app_context() as c: | ||
# db.session.add(Model()) | ||
# db.session.commit() | ||
|
||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade(): | ||
${downgrades if downgrades else "pass"} |
31 changes: 31 additions & 0 deletions
31
alembic/versions/51f3b3b5cd5d_creating_basic_db_structure.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
"""Creating basic DB structure | ||
Revision ID: 51f3b3b5cd5d | ||
Revises: None | ||
Create Date: 2015-13-09 20:13:58.241566 | ||
""" | ||
# revision identifiers, used by Alembic. | ||
revision = '51f3b3b5cd5d' | ||
down_revision = None | ||
|
||
from alembic import op | ||
import sqlalchemy as sa | ||
import datetime | ||
|
||
from sqlalchemy.sql import table, column | ||
from sqlalchemy import String, Integer, Index | ||
from sqlalchemy_utils import URLType | ||
|
||
|
||
def upgrade(): | ||
op.create_table('limits', | ||
sa.Column('id', sa.Integer, primary_key=True), | ||
sa.Column('uid', sa.String(length=255), nullable=False), | ||
sa.Column('field', sa.String(length=255), nullable=True), | ||
sa.Column('filter', sa.Text, nullable=False), | ||
Index('ix_uid', 'uid') | ||
) | ||
|
||
def downgrade(): | ||
op.drop_table('limits') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# -*- coding: utf-8 -*- | ||
""" | ||
solr.models | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
Models for the users (users) of AdsWS | ||
""" | ||
from flask_sqlalchemy import SQLAlchemy | ||
|
||
db = SQLAlchemy() # must be run in the context of a flask application | ||
|
||
class Limits(db.Model): | ||
__bind_key__ = 'solr_service' | ||
__tablename__ = 'limits' | ||
id = db.Column(db.Integer, primary_key=True) | ||
uid = db.Column(db.String(255)) | ||
field = db.Column(db.String(255)) | ||
filter = db.Column(db.Text) | ||
|
||
def toJSON(self): | ||
"""Returns value formatted as python dict.""" | ||
return { | ||
'uid': self.uid, | ||
'field': self.field, | ||
'filter': self.filter or None | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.