Skip to content

Commit a15f1c9

Browse files
committed
first WIP
1 parent dacc1fa commit a15f1c9

29 files changed

+1026
-62
lines changed

docs_src/tutorial/migrations/simple_hero_migration/__init__.py

Whitespace-only changes.
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = migrations
6+
7+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
8+
# Uncomment the line below if you want the files to be prepended with date and time
9+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
10+
# for all available tokens
11+
file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
12+
13+
# sys.path path, will be prepended to sys.path if present.
14+
# defaults to the current working directory.
15+
prepend_sys_path = .
16+
17+
# timezone to use when rendering the date within the migration file
18+
# as well as the filename.
19+
# If specified, requires the python-dateutil library that can be
20+
# installed by adding `alembic[tz]` to the pip requirements
21+
# string value is passed to dateutil.tz.gettz()
22+
# leave blank for localtime
23+
# timezone =
24+
25+
# max length of characters to apply to the
26+
# "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to migrations/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
53+
54+
# set to 'true' to search source files recursively
55+
# in each "version_locations" directory
56+
# new in Alembic version 1.10
57+
# recursive_version_locations = false
58+
59+
# the output encoding used when revision files
60+
# are written from script.py.mako
61+
# output_encoding = utf-8
62+
63+
sqlalchemy.url = sqlite:///database.db
64+
65+
66+
[post_write_hooks]
67+
# post_write_hooks defines scripts or Python functions that are run
68+
# on newly generated revision scripts. See the documentation for further
69+
# detail and examples
70+
71+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
72+
# hooks = black
73+
# black.type = console_scripts
74+
# black.entrypoint = black
75+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
76+
77+
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
78+
# hooks = ruff
79+
# ruff.type = exec
80+
# ruff.executable = %(here)s/.venv/bin/ruff
81+
# ruff.options = --fix REVISION_SCRIPT_FILENAME
82+
83+
# Logging configuration
84+
[loggers]
85+
keys = root,sqlalchemy,alembic
86+
87+
[handlers]
88+
keys = console
89+
90+
[formatters]
91+
keys = generic
92+
93+
[logger_root]
94+
level = WARN
95+
handlers = console
96+
qualname =
97+
98+
[logger_sqlalchemy]
99+
level = WARN
100+
handlers =
101+
qualname = sqlalchemy.engine
102+
103+
[logger_alembic]
104+
level = INFO
105+
handlers =
106+
qualname = alembic
107+
108+
[handler_console]
109+
class = StreamHandler
110+
args = (sys.stderr,)
111+
level = NOTSET
112+
formatter = generic
113+
114+
[formatter_generic]
115+
format = %(levelname)-5.5s [%(name)s] %(message)s
116+
datefmt = %H:%M:%S
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# A generic, single database configuration.
2+
3+
[alembic]
4+
# path to migration scripts
5+
script_location = migrations
6+
7+
# template used to generate migration file names; The default value is %%(rev)s_%%(slug)s
8+
# Uncomment the line below if you want the files to be prepended with date and time
9+
# see https://alembic.sqlalchemy.org/en/latest/tutorial.html#editing-the-ini-file
10+
# for all available tokens
11+
# file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s
12+
13+
# sys.path path, will be prepended to sys.path if present.
14+
# defaults to the current working directory.
15+
prepend_sys_path = .
16+
17+
# timezone to use when rendering the date within the migration file
18+
# as well as the filename.
19+
# If specified, requires the python-dateutil library that can be
20+
# installed by adding `alembic[tz]` to the pip requirements
21+
# string value is passed to dateutil.tz.gettz()
22+
# leave blank for localtime
23+
# timezone =
24+
25+
# max length of characters to apply to the
26+
# "slug" field
27+
# truncate_slug_length = 40
28+
29+
# set to 'true' to run the environment during
30+
# the 'revision' command, regardless of autogenerate
31+
# revision_environment = false
32+
33+
# set to 'true' to allow .pyc and .pyo files without
34+
# a source .py file to be detected as revisions in the
35+
# versions/ directory
36+
# sourceless = false
37+
38+
# version location specification; This defaults
39+
# to migrations/versions. When using multiple version
40+
# directories, initial revisions must be specified with --version-path.
41+
# The path separator used here should be the separator specified by "version_path_separator" below.
42+
# version_locations = %(here)s/bar:%(here)s/bat:migrations/versions
43+
44+
# version path separator; As mentioned above, this is the character used to split
45+
# version_locations. The default within new alembic.ini files is "os", which uses os.pathsep.
46+
# If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas.
47+
# Valid values for version_path_separator are:
48+
#
49+
# version_path_separator = :
50+
# version_path_separator = ;
51+
# version_path_separator = space
52+
version_path_separator = os # Use os.pathsep. Default configuration used for new projects.
53+
54+
# set to 'true' to search source files recursively
55+
# in each "version_locations" directory
56+
# new in Alembic version 1.10
57+
# recursive_version_locations = false
58+
59+
# the output encoding used when revision files
60+
# are written from script.py.mako
61+
# output_encoding = utf-8
62+
63+
sqlalchemy.url = driver://user:pass@localhost/dbname
64+
65+
66+
[post_write_hooks]
67+
# post_write_hooks defines scripts or Python functions that are run
68+
# on newly generated revision scripts. See the documentation for further
69+
# detail and examples
70+
71+
# format using "black" - use the console_scripts runner, against the "black" entrypoint
72+
# hooks = black
73+
# black.type = console_scripts
74+
# black.entrypoint = black
75+
# black.options = -l 79 REVISION_SCRIPT_FILENAME
76+
77+
# lint with attempts to fix using "ruff" - use the exec runner, execute a binary
78+
# hooks = ruff
79+
# ruff.type = exec
80+
# ruff.executable = %(here)s/.venv/bin/ruff
81+
# ruff.options = --fix REVISION_SCRIPT_FILENAME
82+
83+
# Logging configuration
84+
[loggers]
85+
keys = root,sqlalchemy,alembic
86+
87+
[handlers]
88+
keys = console
89+
90+
[formatters]
91+
keys = generic
92+
93+
[logger_root]
94+
level = WARN
95+
handlers = console
96+
qualname =
97+
98+
[logger_sqlalchemy]
99+
level = WARN
100+
handlers =
101+
qualname = sqlalchemy.engine
102+
103+
[logger_alembic]
104+
level = INFO
105+
handlers =
106+
qualname = alembic
107+
108+
[handler_console]
109+
class = StreamHandler
110+
args = (sys.stderr,)
111+
level = NOTSET
112+
formatter = generic
113+
114+
[formatter_generic]
115+
format = %(levelname)-5.5s [%(name)s] %(message)s
116+
datefmt = %H:%M:%S
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
from logging.config import fileConfig
2+
3+
from alembic import context
4+
from sqlalchemy import engine_from_config, pool
5+
from models import Hero
6+
# this is the Alembic Config object, which provides
7+
# access to the values within the .ini file in use.
8+
config = context.config
9+
10+
# Interpret the config file for Python logging.
11+
# This line sets up loggers basically.
12+
if config.config_file_name is not None:
13+
fileConfig(config.config_file_name)
14+
15+
# add your model's MetaData object here
16+
# for 'autogenerate' support
17+
# from myapp import mymodel
18+
# target_metadata = mymodel.Base.metadata
19+
target_metadata = Hero.metadata
20+
21+
# other values from the config, defined by the needs of env.py,
22+
# can be acquired:
23+
# my_important_option = config.get_main_option("my_important_option")
24+
# ... etc.
25+
26+
27+
def run_migrations_offline() -> None:
28+
"""Run migrations in 'offline' mode.
29+
30+
This configures the context with just a URL
31+
and not an Engine, though an Engine is acceptable
32+
here as well. By skipping the Engine creation
33+
we don't even need a DBAPI to be available.
34+
35+
Calls to context.execute() here emit the given string to the
36+
script output.
37+
38+
"""
39+
url = config.get_main_option("sqlalchemy.url")
40+
context.configure(
41+
url=url,
42+
target_metadata=target_metadata,
43+
literal_binds=True,
44+
dialect_opts={"paramstyle": "named"},
45+
)
46+
47+
with context.begin_transaction():
48+
context.run_migrations()
49+
50+
51+
def run_migrations_online() -> None:
52+
"""Run migrations in 'online' mode.
53+
54+
In this scenario we need to create an Engine
55+
and associate a connection with the context.
56+
57+
"""
58+
connectable = engine_from_config(
59+
config.get_section(config.config_ini_section, {}),
60+
prefix="sqlalchemy.",
61+
poolclass=pool.NullPool,
62+
)
63+
64+
with connectable.connect() as connection:
65+
context.configure(
66+
connection=connection, target_metadata=target_metadata
67+
)
68+
69+
with context.begin_transaction():
70+
context.run_migrations()
71+
72+
73+
if context.is_offline_mode():
74+
run_migrations_offline()
75+
else:
76+
run_migrations_online()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Generic single-database configuration.
2+
I'm the best!

docs_src/tutorial/migrations/simple_hero_migration/migrations/__init__.py

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

0 commit comments

Comments
 (0)