Commit d2f9fddb authored by Ross Jones's avatar Ross Jones
Browse files

Add a migration command for db schema changes

Adds a new paster command (migrate) that will update the archival table
if there have been any changes.  These changes are stored in the code
and checks are made against the current state of the database before the
SQL is run.
parent ed5830e7
...@@ -142,6 +142,17 @@ The Archiver can be used in two ways: ...@@ -142,6 +142,17 @@ The Archiver can be used in two ways:
paster archiver --help paster archiver --help
Migrations
----------
Over time it is possible that the database structure will change. In these cases you can use the migrate command to update the database schema.
::
paster --plugin=ckanext-archiver archiver migrate -c <path to CKAN ini file>
This is only necessary if you update ckanext-archiver and already have the database tables in place.
Testing Testing
------- -------
......
...@@ -59,6 +59,10 @@ class Archiver(CkanCommand): ...@@ -59,6 +59,10 @@ class Archiver(CkanCommand):
{2-chars-of-resource-id}/{resource-id}/filename.csv {2-chars-of-resource-id}/{resource-id}/filename.csv
Running this moves them to the new locations and updates the Running this moves them to the new locations and updates the
cache_url on each resource to reflect the new location. cache_url on each resource to reflect the new location.
paster archiver migrate
- Updates the database schema to include new fields.
''' '''
# TODO # TODO
# paster archiver clean-files # paster archiver clean-files
...@@ -118,6 +122,8 @@ class Archiver(CkanCommand): ...@@ -118,6 +122,8 @@ class Archiver(CkanCommand):
self.log.info('Archiver tables are initialized') self.log.info('Archiver tables are initialized')
elif cmd == 'migrate-archive-dirs': elif cmd == 'migrate-archive-dirs':
self.migrate_archive_dirs() self.migrate_archive_dirs()
elif cmd == 'migrate':
self.migrate()
else: else:
self.log.error('Command %s not recognized' % (cmd,)) self.log.error('Command %s not recognized' % (cmd,))
...@@ -352,6 +358,46 @@ class Archiver(CkanCommand): ...@@ -352,6 +358,46 @@ class Archiver(CkanCommand):
print " No cache_filepath: {0}".format(not_cached_deleted) print " No cache_filepath: {0}".format(not_cached_deleted)
print " cache_filepath not on disk: {0}".format(file_not_found_deleted) print " cache_filepath not on disk: {0}".format(file_not_found_deleted)
def migrate(self):
""" Adds any missing columns to the database table for Archival by
checking the schema and adding those that are missing.
If you wish to add a column, add the column name and sql
statement to MIGRATIONS_ADD which will check that the column is
not present before running the query.
If you wish to modify or delete a column, add the column name and
query to the MIGRATIONS_MODIFY which only runs if the column
does exist.
"""
from ckan import model
MIGRATIONS_ADD = {
"etag": "ALTER TABLE archival ADD COLUMN etag character varying",
"last_modified": "ALTER TABLE archival ADD COLUMN last_modified character varying"
}
MIGRATIONS_MODIFY = {
}
q = "select column_name from INFORMATION_SCHEMA.COLUMNS where table_name = 'archival';"
current_cols = list([m[0] for m in model.Session.execute(q)])
for k, v in MIGRATIONS_ADD.iteritems():
if not k in current_cols:
self.log.info(u"Adding column '{0}'".format(k))
self.log.info(u"Executing '{0}'".format(v))
model.Session.execute(v)
model.Session.commit()
for k, v in MIGRATIONS_MODIFY.iteritems():
if k in current_cols:
self.log.info(u"Removing column '{0}'".format(k))
self.log.info(u"Executing '{0}'".format(v))
model.Session.execute(v)
model.Session.commit()
self.log.info("Migrations complete")
def migrate_archive_dirs(self): def migrate_archive_dirs(self):
from ckan import model from ckan import model
from ckan.logic import get_action from ckan.logic import get_action
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment