Skip to content

Commit 81912bc

Browse files
authored
Merge pull request #133 from ckan/2.11-support
CKAN 2.11 support
2 parents ca68f37 + 0098dae commit 81912bc

File tree

13 files changed

+278
-86
lines changed

13 files changed

+278
-86
lines changed

.github/workflows/test.yml

+9-12
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ jobs:
44
lint:
55
runs-on: ubuntu-latest
66
steps:
7-
- uses: actions/checkout@v3
8-
- uses: actions/setup-python@v4
7+
- uses: actions/checkout@v4
8+
- uses: actions/setup-python@v5
99
with:
1010
python-version: '3.10'
1111
- name: Install requirements
@@ -19,16 +19,16 @@ jobs:
1919
needs: lint
2020
strategy:
2121
matrix:
22-
ckan-version: ["2.10", 2.9]
22+
ckan-version: ["2.11", "2.10", 2.9]
2323
fail-fast: false
2424

2525
name: CKAN ${{ matrix.ckan-version }}
2626
runs-on: ubuntu-latest
2727
container:
28-
image: openknowledge/ckan-dev:${{ matrix.ckan-version }}
28+
image: ckan/ckan-dev:${{ matrix.ckan-version }}
2929
services:
3030
solr:
31-
image: ckan/ckan-solr:${{ matrix.ckan-version }}
31+
image: ckan/ckan-solr:${{ matrix.ckan-version }}-solr9
3232
postgres:
3333
image: ckan/ckan-postgres-dev:${{ matrix.ckan-version }}
3434
env:
@@ -46,7 +46,7 @@ jobs:
4646
CKAN_REDIS_URL: redis://redis:6379/1
4747

4848
steps:
49-
- uses: actions/checkout@v2
49+
- uses: actions/checkout@v4
5050
- name: Install requirements
5151
run: |
5252
pip install -r requirements.txt
@@ -57,10 +57,7 @@ jobs:
5757
- name: Setup extension (CKAN >= 2.9)
5858
run: |
5959
ckan -c test.ini db init
60-
ckan -c test.ini pages initdb
60+
ckan -c test.ini db upgrade -p pages
6161
- name: Run tests
62-
run: pytest --ckan-ini=test.ini --cov=ckanext.pages --cov-report=xml --cov-append --disable-warnings ckanext/pages/tests
63-
- name: Upload coverage report to codecov
64-
uses: codecov/codecov-action@v1
65-
with:
66-
file: ./coverage.xml
62+
run: pytest --ckan-ini=test.ini --cov=ckanext.pages --cov-report=term-missing --cov-append --disable-warnings ckanext/pages/tests
63+

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ You need to initialize database from command line with the following commands:
3333

3434
ON CKAN >= 2.9:
3535
```
36-
(pyenv) $ ckan --config=/etc/ckan/default/ckan.ini pages initdb
36+
(pyenv) $ ckan --config=/etc/ckan/default/ckan.ini db upgrade -p pages
3737
```
3838

3939

ckanext/pages/cli.py

-24
This file was deleted.

ckanext/pages/db.py

+26-34
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from six import text_type
66
import sqlalchemy as sa
7+
from sqlalchemy import Column, types
78
from sqlalchemy.orm import class_mapper
89

910
try:
@@ -17,22 +18,40 @@
1718
from ckan import model
1819
from ckan.model.domain_object import DomainObject
1920

21+
try:
22+
from ckan.plugins.toolkit import BaseModel
23+
except ImportError:
24+
# CKAN <= 2.9
25+
from ckan.model.meta import metadata
26+
from sqlalchemy.ext.declarative import declarative_base
27+
28+
BaseModel = declarative_base(metadata=metadata)
29+
2030
pages_table = None
2131

2232

2333
def make_uuid():
2434
return text_type(uuid.uuid4())
2535

2636

27-
def init_db():
28-
if pages_table is None:
29-
define_tables()
37+
class Page(DomainObject, BaseModel):
3038

31-
if not pages_table.exists():
32-
pages_table.create()
39+
__tablename__ = "ckanext_pages"
3340

34-
35-
class Page(DomainObject):
41+
id = Column(types.UnicodeText, primary_key=True, default=make_uuid)
42+
title = Column(types.UnicodeText, default=u'')
43+
name = Column(types.UnicodeText, default=u'')
44+
content = Column(types.UnicodeText, default=u'')
45+
lang = Column(types.UnicodeText, default=u'')
46+
order = Column(types.UnicodeText, default=u'')
47+
private = Column(types.Boolean, default=True)
48+
group_id = Column(types.UnicodeText, default=None)
49+
user_id = Column(types.UnicodeText, default=u'')
50+
publish_date = Column(types.DateTime)
51+
page_type = Column(types.UnicodeText)
52+
created = Column(types.DateTime, default=datetime.datetime.utcnow)
53+
modified = Column(types.DateTime, default=datetime.datetime.utcnow)
54+
extras = Column(types.UnicodeText, default=u'{}')
3655

3756
@classmethod
3857
def get(cls, **kw):
@@ -57,33 +76,6 @@ def pages(cls, **kw):
5776
return query.all()
5877

5978

60-
def define_tables():
61-
types = sa.types
62-
global pages_table
63-
pages_table = sa.Table('ckanext_pages', model.meta.metadata,
64-
sa.Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
65-
sa.Column('title', types.UnicodeText, default=u''),
66-
sa.Column('name', types.UnicodeText, default=u''),
67-
sa.Column('content', types.UnicodeText, default=u''),
68-
sa.Column('lang', types.UnicodeText, default=u''),
69-
sa.Column('order', types.UnicodeText, default=u''),
70-
sa.Column('private', types.Boolean, default=True),
71-
sa.Column('group_id', types.UnicodeText, default=None),
72-
sa.Column('user_id', types.UnicodeText, default=u''),
73-
sa.Column('publish_date', types.DateTime),
74-
sa.Column('page_type', types.UnicodeText),
75-
sa.Column('created', types.DateTime, default=datetime.datetime.utcnow),
76-
sa.Column('modified', types.DateTime, default=datetime.datetime.utcnow),
77-
sa.Column('extras', types.UnicodeText, default=u'{}'),
78-
extend_existing=True
79-
)
80-
81-
model.meta.mapper(
82-
Page,
83-
pages_table,
84-
)
85-
86-
8779
def table_dictize(obj, context, **kw):
8880
'''Get any model object and represent it as a dict'''
8981
result_dict = {}

ckanext/pages/migration/pages/README

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Generic single-database configuration.
+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 = %(here)s
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 /home/adria/dev/pyenvs/ckan-211/ckanext-pages/ckanext/pages/migration/pages/versions. When using multiple version
31+
# directories, initial revisions must be specified with --version-path
32+
# version_locations = %(here)s/bar %(here)s/bat /home/adria/dev/pyenvs/ckan-211/ckanext-pages/ckanext/pages/migration/pages/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 = driver://user:pass@localhost/dbname
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

ckanext/pages/migration/pages/env.py

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

0 commit comments

Comments
 (0)