f3852e1349c4_all_files_only_on_disk.py 2.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. """all files only on disk.
  2. Revision ID: f3852e1349c4
  3. Revises: 913efdf409e5
  4. Create Date: 2017-07-24 17:15:54.278141
  5. """
  6. from alembic import context
  7. from alembic import op
  8. from depot.fields.sqlalchemy import UploadedFileField
  9. from depot.manager import DepotManager
  10. import sqlalchemy as sa
  11. # revision identifiers, used by Alembic.
  12. revision = 'f3852e1349c4'
  13. down_revision = '913efdf409e5'
  14. def configure_depot():
  15. """Configure Depot."""
  16. depot_storage_name = context.config.get_main_option('depot_storage_name')
  17. depot_storage_path = context.config.get_main_option('depot_storage_dir')
  18. depot_storage_settings = {'depot.storage_path': depot_storage_path}
  19. DepotManager.configure(
  20. depot_storage_name,
  21. depot_storage_settings,
  22. )
  23. revision_helper = sa.Table(
  24. 'content_revisions',
  25. sa.MetaData(),
  26. sa.Column('revision_id', sa.Integer, primary_key=True),
  27. sa.Column('file_content', sa.LargeBinary),
  28. sa.Column('depot_file', UploadedFileField, nullable=True),
  29. sa.Column('type', sa.String(32), nullable=False),
  30. )
  31. def upgrade():
  32. """Drop the file content from revision."""
  33. with op.batch_alter_table('content_revisions') as batch_op:
  34. batch_op.drop_column('file_content')
  35. def downgrade():
  36. """Add the file content in revision."""
  37. with op.batch_alter_table('content_revisions') as batch_op:
  38. batch_op.add_column(sa.Column('file_content', sa.LargeBinary))
  39. configure_depot()
  40. depot = DepotManager.get()
  41. connection = op.get_bind()
  42. select_query = revision_helper.select() \
  43. .where(revision_helper.c.type == 'file') \
  44. .where(revision_helper.c.depot_file.isnot(None))
  45. files = connection.execute(select_query).fetchall()
  46. for file in files:
  47. depot_file_content = depot.get(file.depot_file).read()
  48. update_query = revision_helper.update() \
  49. .where(revision_helper.c.revision_id == file.revision_id) \
  50. .values(file_content=depot_file_content)
  51. connection.execute(update_query)