Content-Length: 579824 | pFad | http://github.com/adsabs/graphics_service/commit/fd8975d844684cb7469879807ff2a3d7ab3aba9c

D5 Merge pull request #96 from ehenneken/db_update · adsabs/graphics_service@fd8975d · GitHub
Skip to content

Commit

Permalink
Merge pull request #96 from ehenneken/db_update
Browse files Browse the repository at this point in the history
alembic update
  • Loading branch information
ehenneken authored Feb 10, 2021
2 parents 8216bb5 + 55edae0 commit fd8975d
Show file tree
Hide file tree
Showing 12 changed files with 188 additions and 43 deletions.
22 changes: 22 additions & 0 deletions migrations/alembic.ini → alembic.ini
Original file line number Diff line number Diff line change
@@ -1,13 +1,35 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = alembic

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# set to 'true' to overwrite sqlalchemy.url with value
# SQLALCHEMY_DATABASE_URI (read from flask application config)

use_flask_db_url = true

# sqlalchemy.url = driver://user:pass@localhost/dbname
sqlalchemy.url = 'intentionally wrong'

sqlalchemy.echo = False


# Logging configuration
[loggers]
Expand Down
File renamed without changes.
44 changes: 33 additions & 11 deletions migrations/env.py → alembic/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
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.
Expand All @@ -15,16 +17,18 @@
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
from flask import current_app
config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI'))
target_metadata = current_app.extensions['migrate'].db.metadata
#target_metadata = None
opath = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
if opath not in sys.path:
sys.path.insert(0, opath)
import graphics_service.models
target_metadata = graphics_service.models.Base.metadata

# 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.
Expand All @@ -38,11 +42,22 @@ def run_migrations_offline():
"""
url = config.get_main_option("sqlalchemy.url")
context.configure(url=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 graphics_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.
Expand All @@ -51,14 +66,21 @@ def run_migrations_online():
and associate a connection with the context.
"""
engine = engine_from_config(config.get_section(config.config_ini_section),
prefix='sqlalchemy.',
poolclass=pool.NullPool)
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_DATABASE_URI')


engine = engine_from_config(
cfg,
prefix='sqlalchemy.',
poolclass=pool.NullPool)

connection = engine.connect()
context.configure(connection=connection,
target_metadata=target_metadata,
**current_app.extensions['migrate'].configure_args)
context.configure(
connection=connection,
target_metadata=target_metadata
)

try:
with context.begin_transaction():
Expand Down
10 changes: 6 additions & 4 deletions migrations/script.py.mako → alembic/script.py.mako
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}

from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

def upgrade():
${upgrades if upgrades else "pass"}
Expand Down
24 changes: 24 additions & 0 deletions alembic/versions/437251c0dc94_empty_init.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""Empty init
Revision ID: 437251c0dc94
Revises:
Create Date: 2021-02-05 15:14:10.889359
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '437251c0dc94'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
pass


def downgrade():
pass
28 changes: 28 additions & 0 deletions alembic/versions/80ecdb88cee2_add_thumbnails_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Add thumbnails column
Revision ID: 80ecdb88cee2
Revises: b9308d306405
Create Date: 2021-02-05 16:32:50.041223
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '80ecdb88cee2'
down_revision = 'b9308d306405'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('graphics', sa.Column('thumbnails', postgresql.ARRAY(sa.String()), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('graphics', 'thumbnails')
# ### end Alembic commands ###
39 changes: 39 additions & 0 deletions alembic/versions/b9308d306405_create_db_tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
"""Create db tables
Revision ID: b9308d306405
Revises: 437251c0dc94
Create Date: 2021-02-05 15:18:06.547834
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = 'b9308d306405'
down_revision = '437251c0dc94'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('graphics',
sa.Column('id', sa.Integer(), nullable=False),
sa.Column('bibcode', sa.String(), nullable=False),
sa.Column('doi', sa.String(), nullable=True),
sa.Column('source', sa.String(), nullable=True),
sa.Column('eprint', sa.Boolean(), nullable=True),
sa.Column('figures', postgresql.JSON(astext_type=sa.Text()), nullable=True),
sa.Column('modtime', sa.DateTime(), nullable=True),
sa.PrimaryKeyConstraint('id')
)
op.create_index(op.f('ix_graphics_bibcode'), 'graphics', ['bibcode'], unique=False)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_graphics_bibcode'), table_name='graphics')
op.drop_table('graphics')
# ### end Alembic commands ###
28 changes: 28 additions & 0 deletions alembic/versions/de7cb4b6482f_add_baseurl_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""Add baseurl column
Revision ID: de7cb4b6482f
Revises: 80ecdb88cee2
Create Date: 2021-02-09 12:36:02.897204
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'de7cb4b6482f'
down_revision = '80ecdb88cee2'
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column('graphics', sa.Column('baseurl', sa.String(), nullable=True))
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column('graphics', 'baseurl')
# ### end Alembic commands ###
2 changes: 2 additions & 0 deletions graphics_service/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ class GraphicsModel(Base):
source = Column(String)
eprint = Column(Boolean)
figures = Column(postgresql.JSON)
thumbnails = Column(postgresql.ARRAY(String), default=[])
baseurl = Column(String)
modtime = Column(DateTime)

def execute_SQL_query(bibc):
Expand Down
4 changes: 3 additions & 1 deletion graphics_service/tests/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,12 @@ def test_data_model(self):
sc = Column(String)
bc = Column(Boolean)
jc = Column(postgresql.JSON)
tc = Column(postgresql.ARRAY(String))
uc = Column(String)
dc = Column(DateTime)
cols_expect = list(map(
type, [ic.type, sc.type, sc.type, sc.type, bc.type,
jc.type, dc.type]))
jc.type, tc.type, uc.type, dc.type]))
self.assertEqual([type(c.type)
for c in GraphicsModel.__table__.columns],
cols_expect)
Expand Down
4 changes: 3 additions & 1 deletion graphics_service/tests/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ def test_json_encoder(self):
modtime=datetime.now()
)
results = json.loads(json.dumps(g, cls=AlchemyEncoder))
expected = {'modtime': None, 'bibcode': 'bibcode', 'source': 'TEST', 'doi': 'DOI', 'figures': [], 'eprint': False, 'id': None}
expected = {'modtime': None, 'bibcode': 'bibcode', 'thumbnails': None,
'baseurl': None, 'source': 'TEST', 'doi': 'DOI', 'figures': [],
'eprint': False, 'id': None}
self.assertTrue(results==expected)

class TestLocalConfig(TestCase):
Expand Down
26 changes: 0 additions & 26 deletions migrations/versions/437251c0dc94_.py

This file was deleted.

0 comments on commit fd8975d

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/adsabs/graphics_service/commit/fd8975d844684cb7469879807ff2a3d7ab3aba9c

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy