mirror of
https://we.phorge.it/source/phorge.git
synced 2024-12-28 16:30:59 +01:00
Remove PHID database, add Harbormaster database
Summary: - We currently write every PHID we generate to a table. This was motivated by two concerns: - **Understanding Data**: At Facebook, the data was sometimes kind of a mess. You could look at a random user in the ID tool and see 9000 assocs with random binary data attached to them, pointing at a zillion other objects with no idea how any of it got there. I originally created this table to have a canonical source of truth about PHID basics, at least. In practice, our data model has been really tidy and consistent, and we don't use any of the auxiliary data in this table (or even write it). The handle abstraction is powerful and covers essentially all of the useful data in the app, and we have human-readable types in the keys. So I don't think we have a real need here, and this table isn't serving it if we do. - **Uniqueness**: With a unique key, we can be sure they're unique, even if we get astronomically unlucky and get a collision. But every table we use them in has a unique key anyway. So we actually get pretty much nothing here, except maybe some vague guarantee that we won't reallocate a key later if the original object is deleted. But it's hard to imagine any install will ever have a collision, given that the key space is 36^20 per object type. - We also currently use PHIDs and Users in tests sometimes. This is silly and can break (see D2461). - Drop the PHID database. - Introduce a "Harbormaster" database (the eventual CI tool, after Drydock). - Add a scratch table to the Harbormaster database for doing unit test meta-tests. - Now, PHID generation does no writes, and unit tests are isolated from the application. - @csilvers: This should slightly improve the performance of the large query-bound tail in D2457. Test Plan: Ran unit tests. Ran storage upgrade. Reviewers: btrahan, vrana, jungejason Reviewed By: btrahan CC: csilvers, aran, nh, edward Differential Revision: https://secure.phabricator.com/D2466
This commit is contained in:
parent
a70cf3f675
commit
a89cef8e39
16 changed files with 109 additions and 77 deletions
1
resources/sql/patches/phiddrop.sql
Normal file
1
resources/sql/patches/phiddrop.sql
Normal file
|
@ -0,0 +1 @@
|
|||
DROP DATABASE IF EXISTS {$NAMESPACE}_phid;
|
7
resources/sql/patches/testdatabase.sql
Normal file
7
resources/sql/patches/testdatabase.sql
Normal file
|
@ -0,0 +1,7 @@
|
|||
CREATE TABLE {$NAMESPACE}_harbormaster.harbormaster_scratchtable (
|
||||
`id` int unsigned NOT NULL AUTO_INCREMENT PRIMARY KEY,
|
||||
`data` varchar(64) NOT NULL collate utf8_bin,
|
||||
`dateCreated` int unsigned NOT NULL,
|
||||
`dateModified` int unsigned NOT NULL,
|
||||
KEY (data)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
|
|
@ -423,6 +423,8 @@ phutil_register_library_map(array(
|
|||
'DrydockResourceStatus' => 'applications/drydock/constants/resourcestatus',
|
||||
'DrydockSSHCommandInterface' => 'applications/drydock/interface/command/ssh',
|
||||
'DrydockWebrootInterface' => 'applications/drydock/interface/webroot/base',
|
||||
'HarbormasterDAO' => 'applications/harbormaster/storage/base',
|
||||
'HarbormasterScratchTable' => 'applications/harbormaster/storage/scratchtable',
|
||||
'HeraldAction' => 'applications/herald/storage/action',
|
||||
'HeraldActionConfig' => 'applications/herald/config/action',
|
||||
'HeraldApplyTranscript' => 'applications/herald/storage/transcript/apply',
|
||||
|
@ -783,7 +785,6 @@ phutil_register_library_map(array(
|
|||
'PhabricatorPHID' => 'applications/phid/storage/phid',
|
||||
'PhabricatorPHIDConstants' => 'applications/phid/constants',
|
||||
'PhabricatorPHIDController' => 'applications/phid/controller/base',
|
||||
'PhabricatorPHIDDAO' => 'applications/phid/storage/base',
|
||||
'PhabricatorPHIDLookupController' => 'applications/phid/controller/lookup',
|
||||
'PhabricatorPaste' => 'applications/paste/storage/paste',
|
||||
'PhabricatorPasteController' => 'applications/paste/controller/base',
|
||||
|
@ -1411,6 +1412,8 @@ phutil_register_library_map(array(
|
|||
'DrydockResourceStatus' => 'DrydockConstants',
|
||||
'DrydockSSHCommandInterface' => 'DrydockCommandInterface',
|
||||
'DrydockWebrootInterface' => 'DrydockInterface',
|
||||
'HarbormasterDAO' => 'PhabricatorLiskDAO',
|
||||
'HarbormasterScratchTable' => 'HarbormasterDAO',
|
||||
'HeraldAction' => 'HeraldDAO',
|
||||
'HeraldApplyTranscript' => 'HeraldDAO',
|
||||
'HeraldCommitAdapter' => 'HeraldObjectAdapter',
|
||||
|
@ -1689,9 +1692,7 @@ phutil_register_library_map(array(
|
|||
'PhabricatorOwnersOwner' => 'PhabricatorOwnersDAO',
|
||||
'PhabricatorOwnersPackage' => 'PhabricatorOwnersDAO',
|
||||
'PhabricatorOwnersPath' => 'PhabricatorOwnersDAO',
|
||||
'PhabricatorPHID' => 'PhabricatorPHIDDAO',
|
||||
'PhabricatorPHIDController' => 'PhabricatorController',
|
||||
'PhabricatorPHIDDAO' => 'PhabricatorLiskDAO',
|
||||
'PhabricatorPHIDLookupController' => 'PhabricatorPHIDController',
|
||||
'PhabricatorPaste' => 'PhabricatorPasteDAO',
|
||||
'PhabricatorPasteController' => 'PhabricatorController',
|
||||
|
|
|
@ -16,10 +16,10 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
abstract class PhabricatorPHIDDAO extends PhabricatorLiskDAO {
|
||||
abstract class HarbormasterDAO extends PhabricatorLiskDAO {
|
||||
|
||||
public function getApplicationName() {
|
||||
return 'phid';
|
||||
return 'harbormaster';
|
||||
}
|
||||
|
||||
}
|
|
@ -9,4 +9,4 @@
|
|||
phutil_require_module('phabricator', 'applications/base/storage/lisk');
|
||||
|
||||
|
||||
phutil_require_source('PhabricatorPHIDDAO.php');
|
||||
phutil_require_source('HarbormasterDAO.php');
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
/*
|
||||
* Copyright 2012 Facebook, Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/**
|
||||
* This is just a test table that unit tests can use if they need to test
|
||||
* generic database operations. It won't change and break tests and stuff, and
|
||||
* mistakes in test construction or isolation won't impact the application in
|
||||
* any way.
|
||||
*/
|
||||
final class HarbormasterScratchTable extends HarbormasterDAO {
|
||||
|
||||
protected $data;
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
/**
|
||||
* This file is automatically generated. Lint this module to rebuild it.
|
||||
* @generated
|
||||
*/
|
||||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/harbormaster/storage/base');
|
||||
|
||||
|
||||
phutil_require_source('HarbormasterScratchTable.php');
|
|
@ -16,32 +16,20 @@
|
|||
* limitations under the License.
|
||||
*/
|
||||
|
||||
final class PhabricatorPHID extends PhabricatorPHIDDAO {
|
||||
final class PhabricatorPHID {
|
||||
|
||||
protected $phid;
|
||||
protected $phidType;
|
||||
protected $ownerPHID;
|
||||
protected $parentPHID;
|
||||
|
||||
public static function generateNewPHID($type, array $config = array()) {
|
||||
$owner = idx($config, 'owner');
|
||||
$parent = idx($config, 'parent');
|
||||
|
||||
public static function generateNewPHID($type) {
|
||||
if (!$type) {
|
||||
throw new Exception("Can not generate PHID with no type.");
|
||||
}
|
||||
|
||||
$uniq = Filesystem::readRandomCharacters(20);
|
||||
$phid = 'PHID-'.$type.'-'.$uniq;
|
||||
|
||||
$phid_rec = new PhabricatorPHID();
|
||||
$phid_rec->setPHIDType($type);
|
||||
$phid_rec->setOwnerPHID($owner);
|
||||
$phid_rec->setParentPHID($parent);
|
||||
$phid_rec->setPHID($phid);
|
||||
$phid_rec->save();
|
||||
|
||||
return $phid;
|
||||
return 'PHID-'.$type.'-'.$uniq;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -6,10 +6,7 @@
|
|||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/phid/storage/base');
|
||||
|
||||
phutil_require_module('phutil', 'filesystem');
|
||||
phutil_require_module('phutil', 'utils');
|
||||
|
||||
|
||||
phutil_require_source('PhabricatorPHID.php');
|
||||
|
|
|
@ -83,6 +83,10 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
|
|||
'type' => 'db',
|
||||
'name' => 'flag',
|
||||
),
|
||||
'db.harbormaster' => array(
|
||||
'type' => 'db',
|
||||
'name' => 'harbormaster',
|
||||
),
|
||||
'db.herald' => array(
|
||||
'type' => 'db',
|
||||
'name' => 'herald',
|
||||
|
@ -115,10 +119,6 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
|
|||
'type' => 'db',
|
||||
'name' => 'phame',
|
||||
),
|
||||
'db.phid' => array(
|
||||
'type' => 'db',
|
||||
'name' => 'phid',
|
||||
),
|
||||
'db.phriction' => array(
|
||||
'type' => 'db',
|
||||
'name' => 'phriction',
|
||||
|
@ -871,6 +871,14 @@ final class PhabricatorBuiltinPatchList extends PhabricatorSQLPatchList {
|
|||
'type' => 'sql',
|
||||
'name' => $this->getPatchPath('emailtableremove.sql'),
|
||||
),
|
||||
'phiddrop.sql' => array(
|
||||
'type' => 'sql',
|
||||
'name' => $this->getPatchPath('phiddrop.sql'),
|
||||
),
|
||||
'testdatabase.sql' => array(
|
||||
'type' => 'sql',
|
||||
'name' => $this->getPatchPath('testdatabase.sql'),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -29,15 +29,10 @@ final class AphrontIsolatedDatabaseConnectionTestCase
|
|||
}
|
||||
|
||||
public function testIsolation() {
|
||||
$conn = $this->newIsolatedConnection();
|
||||
$test_phid = $this->generateTestPHID();
|
||||
|
||||
// This will fail if the connection isn't isolated.
|
||||
queryfx(
|
||||
$conn,
|
||||
'INSERT INTO phabricator_phid.phid (phid) VALUES (%s)',
|
||||
$test_phid);
|
||||
|
||||
$this->assertNoSuchPHID($test_phid);
|
||||
$this->newIsolatedConnection(),
|
||||
'INSERT INVALID SYNTAX');
|
||||
}
|
||||
|
||||
public function testInsertGeneratesID() {
|
||||
|
@ -116,23 +111,21 @@ final class AphrontIsolatedDatabaseConnectionTestCase
|
|||
public function testTransactionRollback() {
|
||||
$check = array();
|
||||
|
||||
$phid = new PhabricatorPHID();
|
||||
$phid = new HarbormasterScratchTable();
|
||||
$phid->openTransaction();
|
||||
for ($ii = 0; $ii < 3; $ii++) {
|
||||
$test_phid = $this->generateTestPHID();
|
||||
$key = $this->generateTestData();
|
||||
|
||||
$obj = new PhabricatorPHID();
|
||||
$obj->setPHID($test_phid);
|
||||
$obj->setPHIDType('TEST');
|
||||
$obj->setOwnerPHID('PHID-UNIT-!!!!');
|
||||
$obj = new HarbormasterScratchTable();
|
||||
$obj->setData($key);
|
||||
$obj->save();
|
||||
|
||||
$check[] = $test_phid;
|
||||
$check[] = $key;
|
||||
}
|
||||
$phid->killTransaction();
|
||||
|
||||
foreach ($check as $test_phid) {
|
||||
$this->assertNoSuchPHID($test_phid);
|
||||
foreach ($check as $key) {
|
||||
$this->assertNoSuchRow($key);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -141,19 +134,19 @@ final class AphrontIsolatedDatabaseConnectionTestCase
|
|||
return new AphrontIsolatedDatabaseConnection($config);
|
||||
}
|
||||
|
||||
private function generateTestPHID() {
|
||||
return 'PHID-TEST-'.Filesystem::readRandomCharacters(20);
|
||||
private function generateTestData() {
|
||||
return Filesystem::readRandomCharacters(20);
|
||||
}
|
||||
|
||||
private function assertNoSuchPHID($phid) {
|
||||
private function assertNoSuchRow($data) {
|
||||
try {
|
||||
$real_phid = id(new PhabricatorPHID())->loadOneWhere(
|
||||
'phid = %s',
|
||||
$phid);
|
||||
$row = id(new HarbormasterScratchTable())->loadOneWhere(
|
||||
'data = %s',
|
||||
$data);
|
||||
$this->assertEqual(
|
||||
null,
|
||||
$real_phid,
|
||||
'Expect fake PHID to exist only in isolation.');
|
||||
$row,
|
||||
'Expect fake row to exist only in isolation.');
|
||||
} catch (AphrontQueryConnectionException $ex) {
|
||||
// If we can't connect to the database, conclude that the isolated
|
||||
// connection actually is isolated. Philosophically, this perhaps allows
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/phid/storage/phid');
|
||||
phutil_require_module('phabricator', 'applications/harbormaster/storage/scratchtable');
|
||||
phutil_require_module('phabricator', 'infrastructure/testing/testcase');
|
||||
phutil_require_module('phabricator', 'storage/connection/isolated');
|
||||
phutil_require_module('phabricator', 'storage/queryfx');
|
||||
|
|
|
@ -27,7 +27,7 @@ final class AphrontMySQLDatabaseConnectionTestCase
|
|||
}
|
||||
|
||||
public function testConnectionFailures() {
|
||||
$conn = id(new PhabricatorPHID())->establishConnection('r');
|
||||
$conn = id(new HarbormasterScratchTable())->establishConnection('r');
|
||||
|
||||
queryfx($conn, 'SELECT 1');
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/phid/storage/phid');
|
||||
phutil_require_module('phabricator', 'applications/harbormaster/storage/scratchtable');
|
||||
phutil_require_module('phabricator', 'infrastructure/testing/testcase');
|
||||
phutil_require_module('phabricator', 'storage/queryfx');
|
||||
|
||||
|
|
|
@ -29,38 +29,35 @@ final class LiskFixtureTestCase extends PhabricatorTestCase {
|
|||
// If the user from either test persists, the other test will fail.
|
||||
$this->assertEqual(
|
||||
0,
|
||||
count(id(new PhabricatorUser())->loadAll()));
|
||||
count(id(new HarbormasterScratchTable())->loadAll()));
|
||||
|
||||
id(new PhabricatorUser())
|
||||
->setUserName('alincoln')
|
||||
->setRealName('Abraham Lincoln')
|
||||
id(new HarbormasterScratchTable())
|
||||
->setData('alincoln')
|
||||
->save();
|
||||
}
|
||||
|
||||
public function testTransactionalIsolation2of2() {
|
||||
$this->assertEqual(
|
||||
0,
|
||||
count(id(new PhabricatorUser())->loadAll()));
|
||||
count(id(new HarbormasterScratchTable())->loadAll()));
|
||||
|
||||
id(new PhabricatorUser())
|
||||
->setUserName('ugrant')
|
||||
->setRealName('Ulysses S. Grant')
|
||||
id(new HarbormasterScratchTable())
|
||||
->setData('ugrant')
|
||||
->save();
|
||||
}
|
||||
|
||||
public function testFixturesBasicallyWork() {
|
||||
$this->assertEqual(
|
||||
0,
|
||||
count(id(new PhabricatorUser())->loadAll()));
|
||||
count(id(new HarbormasterScratchTable())->loadAll()));
|
||||
|
||||
id(new PhabricatorUser())
|
||||
->setUserName('gwashington')
|
||||
->setRealName('George Washington')
|
||||
id(new HarbormasterScratchTable())
|
||||
->setData('gwashington')
|
||||
->save();
|
||||
|
||||
$this->assertEqual(
|
||||
1,
|
||||
count(id(new PhabricatorUser())->loadAll()));
|
||||
count(id(new HarbormasterScratchTable())->loadAll()));
|
||||
}
|
||||
|
||||
public function testReadableTransactions() {
|
||||
|
@ -70,18 +67,17 @@ final class LiskFixtureTestCase extends PhabricatorTestCase {
|
|||
LiskDAO::endIsolateAllLiskEffectsToTransactions();
|
||||
try {
|
||||
|
||||
$phid = 'PHID-TEST-'.Filesystem::readRandomCharacters(32);
|
||||
$data = Filesystem::readRandomCharacters(32);
|
||||
|
||||
$obj = new PhabricatorPHID();
|
||||
$obj = new HarbormasterScratchTable();
|
||||
$obj->openTransaction();
|
||||
|
||||
$obj->setPHID($phid);
|
||||
$obj->setPHIDType('TEST');
|
||||
$obj->setData($data);
|
||||
$obj->save();
|
||||
|
||||
$loaded = id(new PhabricatorPHID())->loadOneWhere(
|
||||
'phid = %s',
|
||||
$phid);
|
||||
$loaded = id(new HarbormasterScratchTable())->loadOneWhere(
|
||||
'data = %s',
|
||||
$data);
|
||||
|
||||
$obj->killTransaction();
|
||||
|
||||
|
|
|
@ -6,7 +6,7 @@
|
|||
|
||||
|
||||
|
||||
phutil_require_module('phabricator', 'applications/people/storage/user');
|
||||
phutil_require_module('phabricator', 'applications/harbormaster/storage/scratchtable');
|
||||
phutil_require_module('phabricator', 'applications/phid/storage/phid');
|
||||
phutil_require_module('phabricator', 'infrastructure/testing/testcase');
|
||||
phutil_require_module('phabricator', 'storage/lisk/dao');
|
||||
|
|
Loading…
Reference in a new issue