pragma foreign_keys = 0;

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

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

alter table "homegames" drop column "league.key";

update homegames
set "team.key" = 'WS9'
where "team.key" = 'WAS' and "year.key" between 1891 and 1899;

update homegames
set "team.key" = 'PHP'
where "team.key" = 'PH4' and "year.key" between 1890 and 1891;

CREATE TABLE IF NOT EXISTS main."homegames" (
	"year"	NUMERIC,
	"team"	TEXT,
	"park"	TEXT,
	"first"	TEXT,
	"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;
