Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit 330d502

Browse files
committed
write migrations
1 parent 0cfd687 commit 330d502

File tree

186 files changed

+4363
-3601
lines changed

Some content is hidden

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

186 files changed

+4363
-3601
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tmp/*
2323
solr_runtime/*
2424
fl_notes.txt
2525
*.ini
26+
!ckan/migration/alembic.ini
2627
.noseids
2728
*~
2829
.idea

ckan/migration/README

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1 @@
1-
This is a database migration repository.
2-
3-
More information at
4-
http://code.google.com/p/sqlalchemy-migrate/
1+
Generic single-database configuration.

ckan/migration/alembic.ini

+74
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 = .
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 ckan/migration/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 ckan/migration/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 = postgresql://ckan_default:root@localhost/ckan_alembic
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

ckan/migration/env.py

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

ckan/migration/manage.py

-6
This file was deleted.

ckan/migration/migrate.cfg

-20
This file was deleted.

ckan/migration/script.py.mako

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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+
19+
def upgrade():
20+
${upgrades if upgrades else "pass"}
21+
22+
23+
def downgrade():
24+
${downgrades if downgrades else "pass"}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
"""Create existing tables
2+
3+
Revision ID: 103676e0a497
4+
Revises:
5+
Create Date: 2018-09-04 16:57:42.622504
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
# revision identifiers, used by Alembic.
12+
revision = '103676e0a497'
13+
down_revision = None
14+
branch_labels = None
15+
depends_on = None
16+
17+
18+
def upgrade():
19+
20+
op.create_table(
21+
'state',
22+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
23+
sa.Column('name', sa.Unicode(100)),
24+
)
25+
26+
op.create_table(
27+
'revision',
28+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
29+
sa.Column('timestamp', sa.DateTime(timezone=False)),
30+
sa.Column('author', sa.Unicode(200)),
31+
sa.Column('message', sa.UnicodeText()),
32+
sa.Column('state_id', sa.Integer),
33+
)
34+
35+
op.create_table(
36+
'apikey',
37+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
38+
sa.Column('name', sa.UnicodeText()),
39+
sa.Column('key', sa.UnicodeText()),
40+
)
41+
42+
op.create_table(
43+
'license',
44+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
45+
sa.Column('name', sa.Unicode(100)), sa.Column('state_id', sa.Integer)
46+
)
47+
48+
op.create_table(
49+
'package',
50+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
51+
sa.Column('name', sa.Unicode(100), nullable=False, unique=True),
52+
sa.Column('title', sa.UnicodeText()),
53+
sa.Column('version', sa.Unicode(100)),
54+
sa.Column('url', sa.UnicodeText()),
55+
sa.Column('download_url', sa.UnicodeText()),
56+
sa.Column('notes', sa.UnicodeText()),
57+
sa.Column('license_id', sa.Integer, sa.ForeignKey('license.id')),
58+
sa.Column('state_id', sa.Integer, sa.ForeignKey('state.id')),
59+
sa.Column('revision_id', sa.Integer, sa.ForeignKey('revision.id')),
60+
)
61+
62+
op.create_table(
63+
'package_revision',
64+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
65+
sa.Column('name', sa.Unicode(100), nullable=False),
66+
sa.Column('title', sa.UnicodeText()),
67+
sa.Column('version', sa.Unicode(100)),
68+
sa.Column('url', sa.UnicodeText()),
69+
sa.Column('download_url', sa.UnicodeText()),
70+
sa.Column('notes', sa.UnicodeText()),
71+
sa.Column('license_id', sa.Integer, sa.ForeignKey('license.id')),
72+
sa.Column('state_id', sa.Integer, sa.ForeignKey('state.id')),
73+
sa.Column(
74+
'revision_id',
75+
sa.Integer,
76+
sa.ForeignKey('revision.id'),
77+
primary_key=True
78+
),
79+
sa.Column('continuity_id', sa.Integer, sa.ForeignKey('package.id')),
80+
)
81+
82+
op.create_table(
83+
'tag',
84+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
85+
sa.Column('name', sa.Unicode(100), nullable=False, unique=True),
86+
)
87+
88+
op.create_table(
89+
'package_tag',
90+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
91+
sa.Column('package_id', sa.Integer, sa.ForeignKey('package.id')),
92+
sa.Column('tag_id', sa.Integer, sa.ForeignKey('tag.id')),
93+
sa.Column('state_id', sa.Integer, sa.ForeignKey('state.id')),
94+
sa.Column('revision_id', sa.Integer, sa.ForeignKey('revision.id')),
95+
)
96+
97+
op.create_table(
98+
'package_tag_revision',
99+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
100+
sa.Column('package_id', sa.Integer, sa.ForeignKey('package.id')),
101+
sa.Column('tag_id', sa.Integer, sa.ForeignKey('tag.id')),
102+
sa.Column('state_id', sa.Integer, sa.ForeignKey('state.id')),
103+
sa.Column(
104+
'revision_id',
105+
sa.Integer,
106+
sa.ForeignKey('revision.id'),
107+
primary_key=True
108+
),
109+
sa.Column(
110+
'continuity_id', sa.Integer, sa.ForeignKey('package_tag.id')
111+
),
112+
)
113+
114+
op.create_table(
115+
'package_extra',
116+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
117+
sa.Column('package_id', sa.Integer, sa.ForeignKey('package.id')),
118+
sa.Column('key', sa.UnicodeText()),
119+
sa.Column('value', sa.UnicodeText()),
120+
sa.Column('state_id', sa.Integer, sa.ForeignKey('state.id')),
121+
sa.Column('revision_id', sa.Integer, sa.ForeignKey('revision.id')),
122+
)
123+
124+
op.create_table(
125+
'package_extra_revision',
126+
sa.Column('id', sa.Integer, primary_key=True, nullable=False),
127+
sa.Column('package_id', sa.Integer, sa.ForeignKey('package.id')),
128+
sa.Column('key', sa.UnicodeText()),
129+
sa.Column('value', sa.UnicodeText()),
130+
sa.Column('state_id', sa.Integer, sa.ForeignKey('state.id')),
131+
sa.Column(
132+
'revision_id',
133+
sa.Integer,
134+
sa.ForeignKey('revision.id'),
135+
primary_key=True
136+
),
137+
sa.Column(
138+
'continuity_id', sa.Integer, sa.ForeignKey('package_extra.id')
139+
),
140+
)
141+
142+
143+
def downgrade():
144+
op.drop_table('package_extra_revision')
145+
op.drop_table('package_extra')
146+
op.drop_table('package_tag_revision')
147+
op.drop_table('package_tag')
148+
op.drop_table('tag')
149+
op.drop_table('package_revision')
150+
op.drop_table('package')
151+
op.drop_table('license')
152+
op.drop_table('apikey')
153+
op.drop_table('revision')
154+
op.drop_table('state')

0 commit comments

Comments
 (0)