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:
paster archiver --help
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.
......@@ -59,6 +59,10 @@ class Archiver(CkanCommand):
Running this moves them to the new locations and updates the
cache_url on each resource to reflect the new location.
paster archiver migrate
- Updates the database schema to include new fields.
# paster archiver clean-files
......@@ -118,6 +122,8 @@ class Archiver(CkanCommand):'Archiver tables are initialized')
elif cmd == 'migrate-archive-dirs':
elif cmd == 'migrate':
self.log.error('Command %s not recognized' % (cmd,))
......@@ -352,6 +358,46 @@ class Archiver(CkanCommand):
print " No cache_filepath: {0}".format(not_cached_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
"etag": "ALTER TABLE archival ADD COLUMN etag character varying",
"last_modified": "ALTER TABLE archival ADD COLUMN last_modified character varying"
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:"Adding column '{0}'".format(k))"Executing '{0}'".format(v))
for k, v in MIGRATIONS_MODIFY.iteritems():
if k in current_cols:"Removing column '{0}'".format(k))"Executing '{0}'".format(v))
model.Session.commit()"Migrations complete")
def migrate_archive_dirs(self):
from ckan import model
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