Initial commit
This commit is contained in:
110
migrate_sqlite_to_postgres.py
Normal file
110
migrate_sqlite_to_postgres.py
Normal file
@@ -0,0 +1,110 @@
|
||||
from peewee import *
|
||||
from models import SurveyType, Product, Feature, Platform, PlatformFeature
|
||||
import os
|
||||
|
||||
# Подключение к SQLite
|
||||
sqlite_db = SqliteDatabase('survey.db')
|
||||
|
||||
|
||||
class SQLiteBaseModel(Model):
|
||||
class Meta:
|
||||
database = sqlite_db
|
||||
|
||||
|
||||
class SQLiteSurveyType(SQLiteBaseModel):
|
||||
name = CharField(unique=True)
|
||||
|
||||
class Meta:
|
||||
table_name = 'surveytype' # Имя таблицы в SQLite
|
||||
|
||||
|
||||
class SQLiteProduct(SQLiteBaseModel):
|
||||
name = CharField()
|
||||
survey_type = ForeignKeyField(SQLiteSurveyType, backref='products')
|
||||
|
||||
class Meta:
|
||||
table_name = 'product'
|
||||
|
||||
|
||||
class SQLiteFeature(SQLiteBaseModel):
|
||||
name = CharField()
|
||||
question_text = TextField()
|
||||
product = ForeignKeyField(SQLiteProduct, backref='features')
|
||||
|
||||
class Meta:
|
||||
table_name = 'feature'
|
||||
|
||||
|
||||
class SQLitePlatform(SQLiteBaseModel):
|
||||
name = CharField(unique=True)
|
||||
survey_type = ForeignKeyField(SQLiteSurveyType, backref='platforms')
|
||||
|
||||
class Meta:
|
||||
table_name = 'platform'
|
||||
|
||||
|
||||
class SQLitePlatformFeature(SQLiteBaseModel):
|
||||
platform = ForeignKeyField(SQLitePlatform, backref='features')
|
||||
feature = ForeignKeyField(SQLiteFeature, backref='platforms')
|
||||
supported = BooleanField(default=True)
|
||||
|
||||
class Meta:
|
||||
table_name = 'platformfeature'
|
||||
|
||||
|
||||
def migrate():
|
||||
# Подключаемся к SQLite и создаём соединение
|
||||
sqlite_db.connect()
|
||||
|
||||
# Проверка наличия таблиц (для отладки можно вывести список таблиц)
|
||||
cur = sqlite_db.execute_sql("SELECT name FROM sqlite_master WHERE type='table';")
|
||||
tables = [row[0] for row in cur.fetchall()]
|
||||
print("SQLite tables:", tables)
|
||||
|
||||
# Миграция SurveyType
|
||||
survey_map = {}
|
||||
for s in SQLiteSurveyType.select():
|
||||
new_survey, _ = SurveyType.get_or_create(name=s.name)
|
||||
survey_map[s.id] = new_survey
|
||||
|
||||
# Миграция Product
|
||||
product_map = {}
|
||||
for p in SQLiteProduct.select():
|
||||
new_product, _ = Product.get_or_create(
|
||||
name=p.name,
|
||||
survey_type=survey_map[p.survey_type.id]
|
||||
)
|
||||
product_map[p.id] = new_product
|
||||
|
||||
# Миграция Feature
|
||||
feature_map = {}
|
||||
for f in SQLiteFeature.select():
|
||||
new_feature, _ = Feature.get_or_create(
|
||||
name=f.name,
|
||||
question_text=f.question_text,
|
||||
product=product_map[f.product.id]
|
||||
)
|
||||
feature_map[f.id] = new_feature
|
||||
|
||||
# Миграция Platform
|
||||
platform_map = {}
|
||||
for pl in SQLitePlatform.select():
|
||||
new_platform, _ = Platform.get_or_create(
|
||||
name=pl.name,
|
||||
survey_type=survey_map[pl.survey_type.id]
|
||||
)
|
||||
platform_map[pl.id] = new_platform
|
||||
|
||||
# Миграция PlatformFeature
|
||||
for pf in SQLitePlatformFeature.select():
|
||||
PlatformFeature.get_or_create(
|
||||
platform=platform_map[pf.platform.id],
|
||||
feature=feature_map[pf.feature.id],
|
||||
defaults={"supported": pf.supported}
|
||||
)
|
||||
|
||||
print("✅ Данные успешно мигрированы из SQLite в PostgreSQL.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
migrate()
|
||||
Reference in New Issue
Block a user