graphql-engine/cli/commands
Aravind Shankar 980c65dbe2 cli(migrations): new folder structure and squash (#3072)
### Description
This PR introduces three new features:

- Support for a new migrations folder structure.
- Add `squash` command in preview.
- ~List of migrations on the Console and ability to squash them from console.~

#### New migrations folder structure

Starting with this commit, Hasura CLI supports a new directory structure for migrations folder and defaults to that for all new migrations created. 

Each migration will get a new directory with the name format `timestamp_name` and inside the directory, there will be four files:

```bash
└── migrations
    ├── 1572237730898_squashed
    │   ├── up.sql
    │   ├── up.yaml
    │   ├── down.yaml
    │   └── down.sql
```

Existing files old migration format `timestamp_name.up|down.yaml|sql` will continue to work alongside new migration files.

#### Squash command

Lots of users have expressed their interest in squashing migrations (see #2724 and #2254) and some even built [their own tools](https://github.com/domasx2/hasura-squasher) to do squash. In this PR, we take a systematic approach to squash migrations.

A new command called `migrate squash` is introduced. Note that this command is in **PREVIEW** and the correctness of squashed migration is not guaranteed (especially for down migrations). From our tests, **it works for most use cases**, but we have found some issues with squashing all the down migrations, partly because the console doesn't generate down migrations for all actions.

Hence, until we add an extensive test suite for squashing, we'll keep the command in preview. We recommend you to confirm the correctness yourself by diffing the SQL and Metadata before and after applying the squashed migrations (we're also thinking about embedding some checks into the command itself).

```bash
$ hasura migrate squash --help
(PREVIEW) Squash multiple migrations leading upto the latest one into a single migration file

Usage:
  hasura migrate squash [flags]

Examples:
  # NOTE: This command is in PREVIEW, correctness is not guaranteed and the usage may change.

  # squash all migrations from version 1572238297262 to the latest one:
  hasura migrate squash --from 1572238297262

Flags:
      --from uint             start squashing form this version
      --name string           name for the new squashed migration (default "squashed")
      --delete-source         delete the source files after squashing without any confirmation
```

### Affected components 
<!-- Remove non-affected components from the list -->

- CLI

### Related Issues
<!-- Please make sure you have an issue associated with this Pull Request -->
<!-- And then add `(close #<issue-no>)` to the pull request title -->
<!-- Add the issue number below (e.g. #234) -->
Close #2724, Close #2254, 

### Solution and Design
<!-- How is this issue solved/fixed? What is the design? -->
<!-- It's better if we elaborate -->

For the squash command, a state machine is implemented to track changes to Hasura metadata. After applying each action on the metadata state, a list of incremental changes is created.

### Steps to test and verify
1. Open console via cli and create some migrations.
2. Run `hasura migrate squash --from <version>`

### Limitations, known bugs & workarounds
<!-- Limitations of the PR, known bugs and suggested workarounds -->

<!-- Feel free to delete these comment lines -->
- The `squash` command is in preview
- Support for squashing from the console is WIP
- Support for squashing migrations that are not committed yet is planned.
- Un-tracking or dropping a table will cause inconsistent squashed down migration since console doesn't generate correct down migration.
- If cascade setting is set to `true` on any of the metadata action, generated migration may be wrong
2019-10-31 07:51:15 +05:30
..
completion.go fix bash completion example docs on cli (#1832) 2019-03-28 07:28:20 +05:30
console_test.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
console.go cli(migrations): new folder structure and squash (#3072) 2019-10-31 07:51:15 +05:30
docs.go cli: add new install script, commands (#1556) 2019-02-05 18:21:21 +05:30
help.go cli: add new install script, commands (#1556) 2019-02-05 18:21:21 +05:30
init_test.go rename access-key to admin-secret (close #1347) (#1540) 2019-02-14 15:07:47 +05:30
init.go rename access-key to admin-secret (close #1347) (#1540) 2019-02-14 15:07:47 +05:30
metadata_apply_test.go accept a json or yaml file for metadata apply command (close #1698) (#1746) 2019-03-20 10:40:06 +05:30
metadata_apply.go cli(metadata): add diff command and dry-run flag (#3157) 2019-10-30 19:24:22 +05:30
metadata_clear_test.go rename metadata reset to metadta clear (close #1851) (#1893) 2019-03-29 10:44:56 +05:30
metadata_clear.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
metadata_diff_test.go cli(metadata): add diff command and dry-run flag (#3157) 2019-10-30 19:24:22 +05:30
metadata_diff.go cli(metadata): add diff command and dry-run flag (#3157) 2019-10-30 19:24:22 +05:30
metadata_export_test.go accept a json or yaml file for metadata apply command (close #1698) (#1746) 2019-03-20 10:40:06 +05:30
metadata_export.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
metadata_reload_test.go accept a json or yaml file for metadata apply command (close #1698) (#1746) 2019-03-20 10:40:06 +05:30
metadata_reload.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
metadata.go cli(metadata): add diff command and dry-run flag (#3157) 2019-10-30 19:24:22 +05:30
migrate_apply_test.go added feedback on migration commands (close #1410) (#1931) 2019-04-03 16:59:58 +05:30
migrate_apply.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
migrate_create_test.go added feedback on migration commands (close #1410) (#1931) 2019-04-03 16:59:58 +05:30
migrate_create.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
migrate_squash.go cli(migrations): new folder structure and squash (#3072) 2019-10-31 07:51:15 +05:30
migrate_status_test.go rename access-key to admin-secret (close #1347) (#1540) 2019-02-14 15:07:47 +05:30
migrate_status.go optimise migrate api for console on cli (#2895) 2019-09-18 11:06:16 +05:30
migrate_test.go allow customising graphql schema for a table (close #981) (#2509) 2019-09-19 10:17:36 +05:30
migrate.go cli(migrations): new folder structure and squash (#3072) 2019-10-31 07:51:15 +05:30
root.go add flag to disable ci colors (close #2072) (#2634) 2019-07-30 06:55:38 +00:00
update-cli.go fix some typos in cli (#2708) 2019-08-10 17:13:45 +00:00
version.go rename access-key to admin-secret (close #1347) (#1540) 2019-02-14 15:07:47 +05:30