-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: update env.py to use SQLALCHEMY_DATABASE_URI from Flask config a…
…nd prevent empty migrations - Retrieve SQLALCHEMY_DATABASE_URI from Flask app config based on environment - Add process_revision_directives function to prevent generating empty migration scripts - Log a message when no schema changes are detected - Ensure database URI is correctly set in Alembic config - Update user_role model id to uuid to prevent migration warning - Fix CI to ensure proper database setup for tests
- Loading branch information
1 parent
17dd03a
commit 98a8384
Showing
10 changed files
with
121 additions
and
69 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
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
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 |
---|---|---|
@@ -1,71 +1,64 @@ | ||
import os | ||
from logging.config import fileConfig | ||
import logging | ||
|
||
from sqlalchemy import engine_from_config, pool, text | ||
from sqlalchemy import engine_from_config, pool | ||
from alembic import context | ||
|
||
config = context.config | ||
|
||
if config.config_file_name is not None: | ||
fileConfig(config.config_file_name) | ||
|
||
logger = logging.getLogger('alembic.env') | ||
fileConfig(config.config_file_name) | ||
|
||
from server import create_app | ||
from app.models import db | ||
|
||
config_name = os.getenv('FLASK_CONFIG') or 'development' | ||
app = create_app(config_name) | ||
|
||
with app.app_context(): | ||
config.set_main_option( | ||
'sqlalchemy.url', app.config.get('SQLALCHEMY_DATABASE_URI').replace('%', '%%')) | ||
target_metadata = app.extensions['migrate'].db.metadata | ||
database_uri = app.config['SQLALCHEMY_DATABASE_URI'] | ||
if not database_uri: | ||
raise ValueError("SQLALCHEMY_DATABASE_URI is not set") | ||
|
||
def run_migrations_offline() -> None: | ||
url = config.get_main_option("sqlalchemy.url") | ||
context.configure( | ||
url=url, | ||
target_metadata=target_metadata, | ||
literal_binds=True, | ||
dialect_opts={"paramstyle": "named"}, | ||
) | ||
config.set_main_option('sqlalchemy.url', database_uri.replace('%', '%%')) | ||
target_metadata = app.extensions['migrate'].db.metadata | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
def process_revision_directives(context, revision, directives): | ||
if getattr(config.cmd_opts, 'autogenerate', False): | ||
script = directives[0] | ||
if script.upgrade_ops.is_empty(): | ||
directives[:] = [] | ||
app.logger.info('No changes in schema detected.') | ||
|
||
def run_migrations_online() -> None: | ||
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.') | ||
def run_migrations_offline(): | ||
url = config.get_main_option("sqlalchemy.url") | ||
context.configure( | ||
url=url, | ||
target_metadata=target_metadata, | ||
literal_binds=True, | ||
dialect_opts={"paramstyle": "named"}, | ||
process_revision_directives=process_revision_directives, | ||
) | ||
|
||
connectable = engine_from_config( | ||
config.get_section(config.config_ini_section), | ||
prefix='sqlalchemy.', | ||
poolclass=pool.NullPool, | ||
) | ||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
with connectable.connect() as connection: | ||
context.configure( | ||
connection=connection, | ||
target_metadata=target_metadata, | ||
process_revision_directives=process_revision_directives, | ||
**app.extensions['migrate'].configure_args | ||
) | ||
def run_migrations_online(): | ||
connectable = engine_from_config( | ||
config.get_section(config.config_ini_section), | ||
prefix="sqlalchemy.", | ||
poolclass=pool.NullPool, | ||
) | ||
|
||
logger.info('Creating extension uuid-ossp...') | ||
connection.execute(text('CREATE EXTENSION IF NOT EXISTS "uuid-ossp"')) | ||
logger.info('Extension uuid-ossp created.') | ||
with connectable.connect() as connection: | ||
context.configure( | ||
connection=connection, | ||
target_metadata=target_metadata, | ||
process_revision_directives=process_revision_directives, | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
logger.info('Migrations have been run.') | ||
with context.begin_transaction(): | ||
context.run_migrations() | ||
app.logger.info('Migrations have been run.') | ||
|
||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() | ||
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
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,45 @@ | ||
"""empty message | ||
Revision ID: f8d3ac1848b9 | ||
Revises: 1888b16f6b99 | ||
Create Date: 2024-07-04 22:52:34.174913 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
from sqlalchemy.dialects import postgresql | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'f8d3ac1848b9' | ||
down_revision = '1888b16f6b99' | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
# Add a new column with UUID type | ||
op.add_column('user_role', sa.Column('new_id', postgresql.UUID(as_uuid=True), nullable=False, server_default=sa.text('uuid_generate_v4()'))) | ||
# Copy data from old id column to new_id column (UUIDs will be generated) | ||
op.execute('UPDATE user_role SET new_id = uuid_generate_v4()') | ||
# Drop the old id column | ||
op.drop_column('user_role', 'id') | ||
# Rename the new_id column to id | ||
op.alter_column('user_role', 'new_id', new_column_name='id') | ||
|
||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
|
||
# Add the old id column back with INTEGER type | ||
op.add_column('user_role', sa.Column('old_id', sa.Integer, nullable=False, server_default=sa.text('nextval(\'user_role_id_seq\'::regclass)'))) | ||
# Note: Data copying back from UUID to INTEGER is not straightforward | ||
# Skipping data copying for downgrade | ||
# Drop the new id (UUID) column | ||
op.drop_column('user_role', 'id') | ||
# Rename the old_id column to id | ||
op.alter_column('user_role', 'old_id', new_column_name='id') | ||
|
||
# ### end Alembic commands ### |
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