Skip to content
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

Reintroduce postgres support #560

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 63 additions & 3 deletions ci/serverctl.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def __init__(self, topdir, projectdir, datadir, fileserver, db='sqlite3', seaf_s
def setup(self):
if self.db == 'mysql':
create_mysql_dbs()
elif self.db == 'pgsql':
create_pgsql_dbs()

os.mkdir (self.central_conf_dir, 0o755)
os.mkdir (self.seafile_conf_dir, 0o755)
Expand All @@ -59,7 +61,9 @@ def setup(self):

def init_ccnet(self):
if self.db == 'mysql':
self.add_ccnet_db_conf()
self.add_ccnet_mysql_db_conf()
elif self.db == 'pgsql':
self.add_ccnet_pgsql_db_conf()
else:
self.add_ccnet_sqlite_db_conf()

Expand All @@ -72,7 +76,7 @@ def add_ccnet_sqlite_db_conf(self):
fp.write('\n')
fp.write(ccnet_db_conf)

def add_ccnet_db_conf(self):
def add_ccnet_mysql_db_conf(self):
ccnet_conf = join(self.central_conf_dir, 'ccnet.conf')
ccnet_db_conf = '''\
[Database]
Expand All @@ -83,6 +87,21 @@ def add_ccnet_db_conf(self):
PASSWD = seafile
DB = ccnet
CONNECTION_CHARSET = utf8
'''
with open(ccnet_conf, 'a+') as fp:
fp.write('\n')
fp.write(ccnet_db_conf)

def add_ccnet_pgsql_db_conf(self):
ccnet_conf = join(self.central_conf_dir, 'ccnet.conf')
ccnet_db_conf = '''\
[Database]
ENGINE = pgsql
HOST = 127.0.0.1
PORT = 5432
USER = seafile
PASSWD = seafile
DB = ccnet
'''
with open(ccnet_conf, 'a+') as fp:
fp.write('\n')
Expand Down Expand Up @@ -119,7 +138,7 @@ def add_seafile_sqlite_db_conf(self):
fp.write('\n')
fp.write(seafile_db_conf)

def add_seafile_db_conf(self):
def add_seafile_mysql_db_conf(self):
seafile_conf = join(self.central_conf_dir, 'seafile.conf')
seafile_db_conf = '''\
[database]
Expand All @@ -130,6 +149,21 @@ def add_seafile_db_conf(self):
password = seafile
db_name = seafile
connection_charset = utf8
'''
with open(seafile_conf, 'a+') as fp:
fp.write('\n')
fp.write(seafile_db_conf)

def add_seafile_pgsql_db_conf(self):
seafile_conf = join(self.central_conf_dir, 'seafile.conf')
seafile_db_conf = '''\
[database]
type = pgsql
host = 127.0.0.1
port = 5432
user = seafile
password = seafile
db_name = seafile
'''
with open(seafile_conf, 'a+') as fp:
fp.write('\n')
Expand Down Expand Up @@ -169,6 +203,11 @@ def create_database_tables(self):
seafile_sql_path = join(self.sql_dir, 'mysql', 'seafile.sql')
sql = f'USE ccnet; source {ccnet_sql_path}; USE seafile; source {seafile_sql_path};'.encode()
shell('sudo mysql -u root -proot', inputdata=sql, wait=False)
elif self.db == 'pgsql':
ccnet_sql_path = join(self.sql_dir, 'pgsql', 'ccnet.sql')
seafile_sql_path = join(self.sql_dir, 'pgsql', 'seafile.sql')
shell(f'sudo psql "postgres://seafile:seafile@localhost/ccnet" -f {ccnet_sql_path}', wait=False)
shell(f'sudo psql "postgres://seafile:seafile@localhost/ccnet" -f {seafile_sql_path}', wait=False)
else:
config_sql_path = join(self.sql_dir, 'sqlite', 'config.sql')
groupmgr_sql_path = join(self.sql_dir, 'sqlite', 'groupmgr.sql')
Expand Down Expand Up @@ -257,6 +296,8 @@ def stop(self):
self.fileserver_proc.kill()
if self.db == 'mysql':
del_mysql_dbs()
if self.db == 'pgsql':
del_pgsql_dbs()

def get_seaserv_envs(self):
envs = dict(os.environ)
Expand Down Expand Up @@ -289,3 +330,22 @@ def del_mysql_dbs():
'''

shell('sudo mysql -u root -proot', inputdata=sql)


def create_pgsql_dbs():
sql = b'''\
create user "seafile" with password 'seafile';
create database "ccnet" with owner "seafile" encoding 'utf8';
create database "seafile" with owner "seafile" encoding 'utf8';
'''

shell('sudo psql -u root -proot', inputdata=sql)

def del_pgsql_dbs():
sql = b'''\
drop database "ccnet";
drop database "seafile";
drop user "seafile";
'''

shell('sudo psql -u root -proot', inputdata=sql)
46 changes: 18 additions & 28 deletions common/branch-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,14 @@ open_db (SeafBranchManager *mgr)
return -1;
break;
case SEAF_DB_TYPE_PGSQL:
sql = "CREATE TABLE IF NOT EXISTS Branch ("
"name VARCHAR(10), repo_id CHAR(40), commit_id CHAR(40),"
"PRIMARY KEY (repo_id, name))";
sql = "CREATE TABLE IF NOT EXISTS Branch("
" id BIGSERIAL PRIMARY KEY,"
" name VARCHAR(10),"
" repo_id CHAR(41),"
" commit_id CHAR(41));";
if (seaf_db_query (mgr->seaf->db, sql) < 0)
return -1;
sql = "CREATE UNIQUE INDEX IF NOT EXISTS branch_repoidname_idx ON Branch (repo_id, name);";
if (seaf_db_query (mgr->seaf->db, sql) < 0)
return -1;
break;
Expand Down Expand Up @@ -193,33 +198,18 @@ seaf_branch_manager_add_branch (SeafBranchManager *mgr, SeafBranch *branch)

return 0;
#else
char *sql;
SeafDB *db = mgr->seaf->db;

if (seaf_db_type(db) == SEAF_DB_TYPE_PGSQL) {
gboolean exists, err;
int rc;

sql = "SELECT repo_id FROM Branch WHERE name=? AND repo_id=?";
exists = seaf_db_statement_exists(db, sql, &err,
2, "string", branch->name,
"string", branch->repo_id);
if (err)
return -1;

if (exists)
rc = seaf_db_statement_query (db,
"UPDATE Branch SET commit_id=? "
"WHERE name=? AND repo_id=?",
3, "string", branch->commit_id,
"string", branch->name,
"string", branch->repo_id);
else
rc = seaf_db_statement_query (db,
"INSERT INTO Branch (name, repo_id, commit_id) VALUES (?, ?, ?)",
3, "string", branch->name,
"string", branch->repo_id,
"string", branch->commit_id);
int rc = seaf_db_statement_query (db,
"INSERT INTO Branch (name, repo_id, commit_id)"
" VALUES (?, ?, ?)"
" ON CONFLICT (name, repo_id)"
" DO UPDATE SET commit_id=?",
4, "string", branch->name,
"string", branch->repo_id,
"string", branch->commit_id,
"string", branch->commit_id);
if (rc < 0)
return -1;
} else {
Expand Down Expand Up @@ -349,10 +339,10 @@ seaf_branch_manager_test_and_update_branch (SeafBranchManager *mgr,

switch (seaf_db_type (mgr->seaf->db)) {
case SEAF_DB_TYPE_MYSQL:
case SEAF_DB_TYPE_PGSQL:
sql = "SELECT commit_id FROM Branch WHERE name=? "
"AND repo_id=? FOR UPDATE";
break;
case SEAF_DB_TYPE_PGSQL:
case SEAF_DB_TYPE_SQLITE:
sql = "SELECT commit_id FROM Branch WHERE name=? "
"AND repo_id=?";
Expand Down
35 changes: 25 additions & 10 deletions common/config-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,31 @@ seaf_cfg_manager_init (SeafCfgManager *mgr)
char *sql;
int db_type = seaf_db_type(mgr->db);

if (db_type == SEAF_DB_TYPE_MYSQL)
sql = "CREATE TABLE IF NOT EXISTS SeafileConf ("
"id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, cfg_group VARCHAR(255) NOT NULL,"
"cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER) ENGINE=INNODB";
else
sql = "CREATE TABLE IF NOT EXISTS SeafileConf (cfg_group VARCHAR(255) NOT NULL,"
"cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER)";

if (seaf_db_query (mgr->db, sql) < 0)
return -1;
switch (db_type) {
case SEAF_DB_TYPE_MYSQL:
sql = "CREATE TABLE IF NOT EXISTS SeafileConf ("
"id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT, cfg_group VARCHAR(255) NOT NULL,"
"cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER) ENGINE=INNODB";
if (seaf_db_query (mgr->db, sql) < 0)
return -1;
break;
case SEAF_DB_TYPE_PGSQL:
sql = "CREATE TABLE IF NOT EXISTS SeafileConf("
" id BIGSERIAL PRIMARY KEY,"
" cfg_group VARCHAR(255) NOT NULL,"
" cfg_key VARCHAR(255) NOT NULL,"
" value VARCHAR(255),"
" property INTEGER);";
if (seaf_db_query (mgr->db, sql) < 0)
return -1;
break;
case SEAF_DB_TYPE_SQLITE:
sql = "CREATE TABLE IF NOT EXISTS SeafileConf (cfg_group VARCHAR(255) NOT NULL,"
"cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER)";
if (seaf_db_query (mgr->db, sql) < 0)
return -1;
break;
}

return 0;
}
Expand Down
57 changes: 32 additions & 25 deletions common/group-mgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,46 +185,53 @@ static int check_db_table (CcnetGroupManager *manager, CcnetDB *db)
"`GroupStructure` (`path`)";
if (seaf_db_query (db, sql) < 0)
return -1;

} else if (db_type == SEAF_DB_TYPE_PGSQL) {
g_string_printf (group_sql,
"CREATE TABLE IF NOT EXISTS \"%s\" (group_id SERIAL"
" PRIMARY KEY, group_name VARCHAR(255),"
" creator_name VARCHAR(255), timestamp BIGINT,"
" type VARCHAR(32), parent_group_id INTEGER)", table_name);
"CREATE TABLE IF NOT EXISTS \"%s\" ("
" group_id BIGSERIAL PRIMARY KEY,"
" group_name VARCHAR(255),"
" creator_name VARCHAR(255),"
" timestamp BIGINT,"
" type VARCHAR(32),"
" parent_group_id INTEGER)", table_name);
if (seaf_db_query (db, group_sql->str) < 0) {
g_string_free (group_sql, TRUE);
return -1;
}

sql = "CREATE TABLE IF NOT EXISTS GroupUser (group_id INTEGER,"
" user_name VARCHAR(255), is_staff smallint, UNIQUE "
" (group_id, user_name))";
sql = "CREATE TABLE IF NOT EXISTS GroupUser ("
" id BIGSERIAL PRIMARY KEY,"
" group_id BIGINT,"
" user_name VARCHAR(255),"
" is_staff SMALLINT)";
if (seaf_db_query (db, sql) < 0)
return -1;

//if (!pgsql_index_exists (db, "groupuser_username_idx")) {
// sql = "CREATE INDEX groupuser_username_idx ON GroupUser (user_name)";
// if (seaf_db_query (db, sql) < 0)
// return -1;
//}

sql = "CREATE TABLE IF NOT EXISTS GroupDNPair (group_id INTEGER,"
" dn VARCHAR(255))";
sql = "CREATE UNIQUE INDEX IF NOT EXISTS groupuser_group_id_user_name_key ON GroupUser (group_id, user_name)";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE TABLE IF NOT EXISTS GroupStructure (group_id INTEGER PRIMARY KEY, "
"path VARCHAR(1024))";
sql = "CREATE INDEX IF NOT EXISTS groupuser_username_idx ON GroupUser (user_name)";
if (seaf_db_query (db, sql) < 0)
return -1;

//if (!pgsql_index_exists (db, "structure_path_idx")) {
// sql = "CREATE INDEX structure_path_idx ON GroupStructure (path)";
// if (seaf_db_query (db, sql) < 0)
// return -1;
//}
sql = "CREATE TABLE IF NOT EXISTS GroupDNPair ("
" id BIGSERIAL PRIMARY KEY,"
" group_id INTEGER,"
" dn VARCHAR(255))";
if (seaf_db_query (db, sql) < 0)
return -1;

sql = "CREATE TABLE IF NOT EXISTS GroupStructure ("
" id BIGSERIAL PRIMARY KEY,"
" group_id INTEGER,"
" path VARCHAR(1024));";
if (seaf_db_query (db, sql) < 0)
return -1;
sql = "CREATE UNIQUE INDEX IF NOT EXISTS groupstructure_groupid_idx ON GroupStructure (group_id)";
if (seaf_db_query (db, sql) < 0)
return -1;
sql = "CREATE INDEX IF NOT EXISTS structure_path_idx ON GroupStructure (path)";
if (seaf_db_query (db, sql) < 0)
return -1;
}
g_string_free (group_sql, TRUE);

Expand Down
Loading