If this is not available on your platform, you can publish the SQL script to create/update the desired data. In our case, we will build a simple RESTful API and use the Flask-SQLAlchemy extension to connect our API to a Postgres. Some technologies like Entity Framework allow you to transfer the data via something similar to seeding. Flask is a lightweight micro-framework that is used to build minimal web applications and through third-party libraries, we can tap into its flexibility to build robust and feature-rich web applications. Another option is to share a common database on the internet. Consider that they should run the upgrade prior that import the database. You can take database snapshot/backup/dump and send it to all the team. You have a straightforward solution here. Second, you want your data be shared among the people. In this part, your actual data is not shared or sent to your colleagues. Everybody, including yourself, just upgrade the database based on the available migration. Once you have changes on your entities (data classes) in your code base, you crate the related migration and commit/push the code. If so, the problem is divided into two parts:įirst, you need to keep data structure up-to-date among your team. However, I guess this is not far from Entity Framework migration feature. I am not very familiar with Flask and its migration mechanism. Not sure if this is the right place to ask but I hope I am able to find some answers here.Įdit: So far I am updating data in the database by dropping the database and recreating it with the dump, but I have yet to try it with any schema changes (project is progressing, a little slowly.)Įdit 2: Forgot to mention that all instances of PostgreSQL are hosted locally on our own machines I am not sure if simply importing the data dump with updated schema will mess up the migration commits in Flask Migrate. If I migrate and upgrade my flask database followed by exporting a dump with psql, when my peers receive the updates, do they simply import the data dump? Or do they have to perform a flask db upgrade as well? If so, which one comes first? flask db upgrade followed by dump import, or dump import followed by a flask db upgrade? All is well but if I have updated rows in the database, how do I propagate the schema changes and data changes to my peers? And others that receives my update executes a flask db upgrade to receive the schema changes. I am working in a small team and have some questions regarding database updates.Īs far as I am aware, If I update the database schema in Flask, I will have to perform a flask db migrate and flask db upgrade to apply the changes. Pg_dump -h localhost -U $DBUSER $DBNAME | gzip > $BACKUP_PATH/$DBNAME-$(date +%Y-%m-%d).db.gz 2> $BACKUP_PATH/error.I am currently setting up a project that is based on Python Flask (with Flask Migrate) with a PostgreSQL DB. You can do so using the following commands sudo su postgres or with this sudo su - postgres You can choose to change the password for the postgres user via this command if you want sudo passwd postgres Creating a New User Let us create a new user and assign it some new roles. pgpassįind $BACKUP_PATH -type f -name "*.db.gz" | xargs rm Load the data into database: psql -h localhost -U $DBUSER $DBNAME. You can prepare the sql statements according to the template: alter sequence seq_name restart with max_id+1. You will also need to set value for the sequences, or replace ids in sql file. Grep -vx -f schema.sql dump.sql > data.sqlĮdit dump file, and replace: PRAGMA foreign_keys=OFF > '' The :z suffix solves the permission denied problem for the volume.Ĭreate user and database: psql -h localhost -U postgrespsql -h localhost -U postgresĬhange the database URL in the Flask project to: sudo dnf install postgresql-devel For the puposes of this article that connection is local, and can be described as follows: engine://user:passwordhost/dbname engine -> postgresql user -> postgres (see owner field in previous screenshot) password -> password (my db password is the string, password) host -> localhost (because we are. $ sudo podman run -name postgres12 -pod $PODNAME -e POSTGRES_PASSWORD=$PASSWORD -d -v /home/$USER/podman/volumes/postgres12:/var/lib/postgresql/data:z postgres:12 The SQLALCHEMYDATABASEURI is a string describing our database connection. $ mkdir -p /home/$USER/podman/volumes/postgres12 # Create a Pod to link more containers in the future To make the container a system wide service run the commands as root ( sudo): $ dnf install podman If you use docker then just replace all the podman commands with docker. Install podman (Fedora's docker) and prepare the volume to store the database data persistently. Migrating form SqlLite to Postgresql in Flask Run PostgreSql in container
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |