2026-04-10
- corvée: met à jour les deps - corvée: formate
This commit is contained in:
parent
00f87fedcd
commit
b2a0012590
85 changed files with 132083 additions and 31328 deletions
|
|
@ -1,13 +1,14 @@
|
|||
import { $ } from "bun";
|
||||
import { Array, Console, Effect, Layer, ManagedRuntime, Option, Order, pipe, Schema, ServiceMap } from "effect";
|
||||
import { UnknownError } from "effect/Cause";
|
||||
import type { Option } from "effect";
|
||||
import { Array as FxArray, Console, Effect, Layer, ManagedRuntime, Order, pipe, Schema, ServiceMap } from "effect";
|
||||
import type { UnknownError } from "effect/Cause";
|
||||
import { readdir } from "node:fs/promises";
|
||||
|
||||
class PodmanError extends Schema.TaggedErrorClass<PodmanError>()("PodmanError", {
|
||||
cause: Schema.Error,
|
||||
}) {}
|
||||
|
||||
class FileSystemError extends Schema.TaggedErrorClass<FileSystemError>()("FileSystemError", {
|
||||
class FSError extends Schema.TaggedErrorClass<FSError>()("FSError", {
|
||||
cause: Schema.Error,
|
||||
}) {}
|
||||
|
||||
|
|
@ -21,21 +22,21 @@ class Podman extends ServiceMap.Service<
|
|||
static readonly layer = Layer.effect(
|
||||
Podman,
|
||||
// oxlint-disable-next-line require-yield
|
||||
Effect.gen(function* () {
|
||||
const launchContainers = Effect.fn("launchContainers")(function* () {
|
||||
Effect.gen(function*() {
|
||||
const launchContainers = Effect.fn("launchContainers")(function*() {
|
||||
return yield* pipe(
|
||||
Effect.tryPromise(() => $`podman compose up -d &> /dev/null`),
|
||||
Effect.tryPromise(async () => $`podman compose up -d &> /dev/null`),
|
||||
Effect.map((shell: $.ShellOutput) => shell.text()),
|
||||
Effect.mapError((error: UnknownError) => new PodmanError({ cause: error })),
|
||||
);
|
||||
});
|
||||
|
||||
const importLatestDbInWordPressContainer = Effect.fn("importLatestDbInWordPressContainer")(function* (
|
||||
const importLatestDbInWordPressContainer = Effect.fn("importLatestDbInWordPressContainer")(function*(
|
||||
exportPath: string,
|
||||
) {
|
||||
return yield* pipe(
|
||||
Effect.tryPromise(
|
||||
() =>
|
||||
async () =>
|
||||
$`podman exec -it haikuatelier.fr-wordpress fish -c "cd web && wp --allow-root db import ${exportPath} > /dev/null"`,
|
||||
),
|
||||
Effect.map((shell: $.ShellOutput) => shell.text()),
|
||||
|
|
@ -51,47 +52,44 @@ class Podman extends ServiceMap.Service<
|
|||
);
|
||||
}
|
||||
|
||||
class FileSystem extends ServiceMap.Service<
|
||||
FileSystem,
|
||||
class FS extends ServiceMap.Service<
|
||||
FS,
|
||||
{
|
||||
getLatestDbExport(): Effect.Effect<string, FileSystemError>;
|
||||
getLatestDbExport(): Effect.Effect<string, FSError>;
|
||||
}
|
||||
>()("haikuatelier.fr/scripts/importe-dernier-export-bdd/FileSystem") {
|
||||
>()("haikuatelier.fr/scripts/importe-dernier-export-bdd/FS") {
|
||||
static readonly layer = Layer.effect(
|
||||
FileSystem,
|
||||
FS,
|
||||
// oxlint-disable-next-line require-yield
|
||||
Effect.gen(function* () {
|
||||
const getLatestDbExport = Effect.fn("getLatestDbExport")(function* () {
|
||||
Effect.gen(function*() {
|
||||
const getLatestDbExport = Effect.fn("getLatestDbExport")(function*() {
|
||||
return yield* pipe(
|
||||
Effect.tryPromise(() => readdir(`./db`)),
|
||||
Effect.map((paths: ReadonlyArray<string>) => Array.sort(paths, Order.String)),
|
||||
Effect.map((sortedPaths: ReadonlyArray<string>) => Array.last(sortedPaths)),
|
||||
Effect.tryPromise(async () => readdir(`./db`)),
|
||||
Effect.map((paths: ReadonlyArray<string>) => FxArray.sort(paths, Order.String)),
|
||||
Effect.map((sortedPaths: ReadonlyArray<string>) => FxArray.last(sortedPaths)),
|
||||
Effect.flatMap((path: Option.Option<string>) => Effect.fromOption(path)),
|
||||
Effect.mapError((_) => new FileSystemError({ cause: new Error("Aucun export de BDD n'est disponible.") })),
|
||||
Effect.mapError(_ => new FSError({ cause: new Error("Aucun export de BDD n'est disponible.") })),
|
||||
);
|
||||
});
|
||||
|
||||
return FileSystem.of({
|
||||
return FS.of({
|
||||
getLatestDbExport,
|
||||
});
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
const mainLayer = Layer.mergeAll(Podman.layer, FileSystem.layer);
|
||||
const mainLayer = Layer.mergeAll(Podman.layer, FS.layer);
|
||||
const runtime = ManagedRuntime.make(mainLayer);
|
||||
|
||||
const program = Effect.fn("program")(function* () {
|
||||
yield* Podman.use((podman) => podman.launchContainers());
|
||||
const program = Effect.fn("program")(function*() {
|
||||
yield* Podman.use(podman => podman.launchContainers());
|
||||
yield* Console.log("Containers are launched.");
|
||||
|
||||
const latestExportPath: string = pipe(
|
||||
yield* FileSystem.use((fs) => fs.getLatestDbExport()),
|
||||
(path) => `../db/${path}`,
|
||||
);
|
||||
const latestExportPath: string = pipe(yield* FS.use(fs => fs.getLatestDbExport()), path => `../db/${path}`);
|
||||
yield* Console.log(latestExportPath);
|
||||
|
||||
yield* Podman.use((podman) => podman.importLatestDbInWordPressContainer(latestExportPath));
|
||||
yield* Podman.use(podman => podman.importLatestDbInWordPressContainer(latestExportPath));
|
||||
yield* Console.log("Import done.");
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -1,5 +1,5 @@
|
|||
import { YAML } from "bun";
|
||||
import { Console, Data, Effect, Array as EffectArray, pipe, Record, Schema, SchemaIssue } from "effect";
|
||||
import { Array as EffectArray, Console, Data, Effect, pipe, Record, Schema, SchemaIssue } from "effect";
|
||||
import { SchemaError } from "effect/Schema";
|
||||
|
||||
const COMPOSE_PATH = "compose.yaml";
|
||||
|
|
@ -21,7 +21,7 @@ class ScriptError extends Data.TaggedError("ScriptError")<{ cause: unknown }> {}
|
|||
* @param compose Le fichier _Compose_ sous forme d'objet.
|
||||
* @returns Les noms des Services sous forme de tableau.
|
||||
*/
|
||||
const getServicesFromComposeYaml: (compose: Compose) => ReadonlyArray<string> = (compose) =>
|
||||
const getServicesFromComposeYaml: (compose: Compose) => ReadonlyArray<string> = compose =>
|
||||
Record.keys(compose.services);
|
||||
|
||||
/**
|
||||
|
|
@ -31,7 +31,7 @@ const getServicesFromComposeYaml: (compose: Compose) => ReadonlyArray<string> =
|
|||
* @returns Le contenu textuel du fichier sous forme de chaîne de caractères.
|
||||
*/
|
||||
const getFileContent: (filePath: string) => Effect.Effect<string, ScriptError> = Effect.fn("getFileContent")(
|
||||
function* (filePath) {
|
||||
function*(filePath) {
|
||||
const fileRef: Bun.BunFile = Bun.file(filePath);
|
||||
|
||||
yield* Effect.tryPromise({
|
||||
|
|
@ -56,12 +56,12 @@ const getFileContent: (filePath: string) => Effect.Effect<string, ScriptError> =
|
|||
const getComposeYaml: <ComposeSchema>(
|
||||
path: string,
|
||||
schema: Schema.Schema<ComposeSchema>,
|
||||
) => Effect.Effect<ComposeSchema, ScriptError, unknown> = Effect.fn("getComposeYaml")(function* (path, schema) {
|
||||
) => Effect.Effect<ComposeSchema, ScriptError, unknown> = Effect.fn("getComposeYaml")(function*(path, schema) {
|
||||
return yield* pipe(
|
||||
getFileContent(path),
|
||||
Effect.map((text: string): unknown => YAML.parse(text)),
|
||||
Effect.flatMap((yaml: unknown) =>
|
||||
Schema.decodeUnknownEffect(schema)(yaml, { errors: "all", onExcessProperty: "ignore" }),
|
||||
Schema.decodeUnknownEffect(schema)(yaml, { errors: "all", onExcessProperty: "ignore" })
|
||||
),
|
||||
Effect.mapError((error): ScriptError => {
|
||||
if (error instanceof SchemaError) {
|
||||
|
|
@ -76,7 +76,7 @@ const getComposeYaml: <ComposeSchema>(
|
|||
const program: Effect.Effect<ReadonlyArray<string>, ScriptError> = pipe(
|
||||
getComposeYaml(COMPOSE_PATH, Compose),
|
||||
Effect.map((compose: Compose) => getServicesFromComposeYaml(compose)),
|
||||
Effect.map((keys: ReadonlyArray<string>) => EffectArray.filter(keys, (key) => key !== "wordpress")),
|
||||
Effect.map((keys: ReadonlyArray<string>) => EffectArray.filter(keys, key => key !== "wordpress")),
|
||||
Effect.orElseSucceed(() => [""]),
|
||||
Effect.tap((services: ReadonlyArray<string>) => {
|
||||
Bun.spawn({ cmd: ["podman", "compose", "pull", ...services], timeout: DEFAULT_CMD_TIMEOUT });
|
||||
|
|
|
|||
|
|
@ -14,32 +14,32 @@ global $wpdb;
|
|||
$wp_postmeta = "{$wpdb->prefix}postmeta";
|
||||
|
||||
try {
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
// Error Handling
|
||||
$sql = "UPDATE {$wp_postmeta} SET meta_value = REPLACE(meta_value,'-scaled.jpg','.jpg') WHERE meta_key='_wp_attached_file' AND meta_value LIKE '%-scaled.jpg%'";
|
||||
$result = $pdo->exec($sql);
|
||||
print_r($result);
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
// Error Handling
|
||||
$sql = "UPDATE {$wp_postmeta} SET meta_value = REPLACE(meta_value,'-scaled.jpg','.jpg') WHERE meta_key='_wp_attached_file' AND meta_value LIKE '%-scaled.jpg%'";
|
||||
$result = $pdo->exec($sql);
|
||||
print_r($result);
|
||||
} catch (PDOException $e) {
|
||||
print_r($e->getMessage());
|
||||
print_r($e->getMessage());
|
||||
}
|
||||
|
||||
// replace _wp_attachment_metadata meta_key.
|
||||
$image_metas = [];
|
||||
|
||||
try {
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
// Error Handling
|
||||
$sql = "SELECT * FROM {$wp_postmeta} WHERE meta_value LIKE '%-scaled.jpg%' AND meta_key='_wp_attachment_metadata'";
|
||||
$statement = $pdo->query($sql);
|
||||
$image_metas = $statement->fetchAll();
|
||||
foreach ($image_metas as $meta) {
|
||||
$meta_value = unserialize($meta['meta_value']);
|
||||
$file = $meta_value['file'];
|
||||
$meta_value['file'] = str_replace('-scaled.jpg', '.jpg', $file);
|
||||
update_post_meta($meta['post_id'], $meta['meta_key'], $meta_value);
|
||||
$result = get_post_meta($meta['post_id'], $meta['meta_key']);
|
||||
print_r($result);
|
||||
}
|
||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
||||
// Error Handling
|
||||
$sql = "SELECT * FROM {$wp_postmeta} WHERE meta_value LIKE '%-scaled.jpg%' AND meta_key='_wp_attachment_metadata'";
|
||||
$statement = $pdo->query($sql);
|
||||
$image_metas = $statement->fetchAll();
|
||||
foreach ($image_metas as $meta) {
|
||||
$meta_value = unserialize($meta['meta_value']);
|
||||
$file = $meta_value['file'];
|
||||
$meta_value['file'] = str_replace('-scaled.jpg', '.jpg', $file);
|
||||
update_post_meta($meta['post_id'], $meta['meta_key'], $meta_value);
|
||||
$result = get_post_meta($meta['post_id'], $meta['meta_key']);
|
||||
print_r($result);
|
||||
}
|
||||
} catch (PDOException $e) {
|
||||
print_r($e->getMessage());
|
||||
print_r($e->getMessage());
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue