From 3fca71d5438cf77243f5cb794d923c4c8fe99812 Mon Sep 17 00:00:00 2001 From: Sudo-JHare Date: Thu, 24 Apr 2025 10:34:31 +1000 Subject: [PATCH] initial setup files. --- docker-compose.yml | 14 ++- setup/fhirvine.db | Bin 0 -> 40960 bytes setup/migrations/README | 1 + setup/migrations/alembic.ini | 50 ++++++++ setup/migrations/env.py | 113 ++++++++++++++++++ setup/migrations/script.py.mako | 24 ++++ ...8e416_create_initial_schema_after_purge.py | 86 +++++++++++++ templates/base.html | 4 +- 8 files changed, 288 insertions(+), 4 deletions(-) create mode 100644 setup/fhirvine.db create mode 100644 setup/migrations/README create mode 100644 setup/migrations/alembic.ini create mode 100644 setup/migrations/env.py create mode 100644 setup/migrations/script.py.mako create mode 100644 setup/migrations/versions/4e8d36d8e416_create_initial_schema_after_purge.py diff --git a/docker-compose.yml b/docker-compose.yml index 72fe468..06138a5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,4 +1,4 @@ -# docker-compose.yml for FHIRVINE +version: '3.8' services: fhirvine: @@ -9,13 +9,23 @@ services: volumes: - fhirvine_instance:/app/instance - fhirvine_migrations:/app/migrations + - ./setup/migrations:/app/setup_migrations + - ./setup/fhirvine.db:/app/setup_fhirvine.db + - ./static:/app/static + - ./templates:/app/templates environment: - FLASK_ENV=${FLASK_ENV:-development} env_file: - .env command: > - sh -c "flask db upgrade && waitress-serve --host=0.0.0.0 --port=5001 --call 'app:create_app'" + sh -c "echo 'Checking for setup migrations directory...' && ls -la /app/setup_migrations && if [ ! -d /app/migrations ]; then echo 'Copying migrations directory...' && cp -rv /app/setup_migrations /app/migrations && chown -R 1000:1000 /app/migrations && ls -la /app/migrations || { echo 'Failed to copy migrations'; exit 1; }; fi && if [ ! -f /app/instance/fhirvine.db ]; then echo 'Copying database file...' && cp -v /app/setup_fhirvine.db /app/instance/fhirvine.db && chown -R 1000:1000 /app/instance && ls -la /app/instance || { echo 'Failed to copy database'; exit 1; }; fi && echo 'Starting application...' && waitress-serve --host=0.0.0.0 --port=5001 --call 'app:create_app'" restart: unless-stopped + healthcheck: + test: ["CMD", "curl", "-f", "http://localhost:5001/"] + interval: 30s + timeout: 10s + retries: 3 + start_period: 10s volumes: fhirvine_instance: diff --git a/setup/fhirvine.db b/setup/fhirvine.db new file mode 100644 index 0000000000000000000000000000000000000000..41b99219016af2a38fe8b6e6d5fd3a1161035e86 GIT binary patch literal 40960 zcmeI)U2o#p8314t^4&?;S!on$da>4aM>}+y#2*-NRwLDb2}}Sd20}trS;iRbU~Gd; z5^~3Mq_%hM?Wp|;^8@-1D)pv&-K(mqs;XV7QZKrfJ$4cXGccXew4?3pYb65v*vEb! zzdnFDhw@QDcU8P)^}Di*)7VE?D1`kG$1w~&#jthse>*LqCyTc;=rJ_$eBRTKu$_Zn zZbZJu)>i(EMK&V8+4#%m@9(@=58pBG?4m_5fdB}A00@8p2>h=Eo_%+1Ws9amKmD;Q zHw;xCxSG}1uVh!ZOi8htsxxEJdRpYU3XfN~RDs85tbjjUU(=g-zEt7&c@ZxO6}(g} z6n=oOuX%Oi$DEkXapFUYVWPoOLsv~#LfhW1LJ~x9mA=~4`>NtfgT9Ve_>+oPtD{)9 zI-WBvQwi?mcF&Tc$%dht?f+CPbyXxWc^FZzj=t*Hmg%UHJ4E}tJ;oT}7dozt-Y?xQ z^kVZi(H&=?HYM~HY8HojX`^VIdBulfzQ~EE_yK>4Kh&Fkg=wKwu816}4R3X%8Tyhk zR?G?_pWiQe+dmvHjp8DooUmklO~XZelE>e zZdh&0PrB(dtXQTiEAI6s-YIu2XXc8wW$4OqX3^P2XZAKtlvC16Ogw`wyh^^vd;2kD z2VDlXw@NcmpgWSQqB2B_rv#zEb0z<{Zk9ZQ{&&}RoPIW2#FA_70nBS$aZs<=oy*4Hy6p~JhUle7>Fc)lLNspJ=O+AgSf1K#lKQ(x3Hrt2@|F;t zm!QFDBr-U^G>V4&mhkCyODG#^x1lT2x!QNo0Zz>>1e~S{J*b<_By-RmyU#=`LE6A=q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Vr%Q&Cy*R90G#F*zo+ULVUdv{4`XG_fTrH<$&5Y5mzMn|XlSfWYFIopwS5pd? ztia0q-BWki+SXHNrGrMEC(M}IyC`b0xsmcH(qE6-i-Ab>hNd<$`hGqqY6E`TCar?q zsypd(+1+Q{49mm@oyJ*~enL3!C(>@&HcuO=(q(UZTNGk$O}A7|;s)(vx_IO?+x4rO z#F`boAro^VUH#k7wbifwI8wr1r1Wj}3R2Bhde!n-uJVL4+KB^_qbnulyhd?yO}VIV zYeasV$r^(2j@kPrnOiC5_VqX+4HJTG54dV7aoT2wQddnM57Wn5PE2Qo!g;D5i+AQm z8AVZ!M#@4U%0X}2EScwSx~`euerqmPvW(Ymys)K03+D~+vHj>tHl%e-UezDbnM zMYY#7E80ogsFjYA*<}B?ZdDbfa^C6L@k-IoY{!!^nNhB)^P>Fg*V^je4@XKU5T$ha zbl&s4=NFhgA2tl}frNa__41Zdv;#QKre1Mdt?AA-|t- m+>r@^>i8@BJIyxtGKM literal 0 HcmV?d00001 diff --git a/setup/migrations/README b/setup/migrations/README new file mode 100644 index 0000000..0e04844 --- /dev/null +++ b/setup/migrations/README @@ -0,0 +1 @@ +Single-database configuration for Flask. diff --git a/setup/migrations/alembic.ini b/setup/migrations/alembic.ini new file mode 100644 index 0000000..ec9d45c --- /dev/null +++ b/setup/migrations/alembic.ini @@ -0,0 +1,50 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic,flask_migrate + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[logger_flask_migrate] +level = INFO +handlers = +qualname = flask_migrate + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/setup/migrations/env.py b/setup/migrations/env.py new file mode 100644 index 0000000..4c97092 --- /dev/null +++ b/setup/migrations/env.py @@ -0,0 +1,113 @@ +import logging +from logging.config import fileConfig + +from flask import current_app + +from alembic import context + +# 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) +logger = logging.getLogger('alembic.env') + + +def get_engine(): + try: + # this works with Flask-SQLAlchemy<3 and Alchemical + return current_app.extensions['migrate'].db.get_engine() + except (TypeError, AttributeError): + # this works with Flask-SQLAlchemy>=3 + return current_app.extensions['migrate'].db.engine + + +def get_engine_url(): + try: + return get_engine().url.render_as_string(hide_password=False).replace( + '%', '%%') + except AttributeError: + return str(get_engine().url).replace('%', '%%') + + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +config.set_main_option('sqlalchemy.url', get_engine_url()) +target_db = current_app.extensions['migrate'].db + +# 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 get_metadata(): + if hasattr(target_db, 'metadatas'): + return target_db.metadatas[None] + return target_db.metadata + + +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=get_metadata(), literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +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. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + conf_args = current_app.extensions['migrate'].configure_args + if conf_args.get("process_revision_directives") is None: + conf_args["process_revision_directives"] = process_revision_directives + + connectable = get_engine() + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=get_metadata(), + **conf_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/setup/migrations/script.py.mako b/setup/migrations/script.py.mako new file mode 100644 index 0000000..2c01563 --- /dev/null +++ b/setup/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_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)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/setup/migrations/versions/4e8d36d8e416_create_initial_schema_after_purge.py b/setup/migrations/versions/4e8d36d8e416_create_initial_schema_after_purge.py new file mode 100644 index 0000000..6e93561 --- /dev/null +++ b/setup/migrations/versions/4e8d36d8e416_create_initial_schema_after_purge.py @@ -0,0 +1,86 @@ +"""Create initial schema after purge + +Revision ID: 4e8d36d8e416 +Revises: +Create Date: 2025-04-23 13:08:11.515425 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '4e8d36d8e416' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('configurations', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('key', sa.String(length=100), nullable=False), + sa.Column('value', sa.Text(), nullable=False), + sa.Column('description', sa.Text(), nullable=True), + sa.Column('last_updated', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('key') + ) + op.create_table('oauth_tokens', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('client_id', sa.String(length=100), nullable=False), + sa.Column('token_type', sa.String(length=40), nullable=True), + sa.Column('access_token', sa.String(length=255), nullable=False), + sa.Column('refresh_token', sa.String(length=255), nullable=True), + sa.Column('scope', sa.Text(), nullable=True), + sa.Column('issued_at', sa.Integer(), nullable=False), + sa.Column('expires_in', sa.Integer(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('registered_apps', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('app_name', sa.String(length=100), nullable=False), + sa.Column('client_id', sa.String(length=100), nullable=False), + sa.Column('client_secret_hash', sa.String(length=200), nullable=False), + sa.Column('redirect_uris', sa.Text(), nullable=False), + sa.Column('scopes', sa.Text(), nullable=False), + sa.Column('logo_uri', sa.String(length=255), nullable=True), + sa.Column('contacts', sa.Text(), nullable=True), + sa.Column('tos_uri', sa.String(length=255), nullable=True), + sa.Column('policy_uri', sa.String(length=255), nullable=True), + sa.Column('jwks_uri', sa.String(length=255), nullable=True), + sa.Column('date_registered', sa.DateTime(), nullable=True), + sa.Column('last_updated', sa.DateTime(), nullable=True), + sa.Column('is_test_app', sa.Boolean(), nullable=True), + sa.Column('test_app_expires_at', sa.DateTime(), nullable=True), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('app_name'), + sa.UniqueConstraint('client_id') + ) + op.create_table('authorization_codes', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('code', sa.String(length=255), nullable=False), + sa.Column('client_id', sa.String(length=100), nullable=False), + sa.Column('redirect_uri', sa.Text(), nullable=True), + sa.Column('scope', sa.Text(), nullable=True), + sa.Column('nonce', sa.String(length=255), nullable=True), + sa.Column('code_challenge', sa.String(length=255), nullable=True), + sa.Column('code_challenge_method', sa.String(length=10), nullable=True), + sa.Column('response_type', sa.String(length=40), nullable=True), + sa.Column('state', sa.String(length=255), nullable=True), + sa.Column('issued_at', sa.Integer(), nullable=False), + sa.Column('expires_at', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['client_id'], ['registered_apps.client_id'], name='fk_authorization_codes_client_id'), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('authorization_codes') + op.drop_table('registered_apps') + op.drop_table('oauth_tokens') + op.drop_table('configurations') + # ### end Alembic commands ### diff --git a/templates/base.html b/templates/base.html index e780b4f..024c401 100644 --- a/templates/base.html +++ b/templates/base.html @@ -245,12 +245,12 @@