Skip to content

Commit 10b3802

Browse files
committed
add alembic migrations support
1 parent dc00fff commit 10b3802

File tree

6 files changed

+168
-10
lines changed

6 files changed

+168
-10
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
alembic.ini
12
.tox
23
.noseids
34
rdflib_sqlalchemy.egg-info

alembic.ini

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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 files
8+
# file_template = %%(rev)s_%%(slug)s
9+
10+
# max length of characters to apply to the
11+
# "slug" field
12+
#truncate_slug_length = 40
13+
14+
# set to 'true' to run the environment during
15+
# the 'revision' command, regardless of autogenerate
16+
# revision_environment = false
17+
18+
# set to 'true' to allow .pyc and .pyo files without
19+
# a source .py file to be detected as revisions in the
20+
# versions/ directory
21+
# sourceless = false
22+
23+
# version location specification; this defaults
24+
# to migrations/versions. When using multiple version
25+
# directories, initial revisions must be specified with --version-path
26+
# version_locations = %(here)s/bar %(here)s/bat migrations/versions
27+
28+
# the output encoding used when revision files
29+
# are written from script.py.mako
30+
# output_encoding = utf-8
31+
32+
sqlalchemy.url = driver://user:pass@localhost/dbname
33+
34+
35+
# Logging configuration
36+
[loggers]
37+
keys = root,sqlalchemy,alembic
38+
39+
[handlers]
40+
keys = console
41+
42+
[formatters]
43+
keys = generic
44+
45+
[logger_root]
46+
level = WARN
47+
handlers = console
48+
qualname =
49+
50+
[logger_sqlalchemy]
51+
level = WARN
52+
handlers =
53+
qualname = sqlalchemy.engine
54+
55+
[logger_alembic]
56+
level = INFO
57+
handlers =
58+
qualname = alembic
59+
60+
[handler_console]
61+
class = StreamHandler
62+
args = (sys.stderr,)
63+
level = NOTSET
64+
formatter = generic
65+
66+
[formatter_generic]
67+
format = %(levelname)-5.5s [%(name)s] %(message)s
68+
datefmt = %H:%M:%S

migrations/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.

migrations/env.py

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

migrations/script.py.mako

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""${message}
2+
3+
Revision ID: ${up_revision}
4+
Revises: ${down_revision | comma,n}
5+
Create Date: ${create_date}
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
${imports if imports else ""}
11+
12+
# revision identifiers, used by Alembic.
13+
revision = ${repr(up_revision)}
14+
down_revision = ${repr(down_revision)}
15+
branch_labels = ${repr(branch_labels)}
16+
depends_on = ${repr(depends_on)}
17+
18+
def upgrade():
19+
${upgrades if upgrades else "pass"}
20+
21+
22+
def downgrade():
23+
${downgrades if downgrades else "pass"}

setup.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
license="BSD",
1919
platforms=["any"],
2020
long_description="""
21-
SQLAlchemy store formula-aware implementation. It stores its triples in
22-
the following partitions:
21+
SQLAlchemy store formula-aware implementation.
22+
It stores its triples in the following partitions:
2323
2424
* Asserted non rdf:type statements
2525
* Asserted rdf:type statements (in a table which models Class membership).
@@ -35,7 +35,6 @@
3535
"Programming Language :: Python",
3636
"Programming Language :: Python :: 2",
3737
"Programming Language :: Python :: 3",
38-
"Programming Language :: Python :: 2.6",
3938
"Programming Language :: Python :: 2.7",
4039
"Programming Language :: Python :: 3.3",
4140
"Programming Language :: Python :: 3.4",
@@ -45,16 +44,11 @@
4544
"Operating System :: OS Independent",
4645
"Natural Language :: English",
4746
],
48-
entry_points={
49-
'rdf.plugins.store': [
50-
# 'SQLAlchemy = rdflib_sqlalchemy.SQLAlchemy:SQLAlchemy',
51-
# 'SQLAlchemyBase = rdflib_sqlalchemy.SQLAlchemyBase:SQLAlchemy',
52-
],
53-
},
5447
install_requires=[
48+
"alembic>=0.8.8",
5549
"rdflib>=4.0",
5650
"six>=1.10.0",
57-
"SQLAlchemy",
51+
"SQLAlchemy>=1.1.4",
5852
],
5953
setup_requires=[
6054
"nose>=1.3.6",

0 commit comments

Comments
 (0)