Skip to content

Commit

Permalink
Add schema downgrade action for the web app database. (#114)
Browse files Browse the repository at this point in the history
  • Loading branch information
NiharikaRay authored and matthewwardrop committed Mar 25, 2018
1 parent 6ad6878 commit e64e102
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
4 changes: 4 additions & 0 deletions knowledge_repo/app/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ def db_upgrade(self):
with self.app_context():
command.upgrade(self._alembic_config, "head")

def db_downgrade(self, revision):
with self.app_context():
command.downgrade(self._alembic_config, revision)

def db_migrate(self, message, autogenerate=True):
with self.app_context():
command.revision(self._alembic_config, message=message, autogenerate=autogenerate)
Expand Down
10 changes: 10 additions & 0 deletions scripts/knowledge_repo
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,12 @@ db_upgrade.add_argument('-c', '--config', default=None, help="The config file fr
db_upgrade.add_argument('-m', '--message', help="The message to use for the database revision.")
db_upgrade.add_argument('--autogenerate', action='store_true', help="Whether alembic should automatically populate the migration script.")

db_downgrade = subparsers.add_parser('db_downgrade', help='Downgrade the database to the schema identified by a revision number.')
db_downgrade.set_defaults(action='db_downgrade')
db_downgrade.add_argument('revision', help="The target database revision. Use '-1' for the previous version.")
db_downgrade.add_argument('-db', '--dburi', help='The SQLAlchemy database uri.')
db_downgrade.add_argument('-c', '--config', default=None, help="The config file from which to read server configuration.")

reindex = subparsers.add_parser('reindex', help='Update the index, updating all posts even if they exist in the database already; but will not lose post views and other usage metadata.')
reindex.set_defaults(action='reindex')
reindex.add_argument('-db', '--dburi', help='The SQLAlchemy database uri.')
Expand Down Expand Up @@ -332,6 +338,10 @@ elif args.action == 'db_upgrade':
app = repo.get_app(db_uri=args.dburi, debug=args.debug, config=args.config)
app.db_upgrade()

elif args.action == 'db_downgrade':
app = repo.get_app(db_uri=args.dburi, debug=args.debug, config=args.config)
app.db_downgrade(revision=args.revision)

elif args.action == 'db_migrate':
app = repo.get_app(debug=args.debug, db_uri=args.dburi)
app.db_migrate(args.message, autogenerate=args.autogenerate)
Expand Down

0 comments on commit e64e102

Please sign in to comment.