Skip to content

Commit 30ef59c

Browse files
committed
storage.map_files -> maps_metadata.map_files
1 parent 2d1a8be commit 30ef59c

File tree

4 files changed

+66
-38
lines changed

4 files changed

+66
-38
lines changed
Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,74 @@
1-
from macrostrat.core.migrations import (
2-
Migration,
3-
exists,
4-
has_columns,
5-
)
1+
from macrostrat.core.migrations import Migration, exists, has_columns, _not, _any
62

73

8-
class FileStorageUpdates(Migration):
9-
name = "file-storage-updates"
4+
class MapFiles(Migration):
5+
name = "map-files"
106
subsystem = "maps"
117
description = "Update storage schema for better file management."
128
depends_on = ["api-v3"]
139
preconditions = [
14-
exists("storage", "object"),
15-
exists("storage", "object_group"),
1610
exists("maps_metadata", "ingest_process"),
11+
exists("storage", "object"),
1712
]
1813
postconditions = [
1914
# storage.object no longer has object_group_id
20-
has_columns(
21-
"storage",
22-
"object",
23-
"scheme",
24-
"host",
25-
"bucket",
26-
"key",
27-
"source",
28-
"mime_type",
29-
"sha256_hash",
30-
"created_on",
31-
"updated_on",
32-
"deleted_on",
33-
),
3415
# intersection table exists in storage schema
3516
exists("storage", "map_files"),
3617
# intersection table columns exist
3718
has_columns(
38-
"storage",
19+
"maps_metadata",
3920
"map_files",
4021
"id",
4122
"ingest_process_id",
4223
"object_id",
4324
),
4425
]
26+
27+
28+
class MapFilesChangeSchema(Migration):
29+
name = "map-files-change-schema"
30+
subsystem = "maps"
31+
description = "Change map_files table to use the map_metadata schema"
32+
depends_on = ["map-files"]
33+
preconditions = [
34+
exists("storage", "map_files"),
35+
exists("maps_metadata", "map_files"),
36+
]
37+
postconditions = [_not(exists("storage", "map_files"))]
38+
39+
def apply(self, db):
40+
db.run_sql(
41+
"""
42+
INSERT INTO maps_metadata.map_files (ingest_process_id, object_id)
43+
SELECT ingest_process_id, object_id FROM storage.map_files
44+
ON CONFLICT (ingest_process_id, object_id) DO NOTHING;
45+
46+
DROP TABLE IF EXISTS storage.map_files;
47+
"""
48+
)
49+
50+
51+
has_object_group = _any(
52+
[
53+
exists("storage", "object_group"),
54+
has_columns("storage", "object", "object_group_id"),
55+
]
56+
)
57+
58+
59+
class StorageAddColumns(Migration):
60+
name = "storage-add-columns"
61+
62+
def apply(self, db):
63+
db.run_sql(
64+
"""
65+
ALTER TABLE storage.object DROP COLUMN IF EXISTS object_group_id;
66+
DROP TABLE IF EXISTS storage.object_group;
67+
"""
68+
)
69+
70+
preconditions = [has_object_group]
71+
72+
postconditions = [
73+
_not(has_object_group),
74+
]

cli/macrostrat/cli/database/migrations/file_storage_updates/file_storage_updates.sql

Lines changed: 0 additions & 10 deletions
This file was deleted.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CREATE TABLE IF NOT EXISTS maps_metadata.map_files (
2+
id integer GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
3+
ingest_process_id integer NOT NULL
4+
REFERENCES maps_metadata.ingest_process(id) ON DELETE CASCADE,
5+
object_id integer NOT NULL REFERENCES storage.object(id),
6+
-- No cascade: don't allow deletion of files in use without explicit dereferencing
7+
UNIQUE (ingest_process_id, object_id) -- prevent duplicate entries for the same file in one ingest
8+
);

map-integration/macrostrat/map_integration/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,12 @@ def delete_sources(
158158
print("Ingest Process ID", ingest_process_id)
159159
if file_name is None:
160160
rows = db.run_query(
161-
"select f.object_id from storage.map_files f where ingest_process_id = :ingest_process_id",
161+
"select f.object_id from maps_metadata.map_files f where ingest_process_id = :ingest_process_id",
162162
dict(ingest_process_id=ingest_process_id),
163163
).fetchall()
164164
object_ids = [r[0] for r in rows]
165165
db.run_sql(
166-
"DELETE FROM storage.map_files WHERE ingest_process_id = :ingest_process_id",
166+
"DELETE FROM maps_metadata.map_files WHERE ingest_process_id = :ingest_process_id",
167167
dict(ingest_process_id=ingest_process_id),
168168
)
169169
if object_ids:
@@ -432,7 +432,7 @@ def staging(
432432
for object in object_ids:
433433
db.run_sql(
434434
"""
435-
INSERT INTO storage.map_files (ingest_process_id, object_id)
435+
INSERT INTO maps_metadata.map_files (ingest_process_id, object_id)
436436
VALUES (:ingest_process_id, :object_id)
437437
""",
438438
dict(ingest_process_id=ingest_id, object_id=object),
@@ -674,7 +674,7 @@ def staging_bulk(
674674
for object in object_ids:
675675
db.run_sql(
676676
"""
677-
INSERT INTO storage.map_files (ingest_process_id, object_id)
677+
INSERT INTO maps_metadata.map_files (ingest_process_id, object_id)
678678
VALUES (:ingest_process_id, :object_id)
679679
""",
680680
dict(ingest_process_id=ingest_id, object_id=object),

0 commit comments

Comments
 (0)