Removed duplicate list of database models
This commit is contained in:
parent
a2b73a9286
commit
d28bf18ce8
1 changed files with 26 additions and 42 deletions
|
@ -20,25 +20,26 @@ from app.models.alarms import Alarm, AlarmState
|
||||||
from app.models.onions import Onion, Eotk
|
from app.models.onions import Onion, Eotk
|
||||||
from app.models.tfstate import TerraformState
|
from app.models.tfstate import TerraformState
|
||||||
|
|
||||||
models = {
|
# order matters due to foreign key constraints
|
||||||
"activity": Activity,
|
models = [
|
||||||
"alarm": Alarm,
|
Group,
|
||||||
"automation": Automation,
|
Activity,
|
||||||
"automation_logs": AutomationLogs,
|
Pool,
|
||||||
"bridge": Bridge,
|
PoolGroup,
|
||||||
"bridgeconf": BridgeConf,
|
SmartProxy,
|
||||||
"eotk": Eotk,
|
Origin,
|
||||||
"group": Group,
|
Proxy,
|
||||||
"list": MirrorList,
|
Onion,
|
||||||
"onion": Onion,
|
Alarm,
|
||||||
"origin": Origin,
|
Automation,
|
||||||
"pool": Pool,
|
AutomationLogs,
|
||||||
"pool_group": PoolGroup,
|
BridgeConf,
|
||||||
"proxy": Proxy,
|
Bridge,
|
||||||
"smart_proxy": SmartProxy,
|
Eotk,
|
||||||
"terraform_state": TerraformState,
|
MirrorList,
|
||||||
"webhook": Webhook
|
TerraformState,
|
||||||
}
|
Webhook
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
class ExportEncoder(json.JSONEncoder):
|
class ExportEncoder(json.JSONEncoder):
|
||||||
|
@ -70,8 +71,8 @@ def db_export() -> None:
|
||||||
encoder = ExportEncoder()
|
encoder = ExportEncoder()
|
||||||
output = defaultdict(list)
|
output = defaultdict(list)
|
||||||
for model in models:
|
for model in models:
|
||||||
for row in models[model].query.all(): # type: ignore[attr-defined]
|
for row in model.query.all(): # type: ignore[attr-defined]
|
||||||
output[model].append(model_to_dict(row))
|
output[model.__name__].append(model_to_dict(row))
|
||||||
print(encoder.encode(output))
|
print(encoder.encode(output))
|
||||||
|
|
||||||
|
|
||||||
|
@ -86,9 +87,9 @@ decoder = {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def db_import_model(model: str, data: List[Dict[str, Any]]) -> None:
|
def db_import_model(model: db.Model, data: List[Dict[str, Any]]) -> None: # type: ignore[name-defined]
|
||||||
for row in data:
|
for row in data:
|
||||||
new = models[model]()
|
new = model()
|
||||||
for col in row:
|
for col in row:
|
||||||
type_name, col_name = col.split("_", 1)
|
type_name, col_name = col.split("_", 1)
|
||||||
new.__setattr__(col_name, decoder.get(type_name, lambda x: x)(row[col])) # type: ignore[no-untyped-call]
|
new.__setattr__(col_name, decoder.get(type_name, lambda x: x)(row[col])) # type: ignore[no-untyped-call]
|
||||||
|
@ -98,25 +99,8 @@ def db_import_model(model: str, data: List[Dict[str, Any]]) -> None:
|
||||||
def db_import() -> None:
|
def db_import() -> None:
|
||||||
data = json.load(sys.stdin)
|
data = json.load(sys.stdin)
|
||||||
# import order matters due to foreign key constraints
|
# import order matters due to foreign key constraints
|
||||||
for model in [
|
for model in models:
|
||||||
"group",
|
db_import_model(model, data[model.__name__])
|
||||||
"pool",
|
|
||||||
"pool_group",
|
|
||||||
"smart_proxy",
|
|
||||||
"origin",
|
|
||||||
"proxy",
|
|
||||||
"onion",
|
|
||||||
"alarm",
|
|
||||||
"automation",
|
|
||||||
"automation_logs",
|
|
||||||
"bridgeconf",
|
|
||||||
"bridge",
|
|
||||||
"eotk",
|
|
||||||
"list",
|
|
||||||
"terraform_state",
|
|
||||||
"webhook"
|
|
||||||
]:
|
|
||||||
db_import_model(model, data[model])
|
|
||||||
|
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue