Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move InitTables and have it check for table existence before creation #107

Merged
merged 6 commits into from Apr 23, 2015

Conversation

rolandshoemaker
Copy link
Contributor

This PR moves sa.SqlStorageAuthority.InitTables one usage in boulder/cmd/boulder to sa.NewSQLStorageAuthority so that both boulder (mono) and boulder-sa (poly) will create the necessary tables if they don't already exist.

It also alters InitTables to add checks for the existence of the tables before creating them using the information_schema.tables table. Because this relies on existence of the information_schema schema the new checks cannot be used with the sqlite3 driver since SQLite doesn't support this, to deal with this I've added a basic check in NewSQLStorageAuthority and a bool field to SQLStorageAuthority that indicates if the driver supports the initCheck, if it doesn't it will just revert to the previous behavior of always trying to create the tables. Since at the moment the only other driver being used is mysql which does support information_schema so the check is simply drive != "sqlite3", but this should also work with most other drivers/DBs we might want to use/support in the future (Postgres and MariaDB both support information_schema).

This consequently also allows restarting the monolithic client with a MySQL DB without having to wipe all the tables in the DB each time.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.37%) to 33.32% when pulling 6944f23 on rolandshoemaker:aware-initables into ba622d4 on letsencrypt:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.5%) to 33.19% when pulling 7cf1872 on rolandshoemaker:aware-initables into ba622d4 on letsencrypt:master.

@jcjones
Copy link
Contributor

jcjones commented Apr 22, 2015

I'll have to review this in a couple hours, but you may want to use the optional-create syntax rather than deal with sqlite breakage. You could construct the tables with: CREATE TABLE IF NOT EXISTS ... [reference]

@jsha
Copy link
Contributor

jsha commented Apr 22, 2015

Thanks for making this change! Have been wanting local support for persistent tables. I would also recommend the "CREATE TABLE IF NOT EXISTS" approach.

@jsha
Copy link
Contributor

jsha commented Apr 22, 2015

Another approach: Probably it's not necessary to include routines to initialize the tables in the Go code. Certainly in production we will not be initializing these tables more than once, so it's dev-only code.

Why not move all the CREATE TABLEs into a separate init.sql file, and provide first-time-user instructions to run sqlite3 db.sqlite3 init.sql? For test mode we could still use the inmemory sqlite3 driver, and drop / reinitialize from init.sql on each test case.

@rolandshoemaker
Copy link
Contributor Author

@jcjones kicks self -- I'm not sure how I forgot about CREATE TABLE IF NOT EXISTS, makes much more sense...

@jsha I agree that this will essentially be one-time/dev-only code, but I feel moving it out to another file may make it easier down the line to forget to propagate changes made to SA to the table initializing SQL file.

How would you feel about adding a flag to the monolith (bolder) and polylithic (boulder-sa) clients which users could be instructed to run first, --initdb or something, which would run the InitTables method then exit, allowing us to keep the initialization code in the SA but only bother running it when it is actually necessary.

@jsha
Copy link
Contributor

jsha commented Apr 22, 2015

--initdb sounds fine.

What about consolidating all the initialization SQL into a single, long backtick-delimited blockquote? The code right now seems excessively verbose, checking for error and rolling back after each individual statement.

@rolandshoemaker
Copy link
Contributor Author

After short experimentation sql.Tx.Exec doesn't support multiple line / batch statements (; separated) see golang/go#5171, so it seems this level of verbosity is, currently, required...

@jsha
Copy link
Contributor

jsha commented Apr 23, 2015

Got it, thanks for checking! Let's go ahead with the --initdb.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 33.61% when pulling 2c370de on rolandshoemaker:aware-initables into ba622d4 on letsencrypt:master.

@coveralls
Copy link

Coverage Status

Coverage decreased (-0.07%) to 33.61% when pulling 828ea56 on rolandshoemaker:aware-initables into ba622d4 on letsencrypt:master.

if err != nil {
tx.Rollback()
return
}

// Create certificates table
_, err = tx.Exec("CREATE TABLE certificates (serial string, digest TEXT, value BLOB);")
_, err = tx.Exec("CREATE TABLE IF NOT EXISTS certificates (serial INTEGER, digest TEXT, value BLOB);")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're storing this as a hex-encoded string for a couple of reasons. Mainly that it's easier to eyeball, since there's some internal structure to the serial: The first 8 bits are an intermediate identifier, the next 56 bits are a sequential counter, and the final 64 bits are random. It's useful to be able to eyeball these in the DB. It's also nice to be able to easily query for serials with a given prefix, in order to enumerate all certs by the counter part. So I'd like this to stay as a string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure why I changed this... Anyway, it's been reverted to STRING now.

Not sure how that got changed...
@coveralls
Copy link

Coverage Status

Coverage decreased (-3.06%) to 30.63% when pulling b63c9f3 on rolandshoemaker:aware-initables into ba622d4 on letsencrypt:master.

jsha added a commit that referenced this pull request Apr 23, 2015
Move InitTables and have it check for table existence before creation
@jsha jsha merged commit 6c8f6a2 into letsencrypt:master Apr 23, 2015
@rolandshoemaker rolandshoemaker deleted the aware-initables branch May 2, 2015 22:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants