Skip to content

Commit cf9c0c9

Browse files
committed
consistently make tables with schema argument, add sql-type pass methods
1 parent 7df7b9e commit cf9c0c9

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

stix2/datastore/relational_db/database_backends/database_backend_base.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,24 @@ def schema_for(stix_class):
4040
def schema_for_core():
4141
return ""
4242

43+
# you must implement the next 4 methods in the subclass
44+
4345
@staticmethod
4446
def determine_sql_type_for_property(): # noqa: F811
4547
pass
4648

49+
@staticmethod
50+
def determine_sql_type_for_binary_property(): # noqa: F811
51+
pass
52+
53+
@staticmethod
54+
def determine_sql_type_for_hex_property(): # noqa: F811
55+
pass
56+
57+
@staticmethod
58+
def determine_sql_type_for_timestamp_property(): # noqa: F811
59+
pass
60+
4761
@staticmethod
4862
def determine_sql_type_for_kill_chain_phase(): # noqa: F811
4963
return None

stix2/datastore/relational_db/table_creation.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# from collections import OrderedDict
22

33
from sqlalchemy import ( # create_engine,; insert,
4-
ARRAY, TIMESTAMP, Boolean, CheckConstraint, Column, Float, ForeignKey,
5-
Integer, LargeBinary, Table, Text, UniqueConstraint,
4+
ARRAY, TIMESTAMP, Boolean, CheckConstraint, Column, ForeignKey,
5+
Integer, Table, Text, UniqueConstraint
66
)
77

88
from stix2.datastore.relational_db.add_method import add_method
@@ -60,7 +60,7 @@ def create_array_child_table(metadata, db_backend, table_name, property_name, co
6060
nullable=False,
6161
),
6262
]
63-
return Table(canonicalize_table_name(table_name + "_" + "selector", schema_name), metadata, *columns)
63+
return Table(canonicalize_table_name(table_name + "_" + "selector"), metadata, *columns, schema=schema_name)
6464

6565

6666
def derive_column_name(prop):
@@ -171,11 +171,12 @@ def create_kill_chain_phases_table(name, metadata, db_backend, schema_name, tabl
171171

172172

173173
def create_granular_markings_table(metadata, db_backend, sco_or_sdo):
174+
schema_name = db_backend.schema_for_core()
174175
columns = [
175176
Column(
176177
"id",
177178
db_backend.determine_sql_type_for_key_as_id(),
178-
ForeignKey("common.core_" + sco_or_sdo + ".id", ondelete="CASCADE"),
179+
ForeignKey(canonicalize_table_name("core_" + sco_or_sdo, schema_name) + ".id", ondelete="CASCADE"),
179180
nullable=False,
180181
primary_key=True,
181182
),
@@ -197,14 +198,15 @@ def create_granular_markings_table(metadata, db_backend, sco_or_sdo):
197198

198199
tables = [
199200
Table(
200-
canonicalize_table_name("granular_marking_" + sco_or_sdo, db_backend.schema_for_core()),
201+
canonicalize_table_name("granular_marking_" + sco_or_sdo),
201202
metadata,
202203
*columns,
203204
CheckConstraint(
204205
"""(lang IS NULL AND marking_ref IS NOT NULL)
205206
OR
206207
(lang IS NOT NULL AND marking_ref IS NULL)""",
207208
),
209+
schema=schema_name
208210
),
209211
]
210212
if child_table:

0 commit comments

Comments
 (0)