2021-11-22 16:55:27 -08:00
|
|
|
|
package persistence
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
2022-12-02 20:33:54 -05:00
|
|
|
|
|
2022-03-06 19:17:43 -08:00
|
|
|
|
sql "github.com/jmoiron/sqlx"
|
2021-12-12 20:10:08 -08:00
|
|
|
|
|
|
|
|
|
"offline_twitter/terminal_utils"
|
2021-11-22 16:55:27 -08:00
|
|
|
|
)
|
|
|
|
|
|
2022-12-25 13:06:17 -05:00
|
|
|
|
const ENGINE_DATABASE_VERSION = 15
|
2021-11-22 16:55:27 -08:00
|
|
|
|
|
|
|
|
|
type VersionMismatchError struct {
|
2022-03-13 17:09:43 -07:00
|
|
|
|
EngineVersion int
|
2021-11-22 16:55:27 -08:00
|
|
|
|
DatabaseVersion int
|
|
|
|
|
}
|
2022-03-13 17:09:43 -07:00
|
|
|
|
|
2021-11-22 16:55:27 -08:00
|
|
|
|
func (e VersionMismatchError) Error() string {
|
|
|
|
|
return fmt.Sprintf(
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`This profile was created with database schema version %d, which is newer than this application's database schema version, %d.
|
2021-11-22 16:55:27 -08:00
|
|
|
|
Please upgrade this application to a newer version to use this profile. Or downgrade the profile's schema version, somehow.`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
e.DatabaseVersion, e.EngineVersion,
|
2021-11-22 16:55:27 -08:00
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-12-21 16:05:51 -05:00
|
|
|
|
* The Nth entry is the migration that moves you from version N to version N+1.
|
|
|
|
|
* `len(MIGRATIONS)` should always equal `ENGINE_DATABASE_VERSION`.
|
2021-11-22 16:55:27 -08:00
|
|
|
|
*/
|
|
|
|
|
var MIGRATIONS = []string{
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`create table polls (rowid integer primary key,
|
2021-12-12 19:38:28 -08:00
|
|
|
|
id integer unique not null check(typeof(id) = 'integer'),
|
|
|
|
|
tweet_id integer not null,
|
|
|
|
|
num_choices integer not null,
|
2021-11-22 16:55:27 -08:00
|
|
|
|
|
2021-12-12 19:38:28 -08:00
|
|
|
|
choice1 text,
|
|
|
|
|
choice1_votes integer,
|
|
|
|
|
choice2 text,
|
|
|
|
|
choice2_votes integer,
|
|
|
|
|
choice3 text,
|
|
|
|
|
choice3_votes integer,
|
|
|
|
|
choice4 text,
|
|
|
|
|
choice4_votes integer,
|
|
|
|
|
|
|
|
|
|
voting_duration integer not null, -- in seconds
|
|
|
|
|
voting_ends_at integer not null,
|
|
|
|
|
|
|
|
|
|
last_scraped_at integer not null,
|
|
|
|
|
|
|
|
|
|
foreign key(tweet_id) references tweets(id)
|
2021-12-20 14:07:20 -05:00
|
|
|
|
);`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`alter table tweets add column is_conversation_scraped boolean default 0;
|
2021-12-20 14:07:20 -05:00
|
|
|
|
alter table tweets add column last_scraped_at integer not null default 0`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`update tombstone_types set tombstone_text = 'This Tweet is from a suspended account' where rowid = 2;
|
2022-03-06 15:06:06 -08:00
|
|
|
|
insert into tombstone_types (rowid, short_name, tombstone_text)
|
|
|
|
|
values (5, 'violated', 'This Tweet violated the Twitter Rules'),
|
|
|
|
|
(6, 'no longer exists', 'This Tweet is from an account that no longer exists')`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`alter table videos add column thumbnail_remote_url text not null default "missing";
|
2021-12-23 15:36:14 -05:00
|
|
|
|
alter table videos add column thumbnail_local_filename text not null default "missing"`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`alter table videos add column duration integer not null default 0;
|
2021-12-24 16:26:34 -05:00
|
|
|
|
alter table videos add column view_count integer not null default 0`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`alter table users add column is_banned boolean default 0`,
|
|
|
|
|
`alter table urls add column short_text text not null default ""`,
|
|
|
|
|
`insert into tombstone_types (rowid, short_name, tombstone_text) values (7, 'age-restricted', 'Age-restricted adult content. '
|
2022-03-06 15:06:06 -08:00
|
|
|
|
|| 'This content might not be appropriate for people under 18 years old. To view this media, you’ll need to log in to Twitter')`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`alter table users add column is_followed boolean default 0`,
|
|
|
|
|
`create table fake_user_sequence(latest_fake_id integer not null);
|
2022-02-26 22:09:27 -08:00
|
|
|
|
insert into fake_user_sequence values(0x4000000000000000);
|
|
|
|
|
alter table users add column is_id_fake boolean default 0;`,
|
2022-03-13 17:09:43 -07:00
|
|
|
|
`delete from urls where rowid in (select urls.rowid from tweets join urls on tweets.id = urls.tweet_id where urls.text like
|
2022-03-06 15:06:06 -08:00
|
|
|
|
'https://twitter.com/%/status/' || tweets.quoted_tweet_id || "%")`,
|
2022-05-14 18:28:33 -07:00
|
|
|
|
`create table spaces(rowid integer primary key,
|
|
|
|
|
id text unique not null,
|
|
|
|
|
short_url text not null
|
|
|
|
|
);
|
|
|
|
|
alter table tweets add column space_id text references spaces(id)`,
|
2022-12-02 20:33:54 -05:00
|
|
|
|
`alter table videos add column is_blocked_by_dmca boolean not null default 0`,
|
2022-12-24 13:47:43 -05:00
|
|
|
|
`create index if not exists index_tweets_in_reply_to_id on tweets (in_reply_to_id);
|
|
|
|
|
create index if not exists index_urls_tweet_id on urls (tweet_id);
|
|
|
|
|
create index if not exists index_polls_tweet_id on polls (tweet_id);
|
|
|
|
|
create index if not exists index_images_tweet_id on images (tweet_id);
|
|
|
|
|
create index if not exists index_videos_tweet_id on videos (tweet_id);`,
|
2022-12-25 13:06:17 -05:00
|
|
|
|
`alter table spaces add column created_by_id integer references users(id);
|
|
|
|
|
alter table spaces add column state text not null;
|
|
|
|
|
alter table spaces add column title text not null;
|
|
|
|
|
alter table spaces add column created_at integer not null;
|
|
|
|
|
alter table spaces add column started_at integer not null;
|
|
|
|
|
alter table spaces add column ended_at integer not null;
|
|
|
|
|
alter table spaces add column updated_at integer not null;
|
|
|
|
|
alter table spaces add column is_available_for_replay boolean not null;
|
|
|
|
|
alter table spaces add column replay_watch_count integer;
|
|
|
|
|
alter table spaces add column live_listeners_count integer;
|
|
|
|
|
alter table spaces add column is_details_fetched boolean not null default 0;
|
|
|
|
|
create table space_participants(rowid integer primary key,
|
|
|
|
|
user_id integer not null,
|
|
|
|
|
space_id not null,
|
|
|
|
|
foreign key(space_id) references spaces(id)
|
|
|
|
|
);`,
|
2021-11-22 16:55:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* This should only get called on a newly created Profile.
|
|
|
|
|
* Subsequent updates should change the number, not insert a new row.
|
|
|
|
|
*/
|
|
|
|
|
func InitializeDatabaseVersion(db *sql.DB) {
|
2022-03-06 19:17:43 -08:00
|
|
|
|
db.MustExec("insert into database_version (version_number) values (?)", ENGINE_DATABASE_VERSION)
|
2021-11-22 16:55:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p Profile) GetDatabaseVersion() (int, error) {
|
|
|
|
|
row := p.DB.QueryRow("select version_number from database_version")
|
|
|
|
|
|
|
|
|
|
var version int
|
|
|
|
|
|
|
|
|
|
err := row.Scan(&version)
|
|
|
|
|
if err != nil {
|
2022-03-13 16:13:16 -07:00
|
|
|
|
return 0, fmt.Errorf("Error checking database version:\n %w", err)
|
2021-11-22 16:55:27 -08:00
|
|
|
|
}
|
|
|
|
|
return version, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p Profile) check_and_update_version() error {
|
|
|
|
|
version, err := p.GetDatabaseVersion()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if version > ENGINE_DATABASE_VERSION {
|
|
|
|
|
return VersionMismatchError{ENGINE_DATABASE_VERSION, version}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ENGINE_DATABASE_VERSION > version {
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_YELLOW)
|
|
|
|
|
fmt.Printf("================================================\n")
|
2022-03-06 15:06:06 -08:00
|
|
|
|
fmt.Printf("Database version is out of date. Upgrading database from version %d to version %d!\n", version,
|
|
|
|
|
ENGINE_DATABASE_VERSION)
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_RESET)
|
2021-11-22 17:03:57 -08:00
|
|
|
|
return p.UpgradeFromXToY(version, ENGINE_DATABASE_VERSION)
|
2021-11-22 16:55:27 -08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run all the migrations from version X to version Y, and update the `database_version` table's `version_number`
|
|
|
|
|
*/
|
|
|
|
|
func (p Profile) UpgradeFromXToY(x int, y int) error {
|
|
|
|
|
for i := x; i < y; i++ {
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_CYAN)
|
2021-11-22 16:55:27 -08:00
|
|
|
|
fmt.Println(MIGRATIONS[i])
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_RESET)
|
|
|
|
|
|
2022-03-06 19:17:43 -08:00
|
|
|
|
p.DB.MustExec(MIGRATIONS[i])
|
|
|
|
|
p.DB.MustExec("update database_version set version_number = ?", i+1)
|
|
|
|
|
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_YELLOW)
|
2022-03-06 19:17:43 -08:00
|
|
|
|
fmt.Printf("Now at database schema version %d.\n", i+1)
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_RESET)
|
2021-11-22 16:55:27 -08:00
|
|
|
|
}
|
2021-12-12 20:10:08 -08:00
|
|
|
|
fmt.Printf(terminal_utils.COLOR_GREEN)
|
|
|
|
|
fmt.Printf("================================================\n")
|
|
|
|
|
fmt.Printf("Database version has been upgraded to version %d.\n", y)
|
|
|
|
|
fmt.Printf(terminal_utils.COLOR_RESET)
|
2021-11-22 16:55:27 -08:00
|
|
|
|
return nil
|
|
|
|
|
}
|