Skip to content

Commit

Permalink
Merge pull request #24 from kevung/cmd_history
Browse files Browse the repository at this point in the history
Cmd history
  • Loading branch information
kevung authored Feb 5, 2025
2 parents 893ca19 + 11b9462 commit 5663e3f
Show file tree
Hide file tree
Showing 16 changed files with 592 additions and 50 deletions.
152 changes: 151 additions & 1 deletion db.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,18 @@ func (d *Database) SetupDatabase(path string) error {
return err
}

_, err = d.db.Exec(`
CREATE TABLE IF NOT EXISTS command_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
command TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
fmt.Println("Error creating command_history table:", err)
return err
}

// Insert or update the database version
_, err = d.db.Exec(`INSERT OR REPLACE INTO metadata (key, value) VALUES ('database_version', ?)`, DatabaseVersion)
if err != nil {
Expand All @@ -124,8 +136,20 @@ func (d *Database) OpenDatabase(path string) error {
return err
}

// Check if the required tables exist
// Check the database version
var dbVersion string
err = d.db.QueryRow(`SELECT value FROM metadata WHERE key = 'database_version'`).Scan(&dbVersion)
if err != nil {
fmt.Println("Error querying database version:", err)
return err
}

// Check if the required tables exist based on the database version
requiredTables := []string{"position", "analysis", "comment", "metadata"}
if dbVersion >= "1.1.0" {
requiredTables = append(requiredTables, "command_history")
}

for _, table := range requiredTables {
var tableName string
err = d.db.QueryRow(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`, table).Scan(&tableName)
Expand Down Expand Up @@ -1912,3 +1936,129 @@ func (p *Position) Mirror() Position {
}
return mirrored
}


// SaveCommand saves a command to the command_history table
func (d *Database) SaveCommand(command string) error {
d.mu.Lock()
defer d.mu.Unlock()

// Check if the database version is 1.1.0 or higher
var dbVersion string
err := d.db.QueryRow(`SELECT value FROM metadata WHERE key = 'database_version'`).Scan(&dbVersion)
if err != nil {
fmt.Println("Error querying database version:", err)
return err
}

if dbVersion < "1.1.0" {
return fmt.Errorf("database version is lower than 1.1.0, current version: %s", dbVersion)
}

_, err = d.db.Exec(`INSERT INTO command_history (command) VALUES (?)`, command)
if err != nil {
fmt.Println("Error saving command:", err)
return err
}
return nil
}

// LoadCommandHistory loads the command history from the command_history table
func (d *Database) LoadCommandHistory() ([]string, error) {
d.mu.Lock()
defer d.mu.Unlock()

// Check if the database version is 1.1.0 or higher
var dbVersion string
err := d.db.QueryRow(`SELECT value FROM metadata WHERE key = 'database_version'`).Scan(&dbVersion)
if err != nil {
fmt.Println("Error querying database version:", err)
return nil, err
}

if dbVersion < "1.1.0" {
return nil, fmt.Errorf("database version is lower than 1.1.0, current version: %s", dbVersion)
}

rows, err := d.db.Query(`SELECT command FROM command_history ORDER BY timestamp ASC`)
if err != nil {
fmt.Println("Error loading command history:", err)
return nil, err
}
defer rows.Close()

var history []string
for rows.Next() {
var command string
if err = rows.Scan(&command); err != nil {
fmt.Println("Error scanning command:", err)
return nil, err
}
history = append(history, command)
}
return history, nil
}

func (d *Database) ClearCommandHistory() error {
d.mu.Lock()
defer d.mu.Unlock()

// Check if the database version is 1.1.0 or higher
var dbVersion string
err := d.db.QueryRow(`SELECT value FROM metadata WHERE key = 'database_version'`).Scan(&dbVersion)
if err != nil {
fmt.Println("Error querying database version:", err)
return err
}

if dbVersion < "1.1.0" {
return fmt.Errorf("database version is lower than 1.1.0, current version: %s", dbVersion)
}

_, err = d.db.Exec(`DELETE FROM command_history`)
if err != nil {
fmt.Println("Error clearing command history:", err)
return err
}
return nil
}

func (d *Database) Migrate_1_0_0_to_1_1_0() error {
d.mu.Lock()
defer d.mu.Unlock()

// Check current database version
var dbVersion string
err := d.db.QueryRow(`SELECT value FROM metadata WHERE key = 'database_version'`).Scan(&dbVersion)
if err != nil {
fmt.Println("Error querying database version:", err)
return err
}

if dbVersion != "1.0.0" {
return fmt.Errorf("database version is not 1.0.0, current version: %s", dbVersion)
}

// Create the command_history table
_, err = d.db.Exec(`
CREATE TABLE IF NOT EXISTS command_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
command TEXT,
timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
)
`)
if err != nil {
fmt.Println("Error creating command_history table:", err)
return err
}

// Update the database version to 1.1.0
_, err = d.db.Exec(`UPDATE metadata SET value = ? WHERE key = 'database_version'`, "1.1.0")
if err != nil {
fmt.Println("Error updating database version:", err)
return err
}

fmt.Println("Database successfully migrated from version 1.0.0 to 1.1.0")
return nil
}
25 changes: 25 additions & 0 deletions doc/source/annexe_db_scheme.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
.. _annexe_db_migration:

Annexe: Schéma de la base de données
====================================

Version 1.0.0
-------------

La version 1.0.0 de la base de données contient les tables suivantes :

- **position** : Stocke les positions avec les colonnes `id` (clé primaire) et `state` (état de la position en format JSON).
- **analysis** : Stocke les analyses des positions avec les colonnes `id` (clé primaire), `position_id` (clé étrangère vers `position`), et `data` (données de l'analyse en format JSON).
- **comment** : Stocke les commentaires associés aux positions avec les colonnes `id` (clé primaire), `position_id` (clé étrangère vers `position`), et `text` (texte du commentaire).
- **metadata** : Stocke les métadonnées de la base de données avec les colonnes `key` (clé primaire) et `value` (valeur associée à la clé).

Version 1.1.0
-------------

La version 1.1.0 de la base de données ajoute la table suivante :

- **command_history** : Stocke l'historique des commandes avec les colonnes `id` (clé primaire), `command` (texte de la commande), et `timestamp` (date et heure de l'exécution de la commande).

Les autres tables restent inchangées par rapport à la version 1.0.0.

Pour migrer la base de données de la version 1.0.0 à la version 1.1.0, exécutez la commande ``migrate_from_1_0_to_1_1`` dans blunderDB.
13 changes: 13 additions & 0 deletions doc/source/cmd_mode.rst
Original file line number Diff line number Diff line change
Expand Up @@ -187,3 +187,16 @@ positions en prenant en compte la structure des pions, le score et le cube
de la position éditée où le joueur a entre 20 et 5 pips d'avance à la
course, avec au moins 60% de chances de gain, au moins 10 pions dans la
zone, et l'adversaire a entre 2 et 3 pions arriérés.

.. _cmd_misc:

Commandes diverses
------------------

.. csv-table::
:header: "Commande", "Action"
:widths: 10, 40
:align: center

"clear, cl", "Efface l'historique des commandes."
"migrate_from_1_0_to_1_1", "Migre la base de données de la version 1.0 à la version 1.1."
3 changes: 2 additions & 1 deletion doc/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Historique des versions
Corrections de filtres.

Ajout du support de MacOS."

0.5.0, 15/02/2025, "Ajout de nouveaux filtres (miroir, non contact, jan blot, outfield blot)."

Sommaire
========
Expand All @@ -76,6 +76,7 @@ Sommaire
faq
annexe_windows_securite
annexe_mac_securite
annexe_db_scheme

.. youtube:: Ln7XKVFqfUk
:width: 100%
Expand Down
98 changes: 98 additions & 0 deletions doc/source/locale/en/LC_MESSAGES/annexe_db_scheme.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Copyright (C) 2024, Kevin UNGER <[email protected]>
# This file is distributed under the same license as the blunderDB package.
# FIRST AUTHOR <EMAIL@ADDRESS>, 2025.
#
# SPDX-FileCopyrightText: 2025 unger <[email protected]>
msgid ""
msgstr ""
"Project-Id-Version: blunderDB \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-02-05 15:05+0100\n"
"PO-Revision-Date: 2025-02-05 15:23+0100\n"
"Last-Translator: unger <[email protected]>\n"
"Language: en\n"
"Language-Team: English <[email protected]>\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Generated-By: Babel 2.16.0\n"
"X-Generator: Lokalize 24.12.1\n"

#: ../../source/annexe_db_scheme.rst:4
msgid "Annexe: Schéma de la base de données"
msgstr "Annex: Database Schema"

#: ../../source/annexe_db_scheme.rst:7
msgid "Version 1.0.0"
msgstr "Version 1.0.0"

#: ../../source/annexe_db_scheme.rst:9
msgid "La version 1.0.0 de la base de données contient les tables suivantes :"
msgstr "Version 1.0.0 of the database contains the following tables:"

#: ../../source/annexe_db_scheme.rst:11
msgid ""
"**position** : Stocke les positions avec les colonnes `id` (clé primaire)"
" et `state` (état de la position en format JSON)."
msgstr ""
"**position**: Stores the positions with the columns `id` (primary key) and `st"
"ate` (state of the position in JSON format)."

#: ../../source/annexe_db_scheme.rst:12
msgid ""
"**analysis** : Stocke les analyses des positions avec les colonnes `id` "
"(clé primaire), `position_id` (clé étrangère vers `position`), et `data` "
"(données de l'analyse en format JSON)."
msgstr ""
"**analysis**: Stores the analyses of the positions with the columns `id` (prim"
"ary key), `position_id` (foreign key referencing `position`), and `data` (anal"
"ysis data in JSON format)."

#: ../../source/annexe_db_scheme.rst:13
msgid ""
"**comment** : Stocke les commentaires associés aux positions avec les "
"colonnes `id` (clé primaire), `position_id` (clé étrangère vers "
"`position`), et `text` (texte du commentaire)."
msgstr ""
"**comment**: Stores the comments associated with the positions with the column"
"s `id` (primary key), `position_id` (foreign key referencing `position`), and "
"`text` (comment text)."

#: ../../source/annexe_db_scheme.rst:14
msgid ""
"**metadata** : Stocke les métadonnées de la base de données avec les "
"colonnes `key` (clé primaire) et `value` (valeur associée à la clé)."
msgstr ""
"**metadata**: Stores the metadata of the database with the columns `key` (prim"
"ary key) and `value` (value associated with the key)."

#: ../../source/annexe_db_scheme.rst:17
msgid "Version 1.1.0"
msgstr "Version 1.1.0"

#: ../../source/annexe_db_scheme.rst:19
msgid "La version 1.1.0 de la base de données ajoute la table suivante :"
msgstr "Version 1.1.0 of the database adds the following table:"

#: ../../source/annexe_db_scheme.rst:21
msgid ""
"**command_history** : Stocke l'historique des commandes avec les colonnes"
" `id` (clé primaire), `command` (texte de la commande), et `timestamp` "
"(date et heure de l'exécution de la commande)."
msgstr ""
"**command_history**: Stores the command history with the columns `id` (primary"
" key), `command` (text of the command), and `timestamp` (date and time of comm"
"and execution)."

#: ../../source/annexe_db_scheme.rst:23
msgid "Les autres tables restent inchangées par rapport à la version 1.0.0."
msgstr "The other tables remain unchanged from version 1.0.0."

#: ../../source/annexe_db_scheme.rst:25
msgid ""
"Pour migrer la base de données de la version 1.0.0 à la version 1.1.0, "
"exécutez la commande ``migrate_from_1_0_to_1_1`` dans blunderDB."
msgstr ""
"To migrate the database from version 1.0.0 to version 1.1.0, execute the comma"
"nd ``migrate_from_1_0_to_1_1`` in blunderDB."
24 changes: 22 additions & 2 deletions doc/source/locale/en/LC_MESSAGES/cmd_mode.po
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ msgid ""
msgstr ""
"Project-Id-Version: blunderDB \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2025-02-04 14:00+0100\n"
"PO-Revision-Date: 2025-02-04 14:01+0100\n"
"POT-Creation-Date: 2025-02-05 15:05+0100\n"
"PO-Revision-Date: 2025-02-05 15:24+0100\n"
"Last-Translator: Kévin Unger <[email protected]>\n"
"Language: en\n"
"Language-Team: English <[email protected]>\n"
Expand Down Expand Up @@ -901,6 +901,26 @@ msgstr ""
"ahead in the race, with at least 60% winning chances, at least 10 "
"checkers in the zone, and the opponent has between 2 and 3 backcheckers."

#: ../../source/cmd_mode.rst:183
msgid "Commandes diverses"
msgstr "Various commands"

#: ../../source/cmd_mode.rst:1
msgid "clear, cl"
msgstr "clear, cl"

#: ../../source/cmd_mode.rst:1
msgid "Efface l'historique des commandes."
msgstr "Clear the command history."

#: ../../source/cmd_mode.rst:1
msgid "migrate_from_1_0_to_1_1"
msgstr "migrate_from_1_0_to_1_1"

#: ../../source/cmd_mode.rst:1
msgid "Migre la base de données de la version 1.0 à la version 1.1."
msgstr "Migrate the database from version 1.0 to version 1.1."

#~ msgid ":n"
#~ msgstr ":n"

Expand Down
Loading

0 comments on commit 5663e3f

Please sign in to comment.