mirror of
https://github.com/Llloooggg/TextSouls.git
synced 2026-03-06 04:26:23 +03:00
backend: добавлен скрипт для вставки начальных данных
This commit is contained in:
25
backend/scripts/seed_inserter.py
Normal file
25
backend/scripts/seed_inserter.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import json
|
||||
|
||||
import sqlalchemy
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
with open("textsouls/config.json") as config_file:
|
||||
config_data = json.load(config_file)
|
||||
|
||||
engine = sqlalchemy.create_engine(
|
||||
config_data["DB_SETTINGS"]["SQLALCHEMY_DATABASE_URI"]
|
||||
)
|
||||
metadata = sqlalchemy.MetaData(bind=engine)
|
||||
|
||||
with open("scripts/init_data.json") as seed_file:
|
||||
seed_data = json.load(seed_file)
|
||||
|
||||
for table_data in seed_data:
|
||||
table = sqlalchemy.Table(table_data["table_name"], metadata, autoload=True)
|
||||
|
||||
query = table.insert().values(table_data["records"])
|
||||
|
||||
session = Session(engine)
|
||||
session.execute(query)
|
||||
session.commit()
|
||||
session.close()
|
||||
Reference in New Issue
Block a user