|
@@ -0,0 +1,151 @@
|
|
1
|
+"""all files also on disk.
|
|
2
|
+
|
|
3
|
+Revision ID: 913efdf409e5
|
|
4
|
+Revises: 69fb10c3d6f0
|
|
5
|
+Create Date: 2017-07-12 15:44:20.568447
|
|
6
|
+
|
|
7
|
+"""
|
|
8
|
+
|
|
9
|
+import shutil
|
|
10
|
+
|
|
11
|
+from alembic import op
|
|
12
|
+from depot.fields.sqlalchemy import UploadedFileField
|
|
13
|
+from depot.fields.upload import UploadedFile
|
|
14
|
+from depot.io.utils import FileIntent
|
|
15
|
+from depot.manager import DepotManager
|
|
16
|
+import sqlalchemy as sa
|
|
17
|
+# from sqlalchemy.ext.declarative import declarative_base
|
|
18
|
+# from sqlalchemy.orm import sessionmaker
|
|
19
|
+
|
|
20
|
+# revision identifiers, used by Alembic.
|
|
21
|
+revision = '913efdf409e5'
|
|
22
|
+down_revision = '69fb10c3d6f0'
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+# Session = sessionmaker()
|
|
26
|
+# DeclarativeBase = declarative_base()
|
|
27
|
+
|
|
28
|
+DepotManager.configure(
|
|
29
|
+ 'default', {
|
|
30
|
+ 'depot.storage_path': 'depot/',
|
|
31
|
+})
|
|
32
|
+# DepotManager.set_default('default')
|
|
33
|
+
|
|
34
|
+revision_helper = sa.Table(
|
|
35
|
+ 'content_revisions',
|
|
36
|
+ sa.MetaData(),
|
|
37
|
+ sa.Column('revision_id', sa.Integer, primary_key=True),
|
|
38
|
+ sa.Column('label', sa.String(1024), nullable=False),
|
|
39
|
+ sa.Column('file_extension', sa.String(255), nullable=False),
|
|
40
|
+ sa.Column('file_mimetype', sa.String(255), nullable=False),
|
|
41
|
+ sa.Column('file_content', sa.LargeBinary),
|
|
42
|
+ sa.Column('depot_file', UploadedFileField, nullable=True),
|
|
43
|
+ sa.Column('type', sa.String(32), nullable=False),
|
|
44
|
+)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+# class RevisionForMigration(DeclarativeBase):
|
|
48
|
+# """Revision of Content for this migration."""
|
|
49
|
+
|
|
50
|
+# __tablename__ = 'content_revisions'
|
|
51
|
+
|
|
52
|
+# revision_id = Column(Integer, primary_key=True)
|
|
53
|
+
|
|
54
|
+# label = Column(Unicode(1024), unique=False, nullable=False)
|
|
55
|
+# description = Column(Text(), unique=False, nullable=False, default='')
|
|
56
|
+# file_extension = Column(
|
|
57
|
+# Unicode(255),
|
|
58
|
+# unique=False,
|
|
59
|
+# nullable=False,
|
|
60
|
+# server_default='',
|
|
61
|
+# )
|
|
62
|
+# file_mimetype = Column(Unicode(255), unique=False, nullable=False, default='')
|
|
63
|
+# file_content = deferred(Column(LargeBinary(), unique=False, nullable=True))
|
|
64
|
+# depot_file = Column(UploadedFileField, unique=False, nullable=True)
|
|
65
|
+# properties = Column('properties', Text(), unique=False, nullable=False, default='')
|
|
66
|
+# type = Column(Unicode(32), unique=False, nullable=False)
|
|
67
|
+# status = Column(Unicode(32), unique=False, nullable=False, default=ContentStatus.OPEN)
|
|
68
|
+# created = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
|
69
|
+# updated = Column(DateTime, unique=False, nullable=False, default=datetime.utcnow)
|
|
70
|
+# is_deleted = Column(Boolean, unique=False, nullable=False, default=False)
|
|
71
|
+# is_archived = Column(Boolean, unique=False, nullable=False, default=False)
|
|
72
|
+# is_temporary = Column(Boolean, unique=False, nullable=False, default=False)
|
|
73
|
+# revision_type = Column(Unicode(32), unique=False, nullable=False, default='')
|
|
74
|
+
|
|
75
|
+# def file_name(self) -> str:
|
|
76
|
+# return '{0}{1}'.format(
|
|
77
|
+# self.revision.label,
|
|
78
|
+# self.revision.file_extension,
|
|
79
|
+# )
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+# def fill_depot_file_fields()
|
|
83
|
+# """fills depot file fields when they are null."""
|
|
84
|
+# connection = op.get_bind()
|
|
85
|
+# session = Session(bind=connection)
|
|
86
|
+
|
|
87
|
+# all_files_wo_depot = session.query(RevisionForMigration) \
|
|
88
|
+# .filter_by(type='file', depot_file=None) \
|
|
89
|
+# .all()
|
|
90
|
+# for one_file_wo_depot in all_files_wo_depot:
|
|
91
|
+# # with tempfile.TemporaryFile() as tmp_file:
|
|
92
|
+# # tmp_file.write(one_file_wo_depot.content)
|
|
93
|
+# # one_file_wo_depot.depot_file = tmp_file
|
|
94
|
+# one_file_wo_depot.depot_file = (
|
|
95
|
+# one_file_wo_depot.content,
|
|
96
|
+# one_file_wo_depot.file_name(),
|
|
97
|
+# one_file_wo_depot.file_mimetype,
|
|
98
|
+# )
|
|
99
|
+# session.commit()
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+def upgrade():
|
|
103
|
+ """Sets depot file for file typed revisions."""
|
|
104
|
+ import pudb; pu.db
|
|
105
|
+
|
|
106
|
+ # fill_depot_file_fields()
|
|
107
|
+
|
|
108
|
+ connection = op.get_bind()
|
|
109
|
+ depot = DepotManager.get()
|
|
110
|
+
|
|
111
|
+ file_revision_query = revision_helper.select() \
|
|
112
|
+ .where(revision_helper.c.type == 'file')
|
|
113
|
+ all_file_revisions = connection.execute(file_revision_query)
|
|
114
|
+ for one_revision in all_file_revisions:
|
|
115
|
+ one_revision_filename = '{0}{1}'.format(
|
|
116
|
+ one_revision.label,
|
|
117
|
+ one_revision.file_extension,
|
|
118
|
+ )
|
|
119
|
+ if not one_revision.depot_file:
|
|
120
|
+ depot_file_intent = FileIntent(
|
|
121
|
+ one_revision.file_content,
|
|
122
|
+ one_revision_filename,
|
|
123
|
+ one_revision.file_mimetype,
|
|
124
|
+ )
|
|
125
|
+ depot_file_field = UploadedFile(depot_file_intent)
|
|
126
|
+ one_revision_update = revision_helper.update() \
|
|
127
|
+ .where(revision_helper.c.type == 'file') \
|
|
128
|
+ .where(revision_helper.c.revision_id == one_revision.revision_id) \
|
|
129
|
+ .values(depot_file=depot_file_field) \
|
|
130
|
+ .return_defaults()
|
|
131
|
+ result = connection.execute(one_revision_update)
|
|
132
|
+ else:
|
|
133
|
+ # TODO - A.P - makes the field in DB to be updated as well
|
|
134
|
+ depot_file_uid = depot.replace(
|
|
135
|
+ one_revision.depot_file.file_id,
|
|
136
|
+ one_revision.file_content,
|
|
137
|
+ one_revision_filename,
|
|
138
|
+ one_revision.file_mimetype,
|
|
139
|
+ )
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+def downgrade():
|
|
143
|
+ """Resets depot file for file typed revisions."""
|
|
144
|
+ connection = op.get_bind()
|
|
145
|
+ file_revision_query = revision_helper.update() \
|
|
146
|
+ .where(revision_helper.c.type == 'file') \
|
|
147
|
+ .where(revision_helper.c.depot_file.isnot(None)) \
|
|
148
|
+ .values(depot_file=None) \
|
|
149
|
+ .return_defaults()
|
|
150
|
+ result = connection.execute(file_revision_query)
|
|
151
|
+ shutil.rmtree('depot/', ignore_errors=True)
|