Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENT] Versioned Migration via Atlas #1887

Merged
merged 3 commits into from
May 6, 2024
Merged

Conversation

pxp928
Copy link
Collaborator

@pxp928 pxp928 commented May 3, 2024

Description of the PR

This adds the ability to run versioned migration for ENT whenever the schema changes.

This follows the recommendation provided in: https://entgo.io/docs/versioned-migrations/#quick-guide

Checks have been added to the CI to check that the migration diff has been generated and a linter has been run via atlas.

Note: For production use cases auto migration should be disabled and atlas should be used to apply the initial migration!!

closes #1513

Example usage:

  1. make changes to the schema (for example add a new optional field in artifact)
  2. run make generate which will also run atlas (needs to have docker running). This will generate the migration plan in pkg/assembler/backends/ent/migrate/migrations
  3. Run
atlas migrate apply \
  --dir "file://pkg/assembler/backends/ent/migrate/migrations" \
  --url "postgres://guac:guac@0.0.0.0:5432/guac?search_path=public&sslmode=disable" \

--url must be set to the running production database
--baseline specified the baseline that is currently applied to the running database (if started without atlas)

Note: A new baseline can be created from the production server by running:

atlas migrate diff my_baseline \
  --dir "file://pkg/assembler/backends/ent/migrate/migrations" \
  --dev-url "docker://postgres/15/test?search_path=public" \
  --to "postgres://guac:guac@0.0.0.0:5432/guac?search_path=public&sslmode=disable"

After successful execution, you will see the following message:

  Migrating to version 20240503144258 from 20240503123155 (1 migrations in total):

    -- migrating version 20240503144258
      -> ALTER TABLE "artifacts" ADD COLUMN "test" character varying NULL;
    -- ok (4.024333ms)

    -------------------------
    -- 12.905875ms
    -- 1 migration
    -- 1 sql statement

PR Checklist

  • All commits have a Developer Certificate of Origin (DCO) -- they are generated using -s flag to git commit.
  • All new changes are covered by tests
  • If GraphQL schema is changed, make generate has been run
  • If OpenAPI spec is changed, make generate has been run
  • If collectsub protobuf has been changed, make proto has been run
  • All CI checks are passing (tests and formatting)
  • All dependent PRs have already been merged

pxp928 added 3 commits May 2, 2024 17:31
Signed-off-by: pxp928 <parth.psu@gmail.com>
Signed-off-by: pxp928 <parth.psu@gmail.com>
Signed-off-by: pxp928 <parth.psu@gmail.com>
@pxp928 pxp928 requested a review from mihaimaruseac as a code owner May 3, 2024 14:57
@pxp928 pxp928 changed the title [ENT] Versioned Migration via ENT [ENT] Versioned Migration via Atlas May 3, 2024
Copy link
Collaborator

@mihaimaruseac mihaimaruseac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Copy link
Collaborator

@mrizzi mrizzi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pxp928 Thanks 👍

@kodiakhq kodiakhq bot merged commit 51d3218 into guacsec:main May 6, 2024
8 checks passed
arorasoham9 pushed a commit to arorasoham9/guac that referenced this pull request May 17, 2024
* add new versioned migrations for ent

Signed-off-by: pxp928 <parth.psu@gmail.com>

* add atlas diff and atlas lint to make file

Signed-off-by: pxp928 <parth.psu@gmail.com>

* add atlas lint and check for atlas diff via ci

Signed-off-by: pxp928 <parth.psu@gmail.com>

---------

Signed-off-by: pxp928 <parth.psu@gmail.com>
Signed-off-by: Soham Arora <arorasoham9@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[feature] Allow running DB migration scripts and exit
3 participants