diff --git a/ci/serverctl.py b/ci/serverctl.py index 03e6c09..e061b6d 100755 --- a/ci/serverctl.py +++ b/ci/serverctl.py @@ -62,6 +62,18 @@ class ServerCtl(object): shell(cmd) if self.db == 'mysql': self.add_ccnet_db_conf() + else: + self.add_ccnet_sqlite_db_conf() + + def add_ccnet_sqlite_db_conf(self): + ccnet_conf = join(self.central_conf_dir, 'ccnet.conf') + ccnet_db_conf = '''\ +[Database] +CREATE_TABLES = true +''' + with open(ccnet_conf, 'a+') as fp: + fp.write('\n') + fp.write(ccnet_db_conf) def add_ccnet_db_conf(self): ccnet_conf = join(self.central_conf_dir, 'ccnet.conf') @@ -74,6 +86,7 @@ USER = seafile PASSWD = seafile DB = ccnet CONNECTION_CHARSET = utf8 +CREATE_TABLES = true ''' with open(ccnet_conf, 'a+') as fp: fp.write('\n') @@ -93,6 +106,18 @@ CONNECTION_CHARSET = utf8 shell(cmd) if self.db == 'mysql': self.add_seafile_db_conf() + else: + self.add_seafile_sqlite_db_conf() + + def add_seafile_sqlite_db_conf(self): + seafile_conf = join(self.central_conf_dir, 'seafile.conf') + seafile_db_conf = '''\ +[database] +create_tables = true +''' + with open(seafile_conf, 'a+') as fp: + fp.write('\n') + fp.write(seafile_db_conf) def add_seafile_db_conf(self): seafile_conf = join(self.central_conf_dir, 'seafile.conf') @@ -105,6 +130,7 @@ user = seafile password = seafile db_name = seafile connection_charset = utf8 +create_tables = true ''' with open(seafile_conf, 'a+') as fp: fp.write('\n') diff --git a/common/branch-mgr.c b/common/branch-mgr.c index e3c0a5f..41391e7 100644 --- a/common/branch-mgr.c +++ b/common/branch-mgr.c @@ -127,7 +127,7 @@ seaf_branch_manager_init (SeafBranchManager *mgr) static int open_db (SeafBranchManager *mgr) { - if (!mgr->seaf->create_tables && seaf_db_type (mgr->seaf->db) == SEAF_DB_TYPE_MYSQL) + if (!mgr->seaf->create_tables && seaf_db_type (mgr->seaf->db) != SEAF_DB_TYPE_PGSQL) return 0; #ifndef SEAFILE_SERVER diff --git a/common/seaf-utils.c b/common/seaf-utils.c index b581cea..ef1c750 100644 --- a/common/seaf-utils.c +++ b/common/seaf-utils.c @@ -61,7 +61,6 @@ mysql_db_start (SeafileSession *session) char *host, *user, *passwd, *db, *unix_socket, *charset; int port; gboolean use_ssl = FALSE; - gboolean create_tables = TRUE; int max_connections = 0; GError *error = NULL; @@ -100,12 +99,6 @@ mysql_db_start (SeafileSession *session) use_ssl = g_key_file_get_boolean (session->config, "database", "use_ssl", NULL); - if (g_key_file_has_key (session->config, "database", "create_tables", NULL)) { - create_tables = g_key_file_get_boolean (session->config, - "database", "create_tables", NULL); - } - session->create_tables = create_tables; - charset = seaf_key_file_get_string (session->config, "database", "connection_charset", NULL); @@ -201,6 +194,7 @@ load_database_config (SeafileSession *session) char *type; GError *error = NULL; int ret = 0; + gboolean create_tables = FALSE; type = seaf_key_file_get_string (session->config, "database", "type", &error); /* Default to use sqlite if not set. */ @@ -221,6 +215,12 @@ load_database_config (SeafileSession *session) seaf_warning ("Unsupported db type %s.\n", type); ret = -1; } + if (ret == 0) { + if (g_key_file_has_key (session->config, "database", "create_tables", NULL)) + create_tables = g_key_file_get_boolean (session->config, + "database", "create_tables", NULL); + session->create_tables = create_tables; + } g_free (type); diff --git a/scripts/build/build-server.py b/scripts/build/build-server.py index 9a18fae..5cb5a67 100755 --- a/scripts/build/build-server.py +++ b/scripts/build/build-server.py @@ -628,6 +628,14 @@ def copy_scripts_and_libs(): except Exception, e: error('failed to copy upgrade scripts: %s' % e) + # copy sql scripts + sql_scriptsdir = os.path.join(scripts_srcdir, 'sql') + dst_sql_scriptsdir = os.path.join(serverdir, 'sql') + try: + shutil.copytree(sql_scriptsdir, dst_sql_scriptsdir) + except Exception, e: + error('failed to copy sql scripts: %s' % e) + # copy runtime/seahub.conf runtimedir = os.path.join(serverdir, 'runtime') must_mkdir(runtimedir) diff --git a/scripts/setup-seafile-mysql.py b/scripts/setup-seafile-mysql.py index cafb95d..035befd 100644 --- a/scripts/setup-seafile-mysql.py +++ b/scripts/setup-seafile-mysql.py @@ -881,6 +881,41 @@ class CcnetConfigurator(AbstractConfigurator): default=default, validate=validate) + def do_syncdb(self): + print '----------------------------------------' + print 'Now creating ccnet database tables ...\n' + print '----------------------------------------' + + try: + conn = MySQLdb.connect(host=db_config.mysql_host, + port=db_config.mysql_port, + user=db_config.seafile_mysql_user, + passwd=db_config.seafile_mysql_password, + db=db_config.ccnet_db_name) + except Exception, e: + if isinstance(e, MySQLdb.OperationalError): + Utils.error('Failed to connect to mysql database %s: %s' % (db_config.ccnet_db_name, e.args[1])) + else: + Utils.error('Failed to connect to mysql database %s: %s' % (db_config.ccnet_db_name, e)) + + cursor = conn.cursor() + + sql_file = os.path.join(env_mgr.install_path, 'sql', 'mysql', 'ccnet.sql') + with open(sql_file, 'r') as fp: + content = fp.read() + + sqls = [line.strip() for line in content.split(';') if line.strip()] + for sql in sqls: + try: + cursor.execute(sql) + except Exception, e: + if isinstance(e, MySQLdb.OperationalError): + Utils.error('Failed to init ccnet database: %s' % e.args[1]) + else: + Utils.error('Failed to init ccnet database: %s' % e) + + conn.commit() + class SeafileConfigurator(AbstractConfigurator): def __init__(self): @@ -983,6 +1018,41 @@ class SeafileConfigurator(AbstractConfigurator): with open(seafile_ini, 'w') as fp: fp.write(self.seafile_dir) + def do_syncdb(self): + print '----------------------------------------' + print 'Now creating seafile database tables ...\n' + print '----------------------------------------' + + try: + conn = MySQLdb.connect(host=db_config.mysql_host, + port=db_config.mysql_port, + user=db_config.seafile_mysql_user, + passwd=db_config.seafile_mysql_password, + db=db_config.seafile_db_name) + except Exception, e: + if isinstance(e, MySQLdb.OperationalError): + Utils.error('Failed to connect to mysql database %s: %s' % (db_config.seafile_db_name, e.args[1])) + else: + Utils.error('Failed to connect to mysql database %s: %s' % (db_config.seafile_db_name, e)) + + cursor = conn.cursor() + + sql_file = os.path.join(env_mgr.install_path, 'sql', 'mysql', 'seafile.sql') + with open(sql_file, 'r') as fp: + content = fp.read() + + sqls = [line.strip() for line in content.split(';') if line.strip()] + for sql in sqls: + try: + cursor.execute(sql) + except Exception, e: + if isinstance(e, MySQLdb.OperationalError): + Utils.error('Failed to init seafile database: %s' % e.args[1]) + else: + Utils.error('Failed to init seafile database: %s' % e) + + conn.commit() + class SeahubConfigurator(AbstractConfigurator): def __init__(self): AbstractConfigurator.__init__(self) @@ -1465,6 +1535,8 @@ def main(): gunicorn_config.generate() seahub_config.generate() + ccnet_config.do_syncdb() + seafile_config.do_syncdb() seahub_config.do_syncdb() seahub_config.prepare_avatar_dir() # db_config.create_seahub_admin() diff --git a/scripts/setup-seafile.sh b/scripts/setup-seafile.sh index e4811a5..8c047b2 100755 --- a/scripts/setup-seafile.sh +++ b/scripts/setup-seafile.sh @@ -663,12 +663,48 @@ function get_seahub_admin_passwd () { # fi # fi -echo "Creating seahub database now, it may take one minute, please wait... " +echo "Creating database now, it may take one minute, please wait... " echo +cd ${TOPDIR}/ccnet && mkdir -m 0755 GroupMgr misc OrgMgr PeerMgr && cd - + +ccnet_group_db=${TOPDIR}/ccnet/GroupMgr/groupmgr.db +ccnet_group_sql=${INSTALLPATH}/sql/sqlite/groupmgr.sql +if ! sqlite3 ${ccnet_group_db} ".read ${ccnet_group_sql}" 2>/dev/null 1>&2; then + echo "Failed to sync ccnet groupmgr database." + err_and_quit; +fi + +ccnet_config_db=${TOPDIR}/ccnet/misc/config.db +ccnet_config_sql=${INSTALLPATH}/sql/sqlite/config.sql +if ! sqlite3 ${ccnet_config_db} ".read ${ccnet_config_sql}" 2>/dev/null 1>&2; then + echo "Failed to sync ccnet config database." + err_and_quit; +fi + +ccnet_org_db=${TOPDIR}/ccnet/OrgMgr/orgmgr.db +ccnet_org_sql=${INSTALLPATH}/sql/sqlite/org.sql +if ! sqlite3 ${ccnet_org_db} ".read ${ccnet_org_sql}" 2>/dev/null 1>&2; then + echo "Failed to sync ccnet org database." + err_and_quit; +fi + +ccnet_user_db=${TOPDIR}/ccnet/PeerMgr/usermgr.db +ccnet_user_sql=${INSTALLPATH}/sql/sqlite/user.sql +if ! sqlite3 ${ccnet_user_db} ".read ${ccnet_user_sql}" 2>/dev/null 1>&2; then + echo "Failed to sync ccnet user database." + err_and_quit; +fi + +seafile_db=${TOPDIR}/seafile-data/seafile.db +seafile_sql=${INSTALLPATH}/sql/sqlite/seafile.sql +if ! sqlite3 ${seafile_db} ".read ${seafile_sql}" 2>/dev/null 1>&2; then + echo "Failed to sync seafile database." + err_and_quit; +fi + seahub_db=${TOPDIR}/seahub.db seahub_sqls=${INSTALLPATH}/seahub/sql/sqlite3.sql - if ! sqlite3 ${seahub_db} ".read ${seahub_sqls}" 2>/dev/null 1>&2; then echo "Failed to sync seahub database." err_and_quit; diff --git a/scripts/sql/mysql/ccnet.sql b/scripts/sql/mysql/ccnet.sql new file mode 100644 index 0000000..7e09e85 --- /dev/null +++ b/scripts/sql/mysql/ccnet.sql @@ -0,0 +1,115 @@ +CREATE TABLE `Group` ( + `group_id` bigint(20) NOT NULL AUTO_INCREMENT, + `group_name` varchar(255) DEFAULT NULL, + `creator_name` varchar(255) DEFAULT NULL, + `timestamp` bigint(20) DEFAULT NULL, + `type` varchar(32) DEFAULT NULL, + `parent_group_id` int(11) DEFAULT NULL, + PRIMARY KEY (`group_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `GroupUser` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `group_id` bigint(20) DEFAULT NULL, + `user_name` varchar(255) DEFAULT NULL, + `is_staff` tinyint(4) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `group_id` (`group_id`,`user_name`), + KEY `user_name` (`user_name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `GroupDNPair` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `group_id` int(11) DEFAULT NULL, + `dn` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `GroupStructure` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `group_id` int(11) DEFAULT NULL, + `path` varchar(1024) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `group_id` (`group_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `OrgGroup` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `org_id` int(11) DEFAULT NULL, + `group_id` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `org_id` (`org_id`,`group_id`), + KEY `group_id` (`group_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `Organization` ( + `org_id` bigint(20) NOT NULL AUTO_INCREMENT, + `org_name` varchar(255) DEFAULT NULL, + `url_prefix` varchar(255) DEFAULT NULL, + `creator` varchar(255) DEFAULT NULL, + `ctime` bigint(20) DEFAULT NULL, + PRIMARY KEY (`org_id`), + UNIQUE KEY `url_prefix` (`url_prefix`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `OrgUser` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `org_id` int(11) DEFAULT NULL, + `email` varchar(255) DEFAULT NULL, + `is_staff` tinyint(1) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `org_id` (`org_id`,`email`), + KEY `email` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `Binding` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `email` varchar(255) DEFAULT NULL, + `peer_id` char(41) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `peer_id` (`peer_id`), + KEY `email` (`email`(20)) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `EmailUser` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `email` varchar(255) DEFAULT NULL, + `passwd` varchar(256) DEFAULT NULL, + `is_staff` tinyint(1) NOT NULL, + `is_active` tinyint(1) NOT NULL, + `ctime` bigint(20) DEFAULT NULL, + `reference_id` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `email` (`email`), + UNIQUE KEY `reference_id` (`reference_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `LDAPConfig` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `cfg_group` varchar(255) NOT NULL, + `cfg_key` varchar(255) NOT NULL, + `value` varchar(255) DEFAULT NULL, + `property` int(11) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `LDAPUsers` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `email` varchar(255) NOT NULL, + `password` varchar(255) NOT NULL, + `is_staff` tinyint(1) NOT NULL, + `is_active` tinyint(1) NOT NULL, + `extra_attrs` text, + `reference_id` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `email` (`email`), + UNIQUE KEY `reference_id` (`reference_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `UserRole` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `email` varchar(255) DEFAULT NULL, + `role` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `email` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/scripts/sql/mysql/seafile.sql b/scripts/sql/mysql/seafile.sql new file mode 100644 index 0000000..fe17e64 --- /dev/null +++ b/scripts/sql/mysql/seafile.sql @@ -0,0 +1,227 @@ +CREATE TABLE `Branch` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `name` varchar(10) DEFAULT NULL, + `repo_id` char(41) DEFAULT NULL, + `commit_id` char(41) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`,`name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `GarbageRepos` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(36) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `InnerPubRepo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `permission` char(15) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `OrgQuota` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `org_id` int(11) DEFAULT NULL, + `quota` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `org_id` (`org_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `OrgUserQuota` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `org_id` int(11) DEFAULT NULL, + `user` varchar(255) DEFAULT NULL, + `quota` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `org_id` (`org_id`,`user`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `Repo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoFileCount` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(36) DEFAULT NULL, + `file_count` bigint(20) unsigned DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoGroup` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `group_id` int(11) DEFAULT NULL, + `user_name` varchar(255) DEFAULT NULL, + `permission` char(15) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `group_id` (`group_id`,`repo_id`), + KEY `repo_id` (`repo_id`), + KEY `user_name` (`user_name`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoHead` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `branch_name` varchar(10) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoHistoryLimit` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `days` int(11) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoInfo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(36) DEFAULT NULL, + `name` varchar(255) NOT NULL, + `update_time` bigint(20) DEFAULT NULL, + `version` int(11) DEFAULT NULL, + `is_encrypted` int(11) DEFAULT NULL, + `last_modifier` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoOwner` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `owner_id` varchar(255) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`), + KEY `owner_id` (`owner_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoSize` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `size` bigint(20) unsigned DEFAULT NULL, + `head_id` char(41) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoTokenPeerInfo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `token` char(41) DEFAULT NULL, + `peer_id` char(41) DEFAULT NULL, + `peer_ip` varchar(41) DEFAULT NULL, + `peer_name` varchar(255) DEFAULT NULL, + `sync_time` bigint(20) DEFAULT NULL, + `client_ver` varchar(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `token` (`token`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoTrash` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(36) DEFAULT NULL, + `repo_name` varchar(255) DEFAULT NULL, + `head_id` char(40) DEFAULT NULL, + `owner_id` varchar(255) DEFAULT NULL, + `size` bigint(20) DEFAULT NULL, + `org_id` int(11) DEFAULT NULL, + `del_time` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`), + KEY `owner_id` (`owner_id`), + KEY `org_id` (`org_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoUserToken` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `email` varchar(255) DEFAULT NULL, + `token` char(41) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`,`token`), + KEY `email` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `RepoValidSince` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `timestamp` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `SeafileConf` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `cfg_group` varchar(255) NOT NULL, + `cfg_key` varchar(255) NOT NULL, + `value` varchar(255) DEFAULT NULL, + `property` int(11) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `SharedRepo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `from_email` varchar(255) DEFAULT NULL, + `to_email` varchar(255) DEFAULT NULL, + `permission` char(15) DEFAULT NULL, + PRIMARY KEY (`id`), + KEY `repo_id` (`repo_id`), + KEY `from_email` (`from_email`), + KEY `to_email` (`to_email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `SystemInfo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `info_key` varchar(256) DEFAULT NULL, + `info_value` varchar(1024) DEFAULT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `UserQuota` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user` varchar(255) DEFAULT NULL, + `quota` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user` (`user`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `UserShareQuota` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `user` varchar(255) DEFAULT NULL, + `quota` bigint(20) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `user` (`user`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `VirtualRepo` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(36) DEFAULT NULL, + `origin_repo` char(36) DEFAULT NULL, + `path` text, + `base_commit` char(40) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`), + KEY `origin_repo` (`origin_repo`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `WebAP` ( + `id` bigint(20) NOT NULL AUTO_INCREMENT, + `repo_id` char(37) DEFAULT NULL, + `access_property` char(10) DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `repo_id` (`repo_id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `WebUploadTempFiles` ( + `repo_id` char(40) NOT NULL, + `file_path` text NOT NULL, + `tmp_file_path` text NOT NULL +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/scripts/sql/sqlite/config.sql b/scripts/sql/sqlite/config.sql new file mode 100644 index 0000000..f96afb1 --- /dev/null +++ b/scripts/sql/sqlite/config.sql @@ -0,0 +1 @@ +CREATE TABLE Config (key TEXT PRIMARY KEY, value TEXT); diff --git a/scripts/sql/sqlite/groupmgr.sql b/scripts/sql/sqlite/groupmgr.sql new file mode 100644 index 0000000..fcae1de --- /dev/null +++ b/scripts/sql/sqlite/groupmgr.sql @@ -0,0 +1,9 @@ +CREATE TABLE `Group` (`group_id` INTEGER PRIMARY KEY AUTOINCREMENT, `group_name` VARCHAR(255), `creator_name` VARCHAR(255), `timestamp` BIGINT, `type` VARCHAR(32), `parent_group_id` INTEGER); + +CREATE TABLE `GroupUser` (`group_id` INTEGER, `user_name` VARCHAR(255), `is_staff` tinyint); +CREATE UNIQUE INDEX groupid_username_indx on `GroupUser` (`group_id`, `user_name`); +CREATE INDEX username_indx on `GroupUser` (`user_name`); + +CREATE TABLE GroupDNPair (group_id INTEGER, dn VARCHAR(255)); + +CREATE TABLE GroupStructure (group_id INTEGER PRIMARY KEY, path VARCHAR(1024)); diff --git a/scripts/sql/sqlite/org.sql b/scripts/sql/sqlite/org.sql new file mode 100644 index 0000000..555720d --- /dev/null +++ b/scripts/sql/sqlite/org.sql @@ -0,0 +1,10 @@ +CREATE TABLE OrgGroup (org_id INTEGER, group_id INTEGER); +CREATE INDEX groupid_indx on OrgGroup (group_id); +CREATE UNIQUE INDEX org_group_indx on OrgGroup (org_id, group_id); + +CREATE TABLE Organization (org_id INTEGER PRIMARY KEY AUTOINCREMENT, org_name VARCHAR(255), url_prefix VARCHAR(255), creator VARCHAR(255), ctime BIGINT); +CREATE UNIQUE INDEX url_prefix_indx on Organization (url_prefix); + +CREATE TABLE OrgUser (org_id INTEGER, email TEXT, is_staff bool NOT NULL); +CREATE INDEX email_indx on OrgUser (email); +CREATE UNIQUE INDEX orgid_email_indx on OrgUser (org_id, email); diff --git a/scripts/sql/sqlite/seafile.sql b/scripts/sql/sqlite/seafile.sql new file mode 100644 index 0000000..49c417b --- /dev/null +++ b/scripts/sql/sqlite/seafile.sql @@ -0,0 +1,61 @@ +CREATE TABLE Branch (name VARCHAR(10), repo_id CHAR(41), commit_id CHAR(41),PRIMARY KEY (repo_id, name)); + +CREATE TABLE Repo (repo_id CHAR(37) PRIMARY KEY); + +CREATE TABLE RepoOwner (repo_id CHAR(37) PRIMARY KEY, owner_id TEXT); +CREATE INDEX OwnerIndex ON RepoOwner (owner_id); + +CREATE TABLE RepoGroup (repo_id CHAR(37), group_id INTEGER, user_name TEXT, permission CHAR(15)); +CREATE UNIQUE INDEX groupid_repoid_indx on RepoGroup (group_id, repo_id); +CREATE INDEX repogroup_repoid_index on RepoGroup (repo_id); +CREATE INDEX repogroup_username_indx on RepoGroup (user_name); + +CREATE TABLE InnerPubRepo (repo_id CHAR(37) PRIMARY KEY,permission CHAR(15)); + +CREATE TABLE RepoUserToken (repo_id CHAR(37), email VARCHAR(255), token CHAR(41)); +CREATE UNIQUE INDEX repo_token_indx on RepoUserToken (repo_id, token); +CREATE INDEX repo_token_email_indx on RepoUserToken (email); + +CREATE TABLE RepoTokenPeerInfo (token CHAR(41) PRIMARY KEY, peer_id CHAR(41), peer_ip VARCHAR(41), peer_name VARCHAR(255), sync_time BIGINT, client_ver VARCHAR(20)); + +CREATE TABLE RepoHead (repo_id CHAR(37) PRIMARY KEY, branch_name VARCHAR(10)); + +CREATE TABLE RepoSize (repo_id CHAR(37) PRIMARY KEY,size BIGINT UNSIGNED,head_id CHAR(41)); + +CREATE TABLE RepoHistoryLimit (repo_id CHAR(37) PRIMARY KEY, days INTEGER); + +CREATE TABLE RepoValidSince (repo_id CHAR(37) PRIMARY KEY, timestamp BIGINT); + +CREATE TABLE WebAP (repo_id CHAR(37) PRIMARY KEY, access_property CHAR(10)); + +CREATE TABLE VirtualRepo (repo_id CHAR(36) PRIMARY KEY,origin_repo CHAR(36), path TEXT, base_commit CHAR(40)); +CREATE INDEX virtualrepo_origin_repo_idx ON VirtualRepo (origin_repo); + +CREATE TABLE GarbageRepos (repo_id CHAR(36) PRIMARY KEY); + +CREATE TABLE RepoTrash (repo_id CHAR(36) PRIMARY KEY,repo_name VARCHAR(255), head_id CHAR(40), owner_id VARCHAR(255), size BIGINT UNSIGNED,org_id INTEGER, del_time BIGINT); +CREATE INDEX repotrash_owner_id_idx ON RepoTrash(owner_id); +CREATE INDEX repotrash_org_id_idx ON RepoTrash(org_id); + +CREATE TABLE RepoFileCount (repo_id CHAR(36) PRIMARY KEY,file_count BIGINT UNSIGNED); + +CREATE TABLE RepoInfo (repo_id CHAR(36) PRIMARY KEY, name VARCHAR(255) NOT NULL, update_time INTEGER, version INTEGER, is_encrypted INTEGER, last_modifier VARCHAR(255)); + +CREATE TABLE UserQuota (user VARCHAR(255) PRIMARY KEY,quota BIGINT); + +CREATE TABLE UserShareQuota (user VARCHAR(255) PRIMARY KEY,quota BIGINT); + +CREATE TABLE OrgQuota (org_id INTEGER PRIMARY KEY,quota BIGINT); + +CREATE TABLE OrgUserQuota (org_id INTEGER,user VARCHAR(255), quota BIGINT, PRIMARY KEY (org_id, user)); + +CREATE TABLE SeafileConf (cfg_group VARCHAR(255) NOT NULL,cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER); + +CREATE TABLE SharedRepo (repo_id CHAR(37) , from_email VARCHAR(255), to_email VARCHAR(255), permission CHAR(15)); +CREATE INDEX RepoIdIndex on SharedRepo (repo_id); +CREATE INDEX FromEmailIndex on SharedRepo (from_email); +CREATE INDEX ToEmailIndex on SharedRepo (to_email); + +CREATE TABLE SystemInfo( info_key VARCHAR(256), info_value VARCHAR(1024)); + +CREATE TABLE WebUploadTempFiles (repo_id CHAR(40) NOT NULL, file_path TEXT NOT NULL, tmp_file_path TEXT NOT NULL); diff --git a/scripts/sql/sqlite/user.sql b/scripts/sql/sqlite/user.sql new file mode 100644 index 0000000..2706c10 --- /dev/null +++ b/scripts/sql/sqlite/user.sql @@ -0,0 +1,16 @@ +CREATE TABLE Binding (email TEXT, peer_id TEXT); +CREATE UNIQUE INDEX peer_index on Binding (peer_id); + +CREATE TABLE EmailUser (id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,email TEXT, passwd TEXT, is_staff bool NOT NULL, is_active bool NOT NULL, ctime INTEGER, reference_id TEXT); +CREATE UNIQUE INDEX email_index on EmailUser (email); +CREATE UNIQUE INDEX reference_id_index on EmailUser (reference_id); + +CREATE TABLE LDAPConfig (cfg_group VARCHAR(255) NOT NULL,cfg_key VARCHAR(255) NOT NULL, value VARCHAR(255), property INTEGER); + +CREATE TABLE LDAPUsers (id INTEGER PRIMARY KEY AUTOINCREMENT, email TEXT NOT NULL, password TEXT NOT NULL, is_staff BOOL NOT NULL, is_active BOOL NOT NULL, extra_attrs TEXT, reference_id TEXT); +CREATE UNIQUE INDEX ldapusers_email_index on LDAPUsers(email); +CREATE UNIQUE INDEX ldapusers_reference_id_index on LDAPUsers(reference_id); + +CREATE TABLE UserRole (email TEXT, role TEXT); +CREATE INDEX userrole_email_index on UserRole (email); +CREATE UNIQUE INDEX userrole_userrole_index on UserRole (email, role); diff --git a/server/quota-mgr.c b/server/quota-mgr.c index 9c20096..488659c 100644 --- a/server/quota-mgr.c +++ b/server/quota-mgr.c @@ -79,7 +79,7 @@ int seaf_quota_manager_init (SeafQuotaManager *mgr) { - if (!mgr->session->create_tables && seaf_db_type (mgr->session->db) == SEAF_DB_TYPE_MYSQL) + if (!mgr->session->create_tables && seaf_db_type (mgr->session->db) != SEAF_DB_TYPE_PGSQL) return 0; SeafDB *db = mgr->session->db; diff --git a/server/repo-mgr.c b/server/repo-mgr.c index abdd708..8b82573 100644 --- a/server/repo-mgr.c +++ b/server/repo-mgr.c @@ -1369,7 +1369,7 @@ create_tables_pgsql (SeafRepoManager *mgr) static int create_db_tables_if_not_exist (SeafRepoManager *mgr) { - if (!mgr->seaf->create_tables && seaf_db_type (mgr->seaf->db) == SEAF_DB_TYPE_MYSQL) + if (!mgr->seaf->create_tables && seaf_db_type (mgr->seaf->db) != SEAF_DB_TYPE_PGSQL) return 0; SeafDB *db = mgr->seaf->db; diff --git a/server/seafile-session.c b/server/seafile-session.c index 59f8f72..04155f5 100644 --- a/server/seafile-session.c +++ b/server/seafile-session.c @@ -207,7 +207,7 @@ seafile_session_init (SeafileSession *session) return -1; } - if ((session->create_tables || seaf_db_type(session->db) != SEAF_DB_TYPE_MYSQL) + if ((session->create_tables || seaf_db_type(session->db) == SEAF_DB_TYPE_PGSQL) && seaf_cfg_manager_init (session->cfg_mgr) < 0) { seaf_warning ("Failed to init config manager.\n"); return -1; @@ -444,7 +444,7 @@ schedule_create_system_default_repo (SeafileSession *session) sql = "CREATE TABLE IF NOT EXISTS SystemInfo( " "info_key VARCHAR(256), info_value VARCHAR(1024))"; - if ((session->create_tables || db_type != SEAF_DB_TYPE_MYSQL) + if ((session->create_tables || db_type == SEAF_DB_TYPE_PGSQL) && seaf_db_query (session->db, sql) < 0) return; diff --git a/server/share-mgr.c b/server/share-mgr.c index cba3a92..4f1a1a6 100644 --- a/server/share-mgr.c +++ b/server/share-mgr.c @@ -26,7 +26,7 @@ seaf_share_manager_new (SeafileSession *seaf) int seaf_share_manager_start (SeafShareManager *mgr) { - if (!mgr->seaf->create_tables && seaf_db_type (mgr->seaf->db) == SEAF_DB_TYPE_MYSQL) + if (!mgr->seaf->create_tables && seaf_db_type (mgr->seaf->db) != SEAF_DB_TYPE_PGSQL) return 0; SeafDB *db = mgr->seaf->db; diff --git a/tests/conf/ccnet.conf b/tests/conf/ccnet.conf index fe880e3..d1db59c 100644 --- a/tests/conf/ccnet.conf +++ b/tests/conf/ccnet.conf @@ -16,3 +16,4 @@ PORT = 9999 #USER = seafile #PASSWD = root #DB = ccnet-db +#CREATE_TABLES=true