2022-12-21 13:28:58 +00:00
|
|
|
# Myt - MySQL version control
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
Utilities to ease the maintenance of MySQL or MariaDB database versioning using
|
|
|
|
a Git repository.
|
|
|
|
|
|
|
|
This project is just to bring an idea to life and is still in an early stage of
|
2021-10-23 17:14:09 +00:00
|
|
|
development, so any help is welcomed! Feel free to contribute.
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-12-02 07:35:26 +00:00
|
|
|
## Requirements
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
* Git
|
2022-05-01 08:26:25 +00:00
|
|
|
* Docker (Optional, used to manage local server)
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-11-14 15:08:27 +00:00
|
|
|
## Installation
|
|
|
|
|
|
|
|
It's recommended to install the package globally.
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
```text
|
2022-12-21 13:41:45 +00:00
|
|
|
# npm install -g @verdnatura/myt
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt <command>
|
2020-11-14 15:08:27 +00:00
|
|
|
```
|
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
You can also install locally and use the *npx* command to execute it.
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
```text
|
2022-12-21 13:41:45 +00:00
|
|
|
$ npm install @verdnatura/myt
|
2022-12-21 13:17:50 +00:00
|
|
|
$ npx myt <command>
|
2020-11-14 01:38:56 +00:00
|
|
|
```
|
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
## How to use
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
Execute *myt* with the desired command.
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ [npx] myt [-w|--workspace <string>] [-r|--remote <string>] [-d|--debug]
|
2022-05-01 08:26:25 +00:00
|
|
|
[-h|--help] <command> [<args>]
|
2020-11-14 01:38:56 +00:00
|
|
|
```
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-17 13:11:15 +00:00
|
|
|
The default workspace directory is the current working directory and unless
|
2020-12-04 16:30:26 +00:00
|
|
|
otherwise indicated, the default remote is *local*.
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
Database versioning commands:
|
2020-11-17 10:53:31 +00:00
|
|
|
|
2020-11-19 12:51:27 +00:00
|
|
|
* **init**: Initialize an empty workspace.
|
2021-10-23 17:14:09 +00:00
|
|
|
* **pull**: Incorporate database routine changes into workspace.
|
2020-12-02 07:35:26 +00:00
|
|
|
* **push**: Apply changes into database.
|
2021-10-23 13:20:35 +00:00
|
|
|
* **version**: Creates a new version.
|
2022-12-29 13:27:16 +00:00
|
|
|
* **create**: Creates a new routine file.
|
|
|
|
* **clean**: Cleans old versions.
|
2020-11-17 12:04:57 +00:00
|
|
|
|
2020-12-04 16:30:26 +00:00
|
|
|
Local server management commands:
|
2020-11-17 12:04:57 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
* **dump**: Export database structure and fixtures.
|
2022-06-09 18:41:18 +00:00
|
|
|
* **fixtures**: Export local database fixtures.
|
2021-10-25 13:38:07 +00:00
|
|
|
* **run**: Build and start local database server container.
|
2020-12-02 07:35:26 +00:00
|
|
|
* **start**: Start local database server container.
|
2020-11-15 18:24:25 +00:00
|
|
|
|
2020-11-17 10:53:31 +00:00
|
|
|
Each command can have its own specific commandline options.
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
## Basic information
|
2021-10-23 17:14:09 +00:00
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
First of all you have to initalize the workspace.
|
2021-10-23 17:14:09 +00:00
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt init
|
2021-10-23 17:14:09 +00:00
|
|
|
```
|
|
|
|
|
2022-12-21 13:17:50 +00:00
|
|
|
Now you can configure Myt using *myt.config.yml* file, located at the root of
|
2022-04-04 17:30:43 +00:00
|
|
|
your workspace. This file should include the project codename and schemas/tables
|
|
|
|
wich are exported when you use *pull* or *dump* commands.
|
2021-10-23 17:14:09 +00:00
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
Don't forget to initialize git (if it isn't initialized yet).
|
2021-10-23 17:14:09 +00:00
|
|
|
|
|
|
|
```text
|
2022-04-04 17:30:43 +00:00
|
|
|
$ git init
|
2021-10-23 17:14:09 +00:00
|
|
|
```
|
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
### Remotes
|
2021-10-23 17:14:09 +00:00
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
Create database connection configuration for each environment at *remotes*
|
|
|
|
folder using standard MySQL *ini* configuration files. The convention remote
|
|
|
|
names are *local*, *production* and *test*.
|
2021-10-23 17:14:09 +00:00
|
|
|
|
|
|
|
```text
|
2022-04-04 17:30:43 +00:00
|
|
|
remotes/[remote].ini
|
2021-10-23 17:14:09 +00:00
|
|
|
```
|
2022-04-04 17:30:43 +00:00
|
|
|
### Startup
|
2021-10-23 17:14:09 +00:00
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
Once the basic configuration is done, routines can be imported from the
|
|
|
|
database into the project, it is recommended to use the *production* remote.
|
2021-10-23 17:14:09 +00:00
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt pull production
|
2021-10-23 17:14:09 +00:00
|
|
|
```
|
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
From now on, you can use the project as if it were a standard git repository
|
|
|
|
(since it is). To apply changes to the database run the *push* command on the
|
|
|
|
desired remote.
|
2020-11-15 18:38:23 +00:00
|
|
|
|
2020-11-19 12:51:27 +00:00
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt push [<remote>] [--commit]
|
2020-11-14 01:38:56 +00:00
|
|
|
```
|
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
### Routines
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
Routines are placed inside *routines* folder. All objects that have PL/SQL code
|
|
|
|
are considered routines. It includes events, functions, procedures, triggers
|
|
|
|
and views with the following structure.
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
```text
|
2020-11-14 01:38:56 +00:00
|
|
|
routines
|
|
|
|
`- schema
|
|
|
|
|- events
|
|
|
|
| `- eventName.sql
|
|
|
|
|- functions
|
|
|
|
| `- functionName.sql
|
|
|
|
|- procedures
|
|
|
|
| `- procedureName.sql
|
|
|
|
|- triggers
|
|
|
|
| `- triggerName.sql
|
|
|
|
`- views
|
|
|
|
`- viewName.sql
|
|
|
|
```
|
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
### Versions
|
2020-11-14 01:38:56 +00:00
|
|
|
|
2021-10-23 17:14:09 +00:00
|
|
|
Versions are placed inside *versions* folder with the following structure.
|
2020-11-19 12:51:27 +00:00
|
|
|
Don't place your PL/SQL objects here, use the routines folder!
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-15 18:24:25 +00:00
|
|
|
```text
|
2020-11-19 12:51:27 +00:00
|
|
|
versions
|
2020-11-14 01:38:56 +00:00
|
|
|
|- 00001-firstVersionCodeName
|
|
|
|
| |- 00-firstExecutedScript.sql
|
|
|
|
| |- 01-secondScript.sql
|
|
|
|
| `- 99-lastScript.sql
|
|
|
|
`- 00002-secondVersion
|
|
|
|
|- 00-firstExecutedScript.sql
|
|
|
|
`- 00-sameNumbers.sql
|
|
|
|
```
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-17 12:04:57 +00:00
|
|
|
### Local server
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
The local server is created as a MariaDB Docker container using the base dump
|
|
|
|
created with the *dump* command plus pushing local versions and changed
|
2020-11-17 12:04:57 +00:00
|
|
|
routines.
|
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
### Dumps
|
2021-10-23 17:14:09 +00:00
|
|
|
|
|
|
|
You can create your local fixture and structure files.
|
|
|
|
|
2022-07-12 07:45:05 +00:00
|
|
|
* *dump/beforeDump.sql*
|
|
|
|
* *dump/afterDump.sql*
|
2021-10-23 17:14:09 +00:00
|
|
|
* *dump/fixtures.sql*
|
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
## Versioning commands
|
|
|
|
|
|
|
|
### init
|
|
|
|
|
|
|
|
Initializes an empty workspace.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt init
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### pull
|
|
|
|
|
|
|
|
Incorporates database routine changes into workspace.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt pull [remote] [-f|--force] [-c|--checkout] [-u|--update] [-s|--sums]
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
2022-04-30 00:06:56 +00:00
|
|
|
When *--checkout* option is provided, it does the following before export:
|
2022-04-04 17:30:43 +00:00
|
|
|
|
|
|
|
1. Get the last database push commit (saved in versioning tables).
|
|
|
|
2. Creates and checkout to a new branch based in database commit.
|
|
|
|
|
|
|
|
### push
|
|
|
|
|
|
|
|
Applies versions and routine changes into database.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt push [<remote>] [-f|--force] [-c|--commit] [-s|--sums]
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
2022-04-30 00:33:41 +00:00
|
|
|
Commit is saved into database only if *--commit* option is provided, it
|
2022-04-30 00:06:56 +00:00
|
|
|
prevents from accidentally saving local commits into shared servers, causing
|
|
|
|
subsequent pushes from other clients to fail because they can't get that
|
|
|
|
commit from the git tree in order to get differences.
|
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
### version
|
|
|
|
|
|
|
|
Creates a new version folder, when name is not specified it generates a random
|
|
|
|
name mixing a color with a plant name.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt version [<name>]
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
2022-12-29 13:27:16 +00:00
|
|
|
### create
|
|
|
|
|
|
|
|
Creates a new routine file with a default template.
|
|
|
|
|
|
|
|
```text
|
|
|
|
$ myt create [-t <type>] <schema>.<name>
|
|
|
|
```
|
|
|
|
|
2022-05-08 20:44:40 +00:00
|
|
|
### clean
|
|
|
|
|
|
|
|
Cleans all already applied versions older than *maxOldVersions*.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt clean
|
2022-05-08 20:44:40 +00:00
|
|
|
```
|
2022-05-01 08:26:25 +00:00
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
## Local server commands
|
|
|
|
|
|
|
|
### dump
|
|
|
|
|
|
|
|
Exports database structure and fixtures from remote into hidden files located
|
|
|
|
in *dump* folder. If no remote is specified *production* is used.
|
|
|
|
|
|
|
|
```text
|
2023-04-05 08:11:31 +00:00
|
|
|
$ myt dump [<remote>] [-l|--lock]
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
2022-06-09 18:41:18 +00:00
|
|
|
### fixtures
|
|
|
|
|
|
|
|
Exports local database fixtures into *dump/fixtures.sql* files. If no remote is
|
|
|
|
specified *local* is used.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt fixtures [<remote>]
|
2022-06-09 18:41:18 +00:00
|
|
|
```
|
|
|
|
|
2022-04-04 17:30:43 +00:00
|
|
|
### run
|
|
|
|
|
|
|
|
Builds and starts local database server container. It only rebuilds the image
|
|
|
|
when fixtures have been modified or when the day on which the image was built
|
|
|
|
is different to today.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt run [-c|--ci] [-r|--random]
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
|
|
|
### start
|
|
|
|
|
|
|
|
Starts local database server container. It does the minium effort, if it
|
|
|
|
doesn't exists calls the run command, if it is started does nothing. Keep in
|
|
|
|
mind that when you do not rebuild the docker you may be using an outdated
|
|
|
|
version of it.
|
|
|
|
|
|
|
|
```text
|
2022-12-21 13:17:50 +00:00
|
|
|
$ myt start
|
2022-04-04 17:30:43 +00:00
|
|
|
```
|
|
|
|
|
2020-11-17 11:47:20 +00:00
|
|
|
## Why
|
|
|
|
|
|
|
|
The main reason for starting this project it's because there are no fully free
|
2021-10-25 13:38:07 +00:00
|
|
|
and open source migration tools available that allow versioning database
|
|
|
|
routines with an standard CVS system as if they were normal application code.
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2021-10-25 13:38:07 +00:00
|
|
|
Also, the existing tools are too complex and require too much knowledge to
|
|
|
|
start a small project.
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2022-05-02 09:49:01 +00:00
|
|
|
## ToDo
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2022-01-31 13:07:08 +00:00
|
|
|
* Undo changes when there is an error applying a version using "undo" files.
|
2021-10-25 13:38:07 +00:00
|
|
|
* Console logging via events.
|
|
|
|
* Lock version table row when pushing.
|
2022-02-07 14:43:12 +00:00
|
|
|
* Preserve all characteristics on pull: SQL mode, character set, algorithm...
|
2020-11-17 11:47:20 +00:00
|
|
|
|
2020-11-14 01:38:56 +00:00
|
|
|
## Built With
|
|
|
|
|
|
|
|
* [Git](https://git-scm.com/)
|
|
|
|
* [nodejs](https://nodejs.org/)
|
2021-10-22 14:11:03 +00:00
|
|
|
* [NodeGit](https://www.nodegit.org/)
|
2020-11-14 01:38:56 +00:00
|
|
|
* [docker](https://www.docker.com/)
|