[Midnightbsd-cvs] src [6490] trunk/lib/libmport: Create os_release in packages of the master database to indicate what os_release the installed package is for.

laffer1 at midnightbsd.org laffer1 at midnightbsd.org
Sun Dec 15 00:57:46 EST 2013


Revision: 6490
          http://svnweb.midnightbsd.org/src/?rev=6490
Author:   laffer1
Date:     2013-12-15 00:57:46 -0500 (Sun, 15 Dec 2013)
Log Message:
-----------
Create os_release in packages of the master database to indicate what os_release the installed package is for.

This is important because we may be upgrading from 0.4 to 0.5. We want to be able to indicate to the user that we have old packages or force upgrade functionality on them.

Modified Paths:
--------------
    trunk/lib/libmport/Makefile
    trunk/lib/libmport/db.c
    trunk/lib/libmport/instance.c
    trunk/lib/libmport/mport_private.h

Modified: trunk/lib/libmport/Makefile
===================================================================
--- trunk/lib/libmport/Makefile	2013-12-15 03:59:49 UTC (rev 6489)
+++ trunk/lib/libmport/Makefile	2013-12-15 05:57:46 UTC (rev 6490)
@@ -1,4 +1,4 @@
-# $MidnightBSD: src/lib/libmport/Makefile,v 1.16 2013/03/03 00:25:12 laffer1 Exp $
+# $MidnightBSD$
 
 LIB=		mport
 SRCS=		bundle_write.c bundle_read.c plist.c create_primative.c db.c \
@@ -10,7 +10,6 @@
 INCS=		mport.h 
 
 CFLAGS+=	-I${.CURDIR} -g
-WARNS?=		6
 SHLIB_MAJOR=	1
 MAN=	mport.3
 

Modified: trunk/lib/libmport/db.c
===================================================================
--- trunk/lib/libmport/db.c	2013-12-15 03:59:49 UTC (rev 6489)
+++ trunk/lib/libmport/db.c	2013-12-15 05:57:46 UTC (rev 6490)
@@ -26,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD: src/lib/libmport/db.c,v 1.10 2013/09/07 19:49:51 laffer1 Exp $");
+__MBSDID("$MidnightBSD$");
 
 #include <sqlite3.h>
 #include <stdlib.h>
@@ -34,7 +34,9 @@
 #include "mport.h"
 #include "mport_private.h"
 
+static int mport_upgrade_master_schema_0to2(sqlite3 *);
 
+
 /* mport_db_do(sqlite3 *db, const char *sql, ...)
  * 
  * A wrapper for doing executing a single sql query.  Takes a sqlite3 struct
@@ -113,19 +115,21 @@
  *
  * Returns MPORT_OK on success.
  */
-int mport_attach_stub_db(sqlite3 *db, const char *dir)
+int
+mport_attach_stub_db(sqlite3 *db, const char *dir)
 {
-  char *file;
-  asprintf(&file, "%s/%s", dir, MPORT_STUB_DB_FILE);
+	char *file;
+
+	asprintf(&file, "%s/%s", dir, MPORT_STUB_DB_FILE);
   
-  if (mport_db_do(db, "ATTACH %Q AS stub", file) != MPORT_OK) { 
-    free(file);
-    RETURN_CURRENT_ERROR;
-  }
+	if (mport_db_do(db, "ATTACH %Q AS stub", file) != MPORT_OK) { 
+		free(file);
+		RETURN_CURRENT_ERROR;
+	}
   
-  free(file);
+	free(file);
   
-  return MPORT_OK;
+	return (MPORT_OK);
 }
 
 
@@ -135,12 +139,13 @@
  *
  * Returns MPORT_OK on success.
  */
-int mport_detach_stub_db(sqlite3 *db)
+int
+mport_detach_stub_db(sqlite3 *db)
 {
-  if (mport_db_do(db, "DETACH stub") != MPORT_OK) 
-    RETURN_CURRENT_ERROR;
+	if (mport_db_do(db, "DETACH stub") != MPORT_OK) 
+		RETURN_CURRENT_ERROR;
   
-  return MPORT_OK;
+	return (MPORT_OK);
 }
 
 
@@ -175,28 +180,56 @@
 }
 
 int
+mport_upgrade_master_schema(sqlite3 *db, int databaseVersion) 
+{
+
+	switch (databaseVersion) {
+		case 0:
+		case 1:
+			mport_upgrade_master_schema_0to2(db);
+			mport_set_database_version(db);
+		case 2:
+			break;
+		default:
+			RETURN_ERROR(MPORT_ERR_FATAL, "Invalid master database version");
+	}
+
+	return (MPORT_OK);
+}
+
+static int
+mport_upgrade_master_schema_0to2(sqlite3 *db)
+{
+
+	RUN_SQL(db, "ALTER TABLE packages ADD COLUMN os_release text;");
+	RUN_SQL(db, "update packages set os_release='0.4'");
+
+	return (MPORT_OK);
+}
+
+int
 mport_generate_master_schema(sqlite3 *db) 
 {
 
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS packages (pkg text NOT NULL, version text NOT NULL, origin text NOT NULL, prefix text NOT NULL, lang text, options text, status text default 'dirty', comment text)");
-  RUN_SQL(db, "CREATE UNIQUE INDEX IF NOT EXISTS packages_pkg ON packages (pkg)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS packages_origin ON packages (origin)");
+	RUN_SQL(db, "CREATE TABLE IF NOT EXISTS packages (pkg text NOT NULL, version text NOT NULL, origin text NOT NULL, prefix text NOT NULL, lang text, options text, status text default 'dirty', comment text, os_release text)");
+	RUN_SQL(db, "CREATE UNIQUE INDEX IF NOT EXISTS packages_pkg ON packages (pkg)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS packages_origin ON packages (origin)");
 
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS depends (pkg text NOT NULL, depend_pkgname text NOT NULL, depend_pkgversion text, depend_port text NOT NULL)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_pkg ON depends (pkg)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_dependpkgname ON depends (depend_pkgname)");
+	RUN_SQL(db, "CREATE TABLE IF NOT EXISTS depends (pkg text NOT NULL, depend_pkgname text NOT NULL, depend_pkgversion text, depend_port text NOT NULL)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_pkg ON depends (pkg)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS depends_dependpkgname ON depends (depend_pkgname)");
 
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS log (pkg text NOT NULL, version text NOT NULL, date int NOT NULL, msg text NOT NULL)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS log_pkg ON log (pkg, version)");
+	RUN_SQL(db, "CREATE TABLE IF NOT EXISTS log (pkg text NOT NULL, version text NOT NULL, date int NOT NULL, msg text NOT NULL)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS log_pkg ON log (pkg, version)");
 
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS assets (pkg text NOT NULL, type int NOT NULL, data text, checksum text)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS assets_pkg ON assets (pkg)");
+	RUN_SQL(db, "CREATE TABLE IF NOT EXISTS assets (pkg text NOT NULL, type int NOT NULL, data text, checksum text)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS assets_pkg ON assets (pkg)");
   
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS categories (pkg text NOT NULL, category text NOT NULL)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS categories_pkg ON categories (pkg, category)");
+	RUN_SQL(db, "CREATE TABLE IF NOT EXISTS categories (pkg text NOT NULL, category text NOT NULL)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS categories_pkg ON categories (pkg, category)");
 
-  RUN_SQL(db, "CREATE TABLE IF NOT EXISTS settings (name text NOT NULL, val text NOT NULL)");
-  RUN_SQL(db, "CREATE INDEX IF NOT EXISTS settings_name ON settings (name)");
+	RUN_SQL(db, "CREATE TABLE IF NOT EXISTS settings (name text NOT NULL, val text NOT NULL)");
+	RUN_SQL(db, "CREATE INDEX IF NOT EXISTS settings_name ON settings (name)");
 
-  return MPORT_OK;
+	return (MPORT_OK);
 }

Modified: trunk/lib/libmport/instance.c
===================================================================
--- trunk/lib/libmport/instance.c	2013-12-15 03:59:49 UTC (rev 6489)
+++ trunk/lib/libmport/instance.c	2013-12-15 05:57:46 UTC (rev 6490)
@@ -1,4 +1,5 @@
 /*-
+ * Copyright (c) 2013 Lucas Holt
  * Copyright (c) 2007-2009 Chris Reinhardt
  * All rights reserved.
  *
@@ -25,7 +26,7 @@
  */
 
 #include <sys/cdefs.h>
-__MBSDID("$MidnightBSD: src/lib/libmport/instance.c,v 1.7 2011/07/24 15:59:08 laffer1 Exp $");
+__MBSDID("$MidnightBSD$");
 
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -86,13 +87,46 @@
   mport->progress_free_cb = &mport_default_progress_free_cb;
   mport->confirm_cb       = &mport_default_confirm_cb;
   
-  
+ 
+  mport_upgrade_master_schema(mport->db, mport_get_database_version(mport->db));
 
   /* create tables */
   return mport_generate_master_schema(mport->db);
 }
 
+int 
+mport_get_database_version(sqlite3 *db) {
+	sqlite3_stmt *stmt_version;
+	int databaseVersion = 0;
+	
+	if (sqlite3_prepare_v2(db, "PRAGMA user_version;", -1, &stmt_version, NULL) == SQLITE_OK) {
+		while (sqlite3_step(stmt_version) == SQLITE_ROW) {
+			databaseVersion = sqlite3_column_int(stmt_version, 0);
+		}
+	} else {
+		return -1; /* ERROR condition */
+	}
+	sqlite3_finalize(stmt_version);
 
+	return databaseVersion;
+}
+
+int
+mport_set_database_version(sqlite3 *db) {
+	char *sql;
+
+	asprintf(&sql, "PRAGMA user_version=%d", MPORT_MASTER_VERSION);
+
+	if (mport_db_do(db, sql) != MPORT_OK) {
+		free(sql);
+		RETURN_CURRENT_ERROR;
+	}
+
+	free(sql);
+
+	return (MPORT_OK);
+}
+
 /* Setters for the variable UI callbacks. */
 MPORT_PUBLIC_API void mport_set_msg_cb(mportInstance *mport, mport_msg_cb cb) 
 {

Modified: trunk/lib/libmport/mport_private.h
===================================================================
--- trunk/lib/libmport/mport_private.h	2013-12-15 03:59:49 UTC (rev 6489)
+++ trunk/lib/libmport/mport_private.h	2013-12-15 05:57:46 UTC (rev 6490)
@@ -1,4 +1,4 @@
-/* $MidnightBSD: src/lib/libmport/mport_private.h,v 1.12 2013/09/07 19:49:51 laffer1 Exp $
+/* $MidnightBSD$
  *
  * Copyright (c) 2011, 2013 Lucas Holt
  * Copyright (c) 2007-2009 Chris Reinhardt
@@ -42,6 +42,7 @@
 
 #define MPORT_PUBLIC_API 
 
+#define MPORT_MASTER_VERSION 2
 #define MPORT_BUNDLE_VERSION 2
 #define MPORT_BUNDLE_VERSION_STR "2"
 
@@ -59,7 +60,12 @@
 /* schema */
 int mport_generate_master_schema(sqlite3 *);
 int mport_generate_stub_schema(sqlite3 *);
+int mport_upgrade_master_schema(sqlite3 *, int);
 
+/* instance */
+int mport_get_database_version(sqlite3 *);
+int mport_set_database_version(sqlite3 *);
+
 /* Various database convience functions */
 int mport_attach_stub_db(sqlite3 *, const char *);
 int mport_detach_stub_db(sqlite3 *);
@@ -156,8 +162,6 @@
 #define MPORT_ARCH "i386"
 #elif defined(__amd64__)
 #define MPORT_ARCH "amd64"
-#elif defined(__sparc64__)
-#define MPORT_ARCH "sparc64"
 #else
 #error "Unable to detect arch!"
 #endif



More information about the Midnightbsd-cvs mailing list