MMOframework
Pourquoi l'IA Playground Bibliothèques Développeurs Agence
FR EN
Créer un projet en direct
Index
# Data sync — the site's data, here and there A release ships code. What the site writes while it runs — its JSON collections (`users.json`, `pages.json`...), its SQLite metrics, its oauth keys, the images sent from its backoffice — lives in `shared/` and outlives every release. The `data-*` commands keep that data the same on a target and in the project on this machine, in either direction, and every release looks at it first. | command | what it does | writes | |---|---|---| | `data-status` | every synced file, by who changed it since the last sync: **target**, **local**, **both** | nothing | | `data-diff` | how they differ: a diff from the local version to the target's (mmo.git), a JSON collection document by document | nothing | | `data-pull` | what the target changed comes here | here | | `data-push` | what changed here goes to the target | the target | | `data-merge` | both ways at once | both | ```bash php scripts/admin.php data-status --targets=prod php scripts/admin.php data-diff --targets=prod --paths=db/pages.json php scripts/admin.php data-pull --targets=prod php scripts/admin.php data-merge --targets=prod --prefer=target --yes --i-understand php scripts/admin.php data-status --all -m # one line of json: each target's status, for a hub ``` `data-status -m` prints one line, `{"dataStatus": {target: ...}}`, each target summed up by `DataSync::summary()`: its `status` -- `synced`, `target` (to pull), `local` (to push), `both` (to merge: both sides changed, a file each or the same one) --, its `counts`, the `files` that differ (the first 200, `database` true for a database's), `lastSync`, the live release's edited files, and its database server's last pull or push (`databases`). A target that cannot be read (its server unreachable...) is `unknown` with its `error`, the others are read all the same, and the command ends with 1. A monitoring hub keeps it per site (`SyncStatus`, mmo.admin's `docs/monitoring.md`), and runs `data-pull`, `data-push` and `data-merge` on many sites at once, as batches, the side that wins chosen for the batch (`--prefer=`). ## What is synced A target's `sync.paths` — its `shared` paths unless it says otherwise — every file under them, both sides, minus `sync.exclude` (globs, matched against the path and the file's name): ```json "shared": ["db", "configs/private.key", "configs/public.key", "www/assets/uploads"], "sync": {"exclude": ["db/metrics/folder-*.json"]} ``` Leave out what is about one machine only: a cache keyed by a folder of the server, a log. What is never the site's data is always left out, whatever the paths say: each database's dumps (its repository's backup folder), mmo.core's jobs (`JobStore`'s folder), `.mmo/`, and every `*.lock`. **A full copy, or a quick one.** `sync.optional` names the heavy paths — a client's documents: ```json "sync": {"optional": ["files", "www/files"]} ``` They are synced only when the person syncing asks for a full copy: every data command, and the look a release takes first, asks once for all the targets (`A full copy, with [files, www/files] too ?`, ENTER: no). From a script, `--with-files` makes the full copy, and they are left out otherwise (`--without-files` to say it). A file the project's git tracks (a patch in `db/patches/`, a seed) is code, never data: it is left out whatever the paths say. A file this machine cannot read (one the web server owns) is left out, and said. ## Who changed what Each sync records, per file, the sha256 it left on each side (`.mmo/sync/{target}.json`, git-ignored with the rest of `.mmo/`), and the version both sides then held when it is a small text (`.mmo/sync/{target}/base/`). The next command compares both sides with that: - **target**: only the site changed it since — `data-pull` brings it; - **local**: only this project changed it — `data-push` sends it; - **both**: both changed it — merged (below); - never synced, a file only one side has is that side's, and one both have differently is **both**. A file the same on both sides is synced as of the first command that sees it. A file removed on one side, unchanged on the other, is removed on the other too. ## Merging what both changed From the version both sides last held: - **a JSON collection** (an object of documents by id, as JsonRepository writes it) document by document: a document one side changed is taken from it, a document both changed differently is a conflict. The merged file is written as the original was (one line or pretty, slashes and accents escaped or not), so only the documents change. - **a text** by `git merge-file`; without the common version, each hunk of the diff is a conflict. - **anything else** (an SQLite database, an image) is one side or the other, whole. Each conflict is shown as a diff, local in red and the target in green, and asked: keep the target's, keep this project's, or either one for every conflict after it. From a script, `--prefer=target` or `--prefer=local` answers them all; without it a script with a conflict is refused before anything is written. `data-pull` writes the merge here only: the target keeps its version, and the next `data-push` sends the merge. `data-merge` and `data-push` write it on both sides. ## Databases The data commands move a database through its repository (mmo.core's `iBackupRepository`), never as a file copied while the site writes it: - **a database of files** (JSON collections, SQLite) whose files are synced paths: the target's version comes down as that repository's own dump (`backup()` through the target's shell: SQLite's `.backup` of the database in use), and goes back as a dump made here of the versions to send, put in place by its `restore()` (each file a rename, the others kept). A pull restores it here the same way. - **a database server** (the target's `database`: MongoDB, MySQL), which no file holds: `data-pull` is `backup --restore-locally` -- dumped there, restored here anonymised, or `--plain --reason= --until=` --; `data-push` is a dump of this machine's, then `restore` there, the target's snapshot first. `data-merge` cannot merge a database server: it says so, and leaves it. What a pull or a push of it last did is kept in the state (`databases`: when, and which way): the only thing `data-status` can say of it. The repositories are the controller's `repository` and `repositories`. ## Portable, both ways Nothing that was synced holds an absolute address of one of the site's copies. Before a file is written on either side: - an url of the site — the target's `url`, this machine's (the controller's `localUrls`), `sync.urls` — becomes relative to the application: `https://acme.com/assets/a.jpg` is `assets/a.jpg`, as mmo.editor writes its own; the url alone is `./`. `http:`, `https:`, `//acme.com/` and JSON's `https:\/\/acme.com\/` are all recognised. - a folder of the site becomes the alias `{root}/`, which `\MMO\filename()` resolves wherever it runs: the target's folder followed by `releases/`, `current` or `shared` (as declared, as `pwd -P` reads it on the server, and `sync.folders`), and the project's folder here. A text is rewritten as it is, an SQLite database through its text columns; any other binary file is left alone. What still looks absolute afterwards is warned about, with the line to add to `sync.urls` or `sync.folders`. ## Nothing is lost on the target A push holds the target's deployment lock (no release meanwhile), uploads one archive checked by sha256, archives what it replaces or removes in `{folder}/deploy/data-backups/{stamp}.tar.gz`, and moves each file into place — a rename, so the site reads the old file or the new one, never half of one, with the rights of the file it replaced. ## The live release A file edited on the server inside the live release (a quick fix in `www/`, a configuration) is replaced by the next release. When the build folder still has the artifact the live release was shipped from, `data-status` lists what differs from it — modified, added, deleted — and `data-diff --release-files` shows how. `data-pull --release-files` brings those files into the working tree: review them (`git diff`), commit what is to stay. ## Before each release `release` (and `plan`, which only says it) first runs `data-status` on each target that has data to sync. When the site changed something since the last sync, a terminal asks: - **pull** — bring the site's changes here first (yours stay where only you changed), then release; - **merge** — both ways first, then release; - **push** — overwrite the site's changes with this project's data, then release; - **diff** — show what differs, then ask again; - **release** — release now: the site keeps its data, this project stays behind it; - **abort**. A script says it with `--data=pull|merge|push|release`, or is refused before anything is built. When files of the live release were brought into the working tree, the release stops there: commit them, then release again — a release is built from what is committed. `"sync": {"check": false}` or `--no-data-check` skips the look.