Skip to content

Commit 4f39a2a

Browse files
authored
Merge pull request #191 from cuappdev/sophie/popular-times
Popular times updated migrations
2 parents a5d5874 + 408b4b5 commit 4f39a2a

3 files changed

Lines changed: 66 additions & 6 deletions

File tree

migrations/alembic.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# A generic, single database configuration.
22

33
[alembic]
4+
script_location = migrations
45
# template used to generate migration files
56
# file_template = %%(rev)s_%%(slug)s
67

migrations/env.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
from __future__ import with_statement
2+
import sys
3+
import os
4+
sys.path.append(os.path.abspath(os.path.dirname(__file__) + "/.."))
5+
from app import app
26

37
import logging
48
from logging.config import fileConfig
@@ -21,10 +25,9 @@
2125
# for 'autogenerate' support
2226
# from myapp import mymodel
2327
# target_metadata = mymodel.Base.metadata
24-
from flask import current_app
25-
config.set_main_option('sqlalchemy.url',
26-
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
27-
target_metadata = current_app.extensions['migrate'].db.metadata
28+
with app.app_context():
29+
config.set_main_option('sqlalchemy.url', app.config['SQLALCHEMY_DATABASE_URI'])
30+
target_metadata = app.extensions['migrate'].db.metadata
2831

2932
# other values from the config, defined by the needs of env.py,
3033
# can be acquired:
@@ -82,7 +85,7 @@ def process_revision_directives(context, revision, directives):
8285
connection=connection,
8386
target_metadata=target_metadata,
8487
process_revision_directives=process_revision_directives,
85-
**current_app.extensions['migrate'].configure_args
88+
**app.extensions['migrate'].configure_args
8689
)
8790

8891
with context.begin_transaction():
@@ -92,4 +95,4 @@ def process_revision_directives(context, revision, directives):
9295
if context.is_offline_mode():
9396
run_migrations_offline()
9497
else:
95-
run_migrations_online()
98+
run_migrations_online()
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
"""popular times
2+
3+
Revision ID: 31b1fa20772f
4+
Revises:
5+
Create Date: 2025-02-28 20:57:19.922403
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
from sqlalchemy.dialects import postgresql
11+
12+
13+
# revision identifiers, used by Alembic.
14+
revision = '31b1fa20772f'
15+
down_revision = None
16+
branch_labels = None
17+
depends_on = None
18+
19+
20+
### Ensures alembic does not try to create enum
21+
day_of_week_enum = postgresql.ENUM(
22+
'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday',
23+
name='dayofweekenum', create_type=False
24+
)
25+
26+
def upgrade():
27+
# ### commands auto generated by Alembic - please adjust! ###
28+
op.alter_column('gear', 'cost',
29+
existing_type=postgresql.DOUBLE_PRECISION(precision=53),
30+
nullable=0)
31+
32+
### Alembic is running command multiple times.
33+
op.execute("""
34+
DO $$
35+
BEGIN
36+
IF NOT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'hourly_average_capacity') THEN
37+
CREATE TABLE hourly_average_capacity (
38+
id SERIAL PRIMARY KEY,
39+
facility_id INTEGER NOT NULL REFERENCES facility(id),
40+
average_percent FLOAT NOT NULL,
41+
hour_of_day INTEGER NOT NULL,
42+
day_of_week dayofweekenum,
43+
history NUMERIC[] DEFAULT '{}' NOT NULL
44+
);
45+
END IF;
46+
END $$;
47+
""")
48+
# ### end Alembic commands ###
49+
50+
51+
def downgrade():
52+
# ### commands auto generated by Alembic - please adjust! ###
53+
54+
op.drop_table('hourly_average_capacity')
55+
# ### end Alembic commands ###
56+

0 commit comments

Comments
 (0)