PackageManager just because
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

138 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package Database
  2. import (
  3. "database/sql"
  4. "os"
  5. "path/filepath"
  6. "time"
  7. _ "github.com/mattn/go-sqlite3"
  8. bolt "go.etcd.io/bbolt"
  9. "PackageManager/Variables"
  10. )
  11. var (
  12. DB *sql.DB
  13. FsDB *bolt.DB
  14. )
  15. func InitConfigDir() error {
  16. var err error
  17. _, err = os.Stat(Variables.ConfigDir)
  18. if os.IsNotExist(err) {
  19. return os.MkdirAll(Variables.ConfigDir, 0644)
  20. }
  21. return err
  22. }
  23. func InitDatabaseFiles() error {
  24. var err error
  25. // Initialise sqlite3 database for package versioning
  26. DB, err = sql.Open(
  27. "sqlite3",
  28. filepath.Join(Variables.ConfigDir, Variables.DatabaseName),
  29. )
  30. if err != nil {
  31. return err
  32. }
  33. // Initialise bolt db for filesystem hashing
  34. FsDB, err = bolt.Open(
  35. filepath.Join(Variables.ConfigDir, Variables.FsHashDatabaseName),
  36. 0600,
  37. &bolt.Options{
  38. Timeout: 5 * time.Second,
  39. },
  40. )
  41. return err
  42. }
  43. func init() {
  44. var err error
  45. err = InitConfigDir()
  46. if err != nil {
  47. panic(err)
  48. }
  49. err = InitDatabaseFiles()
  50. if err != nil {
  51. panic(err)
  52. }
  53. }
  54. func InitBoltDB() error {
  55. var (
  56. tx *bolt.Tx
  57. err error
  58. )
  59. tx, err = FsDB.Begin(true)
  60. if err != nil {
  61. return err
  62. }
  63. defer tx.Rollback()
  64. _, err = tx.CreateBucketIfNotExists(Variables.FsHashIndexBucket)
  65. if err != nil {
  66. return err
  67. }
  68. _, err = tx.CreateBucketIfNotExists(Variables.FsHashPicksBucket)
  69. if err != nil {
  70. return err
  71. }
  72. return tx.Commit()
  73. }
  74. func InitSqlite3DB() error {
  75. var (
  76. stmt *sql.Stmt
  77. err error
  78. )
  79. stmt, err = DB.Prepare(`
  80. CREATE TABLE IF NOT EXISTS installed_packages (
  81. id INTEGER PRIMARY KEY AUTOINCREMENT,
  82. name VARCHAR(64) NOT NULL,
  83. version VARCHAR(64) NOT NULL,
  84. installed_at INTEGER NOT NULL
  85. )
  86. `)
  87. if err != nil {
  88. return err
  89. }
  90. _, err = stmt.Exec()
  91. if err != nil {
  92. return err
  93. }
  94. stmt, err = DB.Prepare(`
  95. CREATE TABLE IF NOT EXISTS dependancy_linker (
  96. id INTEGER PRIMARY KEY AUTOINCREMENT,
  97. package_id INTEGER,
  98. dependancy_id INTEGER,
  99. FOREIGN KEY(package_id) REFERENCES installed_packages(id),
  100. FOREIGN KEY(dependancy_id) REFERENCES installed_packages(id)
  101. )
  102. `)
  103. if err != nil {
  104. return err
  105. }
  106. _, err = stmt.Exec()
  107. return err
  108. }
  109. func InitDB() error {
  110. var err error = InitBoltDB()
  111. if err != nil {
  112. return err
  113. }
  114. return InitSqlite3DB()
  115. }