Migrate the Laysense production snapshot to LaySite 1.3
This commit is contained in:
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Content;
|
||||
|
||||
use PDO;
|
||||
use Pdo\Sqlite;
|
||||
|
||||
final class SiteSnapshot
|
||||
{
|
||||
private const TABLES = [
|
||||
'site_settings', 'design_presets', 'pages', 'sections',
|
||||
'article_categories', 'articles', 'article_category_links',
|
||||
'article_redirects', 'article_deletions', 'media',
|
||||
];
|
||||
|
||||
public static function install(PDO $target): void
|
||||
{
|
||||
$readOnly = class_exists(Sqlite::class)
|
||||
? [Sqlite::ATTR_OPEN_FLAGS => Sqlite::OPEN_READONLY]
|
||||
: [PDO::SQLITE_ATTR_OPEN_FLAGS => PDO::SQLITE_OPEN_READONLY];
|
||||
$source = new PDO('sqlite:' . dirname(__DIR__, 2) . '/resources/laysense-site.sqlite',
|
||||
options: $readOnly + [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
|
||||
foreach (self::TABLES as $table) {
|
||||
$insert = null;
|
||||
foreach ($source->query('SELECT * FROM ' . $table) as $row) {
|
||||
$insert ??= $target->prepare(sprintf(
|
||||
'INSERT INTO %s (%s) VALUES (%s)',
|
||||
$table,
|
||||
implode(', ', array_keys($row)),
|
||||
implode(', ', array_fill(0, count($row), '?')),
|
||||
));
|
||||
$insert->execute(array_values($row));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user