Skip to content

Commit

Permalink
[FAB-7967] Add id column to affiliations table
Browse files Browse the repository at this point in the history
MySQL replication requires that each table have a primary
key. But affiliations table does not have a primary key, so
the replication is failing. This change set adds auto increment
primary key column 'id' to the affiliations table for MySQL
database type.

Change-Id: I5400b5fc5592d145d23b3856635427b1951cf0e3
Signed-off-by: Anil Ambati <[email protected]>
  • Loading branch information
Anil Ambati committed Feb 9, 2018
1 parent b9d3e01 commit bc4c06f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/source/users-guide.rst
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ MySQL as described below. Fabric CA supports the following database
versions in a cluster setup:

- PostgreSQL: 9.5.5 or later
- MySQL: 5.17.16 or later
- MySQL: 5.7 or later

PostgreSQL
^^^^^^^^^^
Expand Down
1 change: 1 addition & 0 deletions lib/dbaccessor.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ type UserRecord struct {

// AffiliationRecord defines the properties of an affiliation
type AffiliationRecord struct {
ID int `db:"id"`
Name string `db:"name"`
Prekey string `db:"prekey"`
Level int `db:"level"`
Expand Down
8 changes: 7 additions & 1 deletion lib/dbutil/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ func createMySQLTables(dbName string, db *sqlx.DB) error {
return errors.Wrap(err, "Error creating users table")
}
log.Debug("Creating affiliations table if it doesn't exist")
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS affiliations (name VARCHAR(1024) NOT NULL, prekey VARCHAR(1024), level INTEGER DEFAULT 0)"); err != nil {
if _, err := db.Exec("CREATE TABLE IF NOT EXISTS affiliations (id INT NOT NULL AUTO_INCREMENT, name VARCHAR(1024) NOT NULL, prekey VARCHAR(1024), level INTEGER DEFAULT 0, PRIMARY KEY (id))"); err != nil {
return errors.Wrap(err, "Error creating affiliations table")
}
log.Debug("Creating index on 'name' in the affiliations table")
Expand Down Expand Up @@ -618,6 +618,12 @@ func updateMySQLSchema(db *sqlx.DB) error {
return err
}
}
_, err = db.Exec("ALTER TABLE affiliations ADD COLUMN id INT NOT NULL PRIMARY KEY AUTO_INCREMENT FIRST")
if err != nil {
if !strings.Contains(err.Error(), "1060") { // Already using the latest schema
return err
}
}
_, err = db.Exec("ALTER TABLE affiliations MODIFY name VARCHAR(1024), MODIFY prekey VARCHAR(1024)")
if err != nil {
return err
Expand Down
7 changes: 5 additions & 2 deletions scripts/fvt/dbmigration_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,11 @@ if [ $? != 0 ]; then
ErrorMsg "Database column 'attributes' should have character limit of 65535"
fi

mysql --host=localhost --user=root --password=mysql --database=$DBNAME -e "SELECT column_name, character_maximum_length FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'affiliations';" > $TESTDIR/text.txt
mysql --host=localhost --user=root --password=mysql --database=$DBNAME -e "SELECT column_name, character_maximum_length, data_type, extra FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'affiliations';" > $TESTDIR/text.txt
grep 'id'$'\t''NULL'$'\t''int'$'\t''auto_increment' $TESTDIR/text.txt
if [ $? != 0 ]; then
ErrorMsg "Integer auto_increment column 'id' should be present in the affiliations table"
fi
grep 'name'$'\t''1024' $TESTDIR/text.txt
if [ $? != 0 ]; then
ErrorMsg "Database column 'name' should have character limit of 1024"
Expand All @@ -173,7 +177,6 @@ grep 'prekey'$'\t''1024' $TESTDIR/text.txt
if [ $? != 0 ]; then
ErrorMsg "Database column 'prekey' should have character limit of 1024"
fi

mysql --host=localhost --user=root --password=mysql --database=$DBNAME -e "SELECT column_name, character_maximum_length FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'certificates' AND COLUMN_NAME = 'id';" | grep "255"
if [ $? != 0 ]; then
ErrorMsg "Database column 'id' should have character limit of 255"
Expand Down

0 comments on commit bc4c06f

Please sign in to comment.