pragma foreign_keys = 0;

begin;
attach database 'lahman-raw.db' as 'raw';

create temp table homegames as
select * from "raw".homegames;

alter table "homegames" drop column "leaguekey";

update homegames
set "teamkey" = 'WS9'
where "teamkey" = 'WAS' and "yearkey" between 1891 and 1899;

update homegames
set "teamkey" = 'PHP'
where "teamkey" = 'PH4' and "yearkey" between 1890 and 1891;

CREATE TABLE IF NOT EXISTS main."homegames" (
	"year"	NUMERIC,
	"team"	TEXT,
	"park"	TEXT,
	"first"	TEXT	check(unixepoch("first") <= unixepoch("last")),
	"last"	TEXT,
	"games"	NUMERIC,
	"openings"	NUMERIC,
	"attendance"	NUMERIC,
	PRIMARY KEY("year","team","park"),
	foreign key("park") references "parks"("ID"),
	foreign key("year","team") references "teamseasons"("year","team")
);

insert into main.homegames select distinct * from temp."homegames";
commit;
