-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow downloading sqlite DB #53
Draft
mglaman
wants to merge
9
commits into
main
Choose a base branch
from
export-db
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+163
−14
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
83763f1
Allow downloading sqlite DB
mglaman ecdfaf9
ensure a filename is set
mglaman 8d607b0
add an alert before beginning
mglaman 541037d
instead of downloading DB, try creating a SQL dump
mglaman d162d5b
add indexes, set sequences
mglaman 1ac5eb2
CREATE TABLE to IF NOT EXISTS
mglaman 1de39d6
reduce size and improve sql statements
mglaman dcdc4cb
handle serialized php strings and nul chars
mglaman b5ae891
Merge branch 'refs/heads/main' into export-db
mglaman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/drupal-wasm-1.0.zip | ||
*.sql |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
use Drupal\Core\Cache\Cache; | ||
use Drupal\Core\DrupalKernel; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
$stdErr = fopen('php://stderr', 'w'); | ||
|
||
set_error_handler(static function(...$args) use($stdErr) { | ||
fwrite($stdErr, print_r($args,1)); | ||
}); | ||
|
||
$flavor = file_get_contents('/config/flavor.txt'); | ||
unlink('/config/flavor.txt'); | ||
|
||
$autoloader = require "/persist/$flavor/vendor/autoload.php"; | ||
|
||
$request = Request::createFromGlobals(); | ||
$kernel = DrupalKernel::createFromRequest($request, $autoloader, 'prod'); | ||
chdir($kernel->getAppRoot()); | ||
$kernel->boot(); | ||
|
||
foreach (Cache::getBins() as $cache_backend) { | ||
$cache_backend->deleteAll(); | ||
} | ||
\Drupal::service('plugin.cache_clearer')->clearCachedDefinitions(); | ||
$kernel->invalidateContainer(); | ||
|
||
$sql = 'PRAGMA foreign_keys=OFF;' . PHP_EOL; | ||
$sql .= 'BEGIN TRANSACTION;' . PHP_EOL; | ||
|
||
// adapted from https://github.com/ephestione/php-sqlite-dump/blob/master/sqlite_dump.php | ||
$database = \Drupal::database(); | ||
$tables = $database->query('SELECT [name], [sql] FROM {sqlite_master} WHERE [type] = "table" AND [name] NOT LIKE "sqlite_%" ORDER BY name'); | ||
foreach ($tables as $table) { | ||
$sql .= str_replace('CREATE TABLE ', 'CREATE TABLE IF NOT EXISTS ', $table->sql) . ';' . PHP_EOL; | ||
|
||
$columns = array_map( | ||
static fn (object $row) => "`{$row->name}`", | ||
$database->query("PRAGMA table_info({$table->name})")->fetchAll() | ||
); | ||
$columns = implode(', ', $columns); | ||
$sql .= PHP_EOL; | ||
|
||
$rows = $database->select($table->name)->fields($table->name)->execute()->fetchAll(\PDO::FETCH_ASSOC); | ||
foreach ($rows as $row) { | ||
$values = implode( | ||
', ', | ||
array: array_map( | ||
static function ($value) { | ||
if ($value === NULL) { | ||
return 'NULL'; | ||
} | ||
if ($value === '') { | ||
return "''"; | ||
} | ||
if (is_numeric($value)) { | ||
return $value; | ||
} | ||
if ($value === 'b:0;') { | ||
return false; | ||
} | ||
$is_serialized = $value[1] === ':' && str_ends_with($value, '}'); | ||
|
||
$value = str_replace(["'"], ["''"], $value); | ||
$value = "'$value'"; | ||
|
||
if ($is_serialized) { | ||
$value = sprintf( | ||
"replace(%s, char(1), char(0))", | ||
str_replace(chr(0), chr(1), $value), | ||
); | ||
} | ||
return $value; | ||
}, | ||
array_values($row) | ||
) | ||
); | ||
$sql .= "INSERT INTO {$table->name} VALUES($values);" . PHP_EOL; | ||
} | ||
|
||
$sql .= PHP_EOL; | ||
} | ||
|
||
$sql .= 'DELETE FROM sqlite_sequence;' . PHP_EOL; | ||
$sequences = $database->query('SELECT [name], [seq] FROM sqlite_sequence'); | ||
foreach ($sequences as $sequence) { | ||
$sql .= "INSERT INTO sqlite_sequence VALUES('{$sequence->name}',{$sequence->seq});" . PHP_EOL; | ||
} | ||
|
||
$indexes = $database->query('SELECT [sql] FROM {sqlite_master} WHERE [type] = "index" AND [name] NOT LIKE "sqlite_%" ORDER BY name'); | ||
foreach ($indexes as $index) { | ||
$sql .= $index->sql . ';' . PHP_EOL; | ||
} | ||
|
||
|
||
$sql .= 'COMMIT;' . PHP_EOL; | ||
|
||
// TODO support this collation somehow | ||
// see https://www.drupal.org/project/drupal/issues/3036487 | ||
$sql = str_replace('NOCASE_UTF8', 'NOCASE', $sql); | ||
|
||
file_put_contents('/persist/dump.sql', $sql); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NOCASE_UTF8 is added at runtime by Drupal via PDO. I don't think there is any way to support this