Deployments: Generate Schema File and Content Migrations #3891
Replies: 18 comments 45 replies
-
@1R053 Absolutly agree. Directus app's UI is very impressive, but for a developer it's a nightmare to setup the schema with 'clicks' |
Beta Was this translation helpful? Give feedback.
-
The main issue that I still haven't run into (yet) is manipulating already existing data. Let's take an "events" system, made up of three collections:
My development workflow for now is relatively simple:
For small changes to the Admin App (ie: adding another option to a dropdown) you can probably safely dump the Admin App fields and reupload them again. But for meatier changes we don't have the tools to safely migrate the data. Let's say I want to move the event price from the event_item to the event_date, so my client has more flexibility. I would write another migration with the directus js-sdk that would look something like this (pseudo-code warning):
which is not the end of the world, but it might get muuuch trickier and verbose for more complex stuff like relationships. It would be an absolute lifesaver to have a way to safely write migrations, either using the js-sdk or a custom tool. |
Beta Was this translation helpful? Give feedback.
-
Duplicate Ticket From: @AlexisWalravensI asked yesterday on the discord how would be the best way to make a CI/CD to deploy Directus. I was said that I had to write the migration myself. It would be a lot easier to make changer on a local env, push to some git and deploy and execute the migrations on production server. Response from @rijkvanzanten Yup! 😄 That's the check-everything-into-git approach, but also limits the dynamic nature of setting it up through the GUI. #2927 is more closely aligned with what you're asking for 👍🏻 |
Beta Was this translation helpful? Give feedback.
-
"Thoughts on (system) database migrations" @WoLfulusTo make sure we have a known state in the database and avoid failing migrations between releases, I'd suggest we remove the "seeds" from the current migration process, since that makes the "fresh installation" process somewhat different than the migration process. System (Directus) MigrationsBase migration process
If running
|
Beta Was this translation helpful? Give feedback.
-
"Use a yaml file to define fields and relations" @mattiaz9Now fields and relations are stored in the database and we must define them via the app visually. This is great for simple projects and for non technical users, but it can be tedious for large projects that uses multiple environments, because you have to manually updated each fields in production as you have in development. I was thinking that, in conjunction with the database, we could have a yaml file in the project folder to define manually all the fields and relations. Benefits:
|
Beta Was this translation helpful? Give feedback.
-
"Data model as code under version control" @carlobeltrameHi, first of all I'd like to say thank you for this fantastic project! From trying it out it looks really solid, I love to see your dedication and I'd like to build some solutions with it. I found one big open question though which keeps us from using Directus comfortably. I saw the entry in the FAQ talking about this, but I really cannot believe this is the whole story. When you develop a Directus site for a client at Ranger Studio, do you not give them any testing or integration environment that is set up identically to the production? Do your clients really test and modify their sites on production? What I am looking for is something like the model settings in Strapi. I understand that in Directus, you use your system tables instead of JSON files, which is fine, except for the lack of version control for the model settings. One "solution" that I found so far is to have a DB dump under version control which includes the database structure and the contents of the Directus system tables (but empty content tables). This could be done with an external migration tool, which would however potentially conflict with the Directus-internal migrations. Another approach would be to have an official way to write extensions / plugins that can set up the system table entries. As far as I can tell, none of the documented extension types can do this easily. Granted, I could write an independent piece of code that reads my desired data model config from a file and then executes appropriate API requests to set this up in Directus. This seems like something that should really be built in to the framework though. I'd be happy to help work on something like this, but I think the initiative and architectural decisions would have to come from the Directus team. Maybe there are also improvements in this direction planned in api-next based on Laravel? Maybe I could just define my models as Laravel models then? Should we start building our sites on the api-next alpha? I'd love to hear your thoughts on this subject. CC @web7evgheni |
Beta Was this translation helpful? Give feedback.
-
I see a few questions that need to be answered to arrive at a high level tech design, and I'll give my first guess suggestion as start for a discussion:
In case of applying a system to system migration (e.g. dev -> test -> prod) you can simply leave out the YAML/JSON conversion. The core design work probably needs to go into
|
Beta Was this translation helpful? Give feedback.
-
If I would simply describe my use case it would be:
Not sure if there's any concrete implementation or design already on the way? |
Beta Was this translation helpful? Give feedback.
-
Hi there, I am basically facing the same challenges as you, having multiple environments (development, staging, production), need to do some fields update from time to time in dev and propagate safely those changes. So instead of doing it manually through the database, I was thinking of a config file storing the model where you could keep it in Git, and deploy it through your CI/CD pipeline. As an example, this is the scenario I use in a WordPress project where I use the ACF Pro plugin, this plugin allows you to create advanced custom fields (like Directus supports natively). It stores the fields information in the database (MySQL), but it also allow you to store a synced JSON file in your repo, so at each field update, you have a new timestamp on this file and a new diff, meaning you can deploy the changed to your other environments and it will run a database migration in the background to update the fields. I think that's the only missing feature that make it a bit hard to use in a company level right now, otherwise it's really good, thanks for the involvement. |
Beta Was this translation helpful? Give feedback.
-
A bit late to the party and really new to Directus, still instead of struggling to implement a feature that goes against the current workflow, someone can use a 3rd party script to manage migrations. Github did this way: https://github.blog/2020-02-14-automating-mysql-schema-migrations-with-github-actions-and-more/ |
Beta Was this translation helpful? Give feedback.
-
Can we have a button to dump data of directus_collections, directus_field & directus_relations table into a file from one environment. |
Beta Was this translation helpful? Give feedback.
-
Incase someone is using postgres DB, you can use this script to migrate between servers. I wrote this for my convenience, seems working so far. Suggestions for improvement welcomed. |
Beta Was this translation helpful? Give feedback.
-
Just going to add an idea here based on what we'd use it for: Currently we have 2 workflows:
Now I'm not even sure i'd want a completely automated migration because there might be simultaneous changes on Prod and dev (or another dev's dev) - and the result could be quite unpredictable. These changes could be applied (in order, with a warning when skipping changes, and preventing applying the same change twice), but manually. This way we can only copy the "good" changes and still not waste time redoing them manually (and less risk of mistakes in setting up interfaces/displays). I think this approach would fit well with Directus' 80/20 principle (i'd argue that admins that make changes in SQL directly would probably want their own migration system anyways - and even then it should still work). |
Beta Was this translation helpful? Give feedback.
-
I am currently writing custom migrations for all of my collections. It works, but it is tedious. Today I had the following idea. It is a different approach, and I'd like to explore it a bit further with you. We know what each click does in the directus UI, and it should be pretty possible to "record" those clicks into migration files that could then be used to recreate the current state of the application. Any thoughts on this? / edit: |
Beta Was this translation helpful? Give feedback.
-
Just wanted to through in here, that Prisma.
The CMS KeystoneJS just recently migrated to it with great success benefitting from auto schema migration etc. :) |
Beta Was this translation helpful? Give feedback.
-
I wanted to share my opinion on this topic. First of all, I think that the Directus UI is amazing and we should definitely be able to leverage it. What we are doing now are totally manual migrations. While this is fine for creating custom collections, managing the directus system table is rather painful. I see it as a generally wrong pattern to perform modification on a system that you do not own. What's happening is that we can forget to add/remove a certain value from the system tables, which might later on cause some troubles. Another problem of that approach is that you need to manually sync the changes from the UI to migrations, which is challenging and often error prone. I can think of 2 approaches. One is achievable in the short term and one would require more work and possibly re-thinking of the architecture of the product. Short Term - Migrations APIThe idea here is simple. It's similar to the JS API, but done on the database level. I've tried to do something similar in our codebase, but I strongly believe it should be part of the core app. I imagine it having the same structure as the API - we invoke Benefits: this would streamline the developer experience and reduce the mistakes, as well as incompatibilities when Directus makes changes to the system tables Long Term - Collections SchemaThis suggestion is influences by Strapi. What I like there is that you do changes in the UI and Strapi describes your data using JSON files. Then it would automatically migrate your database based on those JSON files. I haven't checked into details how they are doing it to be honest, but the overall experience is great - you can write JSON or you can use the UI, both produce the same result. Another option would be to use the event system, where every system change would be added to a sequence of events and then a migration can be generated based on those events. Perhaps this approach is closer to what Directus has now. The way I see it:
|
Beta Was this translation helpful? Give feedback.
-
In case that could gave some inputs. I am used to use the Strapi config sync extension. This one is more about "configuration synchronization" between envs. Anything that is configuration stored in database. It is really usefull to configure stuff like permissions on each collection on a development environnment and then to replicate it on another evnironnement. Basically, from the source env, it detects changes with the DB and allow you to export a file. From the target env, it detect changes between DB and file and you can apply changes from the file to the db. |
Beta Was this translation helpful? Give feedback.
-
Happy to report that this was implemented in #7939 |
Beta Was this translation helpful? Give feedback.
-
I think it would be great, if collections, fields, auth and probably other settings could be defined descriptively in code. This would make staging and testing apps much safer and efficient. At least for me it would allow directus to be used in much more complex use cases.
The parse platform implements this in an interesting way: http://docs.parseplatform.org/js/guide/#schema
I think, a similar definition (with probably some additions that parse does not support) should be possible for directus. And with some diffing of the schema it should then be possible, to automatically generate any necessary migrations or settings changes.
Such a solution would also solve issues like #3608
Beta Was this translation helpful? Give feedback.
All reactions