 -- PostgreSQL database dump
 --
 
--- Dumped from database version 9.6.4
--- Dumped by pg_dump version 9.6.4
+-- Dumped from database version 9.6.1
+-- Dumped by pg_dump version 9.6.1
 
 SET statement_timeout = 0;
 SET lock_timeout = 0;



--
-- Name: user_milestones; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE user_milestones (
    id integer NOT NULL,
    user_id integer NOT NULL,
    milestone_id integer NOT NULL,
    created_at timestamp without time zone,
    updated_at timestamp without time zone
);


--
-- Name: user_milestones_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE user_milestones_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: user_milestones_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE user_milestones_id_seq OWNED BY user_milestones.id;


--
-- Name: user_subscriptions; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE user_subscriptions (
    id integer NOT NULL,
    user_id integer,
    subscription_id integer,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone NOT NULL
);


--
-- Name: user_subscriptions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE user_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: user_subscriptions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE user_subscriptions_id_seq OWNED BY user_subscriptions.id;


--
-- Name: users; Type: TABLE; Schema: public; Owner: -
--




--
-- Name: untitled_materialized_view; Type: MATERIALIZED VIEW; Schema: public; Owner: -
--

CREATE MATERIALIZED VIEW untitled_materialized_view AS
 SELECT ((sum(a.total_students) / sum(b.total_students)) * (( SELECT count(DISTINCT s.id) AS students
           FROM ((((((users t
             LEFT JOIN ip_locations ON ((ip_locations.user_id = t.id)))
             LEFT JOIN classrooms ON ((t.id = classrooms.teacher_id)))
             LEFT JOIN users s ON (((classrooms.code)::text = (s.classcode)::text)))
             LEFT JOIN activity_sessions ON ((s.id = activity_sessions.user_id)))
             LEFT JOIN schools_users ON ((t.id = schools_users.user_id)))
             LEFT JOIN schools ON ((schools_users.school_id = schools.id)))
          WHERE (((activity_sessions.state)::text = 'finished'::text) AND (activity_sessions.completed_at < date_trunc('DAY'::text, (('now'::text)::date - '1 year'::interval))) AND ((ip_locations.country IS NULL) OR ((ip_locations.country)::text = 'United States'::text)))))::numeric)
   FROM ( SELECT count(DISTINCT students.id) AS total_students
           FROM ((((schools s
             JOIN schools_users ON ((schools_users.school_id = s.id)))
             JOIN users teacher ON ((schools_users.user_id = teacher.id)))
             JOIN classrooms ON ((teacher.id = classrooms.teacher_id)))
             JOIN users students ON (((students.classcode)::text = (classrooms.code)::text)))
          WHERE ((schools_users.school_id IS NOT NULL) AND (s.free_lunches >= 40))) a,
    ( SELECT count(DISTINCT students.id) AS total_students
           FROM ((((schools s
             JOIN schools_users ON ((schools_users.school_id = s.id)))
             JOIN users teacher ON ((schools_users.user_id = teacher.id)))
             JOIN classrooms ON ((teacher.id = classrooms.teacher_id)))
             JOIN users students ON (((students.classcode)::text = (classrooms.code)::text)))
          WHERE (schools_users.school_id IS NOT NULL)) b
  WITH NO DATA;


--
-- Name: user_milestones; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE user_milestones (
    id integer NOT NULL,
    user_id integer NOT NULL,
    milestone_id integer NOT NULL,
    created_at timestamp without time zone,
    updated_at timestamp without time zone
);


--
-- Name: user_milestones_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE user_milestones_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: user_milestones_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE user_milestones_id_seq OWNED BY user_milestones.id;


--
-- Name: user_subscriptions; Type: TABLE; Schema: public; Owner: -
--

CREATE TABLE user_subscriptions (
    id integer NOT NULL,
    user_id integer,
    subscription_id integer,
    created_at timestamp without time zone NOT NULL,
    updated_at timestamp without time zone NOT NULL
);


--
-- Name: user_subscriptions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

CREATE SEQUENCE user_subscriptions_id_seq
    START WITH 1
    INCREMENT BY 1
    NO MINVALUE
    NO MAXVALUE
    CACHE 1;


--
-- Name: user_subscriptions_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--

ALTER SEQUENCE user_subscriptions_id_seq OWNED BY user_subscriptions.id;


--
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--

