-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathseed_db_containersGeneric.py
More file actions
107 lines (84 loc) · 3.58 KB
/
seed_db_containersGeneric.py
File metadata and controls
107 lines (84 loc) · 3.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import json
from bloom_lims.db import BLOOMdb3
from bloom_lims.bobjs import BloomObj
import sys
import os
from sqlalchemy import text
def ensure_sequence_exists(db, prefix: str) -> None:
"""
Ensure the EUID sequence exists for a given prefix.
The set_generic_instance_euid() trigger requires a sequence named
{lowercase_prefix}_instance_seq for each template prefix. This function
creates the sequence if it doesn't exist, making the seeding process
resilient to new prefixes being added to metadata.json files.
Args:
db: BLOOMdb3 database instance
prefix: The instance_prefix (e.g., 'DAT', 'EM', 'HEV')
"""
seq_name = f"{prefix.lower()}_instance_seq"
# Check if sequence exists
result = db.session.execute(text("""
SELECT 1 FROM information_schema.sequences
WHERE sequence_schema = 'public' AND sequence_name = :seq_name
"""), {"seq_name": seq_name})
if result.fetchone() is None:
# Create the sequence
db.session.execute(text(f"CREATE SEQUENCE IF NOT EXISTS {seq_name} START 1"))
db.session.execute(text(
f"COMMENT ON SEQUENCE {seq_name} IS 'Auto-created EUID sequence for prefix {prefix}'"
))
db.session.commit()
print(f" ✓ Created missing sequence: {seq_name}")
def create_template_from_json(json_file, db):
"""
Parse the JSON file and create new *_template records in the database.
:param json_file: Path to the JSON file.
:param db: Instance of BLOOMdb3 for database interactions.
"""
type_name = os.path.splitext(os.path.basename(json_file))[0]
table_prefix = os.path.dirname(json_file).split("/")[-1]
euid_prefix = os.path.dirname(json_file) + "/metadata.json"
md_json = json.load(open(euid_prefix))
with open(json_file, "r") as file:
data = json.load(file)
for subtype, versions in data.items():
for version, details in versions.items():
# Prepare json_addl field
json_addl = details # json.dumps(details)
obj_prefix = (
md_json["euid_prefixes"]["default"]
if type_name not in md_json["euid_prefixes"]
else md_json["euid_prefixes"][type_name]
)
# Ensure the EUID sequence exists for this prefix
# This auto-creates sequences for new prefixes added to metadata.json
ensure_sequence_exists(db, obj_prefix)
# TapDB uses sequence-based EUID generation via bloom_prefix_sequences.sql
# No need to check for CASE expressions in legacy schema
print(f"Seeding {table_prefix} with prefix {obj_prefix}")
# Create new table_template record
table_template = db.Base.classes.generic_template
new_table_template = table_template(
name=f"{type_name}:{subtype}:{version}",
category=table_prefix,
type=type_name,
subtype=subtype,
version=version,
json_addl=json_addl,
instance_prefix=obj_prefix,
is_singleton=True,
bstatus="ready",
polymorphic_discriminator=f"{table_prefix}_template",
)
db.session.add(new_table_template)
db.session.commit()
def main():
db = BLOOMdb3(app_username="bloom_db_init")
# Path to the JSON file
json_file_path = sys.argv[1]
# Process the JSON and create records
create_template_from_json(json_file_path, db)
# Close the database connection
db.close()
if __name__ == "__main__":
main()