Skip to content

Commit 4298752

Browse files
committed
Starter code for deployment chapter.
1 parent 56c0342 commit 4298752

File tree

146 files changed

+5164
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+5164
-0
lines changed

app/ch15_deploy/final/.idea/.name

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch15_deploy/final/.idea/dictionaries/screencaster.xml

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch15_deploy/final/.idea/flask-testing.iml

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch15_deploy/final/.idea/misc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch15_deploy/final/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch15_deploy/final/.idea/vcs.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/ch15_deploy/final/alembic.ini

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = alembic
6+
7+
# template used to generate migration files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# timezone to use when rendering the date
11+
# within the migration file as well as the filename.
12+
# string value is passed to dateutil.tz.gettz()
13+
# leave blank for localtime
14+
# timezone =
15+
16+
# max length of characters to apply to the
17+
# "slug" field
18+
# truncate_slug_length = 40
19+
20+
# set to 'true' to run the environment during
21+
# the 'revision' command, regardless of autogenerate
22+
# revision_environment = false
23+
24+
# set to 'true' to allow .pyc and .pyo files without
25+
# a source .py file to be detected as revisions in the
26+
# versions/ directory
27+
# sourceless = false
28+
29+
# version ___location specification; this defaults
30+
# to alembic/versions. When using multiple version
31+
# directories, initial revisions must be specified with --version-path
32+
# version_locations = %(here)s/bar %(here)s/bat alembic/versions
33+
34+
# the output encoding used when revision files
35+
# are written from script.py.mako
36+
# output_encoding = utf-8
37+
38+
sqlalchemy.url = sqlite:///./pypi_org/db/pypi.sqlite
39+
40+
41+
# Logging configuration
42+
[loggers]
43+
keys = root,sqlalchemy,alembic
44+
45+
[handlers]
46+
keys = console
47+
48+
[formatters]
49+
keys = generic
50+
51+
[logger_root]
52+
level = WARN
53+
handlers = console
54+
qualname =
55+
56+
[logger_sqlalchemy]
57+
level = WARN
58+
handlers =
59+
qualname = sqlalchemy.engine
60+
61+
[logger_alembic]
62+
level = INFO
63+
handlers =
64+
qualname = alembic
65+
66+
[handler_console]
67+
class = StreamHandler
68+
args = (sys.stderr,)
69+
level = NOTSET
70+
formatter = generic
71+
72+
[formatter_generic]
73+
format = %(levelname)-5.5s [%(name)s] %(message)s
74+
datefmt = %H:%M:%S

app/ch15_deploy/final/alembic/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from alembic import op
2+
from sqlalchemy import engine_from_config
3+
from sqlalchemy.engine import reflection
4+
5+
6+
def table_has_column(table, column):
7+
config = op.get_context().config
8+
engine = engine_from_config(
9+
config.get_section(config.config_ini_section), prefix='sqlalchemy.')
10+
insp = reflection.Inspector.from_engine(engine)
11+
has_column = False
12+
for col in insp.get_columns(table):
13+
if column not in col['name']:
14+
continue
15+
has_column = True
16+
return has_column

app/ch15_deploy/final/alembic/env.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
from logging.config import fileConfig
3+
4+
from sqlalchemy import engine_from_config
5+
from sqlalchemy import pool
6+
7+
from alembic import context
8+
9+
# this is the Alembic Config object, which provides
10+
# access to the values within the .ini file in use.
11+
12+
config = context.config
13+
14+
# Interpret the config file for Python logging.
15+
# This line sets up loggers basically.
16+
fileConfig(config.config_file_name)
17+
18+
# add your model's MetaData object here
19+
# for 'autogenerate' support
20+
# from myapp import mymodel
21+
# target_metadata = mymodel.Base.metadata
22+
23+
import sys
24+
25+
folder = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
26+
sys.path.insert(0, folder)
27+
28+
from pypi_org.data.modelbase import SqlAlchemyBase
29+
# noinspection PyUnresolvedReferences
30+
import pypi_org.data.__all_models
31+
32+
target_metadata = SqlAlchemyBase.metadata
33+
34+
# other values from the config, defined by the needs of env.py,
35+
# can be acquired:
36+
# my_important_option = config.get_main_option("my_important_option")
37+
# ... etc.
38+
39+
40+
def run_migrations_offline():
41+
"""Run migrations in 'offline' mode.
42+
43+
This configures the context with just a URL
44+
and not an Engine, though an Engine is acceptable
45+
here as well. By skipping the Engine creation
46+
we don't even need a DBAPI to be available.
47+
48+
Calls to context.execute() here emit the given string to the
49+
script output.
50+
51+
"""
52+
url = config.get_main_option("sqlalchemy.url")
53+
context.configure(
54+
url=url, target_metadata=target_metadata, literal_binds=True
55+
)
56+
57+
with context.begin_transaction():
58+
context.run_migrations()
59+
60+
61+
def run_migrations_online():
62+
"""Run migrations in 'online' mode.
63+
64+
In this scenario we need to create an Engine
65+
and associate a connection with the context.
66+
67+
"""
68+
connectable = engine_from_config(
69+
config.get_section(config.config_ini_section),
70+
prefix="sqlalchemy.",
71+
poolclass=pool.NullPool,
72+
)
73+
74+
with connectable.connect() as connection:
75+
context.configure(
76+
connection=connection, target_metadata=target_metadata
77+
)
78+
79+
with context.begin_transaction():
80+
context.run_migrations()
81+
82+
83+
if context.is_offline_mode():
84+
run_migrations_offline()
85+
else:
86+
run_migrations_online()

0 commit comments

Comments
 (0)