Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CKAN 2.11 support #133

Merged
merged 6 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v4
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
with:
python-version: '3.10'
- name: Install requirements
Expand All @@ -19,16 +19,16 @@ jobs:
needs: lint
strategy:
matrix:
ckan-version: ["2.10", 2.9]
ckan-version: ["2.11", "2.10", 2.9]
fail-fast: false

name: CKAN ${{ matrix.ckan-version }}
runs-on: ubuntu-latest
container:
image: openknowledge/ckan-dev:${{ matrix.ckan-version }}
image: ckan/ckan-dev:${{ matrix.ckan-version }}
services:
solr:
image: ckan/ckan-solr:${{ matrix.ckan-version }}
image: ckan/ckan-solr:${{ matrix.ckan-version }}-solr9
postgres:
image: ckan/ckan-postgres-dev:${{ matrix.ckan-version }}
env:
Expand All @@ -46,7 +46,7 @@ jobs:
CKAN_REDIS_URL: redis://redis:6379/1

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4
- name: Install requirements
run: |
pip install -r requirements.txt
Expand All @@ -57,10 +57,7 @@ jobs:
- name: Setup extension (CKAN >= 2.9)
run: |
ckan -c test.ini db init
ckan -c test.ini pages initdb
ckan -c test.ini db upgrade -p pages
- name: Run tests
run: pytest --ckan-ini=test.ini --cov=ckanext.pages --cov-report=xml --cov-append --disable-warnings ckanext/pages/tests
- name: Upload coverage report to codecov
uses: codecov/codecov-action@v1
with:
file: ./coverage.xml
run: pytest --ckan-ini=test.ini --cov=ckanext.pages --cov-report=term-missing --cov-append --disable-warnings ckanext/pages/tests

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ You need to initialize database from command line with the following commands:

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


Expand Down
24 changes: 0 additions & 24 deletions ckanext/pages/cli.py

This file was deleted.

60 changes: 26 additions & 34 deletions ckanext/pages/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from six import text_type
import sqlalchemy as sa
from sqlalchemy import Column, types
from sqlalchemy.orm import class_mapper

try:
Expand All @@ -17,22 +18,40 @@
from ckan import model
from ckan.model.domain_object import DomainObject

try:
from ckan.plugins.toolkit import BaseModel
except ImportError:
# CKAN <= 2.9
from ckan.model.meta import metadata
from sqlalchemy.ext.declarative import declarative_base

BaseModel = declarative_base(metadata=metadata)

pages_table = None


def make_uuid():
return text_type(uuid.uuid4())


def init_db():
if pages_table is None:
define_tables()
class Page(DomainObject, BaseModel):

if not pages_table.exists():
pages_table.create()
__tablename__ = "ckanext_pages"


class Page(DomainObject):
id = Column(types.UnicodeText, primary_key=True, default=make_uuid)
title = Column(types.UnicodeText, default=u'')
name = Column(types.UnicodeText, default=u'')
content = Column(types.UnicodeText, default=u'')
lang = Column(types.UnicodeText, default=u'')
order = Column(types.UnicodeText, default=u'')
private = Column(types.Boolean, default=True)
group_id = Column(types.UnicodeText, default=None)
user_id = Column(types.UnicodeText, default=u'')
publish_date = Column(types.DateTime)
page_type = Column(types.UnicodeText)
created = Column(types.DateTime, default=datetime.datetime.utcnow)
modified = Column(types.DateTime, default=datetime.datetime.utcnow)
extras = Column(types.UnicodeText, default=u'{}')

@classmethod
def get(cls, **kw):
Expand All @@ -57,33 +76,6 @@ def pages(cls, **kw):
return query.all()


def define_tables():
types = sa.types
global pages_table
pages_table = sa.Table('ckanext_pages', model.meta.metadata,
sa.Column('id', types.UnicodeText, primary_key=True, default=make_uuid),
sa.Column('title', types.UnicodeText, default=u''),
sa.Column('name', types.UnicodeText, default=u''),
sa.Column('content', types.UnicodeText, default=u''),
sa.Column('lang', types.UnicodeText, default=u''),
sa.Column('order', types.UnicodeText, default=u''),
sa.Column('private', types.Boolean, default=True),
sa.Column('group_id', types.UnicodeText, default=None),
sa.Column('user_id', types.UnicodeText, default=u''),
sa.Column('publish_date', types.DateTime),
sa.Column('page_type', types.UnicodeText),
sa.Column('created', types.DateTime, default=datetime.datetime.utcnow),
sa.Column('modified', types.DateTime, default=datetime.datetime.utcnow),
sa.Column('extras', types.UnicodeText, default=u'{}'),
extend_existing=True
)

model.meta.mapper(
Page,
pages_table,
)


def table_dictize(obj, context, **kw):
'''Get any model object and represent it as a dict'''
result_dict = {}
Expand Down
1 change: 1 addition & 0 deletions ckanext/pages/migration/pages/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Generic single-database configuration.
74 changes: 74 additions & 0 deletions ckanext/pages/migration/pages/alembic.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# A generic, single database configuration.

[alembic]
# path to migration scripts
script_location = %(here)s

# template used to generate migration files
# file_template = %%(rev)s_%%(slug)s

# timezone to use when rendering the date
# within the migration file as well as the filename.
# string value is passed to dateutil.tz.gettz()
# leave blank for localtime
# timezone =

# max length of characters to apply to the
# "slug" field
#truncate_slug_length = 40

# set to 'true' to run the environment during
# the 'revision' command, regardless of autogenerate
# revision_environment = false

# set to 'true' to allow .pyc and .pyo files without
# a source .py file to be detected as revisions in the
# versions/ directory
# sourceless = false

# version location specification; this defaults
# to /home/adria/dev/pyenvs/ckan-211/ckanext-pages/ckanext/pages/migration/pages/versions. When using multiple version
# directories, initial revisions must be specified with --version-path
# version_locations = %(here)s/bar %(here)s/bat /home/adria/dev/pyenvs/ckan-211/ckanext-pages/ckanext/pages/migration/pages/versions

# the output encoding used when revision files
# are written from script.py.mako
# output_encoding = utf-8

sqlalchemy.url = driver://user:pass@localhost/dbname


# Logging configuration
[loggers]
keys = root,sqlalchemy,alembic

[handlers]
keys = console

[formatters]
keys = generic

[logger_root]
level = WARN
handlers = console
qualname =

[logger_sqlalchemy]
level = WARN
handlers =
qualname = sqlalchemy.engine

[logger_alembic]
level = INFO
handlers =
qualname = alembic

[handler_console]
class = StreamHandler
args = (sys.stderr,)
level = NOTSET
formatter = generic

[formatter_generic]
format = %(levelname)-5.5s [%(name)s] %(message)s
datefmt = %H:%M:%S
90 changes: 90 additions & 0 deletions ckanext/pages/migration/pages/env.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
# -*- coding: utf-8 -*-

from __future__ import with_statement
from alembic import context
from sqlalchemy import engine_from_config, pool
from logging.config import fileConfig
from ckan.model.meta import metadata

import os

# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.
config = context.config

# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)

# add your model's MetaData object here
# for 'autogenerate' support
# from myapp import mymodel
# target_metadata = mymodel.Base.metadata
target_metadata = metadata

# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
# ... etc.

name = os.path.basename(os.path.dirname(__file__))


def include_object(object, object_name, type_, reflected, compare_to):
if type_ == "table":
return object_name.startswith(name)
return True


def run_migrations_offline():
"""Run migrations in 'offline' mode.

This configures the context with just a URL
and not an Engine, though an Engine is acceptable
here as well. By skipping the Engine creation
we don't even need a DBAPI to be available.

Calls to context.execute() here emit the given string to the
script output.

"""

url = config.get_main_option(u"sqlalchemy.url")
context.configure(
url=url, target_metadata=target_metadata, literal_binds=True,
version_table=u'{}_alembic_version'.format(name),
include_object=include_object,
)

with context.begin_transaction():
context.run_migrations()


def run_migrations_online():
"""Run migrations in 'online' mode.

In this scenario we need to create an Engine
and associate a connection with the context.

"""
connectable = engine_from_config(
config.get_section(config.config_ini_section),
prefix=u'sqlalchemy.',
poolclass=pool.NullPool)

with connectable.connect() as connection:
context.configure(
connection=connection,
target_metadata=target_metadata,
version_table=u'{}_alembic_version'.format(name),
include_object=include_object,
)

with context.begin_transaction():
context.run_migrations()


if context.is_offline_mode():
run_migrations_offline()
else:
run_migrations_online()
24 changes: 24 additions & 0 deletions ckanext/pages/migration/pages/script.py.mako
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""${message}

Revision ID: ${up_revision}
Revises: ${down_revision | comma,n}
Create Date: ${create_date}

"""
from alembic import op
import sqlalchemy as sa
${imports if imports else ""}

# revision identifiers, used by Alembic.
revision = ${repr(up_revision)}
down_revision = ${repr(down_revision)}
branch_labels = ${repr(branch_labels)}
depends_on = ${repr(depends_on)}


def upgrade():
${upgrades if upgrades else "pass"}


def downgrade():
${downgrades if downgrades else "pass"}
Loading