make these columns unique

This commit is contained in:
Christine Dodrill 2015-07-28 13:40:13 -07:00
parent 4ecee40c95
commit 7d5adcba66
2 changed files with 31 additions and 18 deletions

View File

@ -1,6 +1,6 @@
CREATE TABLE IF NOT EXISTS Posts (
id INTEGER PRIMARY KEY
, oid TEXT NOT NULL
, oid TEXT UNIQUE NOT NULL
, body TEXT NOT NULL
, mdown TEXT NOT NULL
, author TEXT NOT NULL
@ -10,6 +10,6 @@ CREATE TABLE IF NOT EXISTS Posts (
CREATE TABLE IF NOT EXISTS Users (
id INTEGER PRIMARY KEY
, oid TEXT UNIQUE NOT NULL
, name TEXT NOT NULL
, name TEXT UNIQUE NOT NULL
, avatar_url TEXT NOT NULL
);

View File

@ -5,23 +5,28 @@ sqlite3 = require "lsqlite3"
authors = {}
with fin = io.open "../raw/authors.txt", "r"
while true
line = fin\read "*l"
break unless line
table.insert authors, line
assert #authors == 193
db = assert sqlite3.open "../db/posts.db"
insert_stmt = assert db\prepare "INSERT INTO Users VALUES (NULL, ?, ?, ?)"
db\exec [[ BEGIN TRANSACTION; ]]
db\trace (ud, sql) ->
print sql
for i, author in pairs authors
print "fetching for #{author}..."
print [[ BEGIN TRANSACTION; ]]
for row in db\nrows "SELECT * FROM Posts"
do
for k,v in pairs row
print " -- #{k}: #{v}"
if authors[row.author]
-- print "Skipping dupe #{row.author}"
continue
author = row.author
authors[author] = true
print "-- #{os.date!} fetching for #{author}..."
reply, code, headers = https.request "https://derpibooru.org/profiles/#{author}.json"
if code ~= 200
@ -33,14 +38,22 @@ for i, author in pairs authors
db\exec [[ ROLLBACK; ]]
error err
print "found #{person.name}"
print "-- #{os.date} found #{person.name}"
person.avatar = "//derpicdn.net/assets/no_avatar_125x125-2c4e2d8e68cb13a208dae3c6d6877b45c5390dd367920d927c80dde1665bc0ed.png" unless person.avatar
do
insert_stmt\bind_values person.id, person.name, person.avatar
insert_stmt\step!
insert_stmt\reset!
print "done at #{os.date!}"
print "-- #{os.date!} adding #{person.id}, #{person.name}"
err = insert_stmt\step!
if err ~= sqlite3.OK and err ~= sqlite3.DONE
print err
db\exec [[ ROLLBACK; ]]
error db\errmsg!
insert_stmt\reset!
socket.sleep 1