Merge pull request 'Delete all the db creation old code to start from scratch' (#4) from refactor-db-creation into main

Reviewed-on: https://codeberg.org/kirbylife/harbour-wallaread/pulls/4
main
kirbylife 2024-03-27 06:37:26 +00:00
commit 294cf5cb91
1 changed files with 39 additions and 105 deletions

View File

@ -813,7 +813,7 @@ function setArticleRead( server, id, read )
Internal functions
*/
var DBVERSION = "0.3"
var DBVERSION = "0.5";
var _db = null;
function getDatabase()
@ -827,64 +827,52 @@ function getDatabase()
return _db;
}
function checkDatabaseStatus( db )
{
if ( db.version === "" ) {
function checkDatabaseStatus(db) {
if (db.version === "") {
createLatestDatabase( db );
}
// _updateSchema_v* will take care of calling the relevant update methods
// to bring the database to the latest version
else if ( db.version === "0.2" ) {
_updateSchema_v3( db );
}
else if ( db.version === "0.3" ) {
_updateSchema_v4( db )
}
}
function createLatestDatabase( db )
{
function createLatestDatabase(db) {
var version = db.version;
if ( version !== DBVERSION ) {
db.transaction(
function( tx ) {
tx.executeSql( "CREATE TABLE IF NOT EXISTS servers (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT NOT NULL, " +
"url TEXT NOT NULL, " +
"user TEXT NOT NULL, " +
"password TEXT NOT NULL, " +
"clientId TEXT NOT NULL, " +
"clientSecret TEXT NOT NULL, " +
"lastSync INTEGER DEFAULT 0" +
")"
);
db.transaction(
function(tx) {
tx.executeSql("CREATE TABLE IF NOT EXISTS servers (" +
"id INTEGER PRIMARY KEY AUTOINCREMENT, " +
"name TEXT NOT NULL, " +
"url TEXT NOT NULL, " +
"user TEXT NOT NULL, " +
"password TEXT NOT NULL, " +
"clientId TEXT NOT NULL, " +
"clientSecret TEXT NOT NULL, " +
"fetchUnread INTEGER DEFAULT 0, " +
"lastSync INTEGER DEFAULT 0" +
")"
);
tx.executeSql(
"CREATE TABLE IF NOT EXISTS articles (" +
"id INTEGER, " +
"server INTEGER REFERENCES servers(id), " +
"created TEXT, " +
"updated TEXT, " +
"mimetype TEXT, " +
"language TEXT, " +
"readingTime INTEGER DEFAULT 0, " +
"url TEXT, " +
"domain TEXT, " +
"archived INTEGER DEFAULT 0, " +
"starred INTEGER DEFAULT 0, " +
"title TEXT, " +
"previewPicture BLOB, " +
"content TEXT, " +
"PRIMARY KEY(id, server)" +
")"
);
tx.executeSql("CREATE TABLE IF NOT EXISTS articles (" +
"id INTEGER, " +
"server INTEGER REFERENCES servers(id), " +
"created TEXT, " +
"updated TEXT, " +
"mimetype TEXT, " +
"language TEXT, " +
"readingTime INTEGER DEFAULT 0, " +
"url TEXT, " +
"domain TEXT, " +
"archived INTEGER DEFAULT 0, " +
"starred INTEGER DEFAULT 0, " +
"title TEXT, " +
"previewPicture BLOB, " +
"content TEXT, " +
"PRIMARY KEY(id, server)" +
")"
);
db.changeVersion( version, DBVERSION );
}
);
}
db.changeVersion(version, DBVERSION);
}
);
}
function resetDatabase()
@ -902,57 +890,3 @@ function resetDatabase()
);
}
function _updateSchema_v3( db )
{
db.transaction(
function( tx ) {
tx.executeSql(
"CREATE TABLE IF NOT EXISTS articles_next (" +
"id INTEGER, " +
"server INTEGER REFERENCES servers(id), " +
"created TEXT, " +
"updated TEXT, " +
"mimetype TEXT, " +
"language TEXT, " +
"readingTime INTEGER DEFAULT 0, " +
"url TEXT, " +
"domain TEXT, " +
"archived INTEGER DEFAULT 0, " +
"starred INTEGER DEFAULT 0, " +
"title TEXT, " +
"previewPicture BLOB, " +
"content TEXT, " +
"PRIMARY KEY(id, server)" +
")"
);
tx.executeSql( "INSERT INTO articles_next SELECT * FROM articles" );
tx.executeSql( "DROP TABLE articles" );
tx.executeSql( "ALTER TABLE articles_next RENAME TO articles" );
db.changeVersion( db.version, "0.3" );
_updateSchema_v4( db )
}
);
}
var errorFlag = false;
function _updateSchema_v4( db )
{
db.transaction(
function ( tx ) {
try {
tx.executeSql( "ALTER TABLE servers ADD COLUMN fetchUnread INTEGER DEFAULT 0" );
tx.executeSql( "UPDATE servers SET fetchUnread=0" );
} catch ( e ) {
if ( errorFlag ) throw e;
errorFlag = true;
resetDatabase();
_updateSchema_v4( db );
}
db.changeVersion( db.version, "0.4" );
}
);
}