Deploying model and database migrations

Hi Strapi friends,

When I make changes to the models do I then I have to manually handle database migrations or is that handled automatically when pushing the model code to production environment?

Is it the same for postgress and mongodb?

I have local dev enviroment with a postgresql db and plan to have a prod environment with matching same db as per the requirements in docs.

First post here in the new forums. Wish me luck :slight_smile:

Jesper

1 Like

Hi @jesperordrup,

Yes currently that is the case, it’s something we hope to tackle here soon. We have a tracking issue on GitHub about this here: Deleting/Updating content-type doesn't drop/migrate the database · Issue #1114 · strapi/strapi · GitHub if you want to read more about the discussion.

I believe it’s something we may look into for the database refresh we are going to be doing in Q4 2020.

Also, welcome to the new forums!

1 Like

Just to be clear yes means manually :slight_smile:

So it seems that new collections / tabels are created automatically right?

How about new fields, changing fieldtypes - does it work?

If I drop all tables then strapi will create all correct I guess?

Sorry bout all the qs. :smile:

No problem, that’s what the forums are for :wink: New tables and columns will be created automatically and certain changes will also automatically be updated (adding constraints, changing the field type)

Some cases that require manual migration is renaming of a content-type doesn’t automatically update the table, and the same applies to field renaming.

In the case of field renames, the old field is left in the database with it’s content, the new field is created (column) and data migration doesn’t happen automatically. When you rename a field in the AdminUI it’s not actually changed logically, it’s done as a visual change, this is temporary and was added until we can properly address the database issue I linked above.

If you drop the tables and restart, yes Strapi will completely rebuild the database structure on it’s own, including default roles, permissions, ect.

1 Like

Thanks you @DMehaffy! Good to know. Maybe this should be added to docs.

Just thinking, about the database representation of the model? Instead of a collection = tableName how about:

  • a table for collection definitions
  • a table for fields which defined the fields related to the collection
  • a table for data containing each column of field data.

Its seen in Umbraco CMS database structure. It sure has some drawbacks (huge queries)

We have avoided putting the information into the documentation* because it is certainly something we will be changing, proper migrations is very important to us (as well as our users). But I’ll leave the design of the migrations and table structure to our backend engineers.

Once we have more information to share about our plans I will be sure to share it. :wink:

2 Likes

@jacksbox This is an open source project, personally if I was not contributing pull requests I would either politely ask or avoid bringing down the morale of people who are actually doing some work, just a thought :thinking:

2 Likes

@bernardo

Your totally right. I’m apologising for this comment - guess I was a bit stressed at the time of writing (but this does not apologise my tone).
I’ll see how I can contribute the project and improve the things I’m missing.

2 Likes

@jacksbox we will absolutely be working on this in the future, we did have to push it to Q3 but we understand the importance of the topic. I believe it took a little time for everyone to actually see the importance.

Priorities are always a problem in working on a project such as this, I can’t say we were right or wrong in our choice of priorities but rest assured there are many of us (myself included) that see this as priority #1. <3

1 Like

@DMehaffy sorry. May I just know the follownig.

When I redeploy Strapi App, I do not get any data from Postgres DB that I had in previous deploy.Why that happens?

Thank you

Hi @dimatic, maybe there’s something amiss with your DB config? If not that, what does your deployment environment look like?

Thank you @Richard_Nsama

I use Dokku on Hetzner Servers.
I deployed Strapi with Postgres DB. Here is the database config file:

const parse = require("pg-connection-string").parse;

const config = parse(
  "postgres://postgres:f24956a99e6194160a623f1b5fd29751@dokku-postgres-db:5432/db"
);

module.exports = ({ env }) => {

    return {
      defaultConnection: "default",
  connections: {
    default: {
      connector: "bookshelf",
      settings: {
        client: "postgres",
        host: config.host,
        port: config.port,
        database: config.database,
        username: config.user,
        password: config.password,
      },
      options: {
        ssl: false,
      },
      },
    }

    };

};

After each redeploy, I miss database data.

Here is also simple guide, that I use to deploy Strapi App on Hetzner Cloud Server (costs 2.99 Eur).

As a Result, I get this:

I create Hetzner server with firewall rules (allow some basic ports like 80, 443, 5000, 1337). Than I connect to the server via SSH.

The I use the commands:
apt install
apt upgrade

wget https://raw.githubusercontent.com/dokku/dokku/v0.24.7/bootstrap.sh;
sudo DOKKU_TAG=v0.24.7 bash bootstrap.sh

Then open either IP of server or domain and apply it.

After that:

dokku apps:create strapi
dokku postgres:create db
dokku postgres:link db strapi

(this is optional)
dokku proxy:set http:80:1337
dokku proxy:set https:443:1337
dokku proxy:enable strapi

After that on the client, where Strapi is located:

git init
git add .
git commit -m "comment"
git push dokku master

App has been deployed.

I’m not entirely familiar with dokku but after looking it up, did you make sure to store the data created by Postgres to a persistent volume in your config? It might be that you’re storing state (database data) in a stateless environment (PaaS which is what Dokku says it is from the website)

1 Like