2018-06-24 16:40:48 +03:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
2020-04-28 14:59:57 +03:00
|
|
|
"crypto/tls"
|
2018-06-24 16:40:48 +03:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
nurl "net/url"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
ErrLocked = fmt.Errorf("can't acquire lock")
|
|
|
|
)
|
|
|
|
|
|
|
|
const NilVersion int64 = -1
|
|
|
|
|
|
|
|
var driversMu sync.RWMutex
|
|
|
|
var drivers = make(map[string]Driver)
|
|
|
|
|
|
|
|
// Driver is the interface every database driver must implement.
|
|
|
|
//
|
|
|
|
// How to implement a database driver?
|
|
|
|
// 1. Implement this interface.
|
|
|
|
// 2. Optionally, add a function named `WithInstance`.
|
|
|
|
// This function should accept an existing DB instance and a Config{} struct
|
|
|
|
// and return a driver instance.
|
|
|
|
// 3. Add a test that calls database/testing.go:Test()
|
|
|
|
// 4. Add own tests for Open(), WithInstance() (when provided) and Close().
|
|
|
|
// All other functions are tested by tests in database/testing.
|
|
|
|
// Saves you some time and makes sure all database drivers behave the same way.
|
|
|
|
// 5. Call Register in init().
|
|
|
|
// 6. Create a migrate/cli/build_<driver-name>.go file
|
|
|
|
// 7. Add driver name in 'DATABASE' variable in Makefile
|
|
|
|
//
|
|
|
|
// Guidelines:
|
|
|
|
// * Don't try to correct user input. Don't assume things.
|
|
|
|
// When in doubt, return an error and explain the situation to the user.
|
|
|
|
// * All configuration input must come from the URL string in func Open()
|
|
|
|
// or the Config{} struct in WithInstance. Don't os.Getenv().
|
|
|
|
type Driver interface {
|
|
|
|
// Open returns a new driver instance configured with parameters
|
|
|
|
// coming from the URL string. Migrate will call this function
|
|
|
|
// only once per instance.
|
2020-04-28 14:59:57 +03:00
|
|
|
Open(url string, isCMD bool, tlsConfig *tls.Config, logger *log.Logger) (Driver, error)
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
// Close closes the underlying database instance managed by the driver.
|
|
|
|
// Migrate will call this function only once per instance.
|
|
|
|
Close() error
|
|
|
|
|
2019-09-18 08:36:16 +03:00
|
|
|
Scan() error
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
// Lock should acquire a database lock so that only one migration process
|
|
|
|
// can run at a time. Migrate will call this function before Run is called.
|
|
|
|
// If the implementation can't provide this functionality, return nil.
|
|
|
|
// Return database.ErrLocked if database is already locked.
|
|
|
|
Lock() error
|
|
|
|
|
|
|
|
// Unlock should release the lock. Migrate will call this function after
|
|
|
|
// all migrations have been run.
|
|
|
|
UnLock() error
|
|
|
|
|
|
|
|
// Run applies a migration to the database. migration is garantueed to be not nil.
|
2018-12-03 14:20:53 +03:00
|
|
|
Run(migration io.Reader, fileType, fileName string) error
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
// Reset Migration Query Args
|
|
|
|
ResetQuery()
|
|
|
|
|
2018-06-28 11:36:57 +03:00
|
|
|
// InsertVersion saves version
|
2018-06-24 16:40:48 +03:00
|
|
|
// Migrate will call this function before and after each call to Run.
|
|
|
|
// version must be >= -1. -1 means NilVersion.
|
2018-06-28 11:36:57 +03:00
|
|
|
InsertVersion(version int64) error
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
// SetVersion saves version and dirty state.
|
|
|
|
// Migrate will call this function before and after each call to Run.
|
|
|
|
// version must be >= -1. -1 means NilVersion.
|
2018-06-28 11:36:57 +03:00
|
|
|
RemoveVersion(version int64) error
|
2018-06-24 16:40:48 +03:00
|
|
|
|
|
|
|
// Version returns the currently active version and if the database is dirty.
|
|
|
|
// When no migration has been applied, it must return version -1.
|
|
|
|
// Dirty means, a previous migration failed and user interaction is required.
|
|
|
|
Version() (version int64, dirty bool, err error)
|
|
|
|
|
|
|
|
// First returns the very first migration version available to the driver.
|
|
|
|
// Migrate will call this function multiple times
|
|
|
|
First() (version uint64, ok bool)
|
|
|
|
|
|
|
|
// Last returns the latest version available in database
|
|
|
|
Last() (version uint64, ok bool)
|
|
|
|
|
|
|
|
// Prev returns the previous version for a given version available to the driver.
|
|
|
|
// Migrate will call this function multiple times.
|
|
|
|
// If there is no previous version available, it must return os.ErrNotExist.
|
|
|
|
Prev(version uint64) (prevVersion uint64, ok bool)
|
|
|
|
|
|
|
|
// Next returns the next version for a given version available to the driver.
|
|
|
|
// Migrate will call this function multiple times.
|
|
|
|
// If there is no next version available, it must return os.ErrNotExist.
|
|
|
|
Next(version uint64) (nextVersion uint64, ok bool)
|
|
|
|
|
|
|
|
Read(version uint64) (ok bool)
|
|
|
|
|
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 05:21:15 +03:00
|
|
|
PushToList(migration io.Reader, fileType string, list *CustomList) error
|
|
|
|
|
|
|
|
Squash(list *CustomList, ret chan<- interface{})
|
|
|
|
|
2018-06-24 16:40:48 +03:00
|
|
|
SettingsDriver
|
|
|
|
|
|
|
|
MetadataDriver
|
2019-04-30 11:34:08 +03:00
|
|
|
|
2020-02-24 19:14:46 +03:00
|
|
|
GraphQLDriver
|
|
|
|
|
2019-04-30 11:34:08 +03:00
|
|
|
SchemaDriver
|
2020-06-16 15:15:04 +03:00
|
|
|
|
|
|
|
SeedDriver
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open returns a new driver instance.
|
2020-04-28 14:59:57 +03:00
|
|
|
func Open(url string, isCMD bool, tlsConfig *tls.Config, logger *log.Logger) (Driver, error) {
|
2018-06-24 16:40:48 +03:00
|
|
|
u, err := nurl.Parse(url)
|
|
|
|
if err != nil {
|
|
|
|
log.Debug(err)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
driversMu.RLock()
|
|
|
|
if u.Scheme == "" {
|
|
|
|
return nil, fmt.Errorf("database driver: invalid URL scheme")
|
|
|
|
}
|
|
|
|
driversMu.RUnlock()
|
|
|
|
|
|
|
|
d, ok := drivers[u.Scheme]
|
|
|
|
if !ok {
|
2020-02-24 19:14:46 +03:00
|
|
|
return nil, fmt.Errorf("database driver: unknown driver %v", u.Scheme)
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
2018-07-09 16:47:38 +03:00
|
|
|
if logger == nil {
|
|
|
|
logger = log.New()
|
|
|
|
}
|
|
|
|
|
2020-04-28 14:59:57 +03:00
|
|
|
return d.Open(url, isCMD, tlsConfig, logger)
|
2018-06-24 16:40:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func Register(name string, driver Driver) {
|
2020-02-24 19:14:46 +03:00
|
|
|
driversMu.Lock()
|
|
|
|
defer driversMu.Unlock()
|
|
|
|
if driver == nil {
|
|
|
|
panic("Register driver is nil")
|
|
|
|
}
|
|
|
|
if _, dup := drivers[name]; dup {
|
|
|
|
panic("Register called twice for driver " + name)
|
|
|
|
}
|
2018-06-24 16:40:48 +03:00
|
|
|
drivers[name] = driver
|
|
|
|
}
|