diff --git a/README.md b/README.md index 69114ae..07deaa8 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # TextSouls + ### Бэкенд Все действия производить из папки backend! @@ -12,11 +13,18 @@ flask2postman textsouls.app --name "TextSouls" --folders > text_souls_postman.js ``` scripts/updatedb.sh ``` + +###### Вставка начальных данных +``` +python ./scripts/seed_inserter.py +``` + ###### Запуск ``` scripts/runserver.sh ``` + ### Запуск Telegram Из папки Telegram: ``` diff --git a/backend/scripts/init_data.json b/backend/scripts/init_data.json new file mode 100644 index 0000000..953abdd --- /dev/null +++ b/backend/scripts/init_data.json @@ -0,0 +1,15 @@ +[ + { + "table_name": "character_classes", + "records": [ + { + "id": 1, + "name": "Тест1" + }, + { + "id": 2, + "name": "Тест2" + } + ] + } +] \ No newline at end of file diff --git a/backend/scripts/seed_inserter.py b/backend/scripts/seed_inserter.py new file mode 100644 index 0000000..6a56789 --- /dev/null +++ b/backend/scripts/seed_inserter.py @@ -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()