Skip to content

Commit 032b03c

Browse files
committed
aasdasd
1 parent ce0c488 commit 032b03c

File tree

5 files changed

+70
-107
lines changed

5 files changed

+70
-107
lines changed

migrations/env.py

Lines changed: 34 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
from __future__ import with_statement
2-
31
import logging
42
from logging.config import fileConfig
53

@@ -16,22 +14,43 @@
1614
fileConfig(config.config_file_name)
1715
logger = logging.getLogger('alembic.env')
1816

17+
18+
def get_engine():
19+
try:
20+
# this works with Flask-SQLAlchemy<3 and Alchemical
21+
return current_app.extensions['migrate'].db.get_engine()
22+
except (TypeError, AttributeError):
23+
# this works with Flask-SQLAlchemy>=3
24+
return current_app.extensions['migrate'].db.engine
25+
26+
27+
def get_engine_url():
28+
try:
29+
return get_engine().url.render_as_string(hide_password=False).replace(
30+
'%', '%%')
31+
except AttributeError:
32+
return str(get_engine().url).replace('%', '%%')
33+
34+
1935
# add your model's MetaData object here
2036
# for 'autogenerate' support
2137
# from myapp import mymodel
2238
# target_metadata = mymodel.Base.metadata
23-
config.set_main_option(
24-
'sqlalchemy.url',
25-
str(current_app.extensions['migrate'].db.get_engine().url).replace(
26-
'%', '%%'))
27-
target_metadata = current_app.extensions['migrate'].db.metadata
39+
config.set_main_option('sqlalchemy.url', get_engine_url())
40+
target_db = current_app.extensions['migrate'].db
2841

2942
# other values from the config, defined by the needs of env.py,
3043
# can be acquired:
3144
# my_important_option = config.get_main_option("my_important_option")
3245
# ... etc.
3346

3447

48+
def get_metadata():
49+
if hasattr(target_db, 'metadatas'):
50+
return target_db.metadatas[None]
51+
return target_db.metadata
52+
53+
3554
def run_migrations_offline():
3655
"""Run migrations in 'offline' mode.
3756
@@ -46,7 +65,7 @@ def run_migrations_offline():
4665
"""
4766
url = config.get_main_option("sqlalchemy.url")
4867
context.configure(
49-
url=url, target_metadata=target_metadata, literal_binds=True
68+
url=url, target_metadata=get_metadata(), literal_binds=True
5069
)
5170

5271
with context.begin_transaction():
@@ -71,14 +90,17 @@ def process_revision_directives(context, revision, directives):
7190
directives[:] = []
7291
logger.info('No changes in schema detected.')
7392

74-
connectable = current_app.extensions['migrate'].db.get_engine()
93+
conf_args = current_app.extensions['migrate'].configure_args
94+
if conf_args.get("process_revision_directives") is None:
95+
conf_args["process_revision_directives"] = process_revision_directives
96+
97+
connectable = get_engine()
7598

7699
with connectable.connect() as connection:
77100
context.configure(
78101
connection=connection,
79-
target_metadata=target_metadata,
80-
process_revision_directives=process_revision_directives,
81-
**current_app.extensions['migrate'].configure_args
102+
target_metadata=get_metadata(),
103+
**conf_args
82104
)
83105

84106
with context.begin_transaction():

migrations/versions/.placeholder

Whitespace-only changes.

migrations/versions/9b6c840bcc28_create_tables_based_on_current_models.py

Lines changed: 0 additions & 37 deletions
This file was deleted.

migrations/versions/f7006424e5cd_aaaaaaaaassaaz.py

Lines changed: 0 additions & 58 deletions
This file was deleted.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
"""Recrear migración perdida
2+
3+
Revision ID: f7006424e5cd
4+
Revises:
5+
Create Date: 2025-05-08 20:08:20.805324
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'f7006424e5cd'
14+
down_revision = None
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# Crear la tabla image_data
21+
op.create_table(
22+
'image_data',
23+
sa.Column('id', sa.Integer(), nullable=False),
24+
sa.Column('filename', sa.String(length=255), nullable=False),
25+
sa.Column('pixel_red', sa.Integer(), nullable=False),
26+
sa.Column('pixel_green', sa.Integer(), nullable=False),
27+
sa.Column('pixel_blue', sa.Integer(), nullable=False),
28+
sa.Column('username', sa.String(length=100), nullable=False),
29+
sa.Column('upload_time', sa.DateTime(), nullable=False),
30+
sa.PrimaryKeyConstraint('id')
31+
)
32+
33+
34+
def downgrade():
35+
# Eliminar la tabla image_data en caso de rollback
36+
op.drop_table('image_data')

0 commit comments

Comments
 (0)