discogs basic backend
This commit is contained in:
@@ -49,15 +49,15 @@ func (m *MySQL) PrepareDatabase(ctx context.Context) error {
|
||||
return fmt.Errorf("uninitialized mysql client")
|
||||
}
|
||||
|
||||
tablecheck := `SELECT count(*) AS count
|
||||
tablecheck := fmt.Sprintf(`SELECT count(*) AS count
|
||||
FROM information_schema.TABLES
|
||||
WHERE TABLE_NAME = '` + m.versionTable + `'
|
||||
AND TABLE_SCHEMA in (SELECT DATABASE());`
|
||||
tableschema := `CREATE TABLE ` + m.versionTable + `(
|
||||
WHERE TABLE_NAME = '%s'
|
||||
AND TABLE_SCHEMA in (SELECT DATABASE());`, m.versionTable)
|
||||
tableschema := fmt.Sprintf(`CREATE TABLE %s (
|
||||
id INT NOT NULL,
|
||||
name VARCHAR(100) NOT NULL,
|
||||
datetime DATE,
|
||||
PRIMARY KEY (id))`
|
||||
PRIMARY KEY (id))`, m.versionTable)
|
||||
|
||||
var versionTableExists int
|
||||
m.connection.QueryRowContext(ctx, tablecheck).Scan(&versionTableExists)
|
||||
@@ -73,8 +73,9 @@ func (m *MySQL) GetLatestMigration(ctx context.Context) (int, error) {
|
||||
return 0, fmt.Errorf("uninitialized mysql client")
|
||||
}
|
||||
|
||||
migrationCheck := fmt.Sprintf("SELECT COALESCE(MAX(id), 0) FROM %s", m.versionTable)
|
||||
var latestMigration int
|
||||
err := m.connection.QueryRowContext(ctx, "SELECT COALESCE(MAX(id), 0) FROM "+m.versionTable).Scan(&latestMigration)
|
||||
err := m.connection.QueryRowContext(ctx, migrationCheck).Scan(&latestMigration)
|
||||
return latestMigration, err
|
||||
}
|
||||
|
||||
@@ -97,6 +98,9 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) {
|
||||
}
|
||||
mig.id, mig.name = id, name
|
||||
mig.content, err = fs.ReadFile(migrationsFS, m.migrationsDirectory+"/"+dir[f].Name())
|
||||
if err != nil {
|
||||
return 0, 0, fmt.Errorf("failure loading migration: %w", err)
|
||||
}
|
||||
migrations[mig.id] = mig
|
||||
}
|
||||
}
|
||||
@@ -116,6 +120,7 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) {
|
||||
if err != nil {
|
||||
return latestMigrationRan, 0, err
|
||||
}
|
||||
migrationLogSql := fmt.Sprintf("INSERT INTO %s (id, name, datetime) VALUES (?, ?, ?)", m.versionTable)
|
||||
migrationsRun := 0
|
||||
for migrationsToRun := true; migrationsToRun; _, migrationsToRun = migrations[latestMigrationRan+1] {
|
||||
mig := migrations[latestMigrationRan+1]
|
||||
@@ -127,7 +132,7 @@ func (m *MySQL) RunMigrations(ctx context.Context) (int, int, error) {
|
||||
}
|
||||
return latestMigrationRan, migrationsRun, err
|
||||
}
|
||||
_, err = tx.ExecContext(ctx, "INSERT INTO "+m.versionTable+" (id, name, datetime) VALUES (?, ?, ?)", mig.id, mig.name, time.Now())
|
||||
_, err = tx.ExecContext(ctx, migrationLogSql, mig.id, mig.name, time.Now())
|
||||
if err != nil {
|
||||
nestederr := tx.Rollback()
|
||||
if nestederr != nil {
|
||||
@@ -147,26 +152,14 @@ func (m *MySQL) GetAllBooks(ctx context.Context) ([]media.Book, error) {
|
||||
return nil, fmt.Errorf("uninitialized mysql client")
|
||||
}
|
||||
|
||||
allBooksQuery := fmt.Sprintf(`SELECT
|
||||
id, title, authors, sortauthor, isbn10, isbn13, format,
|
||||
genre, publisher, series, volume, year, signed,
|
||||
description, notes, onloan, coverurl
|
||||
FROM %s`, m.tableName)
|
||||
|
||||
books := []media.Book{}
|
||||
rows, err := m.connection.QueryContext(ctx, `
|
||||
SELECT id,
|
||||
title,
|
||||
authors,
|
||||
sortauthor,
|
||||
isbn10,
|
||||
isbn13,
|
||||
format,
|
||||
genre,
|
||||
publisher,
|
||||
series,
|
||||
volume,
|
||||
year,
|
||||
signed,
|
||||
description,
|
||||
notes,
|
||||
onloan,
|
||||
coverurl
|
||||
FROM `+m.tableName)
|
||||
rows, err := m.connection.QueryContext(ctx, allBooksQuery)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user