Skip to content

Commit 4f31ccd

Browse files
Merge pull request #776 from nextcloud/fix/763-tidy-up-database-on-migration
Fix/763: Tidy up database on migration
2 parents bbd15f3 + e3f2442 commit 4f31ccd

File tree

9 files changed

+363
-24
lines changed

9 files changed

+363
-24
lines changed

.github/actions/run-tests/reset-from-container.sh

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#! /bin/sh -e
1+
#! /bin/bash -e
22

33
BACKUP="$1"
44

@@ -13,8 +13,30 @@ case "$INPUT_DB" in
1313
mysql -u root -p"$MYSQL_ROOT_PASSWORD" -h mysql < "/dumps/$BACKUP/sql/dump.sql"
1414
;;
1515
pgsql)
16+
echo 'Dropping old data'
1617
PGPASSWORD="$POSTGRES_PASSWORD" \
17-
psql -d "$POSTGRES_DB" -h postgres -U "$POSTGRES_USER" < "/dumps/$BACKUP/sql/dump.sql"
18+
psql -d "$POSTGRES_DB" -h postgres -U "$POSTGRES_USER" -v 'ON_ERROR_STOP=1' <<- EOF || exit 1
19+
DROP SCHEMA public CASCADE;
20+
CREATE SCHEMA public;
21+
GRANT ALL ON SCHEMA public TO $POSTGRES_USER;
22+
GRANT ALL ON SCHEMA public TO public;
23+
EOF
24+
25+
# PGPASSWORD="$POSTGRES_PASSWORD" \
26+
# psql -d "$POSTGRES_DB" -h postgres -U "$POSTGRES_USER" <<- EOF
27+
# \l
28+
# \d
29+
# SELECT * FROM oc_preferences WHERE appid='cookbook';
30+
# EOF
31+
32+
echo 'Inserting dump data'
33+
PGPASSWORD="$POSTGRES_PASSWORD" \
34+
psql -d "$POSTGRES_DB" -h postgres -U "$POSTGRES_USER" -f "/dumps/$BACKUP/sql/dump.sql" -v 'ON_ERROR_STOP=1' || exit 1
35+
36+
# PGPASSWORD="$POSTGRES_PASSWORD" \
37+
# psql -d "$POSTGRES_DB" -h postgres -U "$POSTGRES_USER" <<- EOF
38+
# SELECT * FROM oc_preferences WHERE appid='cookbook';
39+
# EOF
1840
;;
1941
sqlite)
2042
echo "Doing nothing as it was already restored during data restoration."

.github/actions/run-tests/run-locally.sh

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,12 @@ drop_environment(){
274274
fi
275275
;;
276276
pgsql)
277-
local tables=$(echo "SELECT tablename FROM pg_tables WHERE schemaname = 'public';" | call_postgres | head -n -1 )
278-
if [ -n "$tables" ]; then
279-
echo "$tables" | sed 's@.*@DROP TABLE \0;@' | call_postgres
280-
fi
277+
call_postgres <<- EOF
278+
DROP SCHEMA public CASCADE;
279+
CREATE SCHEMA public;
280+
GRANT ALL ON SCHEMA public TO $POSTGRES_USER;
281+
GRANT ALL ON SCHEMA public TO public;
282+
EOF
281283
;;
282284
esac
283285

@@ -337,6 +339,12 @@ restore_env_dump() {
337339
;;
338340
pgsql)
339341
echo "Restoring Postgres database from dump"
342+
call_postgres <<- EOF
343+
DROP SCHEMA public CASCADE;
344+
CREATE SCHEMA public;
345+
GRANT ALL ON SCHEMA public TO $POSTGRES_USER;
346+
GRANT ALL ON SCHEMA public TO public;
347+
EOF
340348
cat "volumes/dumps/$ENV_DUMP_PATH/sql/dump.sql" | call_postgres
341349
;;
342350
sqlite)
@@ -424,7 +432,7 @@ function call_mysql() {
424432

425433
function call_postgres() {
426434
#docker-compose exec -T postgres psql -t nc_test tester
427-
docker-compose exec -T postgres psql -U "$POSTGRES_USER" "$POSTGRES_DB"
435+
docker-compose exec -T postgres psql -U "$POSTGRES_USER" "$POSTGRES_DB" -v 'ON_ERROR_STOP=1' --quiet "$@"
428436
}
429437

430438
##### Parameters as extracted from the CLI
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#! /bin/sh -e
2+
3+
#set -x
4+
5+
cd /nextcloud
6+
php occ "$@"
7+
8+
exit $?
9+

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
### Fixed
88
- Fixed changes from #774 and minor extensions
99
[#775](https://github.com/nextcloud/cookbook/pull/775) @christianlupus
10+
- Clean tables from old, redundant, and non-unique data to allow migrations (see #762 #763)
11+
[#776](https://github.com/nextcloud/cookbook/pull/776) @christianlupus
1012

1113

1214
## 0.9.1 - 2021-07-05

lib/Migration/Version000000Date20210701093123.php

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,84 @@
44

55
namespace OCA\Cookbook\Migration;
66

7-
use Closure;
7+
use OCP\IDBConnection;
88
use OCP\DB\ISchemaWrapper;
99
use OCP\Migration\IOutput;
1010
use OCP\Migration\SimpleMigrationStep;
11+
use Closure;
12+
use OCP\DB\QueryBuilder\IQueryBuilder;
1113

1214
/**
1315
* Auto-generated migration step: Please modify to your needs!
1416
*/
1517
class Version000000Date20210701093123 extends SimpleMigrationStep {
1618

19+
/**
20+
* @var IDBConnection
21+
*/
22+
private $db;
23+
24+
public function __construct(IDBConnection $db) {
25+
$this->db = $db;
26+
}
27+
28+
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {
29+
$this->db->beginTransaction();
30+
try {
31+
$qb = $this->db->getQueryBuilder();
32+
33+
// Fetch all rows that are non-unique
34+
$qb->selectAlias('n.user_id', 'user')
35+
->selectAlias('n.recipe_id', 'recipe')
36+
->from('cookbook_names', 'n')
37+
->groupBy('n.user_id', 'n.recipe_id')
38+
->having('COUNT(*) > 1');
39+
//echo $qb->getSQL() . "\n";
40+
41+
$cursor = $qb->execute();
42+
$result = $cursor->fetchAll();
43+
44+
if (sizeof($result) > 0) {
45+
// We have to fix the database
46+
47+
// Drop all redundant rows
48+
$qb = $this->db->getQueryBuilder();
49+
$qb->delete('cookbook_names')
50+
->where(
51+
'user_id = :user',
52+
'recipe_id = :recipe'
53+
);
54+
55+
$qb2 = $this->db->getQueryBuilder();
56+
$qb2->update('preferences')
57+
->set('configvalue', $qb->expr()->literal('1', IQueryBuilder::PARAM_STR))
58+
->where(
59+
'userid = :user',
60+
'appid = :app',
61+
'configkey = :property'
62+
);
63+
$qb2->setParameter('app', 'cookbook');
64+
$qb2->setParameter('property', 'last_index_update');
65+
66+
foreach ($result as $r) {
67+
$qb->setParameter('user', $r['user']);
68+
$qb->setParameter('recipe', $r['recipe']);
69+
$qb->execute();
70+
71+
$qb2->setParameter('user', $r['user']);
72+
$qb2->execute();
73+
}
74+
}
75+
76+
// Finish the transaction
77+
$this->db->commit();
78+
} catch (\Exception $e) {
79+
// Abort the transaction
80+
$this->db->rollBack();
81+
throw $e;
82+
}
83+
}
84+
1785
/**
1886
* @param IOutput $output
1987
* @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`

phpunit.integration.xml

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
<phpunit bootstrap="tests/bootstrap.php" colors="true">
1+
<phpunit
2+
bootstrap="tests/bootstrap.php"
3+
colors="true"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
cacheResult="true"
7+
cacheResultFile="/tmp/phpunit.cache"
8+
>
29
<testsuites>
310
<testsuite name="integration">
411
<directory>./tests/Integration</directory>
512
</testsuite>
613
</testsuites>
714
<filter>
815
<whitelist processUncoveredFilesFromWhitelist="true" addUncoveredFilesFromWhitelist="true">
9-
<directory suffix=".php">lib/</directory>
10-
<!--<directory suffix=".php">lib/Controller</directory>
11-
<directory suffix=".php">lib/Db</directory>
12-
<directory suffix=".php">lib/Exception</directory>
13-
<directory suffix=".php">lib/Migration</directory>
14-
<directory suffix=".php">lib/Service</directory>-->
16+
<directory suffix=".php">lib</directory>
1517
</whitelist>
1618
</filter>
1719
</phpunit>

phpunit.xml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
<phpunit bootstrap="tests/bootstrap.php" colors="true">
1+
<phpunit
2+
bootstrap="tests/bootstrap.php"
3+
colors="true"
4+
backupGlobals="false"
5+
backupStaticAttributes="false"
6+
cacheResult="true"
7+
cacheResultFile="/tmp/phpunit.cache"
8+
>
29
<testsuites>
310
<testsuite name="unit">
411
<directory>./tests/Unit</directory>
@@ -7,11 +14,6 @@
714
<filter>
815
<whitelist processUncoveredFilesFromWhitelist="true" addUncoveredFilesFromWhitelist="true">
916
<directory suffix=".php">lib</directory>
10-
<!--<directory suffix=".php">lib/Controller</directory>
11-
<directory suffix=".php">lib/Db</directory>
12-
<directory suffix=".php">lib/Exception</directory>
13-
<directory suffix=".php">lib/Migration</directory>
14-
<directory suffix=".php">lib/Service</directory>-->
1517
</whitelist>
1618
</filter>
1719
</phpunit>

0 commit comments

Comments
 (0)