init函数中的数据库初始化操作逻辑
err = ca.initDB(ca.server.dbMetrics)
1 定义数据库文件
if dbCfg.Datasource == "" {
dbCfg.Datasource = "fabric-ca-server.db"
}
2021/03/26 16:01:03 [DEBUG] Initializing 'sqlite3' database at 'K:\fabric\fabric-ca-master\lib\rootDir\fabric-ca-server.db'
2 连接数据库
err = caDB.Connect()
2021/03/26 16:01:31 [DEBUG] Using sqlite database, connect to database in home (K:\fabric\fabric-ca-master\lib\rootDir\fabric-ca-server.db) directory
2021/03/26 16:01:49 [DEBUG] Creating SQLite database (K:\fabric\fabric-ca-master\lib\rootDir\fabric-ca-server.db) if it does not exist...
3 创建表
sqlxdb, err := caDB.Create()
具体创建下面的表
func createAllSQLiteTables(tx Create, args ...interface{}) error {
err := createIdentityTable(tx)
if err != nil {
return err
}
err = createAffiliationTable(tx)
if err != nil {
return err
}
err = createCertificateTable(tx)
if err != nil {
return err
}
err = createCredentialsTable(tx)
if err != nil {
return err
}
err = createRevocationComponentTable(tx)
if err != nil {
return err
}
err = createNoncesTable(tx)
if err != nil {
return err
}
err = createPropertiesTable(tx)
if err != nil {
return err
}
return nil
}
以第一个方法举例:
func createIdentityTable(tx Create) error {
log.Debug("Creating users table if it does not exist")
if _, err := tx.Exec("CreateUsersTable", "CREATE TABLE IF NOT EXISTS users (id VARCHAR(255), token bytea, type VARCHAR(256), affiliation VARCHAR(1024), attributes TEXT, state INTEGER, max_enrollments INTEGER, level INTEGER DEFAULT 0, incorrect_password_attempts INTEGER DEFAULT 0, PRIMARY KEY (id))"); err != nil {
return errors.Wrap(err, "Error creating users table")
}
return nil
}
4 合并表
注意在合并表之前,表内数据都是空的,没有插入任何数据
func getMigrator(driverName string, tx cadb.FabricCATx, curLevels, srvLevels *dbutil.Levels) (cadb.Migrator, error)
err = db.Migrate(migrator, curLevels, ca.server.levels)
判断level中的每一个项目
if currentLevels.Identity < srvLevels.Identity {
log.Debug("Migrating users table...")
err := migrator.MigrateUsersTable()
if err != nil {
log.Errorf("Error encountered while migrating users table, rolling back changes: %s", err)
return migrator.Rollback()
}
}
2021/03/18 09:43:14 [DEBUG] Migrating users table...
case 0:
log.Debug("Upgrade identity table to level 1")
_, err := tx.Exec(funcName, "ALTER TABLE users RENAME TO users_old")
if err != nil {
return err
}
err = createIdentityTable(tx)
if err != nil {
return err
}
// If coming from a table that did not yet have the level column then we can only copy columns that exist in both the tables
_, err = tx.Exec(funcName, "INSERT INTO users (id, token, type, affiliation, attributes, state, max_enrollments) SELECT id, token, type, affiliation, attributes, state, max_enrollments FROM users_old")
if err != nil {
return err
}
_, err = tx.Exec(funcName, "DROP TABLE users_old")
if err != nil {
return err
}
fallthrough
最后通过commit统一执行事务
return migrator.Commit()
此时还是没有插入user中用户信息,现在需要插入Identity数据。
如何插入一个用户信息,就是前面生成的admin的cert等信息
if !ca.Config.LDAP.Enabled {
err = ca.loadUsersTable()
if err != nil {
log.Error(err)
dbError = true
if caerrors.IsFatalError(err) {
return err
}
}
err = ca.loadAffiliationsTable()
if err != nil {
log.Error(err)
dbError = true
}
}
func (ca *CA) loadUsersTable() error {
log.Debug("Loading identity table")
registry := &ca.Config.Registry
for _, id := range registry.Identities {
log.Debugf("Loading identity '%s'", id.Name)
err := ca.addIdentity(&id, false)
if err != nil {
return errors.WithMessage(err, "Failed to load identity table")
}
}
log.Debug("Successfully loaded identity table")
return nil
}
registry信息:
构造数据库user实例,插入
rec := cadbuser.Info{
Name: id.Name,
Pass: id.Pass,
Type: id.Type,
Affiliation: id.Affiliation,
Attributes: attrs,
MaxEnrollments: id.MaxEnrollments,
Level: ca.levels.Identity,
}
err = ca.registry.InsertUser(&rec)
继续进行隶属关系表的插入
err = ca.loadAffiliationsTable()
如下
完成数据库的初始化。注意 ca-cert.pem 并不会插入到数据库中,应该一直驻留在内存中。
user的信息中也没有关于证书的内容。