Skip to content

Commit 4ad0d69

Browse files
committed
fix: leadership to respect sqlite
1 parent 5b58046 commit 4ad0d69

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

leadership/leadership.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ func (l *LeaderElection) createLeadershipTable() error {
9393
case string(storage.MYSQL):
9494
statement = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id VARCHAR(50) PRIMARY KEY, registration BIGINT, heartbeat BIGINT)", l.storage.GetSchemaName(), l.tableName)
9595
case string(storage.SQLITE):
96-
statement = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s.%s (id TEXT PRIMARY KEY, registration INTEGER, heartbeat INTEGER)", l.storage.GetSchemaName(), l.tableName)
96+
statement = fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (id TEXT PRIMARY KEY, registration INTEGER, heartbeat INTEGER)", l.tableName)
9797
}
9898
return l.storage.Execute(statement)
9999

@@ -161,7 +161,15 @@ func (l *LeaderElection) updateMembershipTable() error {
161161

162162
switch l.storageType {
163163
case string(storage.SQL):
164-
statement := fmt.Sprintf(`INSERT INTO %s.%s VALUES('%v', %v, %v)`, l.storage.GetSchemaName(), l.tableName, l.Id, now, now)
164+
var statement string
165+
166+
switch l.storageProvider {
167+
case string(storage.SQLITE):
168+
statement = fmt.Sprintf(`INSERT INTO %s VALUES('%v', %v, %v)`, l.tableName, l.Id, now, now)
169+
default:
170+
statement = fmt.Sprintf(`INSERT INTO %s.%s VALUES('%v', %v, %v)`, l.storage.GetSchemaName(), l.tableName, l.Id, now, now)
171+
}
172+
165173
return l.storage.Execute(statement)
166174
case string(storage.DYNAMODB):
167175
statement := fmt.Sprintf(`INSERT INTO %s VALUE {'id': '%v', 'registration': %v, 'heartbeat': %v}`, l.tableName, l.Id, now, now)
@@ -276,7 +284,13 @@ func (l *LeaderElection) getLeader() (Member, error) {
276284
switch l.storageType {
277285
case string(storage.SQL):
278286
a := l.storage.(*storage.SQLAdapter)
279-
statement := fmt.Sprintf(`SELECT * FROM %s.%s WHERE id='%s'`, l.storage.GetSchemaName(), l.tableName, l.Leader.Id)
287+
var statement string
288+
switch l.storageProvider {
289+
case string(storage.SQLITE):
290+
statement = fmt.Sprintf(`SELECT * FROM %s WHERE id='%s'`, l.tableName, l.Leader.Id)
291+
default:
292+
statement = fmt.Sprintf(`SELECT * FROM %s.%s WHERE id='%s'`, l.storage.GetSchemaName(), l.tableName, l.Leader.Id)
293+
}
280294
result := a.DB.Raw(statement).Scan(&member)
281295

282296
if result.Error != nil {
@@ -310,7 +324,13 @@ func (l *LeaderElection) Members() ([]Member, error) {
310324

311325
switch l.storageType {
312326
case string(storage.SQL):
313-
statement := fmt.Sprintf("SELECT * FROM %s.%s", l.storage.GetSchemaName(), l.tableName)
327+
var statement string
328+
switch l.storageProvider {
329+
case string(storage.SQLITE):
330+
statement = fmt.Sprintf(`SELECT * FROM %s`, l.tableName)
331+
default:
332+
statement = fmt.Sprintf("SELECT * FROM %s.%s", l.storage.GetSchemaName(), l.tableName)
333+
}
314334
a := l.storage.(*storage.SQLAdapter)
315335
result := a.DB.Raw(statement).Scan(&members)
316336
if result.Error != nil {

0 commit comments

Comments
 (0)