Migrating from SQLite to MySql or Postgres using peewee

I've recently wrote a Medium article about migrating the default Sqlite schema to a Mysql or Postgres sever database and I thought I share a code snippet here for others to check!

from peewee import MySQLDatabase, SqliteDatabase
from playhouse.reflection import Introspector

sqlite_db = SqliteDatabase("prodigy.db")
introspector = Introspector.from_database(sqlite_db)
models = introspector.generate_models()

mysql_db = MySQLDatabase(
    user="user name",
    password="user password",
    host="host name",
    port=3306,
    database="database name",
)

mysql_db.create_tables(list(models.values()))
mysql_db.close()

Happy prodigy coding!

3 Likes

Wow, thanks so much for sharing your code and the write-up :pray: :sparkles: (Also glad to hear that the solution was pretty straightforward – especially on the scale of "database migrations" :sweat_smile:)