我试着编写可以重置postgresql数据库中的表的函数,但它总是让我想起
错误:超出5000毫秒的超时。确保在此测试中调用done()回调。 在mocha.js:4455:19
我的代码如下。我该如何解决这个问题?
function reset_database($dbh) {
$query = "DROP TABLE IF EXISTS 'users', 'posts', 'voting', 'hashtag';";
$result = pg_query($dbh, $query);
if (!result) {
return array( 'status' => 0 );
}
else {
return array( 'status' => 1 );
$query = "CREATE TABLE users(username VARCHAR(50) PRIMARY KEY, password VARCHAR(32) NOT NULL);";
$result = pg_query($dbh, $query);
if (!result) {
return array( 'status' => 0 );
}
else {
return array( 'status' => 1 );
$query = "CREATE TABLE posts(pID serial, username VARCHAR(50) NOT NULL, title VARCHAR(20) NOT NULL, content VARCHAR(42),time timestamp,coorX INTEGER,coorY INTEGER, PRIMARY KEY(pID),FOREIGN KEY(username) REFERENCES users(username) ON DELETE CASCADE);";
$result = pg_query($dbh, $query);
if (!result) {
return array( 'status' => 0 );
}
else {
return array( 'status' => 1 );
$query = "CREATE TABLE voting(username VARCHAR(50) NOT NULL,pID INTEGER NOT NULL,PRIMARY KEY(username, pID),FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE,FOREIGN KEY (pID) REFERENCES posts ON DELETE CASCADE);";
$result = pg_query($dbh, $query);
if (!result) {
return array( 'status' => 0 );
}
else {
return array( 'status' => 1 );
$query = "CREATE TABLE hashtag(tag VARCHAR(42) NOT NULL,pID INTEGER NOT NULL,PRIMARY KEY(tag),FOREIGN KEY(pID) REFERENCES posts(pID) ON DELETE CASCADE);";
$result = pg_query($dbh, $query);
if (!result) {
return array( 'status' => 0 );
}
else {
return array( 'status' => 1 );
}
}
答案 0 :(得分:0)
您可以使用TRUNCATE
命令:
$query = "TRUNCATE ONLY users, posts, voting, hashtag RESTART IDENTITY";
$result = pg_query($dbh, $query);
if (!result) {
return array( 'status' => 0 );
} else {
return array( 'status' => 1 );
}
参考:https://www.postgresql.org/docs/current/static/sql-truncate.html